]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_device.c
add and use name_list_cleanup() for cleaning up the string lists
[thirdparty/systemd.git] / udev_device.c
1 /*
2 * udev_utils.c - generic stuff used by udev
3 *
4 * Copyright (C) 2004, 2005 Kay Sievers <kay.sievers@vrfy.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <syslog.h>
31 #include <sys/wait.h>
32 #include <sys/stat.h>
33 #include <sys/mman.h>
34 #include <sys/utsname.h>
35
36 #include "udev_libc_wrapper.h"
37 #include "udev.h"
38 #include "logging.h"
39 #include "udev_utils.h"
40 #include "udev_sysfs.h"
41 #include "list.h"
42
43
44 int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem, const char *action)
45 {
46 char *pos;
47
48 memset(udev, 0x00, sizeof(struct udevice));
49 INIT_LIST_HEAD(&udev->symlink_list);
50 INIT_LIST_HEAD(&udev->run_list);
51 INIT_LIST_HEAD(&udev->env_list);
52
53 if (subsystem)
54 strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));
55
56 if (action)
57 strlcpy(udev->action, action, sizeof(udev->action));
58
59 if (devpath) {
60 strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
61 remove_trailing_char(udev->devpath, '/');
62
63 if (strncmp(udev->devpath, "/block/", 7) == 0)
64 udev->type = DEV_BLOCK;
65 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
66 udev->type = DEV_NET;
67 else if (strncmp(udev->devpath, "/class/", 7) == 0)
68 udev->type = DEV_CLASS;
69 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
70 udev->type = DEV_DEVICE;
71
72 /* get kernel name */
73 pos = strrchr(udev->devpath, '/');
74 if (pos) {
75 strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
76 dbg("kernel_name='%s'", udev->kernel_name);
77
78 /* Some block devices have '!' in their name, change that to '/' */
79 pos = udev->kernel_name;
80 while (pos[0] != '\0') {
81 if (pos[0] == '!')
82 pos[0] = '/';
83 pos++;
84 }
85
86 /* get kernel number */
87 pos = &udev->kernel_name[strlen(udev->kernel_name)];
88 while (isdigit(pos[-1]))
89 pos--;
90 strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
91 dbg("kernel_number='%s'", udev->kernel_number);
92 }
93 }
94
95 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
96 udev->mode = 0660;
97 strcpy(udev->owner, "root");
98 strcpy(udev->group, "root");
99 }
100
101 return 0;
102 }
103
104 void udev_cleanup_device(struct udevice *udev)
105 {
106 name_list_cleanup(&udev->symlink_list);
107 name_list_cleanup(&udev->run_list);
108 name_list_cleanup(&udev->env_list);
109 }