chiark / gitweb /
3b3ebd5bbbcce4e397cc5fe1c9a8f7d016ac5daa
[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 make_node(char *filename, int major, int minor, unsigned int mode, uid_t uid, gid_t gid)
103 {
104         int retval;
105
106         retval = mknod(filename, mode, makedev(major, minor));
107         if (retval != 0) {
108                 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
109                     filename, mode, major, minor, strerror(errno));
110                 return retval;
111         }
112
113         dbg("chmod(%s, %#o)", filename, mode);
114         retval = chmod(filename, mode);
115         if (retval != 0) {
116                 dbg("chmod(%s, %#o) failed with error '%s'",
117                     filename, mode, strerror(errno));
118                 return retval;
119         }
120
121         if (uid != 0 || gid != 0) {
122                 dbg("chown(%s, %u, %u)", filename, uid, gid);
123                 retval = chown(filename, uid, gid);
124                 if (retval != 0) {
125                         dbg("chown(%s, %u, %u) failed with error '%s'",
126                             filename, uid, gid, strerror(errno));
127                         return retval;
128                 }
129         }
130
131         return 0;
132 }
133
134 static int create_node(struct udevice *dev, int fake)
135 {
136         struct stat stats;
137         char filename[255];
138         char linktarget[255];
139         char partitionname[255];
140         char *linkname;
141         char *symlinks;
142         int retval = 0;
143         uid_t uid = 0;
144         gid_t gid = 0;
145         int i;
146         int tail;
147
148         strncpy(filename, udev_root, sizeof(filename));
149         strncat(filename, dev->name, sizeof(filename));
150
151         switch (dev->type) {
152         case 'b':
153                 dev->mode |= S_IFBLK;
154                 break;
155         case 'c':
156         case 'u':
157                 dev->mode |= S_IFCHR;
158                 break;
159         case 'p':
160                 dev->mode |= S_IFIFO;
161                 break;
162         default:
163                 dbg("unknown node type %c\n", dev->type);
164                 return -EINVAL;
165         }
166
167         /* create parent directories if needed */
168         if (strrchr(dev->name, '/'))
169                 create_path(filename);
170
171         if (dev->owner[0] != '\0') {
172                 char *endptr;
173                 unsigned long id = strtoul(dev->owner, &endptr, 10);
174                 if (endptr[0] == '\0')
175                         uid = (uid_t) id;
176                 else {
177                         struct passwd *pw = getpwnam(dev->owner);
178                         if (pw == NULL)
179                                 dbg("specified user unknown '%s'", dev->owner);
180                         else
181                                 uid = pw->pw_uid;
182                 }
183         }
184
185         if (dev->group[0] != '\0') {
186                 char *endptr;
187                 unsigned long id = strtoul(dev->group, &endptr, 10);
188                 if (endptr[0] == '\0')
189                         gid = (gid_t) id;
190                 else {
191                         struct group *gr = getgrnam(dev->group);
192                         if (gr == NULL)
193                                 dbg("specified group unknown '%s'", dev->group);
194                         else
195                                 gid = gr->gr_gid;
196                 }
197         }
198
199         if (!fake)
200                 info("creating device node '%s'", filename);
201                 make_node(filename, dev->major, dev->minor, dev->mode, uid, gid);
202
203         /* create partitions if requested */
204         if (dev->partitions > 0) {
205                 info("creating device partition nodes '%s[1-%i]'", filename, dev->partitions);
206                 for (i = 1; i <= dev->partitions; i++) {
207                         sprintf(partitionname, "%s%i", filename, i);
208                         make_node(partitionname, dev->major, dev->minor + i,
209                                     dev->mode, uid, gid);
210                 }
211         }
212
213         /* create symlink if requested */
214         if (dev->symlink[0] != '\0') {
215                 symlinks = dev->symlink;
216                 while (1) {
217                         linkname = strsep(&symlinks, " ");
218                         if (linkname == NULL || linkname[0] == '\0')
219                                 break;
220
221                         strncpy(filename, udev_root, sizeof(filename));
222                         strncat(filename, linkname, sizeof(filename));
223                         dbg("symlink '%s' to node '%s' requested", filename, dev->name);
224                         if (!fake)
225                                 if (strrchr(linkname, '/'))
226                                         create_path(filename);
227
228                         /* optimize relative link */
229                         linktarget[0] = '\0';
230                         i = 0;
231                         tail = 0;
232                         while ((dev->name[i] == linkname[i]) && dev->name[i]) {
233                                 if (dev->name[i] == '/')
234                                         tail = i+1;
235                                 i++;
236                         }
237                         while (linkname[i] != '\0') {
238                                 if (linkname[i] == '/')
239                                         strcat(linktarget, "../");
240                                 i++;
241                         }
242
243                         if (linktarget[0] == '\0')
244                                 strcpy(linktarget, "./");
245                         strcat(linktarget, &dev->name[tail]);
246
247                         /* unlink existing files to ensure that our symlink is created */
248                         if (!fake && (lstat(filename, &stats) == 0)) {
249                                 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
250                                         if (unlink(filename))
251                                                 dbg("unlink(%s) failed with error '%s'",
252                                                     filename, strerror(errno));
253                                 }
254                         }
255
256                         dbg("symlink(%s, %s)", linktarget, filename);
257                         if (!fake) {
258                                 retval = symlink(linktarget, filename);
259                                 if (retval != 0)
260                                         dbg("symlink(%s, %s) failed with error '%s'",
261                                             linktarget, filename, strerror(errno));
262                         }
263                 }
264         }
265
266         return retval;
267 }
268
269 static struct sysfs_class_device *get_class_dev(char *device_name)
270 {
271         char dev_path[SYSFS_PATH_MAX];
272         struct sysfs_class_device *class_dev = NULL;
273
274         strcpy(dev_path, sysfs_path);
275         strcat(dev_path, device_name);
276         dbg("looking at '%s'", dev_path);
277
278         /* open up the sysfs class device for this thing... */
279         class_dev = sysfs_open_class_device_path(dev_path);
280         if (class_dev == NULL) {
281                 dbg ("sysfs_open_class_device_path failed");
282                 goto exit;
283         }
284         dbg("class_dev->name='%s'", class_dev->name);
285
286 exit:
287         return class_dev;
288 }
289
290 /* wait for the "dev" file to show up in the directory in sysfs.
291  * If it doesn't happen in about 10 seconds, give up.
292  */
293 #define SECONDS_TO_WAIT_FOR_DEV         10
294 static int sleep_for_dev(char *path)
295 {
296         char filename[SYSFS_PATH_MAX + 6];
297         int loop = SECONDS_TO_WAIT_FOR_DEV;
298         int retval;
299
300         strcpy(filename, sysfs_path);
301         strcat(filename, path);
302         strcat(filename, "/dev");
303
304         while (loop--) {
305                 struct stat buf;
306
307                 dbg("looking for '%s'", filename);
308                 retval = stat(filename, &buf);
309                 if (retval == 0)
310                         goto exit;
311
312                 /* sleep to give the kernel a chance to create the dev file */
313                 sleep(1);
314         }
315         retval = -ENODEV;
316 exit:
317         return retval;
318 }
319
320 int udev_add_device(char *path, char *subsystem, int fake)
321 {
322         struct sysfs_class_device *class_dev = NULL;
323         struct udevice dev;
324         int retval = -EINVAL;
325
326         memset(&dev, 0x00, sizeof(dev));
327
328         /* for now, the block layer is the only place where block devices are */
329         if (strcmp(subsystem, "block") == 0)
330                 dev.type = 'b';
331         else
332                 dev.type = 'c';
333
334         retval = sleep_for_dev(path);
335         if (retval != 0)
336                 goto exit;
337
338         class_dev = get_class_dev(path);
339         if (class_dev == NULL)
340                 goto exit;
341
342         retval = get_major_minor(class_dev, &dev);
343         if (retval != 0) {
344                 dbg("get_major_minor failed");
345                 goto exit;
346         }
347
348         retval = namedev_name_device(class_dev, &dev);
349         if (retval != 0)
350                 goto exit;
351
352         if (!fake) {
353                 retval = udevdb_add_dev(path, &dev);
354                 if (retval != 0)
355                         dbg("udevdb_add_dev failed, but we are going to try "
356                             "to create the node anyway. But remove might not "
357                             "work properly for this device.");
358
359         }
360         dbg("name='%s'", dev.name);
361         retval = create_node(&dev, fake);
362
363         if ((retval == 0) && (!fake))
364                 sysbus_send_create(&dev, path);
365
366 exit:
367         if (class_dev)
368                 sysfs_close_class_device(class_dev);
369
370         return retval;
371 }