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