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