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