chiark / gitweb /
7906638a7f066670fbd2bea6d5a603d523fdb309
[elogind.git] / udev-add.c
1 /*
2  * udev-add.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7  *
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include "udev.h"
32 #include "udev_version.h"
33 #include "namedev.h"
34 #include "udevdb.h"
35 #include "libsysfs/libsysfs.h"
36
37 static char sysfs_path[SYSFS_PATH_MAX];
38
39 /* 
40  * Right now the major/minor of a device is stored in a file called
41  * "dev" in sysfs.
42  * The number is stored as:
43  *      MM:mm
44  *              MM is the major
45  *              mm is the minor
46  *              The value is in decimal.
47  */
48 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
49 {
50         int retval = -ENODEV;
51
52         char *dev;
53
54         dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
55         if (dev == NULL)
56                 goto exit;
57
58         dbg("dev = %s", dev);
59
60         if (sscanf(dev, "%u:%u", major, minor) != 2)
61                 goto exit;
62
63         dbg("found major = %d, minor = %d", *major, *minor);
64
65         retval = 0;
66 exit:
67         return retval;
68 }
69
70 /*
71  * We also want to add some permissions here, and possibly some symlinks
72  */
73 static int create_node(struct udevice *dev)
74 {
75         char filename[255];
76         int retval = 0;
77
78         strncpy(filename, UDEV_ROOT, sizeof(filename));
79         strncat(filename, dev->name, sizeof(filename));
80
81         switch (dev->type) {
82         case 'b':
83                 dev->mode |= S_IFBLK;
84                 break;
85         case 'c':
86         case 'u':
87                 dev->mode |= S_IFCHR;
88                 break;
89         case 'p':
90                 dev->mode |= S_IFIFO;
91                 break;
92         default:
93                 dbg("unknown node type %c\n", dev->type);
94                 return -EINVAL;
95         }
96
97         dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
98         retval = mknod(filename, dev->mode, makedev(dev->major, dev->minor));
99         if (retval)
100                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
101                     filename, dev->mode, dev->major, dev->minor, strerror(errno));
102
103         // FIXME set the ownership of the node
104         return retval;
105 }
106
107 static struct sysfs_class_device *get_class_dev(char *device_name)
108 {
109         char dev_path[SYSFS_PATH_MAX];
110         struct sysfs_class_device *class_dev = NULL;
111
112         strcpy(dev_path, sysfs_path);
113         strcat(dev_path, device_name);
114
115         dbg("looking at %s", dev_path);
116
117         /* open up the sysfs class device for this thing... */
118         class_dev = sysfs_open_class_device(dev_path);
119         if (class_dev == NULL) {
120                 dbg ("sysfs_open_class_device failed");
121                 goto exit;
122         }
123         dbg("class_dev->name = %s", class_dev->name);
124
125 exit:
126         return class_dev;
127 }
128
129 /* wait for the "dev" file to show up in the directory in sysfs.
130  * If it doesn't happen in about 10 seconds, give up.
131  */
132 #define SECONDS_TO_WAIT_FOR_DEV         10
133 int sleep_for_dev(char *path)
134 {
135         char filename[SYSFS_PATH_MAX + 6];
136         struct stat buf;
137         int loop = 0;
138         int retval = -ENODEV;
139
140         strcpy(filename, sysfs_path);
141         strcat(filename, path);
142         strcat(filename, "/dev");
143
144         while (loop < SECONDS_TO_WAIT_FOR_DEV) {
145                 dbg("looking for %s", filename);
146                 retval = stat(filename, &buf);
147                 if (retval == 0) {
148                         retval = 0;
149                         goto exit;
150                 }
151
152                 /* sleep for a second or two to give the kernel a chance to
153                  * create the dev file */
154                 sleep(1);
155         }
156         retval = -ENODEV;
157 exit:
158         return retval;
159 }
160
161 int udev_add_device(char *path, char *subsystem)
162 {
163         struct sysfs_class_device *class_dev;
164         struct udevice dev;
165         struct device_attr attr;
166         int retval = -EINVAL;
167
168         /* for now, the block layer is the only place where block devices are */
169         if (strcmp(subsystem, "block") == 0)
170                 dev.type = 'b';
171         else
172                 dev.type = 'c';
173
174         retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
175         dbg("sysfs_path = %s", sysfs_path);
176         if (retval) {
177                 dbg("sysfs_get_mnt_path failed");
178                 goto exit;
179         }
180
181         retval = sleep_for_dev(path);
182         if (retval)
183                 goto exit;
184
185         class_dev = get_class_dev(path);
186         if (class_dev == NULL)
187                 goto exit;
188
189         retval = namedev_name_device(class_dev, &attr);
190         if (retval)
191                 return retval;
192
193         retval = get_major_minor(class_dev, &dev.major, &dev.minor);
194         if (retval) {
195                 dbg("get_major_minor failed");
196                 goto exit;
197         }
198
199         strcpy(dev.name, attr.name);
200         strcpy(dev.owner, attr.owner);
201         strcpy(dev.group, attr.group);
202         dev.mode = attr.mode;
203         
204         retval = udevdb_add_dev(path, &dev);
205         if (retval != 0)
206                 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
207                     "But remove might not work properly for this device.");
208
209         sysfs_close_class_device(class_dev);
210
211         dbg("name = %s", dev.name);
212         retval = create_node(&dev);
213
214 exit:
215         return retval;
216 }
217