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