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