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