chiark / gitweb /
[PATCH] udevd - next round of fixes
[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) {
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)
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         dev_t res;
113         int i;
114         int tail;
115
116         strncpy(filename, udev_root, sizeof(filename));
117         strncat(filename, dev->name, sizeof(filename));
118
119 #ifdef __KLIBC__
120         res = (dev->major << 8) | (dev->minor);
121 #else
122         res = makedev(dev->major, dev->minor);
123 #endif
124
125         switch (dev->type) {
126         case 'b':
127                 dev->mode |= S_IFBLK;
128                 break;
129         case 'c':
130         case 'u':
131                 dev->mode |= S_IFCHR;
132                 break;
133         case 'p':
134                 dev->mode |= S_IFIFO;
135                 break;
136         default:
137                 dbg("unknown node type %c\n", dev->type);
138                 return -EINVAL;
139         }
140
141         /* create parent directories if needed */
142         if (strrchr(dev->name, '/'))
143                 create_path(filename);
144
145         info("creating device node '%s'", filename);
146         dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
147         retval = mknod(filename, dev->mode, res);
148         if (retval)
149                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
150                     filename, dev->mode, dev->major, dev->minor, strerror(errno));
151
152         dbg("chmod(%s, %#o)", filename, dev->mode);
153         retval = chmod(filename, dev->mode);
154         if (retval)
155                 dbg("chmod(%s, %#o) failed with error '%s'",
156                     filename, dev->mode, strerror(errno));
157
158         if (dev->owner[0]) {
159                 char *endptr;
160                 unsigned long id = strtoul(dev->owner, &endptr, 10);
161                 if (endptr[0] == '\0')
162                         uid = (uid_t) id;
163                 else {
164                         struct passwd *pw = getpwnam(dev->owner);
165                         if (pw == NULL)
166                                 dbg("specified user unknown '%s'", dev->owner);
167                         else
168                                 uid = pw->pw_uid;
169                 }
170         }
171
172         if (dev->group[0]) {
173                 char *endptr;
174                 unsigned long id = strtoul(dev->group, &endptr, 10);
175                 if (endptr[0] == '\0')
176                         gid = (gid_t) id;
177                 else {
178                         struct group *gr = getgrnam(dev->group);
179                         if (gr == NULL)
180                                 dbg("specified group unknown '%s'", dev->group);
181                         else
182                                 gid = gr->gr_gid;
183                 }
184         }
185
186         if (uid || gid) {
187                 dbg("chown(%s, %u, %u)", filename, uid, gid);
188                 retval = chown(filename, uid, gid);
189                 if (retval)
190                         dbg("chown(%s, %u, %u) failed with error '%s'",
191                             filename, uid, gid, strerror(errno));
192         }
193
194         /* create symlink if requested */
195         if (dev->symlink[0]) {
196                 symlinks = dev->symlink;
197                 while (1) {
198                         linkname = strsep(&symlinks, " ");
199                         if (linkname == NULL || linkname[0] == '\0')
200                                 break;
201
202                         strncpy(filename, udev_root, sizeof(filename));
203                         strncat(filename, linkname, sizeof(filename));
204                         dbg("symlink '%s' to node '%s' requested", filename, dev->name);
205                         if (strrchr(linkname, '/'))
206                                 create_path(filename);
207
208                         /* optimize relative link */
209                         linktarget[0] = '\0';
210                         i = 0;
211                         tail = 0;
212                         while ((dev->name[i] == linkname[i]) && dev->name[i]) {
213                                 if (dev->name[i] == '/')
214                                         tail = i+1;
215                                 i++;
216                         }
217                         while (linkname[i]) {
218                                 if (linkname[i] == '/')
219                                         strcat(linktarget, "../");
220                                 i++;
221                         }
222
223                         if (*linktarget == '\0')
224                                 strcpy(linktarget, "./");
225                         strcat(linktarget, &dev->name[tail]);
226
227                         /* unlink existing non-directories to ensure that our symlink
228                          * is created */
229                         if (lstat(filename, &stats) == 0) {
230                                 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
231                                         if (unlink(filename))
232                                                 dbg("unlink(%s) failed with error '%s'",
233                                                     filename, strerror(errno));
234                                 }
235                         }
236
237                         dbg("symlink(%s, %s)", linktarget, filename);
238                         retval = symlink(linktarget, filename);
239                         if (retval)
240                                 dbg("symlink(%s, %s) failed with error '%s'",
241                                     linktarget, filename, strerror(errno));
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)
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)
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)
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) {
323                 dbg("get_major_minor failed");
324                 goto exit;
325         }
326
327         retval = namedev_name_device(class_dev, &dev);
328         if (retval)
329                 goto exit;
330
331         retval = udevdb_add_dev(path, &dev);
332         if (retval != 0)
333                 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
334                     "But remove might not work properly for this device.");
335
336         dbg("name='%s'", dev.name);
337         retval = create_node(&dev);
338
339         if (retval == 0)
340                 sysbus_send_create(&dev, path);
341
342 exit:
343         if (class_dev)
344                 sysfs_close_class_device(class_dev);
345
346         return retval;
347 }