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