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