chiark / gitweb /
174331fadeae7318c24894e0fca79ace631d4e7b
[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 #include <sys/types.h>
32 #include <grp.h>
33 #ifndef __KLIBC__
34 #include <pwd.h>
35 #endif
36
37 #include "udev.h"
38 #include "udev_version.h"
39 #include "namedev.h"
40 #include "udevdb.h"
41 #include "libsysfs/libsysfs.h"
42 #include "klibc_fixups.h"
43
44 /* 
45  * Right now the major/minor of a device is stored in a file called
46  * "dev" in sysfs.
47  * The number is stored as:
48  *      MM:mm
49  *              MM is the major
50  *              mm is the minor
51  *              The value is in decimal.
52  */
53 static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev)
54 {
55         int retval = -ENODEV;
56
57         char *dev;
58
59         dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
60         if (dev == NULL)
61                 goto exit;
62
63         dbg("dev='%s'", dev);
64
65         if (sscanf(dev, "%u:%u", &udev->major, &udev->minor) != 2)
66                 goto exit;
67
68         dbg("found major=%d, minor=%d", udev->major, udev->minor);
69
70         retval = 0;
71 exit:
72         return retval;
73 }
74
75 static int create_path(char *file)
76 {
77         char p[NAME_SIZE];
78         char *pos;
79         int retval;
80         struct stat stats;
81         
82         strncpy(p, file, sizeof(p));
83         pos = strchr(p+1, '/');
84         while (1) {
85                 pos = strchr(pos+1, '/');
86                 if (pos == NULL)
87                         break;
88                 *pos = 0x00;
89                 if (stat(p, &stats)) {
90                         retval = mkdir(p, 0755);
91                         if (retval) {
92                                 dbg("mkdir(%s) failed with error '%s'",
93                                     p, strerror(errno));
94                                 return retval;
95                         }
96                         dbg("created '%s'", p);
97                 }
98                 *pos = '/';
99         }
100         return 0;
101 }
102
103 #ifdef USE_DBUS
104 /** Send out a signal that a device node is created
105  *
106  *  @param  dev                 udevice object
107  *  @param  path                Sysfs path of device
108  */
109 static void sysbus_send_create(struct udevice *dev, const char *path)
110 {
111         char filename[255];
112         DBusMessage* message;
113         DBusMessageIter iter;
114
115         if (sysbus_connection == NULL)
116                 return;
117
118         strncpy(filename, udev_root, sizeof(filename));
119         strncat(filename, dev->name, sizeof(filename));
120
121         /* object, interface, member */
122         message = dbus_message_new_signal("/org/kernel/udev/NodeMonitor", 
123                                           "org.kernel.udev.NodeMonitor",
124                                           "NodeCreated");
125         
126         dbus_message_iter_init(message, &iter);
127         dbus_message_iter_append_string(&iter, filename);
128         dbus_message_iter_append_string(&iter, path);
129
130         if ( !dbus_connection_send(sysbus_connection, message, NULL) )
131                 dbg("error sending d-bus signal");
132
133         dbus_message_unref(message);
134         
135         dbus_connection_flush(sysbus_connection);
136 }
137 #endif /* USE_DBUS */
138
139 /*
140  * we possibly want to add some symlinks here
141  * only numeric owner/group id's are supported
142  */
143 static int create_node(struct udevice *dev)
144 {
145         char filename[255];
146         char linktarget[255];
147         int retval = 0;
148         uid_t uid = 0;
149         gid_t gid = 0;
150         dev_t res;
151         int i;
152         int tail;
153
154
155         strncpy(filename, udev_root, sizeof(filename));
156         strncat(filename, dev->name, sizeof(filename));
157
158 #ifdef __KLIBC__
159         res = (dev->major << 8) | (dev->minor);
160 #else
161         res = makedev(dev->major, dev->minor);
162 #endif
163
164         switch (dev->type) {
165         case 'b':
166                 dev->mode |= S_IFBLK;
167                 break;
168         case 'c':
169         case 'u':
170                 dev->mode |= S_IFCHR;
171                 break;
172         case 'p':
173                 dev->mode |= S_IFIFO;
174                 break;
175         default:
176                 dbg("unknown node type %c\n", dev->type);
177                 return -EINVAL;
178         }
179
180         /* create parent directories if needed */
181         if (strrchr(dev->name, '/'))
182                 create_path(filename);
183
184         dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
185         retval = mknod(filename, dev->mode, res);
186         if (retval)
187                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
188                     filename, dev->mode, dev->major, dev->minor, strerror(errno));
189
190         dbg("chmod(%s, %#o)", filename, dev->mode);
191         retval = chmod(filename, dev->mode);
192         if (retval)
193                 dbg("chmod(%s, %#o) failed with error '%s'",
194                     filename, dev->mode, strerror(errno));
195
196         if (*dev->owner) {
197                 char *endptr;
198                 unsigned long id = strtoul(dev->owner, &endptr, 10);
199                 if (*endptr == 0x00)
200                         uid = (uid_t) id;
201                 else {
202                         struct passwd *pw = getpwnam(dev->owner);
203                         if (!pw)
204                                 dbg("user unknown '%s'", dev->owner);
205                         else
206                                 uid = pw->pw_uid;
207                 }
208         }
209
210         if (*dev->group) {
211                 char *endptr;
212                 unsigned long id = strtoul(dev->group, &endptr, 10);
213                 if (*endptr == 0x00)
214                         gid = (gid_t) id;
215                 else {
216                         struct group *gr = getgrnam(dev->group);
217                         if (!gr)
218                                 dbg("group unknown '%s'", dev->group);
219                         else
220                                 gid = gr->gr_gid;
221                 }
222         }
223
224         if (uid || gid) {
225                 dbg("chown(%s, %u, %u)", filename, uid, gid);
226                 retval = chown(filename, uid, gid);
227                 if (retval)
228                         dbg("chown(%s, %u, %u) failed with error '%s'",
229                             filename, uid, gid, strerror(errno));
230         }
231
232
233         /* create symlink if requested */
234         if (*dev->symlink) {
235                 strncpy(filename, udev_root, sizeof(filename));
236                 strncat(filename, dev->symlink, sizeof(filename));
237                 dbg("symlink '%s' to node '%s' requested", filename, dev->name);
238                 if (strrchr(dev->symlink, '/'))
239                         create_path(filename);
240
241                 /* optimize relative link */
242                 linktarget[0] = '\0';
243                 i = 0;
244                 tail = 0;
245                 while ((dev->name[i] == dev->symlink[i]) && dev->name[i]) {
246                         if (dev->name[i] == '/')
247                                 tail = i+1;
248                         i++;
249                 }
250                 while (dev->symlink[i]) {
251                         if (dev->symlink[i] == '/')
252                                 strcat(linktarget, "../");
253                         i++;
254                 }
255
256                 if (*linktarget == '\0')
257                         strcpy(linktarget, "./");
258                 strcat(linktarget, &dev->name[tail]);
259
260                 dbg("symlink(%s, %s)", linktarget, filename);
261                 retval = symlink(linktarget, filename);
262                 if (retval)
263                         dbg("symlink(%s, %s) failed with error '%s'",
264                             linktarget, filename, strerror(errno));
265         }
266
267         return retval;
268 }
269
270 static struct sysfs_class_device *get_class_dev(char *device_name)
271 {
272         char dev_path[SYSFS_PATH_MAX];
273         struct sysfs_class_device *class_dev = NULL;
274
275         strcpy(dev_path, sysfs_path);
276         strcat(dev_path, device_name);
277
278         dbg("looking at '%s'", dev_path);
279
280         /* open up the sysfs class device for this thing... */
281         class_dev = sysfs_open_class_device(dev_path);
282         if (class_dev == NULL) {
283                 dbg ("sysfs_open_class_device failed");
284                 goto exit;
285         }
286         dbg("class_dev->name='%s'", class_dev->name);
287
288 exit:
289         return class_dev;
290 }
291
292 /* wait for the "dev" file to show up in the directory in sysfs.
293  * If it doesn't happen in about 10 seconds, give up.
294  */
295 #define SECONDS_TO_WAIT_FOR_DEV         10
296 static int sleep_for_dev(char *path)
297 {
298         char filename[SYSFS_PATH_MAX + 6];
299         int loop = SECONDS_TO_WAIT_FOR_DEV;
300         int retval;
301
302         strcpy(filename, sysfs_path);
303         strcat(filename, path);
304         strcat(filename, "/dev");
305
306         while (loop--) {
307                 struct stat buf;
308
309                 dbg("looking for '%s'", filename);
310                 retval = stat(filename, &buf);
311                 if (!retval)
312                         goto exit;
313
314                 /* sleep to give the kernel a chance to create the dev file */
315                 sleep(1);
316         }
317         retval = -ENODEV;
318 exit:
319         return retval;
320 }
321
322 int udev_add_device(char *path, char *subsystem)
323 {
324         struct sysfs_class_device *class_dev = NULL;
325         struct udevice dev;
326         int retval = -EINVAL;
327
328         memset(&dev, 0x00, sizeof(dev));
329
330         /* for now, the block layer is the only place where block devices are */
331         if (strcmp(subsystem, "block") == 0)
332                 dev.type = 'b';
333         else
334                 dev.type = 'c';
335
336         retval = sleep_for_dev(path);
337         if (retval)
338                 goto exit;
339
340         class_dev = get_class_dev(path);
341         if (class_dev == NULL)
342                 goto exit;
343
344         retval = get_major_minor(class_dev, &dev);
345         if (retval) {
346                 dbg("get_major_minor failed");
347                 goto exit;
348         }
349
350         retval = namedev_name_device(class_dev, &dev);
351         if (retval)
352                 goto exit;
353
354         retval = udevdb_add_dev(path, &dev);
355         if (retval != 0)
356                 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
357                     "But remove might not work properly for this device.");
358
359         dbg("name='%s'", dev.name);
360         retval = create_node(&dev);
361
362 #ifdef USE_DBUS
363         if (retval == 0) {
364                 sysbus_send_create(&dev, path);
365         }
366 #endif /* USE_DBUS */
367
368 exit:
369         if (class_dev)
370                 sysfs_close_class_device(class_dev);
371
372         return retval;
373 }