summaryrefslogtreecommitdiff
path: root/udev_device.c
blob: 45bb6a28765ab39cb88ed51136c53a66e1a5641d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * udev_utils.c - generic stuff used by udev
 *
 * Copyright (C) 2004, 2005 Kay Sievers <kay.sievers@vrfy.org>
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 * 
 *	This program is distributed in the hope that it will be useful, but
 *	WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	General Public License for more details.
 * 
 *	You should have received a copy of the GNU General Public License along
 *	with this program; if not, write to the Free Software Foundation, Inc.,
 *	675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <dirent.h>
#include <syslog.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/utsname.h>

#include "udev_libc_wrapper.h"
#include "udev.h"
#include "logging.h"
#include "udev_utils.h"
#include "udev_sysfs.h"
#include "list.h"


int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem, const char *action)
{
	char *pos;

	memset(udev, 0x00, sizeof(struct udevice));
	INIT_LIST_HEAD(&udev->symlink_list);
	INIT_LIST_HEAD(&udev->run_list);
	INIT_LIST_HEAD(&udev->env_list);

	if (subsystem)
		strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));

	if (action)
		strlcpy(udev->action, action, sizeof(udev->action));

	if (devpath) {
		strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
		remove_trailing_char(udev->devpath, '/');

		if (strncmp(udev->devpath, "/block/", 7) == 0)
			udev->type = DEV_BLOCK;
		else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
			udev->type = DEV_NET;
		else if (strncmp(udev->devpath, "/class/", 7) == 0)
			udev->type = DEV_CLASS;
		else if (strncmp(udev->devpath, "/devices/", 9) == 0)
			udev->type = DEV_DEVICE;

		/* get kernel name */
		pos = strrchr(udev->devpath, '/');
		if (pos) {
			strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
			dbg("kernel_name='%s'", udev->kernel_name);

			/* Some block devices have '!' in their name, change that to '/' */
			pos = udev->kernel_name;
			while (pos[0] != '\0') {
				if (pos[0] == '!')
					pos[0] = '/';
				pos++;
			}

			/* get kernel number */
			pos = &udev->kernel_name[strlen(udev->kernel_name)];
			while (isdigit(pos[-1]))
				pos--;
			strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
			dbg("kernel_number='%s'", udev->kernel_number);
		}
	}

	if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
		udev->mode = 0660;
		strcpy(udev->owner, "root");
		strcpy(udev->group, "root");
	}

	return 0;
}

void udev_cleanup_device(struct udevice *udev)
{
	struct name_entry *name_loop;
	struct name_entry *temp_loop;

	list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
		list_del(&name_loop->node);
		free(name_loop);
	}
	list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
		list_del(&name_loop->node);
		free(name_loop);
	}
	list_for_each_entry_safe(name_loop, temp_loop, &udev->env_list, node) {
		list_del(&name_loop->node);
		free(name_loop);
	}
}