chiark / gitweb /
078 release
[elogind.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 "list.h"
41
42
43 int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem, const char *action)
44 {
45         char *pos;
46
47         memset(udev, 0x00, sizeof(struct udevice));
48         INIT_LIST_HEAD(&udev->symlink_list);
49         INIT_LIST_HEAD(&udev->run_list);
50         INIT_LIST_HEAD(&udev->env_list);
51
52         if (subsystem)
53                 strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));
54
55         if (action)
56                 strlcpy(udev->action, action, sizeof(udev->action));
57
58         if (devpath) {
59                 strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
60                 remove_trailing_chars(udev->devpath, '/');
61
62                 if (strncmp(udev->devpath, "/block/", 7) == 0)
63                         udev->type = DEV_BLOCK;
64                 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
65                         udev->type = DEV_NET;
66                 else if (strncmp(udev->devpath, "/class/", 7) == 0)
67                         udev->type = DEV_CLASS;
68                 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
69                         udev->type = DEV_DEVICE;
70
71                 /* get kernel name */
72                 pos = strrchr(udev->devpath, '/');
73                 if (pos) {
74                         strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
75                         dbg("kernel_name='%s'", udev->kernel_name);
76
77                         /* Some block devices have '!' in their name, change that to '/' */
78                         pos = udev->kernel_name;
79                         while (pos[0] != '\0') {
80                                 if (pos[0] == '!')
81                                         pos[0] = '/';
82                                 pos++;
83                         }
84
85                         /* get kernel number */
86                         pos = &udev->kernel_name[strlen(udev->kernel_name)];
87                         while (isdigit(pos[-1]))
88                                 pos--;
89                         strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
90                         dbg("kernel_number='%s'", udev->kernel_number);
91                 }
92         }
93
94         if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
95                 udev->mode = 0660;
96                 strcpy(udev->owner, "root");
97                 strcpy(udev->group, "root");
98         }
99
100         return 0;
101 }
102
103 void udev_cleanup_device(struct udevice *udev)
104 {
105         name_list_cleanup(&udev->symlink_list);
106         name_list_cleanup(&udev->run_list);
107         name_list_cleanup(&udev->env_list);
108 }