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