chiark / gitweb /
[PATCH] add support for subdirs
[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 #include <sys/stat.h>
31
32 #include "udev.h"
33 #include "udev_version.h"
34 #include "namedev.h"
35 #include "udevdb.h"
36 #include "libsysfs/libsysfs.h"
37
38 /* 
39  * Right now the major/minor of a device is stored in a file called
40  * "dev" in sysfs.
41  * The number is stored as:
42  *      MM:mm
43  *              MM is the major
44  *              mm is the minor
45  *              The value is in decimal.
46  */
47 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
48 {
49         int retval = -ENODEV;
50
51         char *dev;
52
53         dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
54         if (dev == NULL)
55                 goto exit;
56
57         dbg("dev = %s", dev);
58
59         if (sscanf(dev, "%u:%u", major, minor) != 2)
60                 goto exit;
61
62         dbg("found major = %d, minor = %d", *major, *minor);
63
64         retval = 0;
65 exit:
66         return retval;
67 }
68
69 /*
70  * we possibly want to add some symlinks here
71  * only numeric owner/group id's are supported
72  */
73 static int create_node(struct udevice *dev)
74 {
75         char filename[255];
76         int retval = 0;
77         dev_t res;
78
79         strncpy(filename, udev_root, sizeof(filename));
80         strncat(filename, dev->name, sizeof(filename));
81
82 #ifdef __KLIBC__
83         res = (dev->major << 8) | (dev->minor);
84 #else
85         res = makedev(dev->major, dev->minor);
86 #endif
87
88         switch (dev->type) {
89         case 'b':
90                 dev->mode |= S_IFBLK;
91                 break;
92         case 'c':
93         case 'u':
94                 dev->mode |= S_IFCHR;
95                 break;
96         case 'p':
97                 dev->mode |= S_IFIFO;
98                 break;
99         default:
100                 dbg("unknown node type %c\n", dev->type);
101                 return -EINVAL;
102         }
103
104         /* create subdirectories if requested */
105         if (strchr(dev->name, '/')) {
106                 char path[255];
107                 char *pos;
108                 struct stat stats;
109
110                 strncpy(path, filename, sizeof(path));
111                 pos = strchr(path+1, '/');
112                 while (1) {
113                         pos = strchr(pos+1, '/');
114                         if (pos == NULL)
115                                 break;
116                         *pos = 0x00;
117                         if (stat(path, &stats)) {
118                                 retval = mkdir(path, 0755);
119                                 if (retval) {
120                                         dbg("mkdir(%s) failed with error '%s'",
121                                             path, strerror(errno));
122                                         return retval;
123                                 }
124                                 dbg("created %s", path);
125                         }
126                         *pos = '/';
127                 }
128         }
129
130         dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
131         retval = mknod(filename, dev->mode, res);
132         if (retval)
133                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
134                     filename, dev->mode, dev->major, dev->minor, strerror(errno));
135
136         uid_t uid = 0;
137         gid_t gid = 0;
138
139         if (*dev->owner) {
140                 char *endptr;
141                 unsigned long id = strtoul(dev->owner, &endptr, 10);
142                 if (*endptr == 0x00)
143                         uid = (uid_t) id;
144                 else
145                         dbg("only numeric owner id supported: %s", dev->owner);
146         }
147
148         if (*dev->group) {
149                 char *endptr;
150                 unsigned long id = strtoul(dev->group, &endptr, 10);
151                 if (*endptr == 0x00)
152                         gid = (gid_t) id;
153                 else
154                         dbg("only numeric group id supported: %s", dev->group);
155         }
156
157         if (uid || gid) {
158                 dbg("chown(%s, %u, %u)", filename, uid, gid);
159                 retval = chown(filename, uid, gid);
160                 if (retval)
161                         dbg("chown(%s, %u, %u) failed with error '%s'", filename,
162                             uid, gid, strerror(errno));
163         }
164
165         return retval;
166 }
167
168 static struct sysfs_class_device *get_class_dev(char *device_name)
169 {
170         char dev_path[SYSFS_PATH_MAX];
171         struct sysfs_class_device *class_dev = NULL;
172
173         strcpy(dev_path, sysfs_path);
174         strcat(dev_path, device_name);
175
176         dbg("looking at %s", dev_path);
177
178         /* open up the sysfs class device for this thing... */
179         class_dev = sysfs_open_class_device(dev_path);
180         if (class_dev == NULL) {
181                 dbg ("sysfs_open_class_device failed");
182                 goto exit;
183         }
184         dbg("class_dev->name = %s", class_dev->name);
185
186 exit:
187         return class_dev;
188 }
189
190 /* wait for the "dev" file to show up in the directory in sysfs.
191  * If it doesn't happen in about 10 seconds, give up.
192  */
193 #define SECONDS_TO_WAIT_FOR_DEV         10
194 static int sleep_for_dev(char *path)
195 {
196         char filename[SYSFS_PATH_MAX + 6];
197         int loop = SECONDS_TO_WAIT_FOR_DEV;
198         int retval;
199
200         strcpy(filename, sysfs_path);
201         strcat(filename, path);
202         strcat(filename, "/dev");
203
204         while (loop--) {
205                 struct stat buf;
206
207                 dbg("looking for %s", filename);
208                 retval = stat(filename, &buf);
209                 if (!retval)
210                         goto exit;
211
212                 /* sleep for a second or two to give the kernel a chance to
213                  * create the dev file */
214                 sleep(1);
215         }
216         retval = -ENODEV;
217 exit:
218         return retval;
219 }
220
221 int udev_add_device(char *path, char *subsystem)
222 {
223         struct sysfs_class_device *class_dev;
224         struct udevice dev;
225         int retval = -EINVAL;
226
227         /* for now, the block layer is the only place where block devices are */
228         if (strcmp(subsystem, "block") == 0)
229                 dev.type = 'b';
230         else
231                 dev.type = 'c';
232
233         retval = sleep_for_dev(path);
234         if (retval)
235                 goto exit;
236
237         class_dev = get_class_dev(path);
238         if (class_dev == NULL)
239                 goto exit;
240
241         retval = namedev_name_device(class_dev, &dev);
242         if (retval)
243                 return retval;
244
245         retval = get_major_minor(class_dev, &dev.major, &dev.minor);
246         if (retval) {
247                 dbg("get_major_minor failed");
248                 goto exit;
249         }
250
251 //      strcpy(dev.name, attr.name);
252 //      strcpy(dev.owner, attr.owner);
253 //      strcpy(dev.group, attr.group);
254 //      dev.mode = attr.mode;
255         
256         retval = udevdb_add_dev(path, &dev);
257         if (retval != 0)
258                 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
259                     "But remove might not work properly for this device.");
260
261         sysfs_close_class_device(class_dev);
262
263         dbg("name = %s", dev.name);
264         retval = create_node(&dev);
265
266 exit:
267         return retval;
268 }
269