chiark / gitweb /
[PATCH] replace fgets() with mmap() and introduce udev_lib.[hc]
[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 #include <utmp.h>
36 #endif
37
38 #include "libsysfs/sysfs/libsysfs.h"
39 #include "udev.h"
40 #include "udev_lib.h"
41 #include "udev_version.h"
42 #include "udev_dbus.h"
43 #include "udev_selinux.h"
44 #include "logging.h"
45 #include "namedev.h"
46 #include "udevdb.h"
47 #include "klibc_fixups.h"
48
49 #define LOCAL_USER "$local"
50
51 /* 
52  * Right now the major/minor of a device is stored in a file called
53  * "dev" in sysfs.
54  * The number is stored as:
55  *      MM:mm
56  *              MM is the major
57  *              mm is the minor
58  *              The value is in decimal.
59  */
60 static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev)
61 {
62         int retval = -ENODEV;
63         struct sysfs_attribute *attr = NULL;
64
65         attr = sysfs_get_classdev_attr(class_dev, "dev");
66         if (attr == NULL)
67                 goto exit;
68         dbg("dev='%s'", attr->value);
69
70         if (sscanf(attr->value, "%u:%u", &udev->major, &udev->minor) != 2)
71                 goto exit;
72         dbg("found major=%d, minor=%d", udev->major, udev->minor);
73
74         retval = 0;
75 exit:
76         return retval;
77 }
78
79 static int create_path(char *file)
80 {
81         char p[NAME_SIZE];
82         char *pos;
83         int retval;
84         struct stat stats;
85         
86         strfieldcpy(p, file);
87         pos = strchr(p+1, '/');
88         while (1) {
89                 pos = strchr(pos+1, '/');
90                 if (pos == NULL)
91                         break;
92                 *pos = 0x00;
93                 if (stat(p, &stats)) {
94                         retval = mkdir(p, 0755);
95                         if (retval != 0) {
96                                 dbg("mkdir(%s) failed with error '%s'",
97                                     p, strerror(errno));
98                                 return retval;
99                         }
100                         dbg("created '%s'", p);
101                 }
102                 *pos = '/';
103         }
104         return 0;
105 }
106
107 static int make_node(char *filename, int major, int minor, unsigned int mode, uid_t uid, gid_t gid)
108 {
109         int retval;
110
111         retval = mknod(filename, mode, makedev(major, minor));
112         if (retval != 0) {
113                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
114                     filename, mode, major, minor, strerror(errno));
115                 return retval;
116         }
117
118         dbg("chmod(%s, %#o)", filename, mode);
119         retval = chmod(filename, mode);
120         if (retval != 0) {
121                 dbg("chmod(%s, %#o) failed with error '%s'",
122                     filename, mode, strerror(errno));
123                 return retval;
124         }
125
126         if (uid != 0 || gid != 0) {
127                 dbg("chown(%s, %u, %u)", filename, uid, gid);
128                 retval = chown(filename, uid, gid);
129                 if (retval != 0) {
130                         dbg("chown(%s, %u, %u) failed with error '%s'",
131                             filename, uid, gid, strerror(errno));
132                         return retval;
133                 }
134         }
135
136         return 0;
137 }
138
139 /* get the local logged in user */
140 static void set_to_local_user(char *user)
141 {
142         struct utmp *u;
143         time_t recent = 0;
144
145         strfieldcpymax(user, default_owner_str, OWNER_SIZE);
146         setutent();
147         while (1) {
148                 u = getutent();
149                 if (u == NULL)
150                         break;
151
152                 /* is this a user login ? */
153                 if (u->ut_type != USER_PROCESS)
154                         continue;
155
156                 /* is this a local login ? */
157                 if (strcmp(u->ut_host, ""))
158                         continue;
159
160                 if (u->ut_time > recent) {
161                         recent = u->ut_time;
162                         strfieldcpymax(user, u->ut_user, OWNER_SIZE);
163                         dbg("local user is '%s'", user);
164                         break;
165                 }
166         }
167         endutent();
168 }
169
170 /* Used to unlink existing files to ensure that our new file/symlink is created */
171 static int unlink_entry(char *filename)
172 {
173         struct stat stats;
174         int retval = 0;
175         
176         if (lstat(filename, &stats) == 0) {
177                 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
178                         retval = unlink(filename);
179                         if (retval) {
180                                 dbg("unlink(%s) failed with error '%s'",
181                                     filename, strerror(errno));
182                         }
183                 }
184         }
185         return retval;
186 }
187
188 static int create_node(struct udevice *dev, int fake)
189 {
190         char filename[NAME_SIZE];
191         char linkname[NAME_SIZE];
192         char linktarget[NAME_SIZE];
193         char partitionname[NAME_SIZE];
194         int retval = 0;
195         uid_t uid = 0;
196         gid_t gid = 0;
197         int i;
198         int tail;
199         char *pos;
200         int len;
201
202         strfieldcpy(filename, udev_root);
203         strfieldcat(filename, dev->name);
204
205         switch (dev->type) {
206         case 'b':
207                 dev->mode |= S_IFBLK;
208                 break;
209         case 'c':
210         case 'u':
211                 dev->mode |= S_IFCHR;
212                 break;
213         case 'p':
214                 dev->mode |= S_IFIFO;
215                 break;
216         default:
217                 dbg("unknown node type %c\n", dev->type);
218                 return -EINVAL;
219         }
220
221         /* create parent directories if needed */
222         if (strrchr(dev->name, '/'))
223                 create_path(filename);
224
225         if (dev->owner[0] != '\0') {
226                 char *endptr;
227                 unsigned long id = strtoul(dev->owner, &endptr, 10);
228                 if (endptr[0] == '\0')
229                         uid = (uid_t) id;
230                 else {
231                         struct passwd *pw;
232                         if (strncmp(dev->owner, LOCAL_USER, sizeof(LOCAL_USER)) == 0)
233                                 set_to_local_user(dev->owner);
234
235                         pw = getpwnam(dev->owner);
236                         if (pw == NULL)
237                                 dbg("specified user unknown '%s'", dev->owner);
238                         else
239                                 uid = pw->pw_uid;
240                 }
241         }
242
243         if (dev->group[0] != '\0') {
244                 char *endptr;
245                 unsigned long id = strtoul(dev->group, &endptr, 10);
246                 if (endptr[0] == '\0')
247                         gid = (gid_t) id;
248                 else {
249                         struct group *gr = getgrnam(dev->group);
250                         if (gr == NULL)
251                                 dbg("specified group unknown '%s'", dev->group);
252                         else
253                                 gid = gr->gr_gid;
254                 }
255         }
256
257         if (!fake) {
258                 unlink_entry(filename);
259                 info("creating device node '%s'", filename);
260                 make_node(filename, dev->major, dev->minor, dev->mode, uid, gid);
261         } else {
262                 info("creating device node '%s', major = '%d', minor = '%d', "
263                      "mode = '%#o', uid = '%d', gid = '%d'", filename,
264                      dev->major, dev->minor, (mode_t)dev->mode, uid, gid);
265         }
266
267         /* create partitions if requested */
268         if (dev->partitions > 0) {
269                 info("creating device partition nodes '%s[1-%i]'", filename, dev->partitions);
270                 if (!fake) {
271                         for (i = 1; i <= dev->partitions; i++) {
272                                 strfieldcpy(partitionname, filename);
273                                 strintcat(partitionname, i);
274                                 unlink_entry(partitionname);
275                                 make_node(partitionname, dev->major,
276                                           dev->minor + i, dev->mode, uid, gid);
277                         }
278                 }
279         }
280
281         if (!fake)
282                 selinux_add_node(filename);
283
284         /* create symlink if requested */
285         foreach_strpart(dev->symlink, " ", pos, len) {
286                 strfieldcpymax(linkname, pos, len+1);
287                 strfieldcpy(filename, udev_root);
288                 strfieldcat(filename, linkname);
289                 dbg("symlink '%s' to node '%s' requested", filename, dev->name);
290                 if (!fake)
291                         if (strrchr(linkname, '/'))
292                                 create_path(filename);
293
294                 /* optimize relative link */
295                 linktarget[0] = '\0';
296                 i = 0;
297                 tail = 0;
298                 while ((dev->name[i] == linkname[i]) && dev->name[i]) {
299                         if (dev->name[i] == '/')
300                                 tail = i+1;
301                         i++;
302                 }
303                 while (linkname[i] != '\0') {
304                         if (linkname[i] == '/')
305                                 strfieldcat(linktarget, "../");
306                         i++;
307                 }
308
309                 strfieldcat(linktarget, &dev->name[tail]);
310
311                 if (!fake)
312                         unlink_entry(filename);
313
314                 dbg("symlink(%s, %s)", linktarget, filename);
315                 if (!fake) {
316                         retval = symlink(linktarget, filename);
317                         if (retval != 0)
318                                 dbg("symlink(%s, %s) failed with error '%s'",
319                                     linktarget, filename, strerror(errno));
320                 }
321         }
322
323         return retval;
324 }
325
326 static struct sysfs_class_device *get_class_dev(char *device_name)
327 {
328         char dev_path[SYSFS_PATH_MAX];
329         struct sysfs_class_device *class_dev = NULL;
330
331         strfieldcpy(dev_path, sysfs_path);
332         strfieldcat(dev_path, device_name);
333         dbg("looking at '%s'", dev_path);
334
335         /* open up the sysfs class device for this thing... */
336         class_dev = sysfs_open_class_device_path(dev_path);
337         if (class_dev == NULL) {
338                 dbg ("sysfs_open_class_device_path failed");
339                 goto exit;
340         }
341         dbg("class_dev->name='%s'", class_dev->name);
342
343 exit:
344         return class_dev;
345 }
346
347 /* wait for the "dev" file to show up in the directory in sysfs.
348  * If it doesn't happen in about 10 seconds, give up.
349  */
350 #define SECONDS_TO_WAIT_FOR_DEV         10
351 static int sleep_for_dev(char *path)
352 {
353         char filename[SYSFS_PATH_MAX + 6];
354         int loop = SECONDS_TO_WAIT_FOR_DEV;
355         int retval;
356
357         strfieldcpy(filename, sysfs_path);
358         strfieldcat(filename, path);
359         strfieldcat(filename, "/dev");
360
361         while (loop--) {
362                 struct stat buf;
363
364                 dbg("looking for '%s'", filename);
365                 retval = stat(filename, &buf);
366                 if (retval == 0)
367                         goto exit;
368
369                 /* sleep to give the kernel a chance to create the dev file */
370                 sleep(1);
371         }
372         retval = -ENODEV;
373 exit:
374         return retval;
375 }
376
377 int udev_add_device(char *path, char *subsystem, int fake)
378 {
379         struct sysfs_class_device *class_dev = NULL;
380         struct udevice dev;
381         int retval = -EINVAL;
382
383         memset(&dev, 0x00, sizeof(dev));
384
385         /* for now, the block layer is the only place where block devices are */
386         if (strcmp(subsystem, "block") == 0)
387                 dev.type = 'b';
388         else
389                 dev.type = 'c';
390
391         retval = sleep_for_dev(path);
392         if (retval != 0)
393                 goto exit;
394
395         class_dev = get_class_dev(path);
396         if (class_dev == NULL)
397                 goto exit;
398
399         retval = get_major_minor(class_dev, &dev);
400         if (retval != 0) {
401                 dbg("get_major_minor failed");
402                 goto exit;
403         }
404
405         retval = namedev_name_device(class_dev, &dev);
406         if (retval != 0)
407                 goto exit;
408
409         if (!fake) {
410                 retval = udevdb_add_dev(path, &dev);
411                 if (retval != 0)
412                         dbg("udevdb_add_dev failed, but we are going to try "
413                             "to create the node anyway. But remove might not "
414                             "work properly for this device.");
415
416         }
417         dbg("name='%s'", dev.name);
418         retval = create_node(&dev, fake);
419
420         if ((retval == 0) && (!fake))
421                 sysbus_send_create(&dev, path);
422
423 exit:
424         if (class_dev)
425                 sysfs_close_class_device(class_dev);
426
427         return retval;
428 }