]>
Commit | Line | Data |
---|---|---|
ea733a2f GKH |
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> | |
32ff5bca | 30 | #include <sys/stat.h> |
10950dfe GKH |
31 | #include <sys/types.h> |
32 | #include <grp.h> | |
33 | #ifndef __KLIBC__ | |
34 | #include <pwd.h> | |
35 | #endif | |
ea733a2f GKH |
36 | |
37 | #include "udev.h" | |
38 | #include "udev_version.h" | |
7ac0feeb | 39 | #include "udev_dbus.h" |
ea733a2f | 40 | #include "namedev.h" |
8e41d35d | 41 | #include "udevdb.h" |
ea733a2f | 42 | #include "libsysfs/libsysfs.h" |
10950dfe | 43 | #include "klibc_fixups.h" |
ea733a2f | 44 | |
ea733a2f GKH |
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: | |
727d1ba5 | 49 | * MM:mm |
ea733a2f GKH |
50 | * MM is the major |
51 | * mm is the minor | |
727d1ba5 | 52 | * The value is in decimal. |
ea733a2f | 53 | */ |
30defadd | 54 | static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev) |
ea733a2f | 55 | { |
ea256f90 | 56 | int retval = -ENODEV; |
ea733a2f GKH |
57 | char *dev; |
58 | ||
59 | dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev"); | |
60 | if (dev == NULL) | |
ea256f90 | 61 | goto exit; |
f7b4eca4 | 62 | dbg("dev='%s'", dev); |
ea733a2f | 63 | |
30defadd | 64 | if (sscanf(dev, "%u:%u", &udev->major, &udev->minor) != 2) |
ea256f90 | 65 | goto exit; |
f7b4eca4 | 66 | dbg("found major=%d, minor=%d", udev->major, udev->minor); |
ea733a2f GKH |
67 | |
68 | retval = 0; | |
ea256f90 | 69 | exit: |
ea733a2f GKH |
70 | return retval; |
71 | } | |
72 | ||
3d150dfb KS |
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 | ||
5840bc63 | 101 | static int create_node(struct udevice *dev) |
ea733a2f | 102 | { |
ea733a2f | 103 | char filename[255]; |
3d150dfb | 104 | char linktarget[255]; |
4763256c KS |
105 | char *linkname; |
106 | char *symlinks; | |
ea733a2f | 107 | int retval = 0; |
e308ebb7 GKH |
108 | uid_t uid = 0; |
109 | gid_t gid = 0; | |
1e959a4b | 110 | dev_t res; |
3d150dfb KS |
111 | int i; |
112 | int tail; | |
113 | ||
c056c514 | 114 | strncpy(filename, udev_root, sizeof(filename)); |
5840bc63 GKH |
115 | strncat(filename, dev->name, sizeof(filename)); |
116 | ||
1e959a4b GKH |
117 | #ifdef __KLIBC__ |
118 | res = (dev->major << 8) | (dev->minor); | |
119 | #else | |
120 | res = makedev(dev->major, dev->minor); | |
121 | #endif | |
122 | ||
5840bc63 | 123 | switch (dev->type) { |
1331c889 | 124 | case 'b': |
5840bc63 | 125 | dev->mode |= S_IFBLK; |
1331c889 GKH |
126 | break; |
127 | case 'c': | |
128 | case 'u': | |
5840bc63 | 129 | dev->mode |= S_IFCHR; |
1331c889 GKH |
130 | break; |
131 | case 'p': | |
5840bc63 | 132 | dev->mode |= S_IFIFO; |
1331c889 GKH |
133 | break; |
134 | default: | |
5840bc63 | 135 | dbg("unknown node type %c\n", dev->type); |
1331c889 GKH |
136 | return -EINVAL; |
137 | } | |
0abf54fc | 138 | |
3d150dfb KS |
139 | /* create parent directories if needed */ |
140 | if (strrchr(dev->name, '/')) | |
141 | create_path(filename); | |
218eae87 | 142 | |
5840bc63 | 143 | dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor); |
1e959a4b | 144 | retval = mknod(filename, dev->mode, res); |
1331c889 GKH |
145 | if (retval) |
146 | dbg("mknod(%s, %#o, %u, %u) failed with error '%s'", | |
5840bc63 GKH |
147 | filename, dev->mode, dev->major, dev->minor, strerror(errno)); |
148 | ||
5bd1061c GKH |
149 | dbg("chmod(%s, %#o)", filename, dev->mode); |
150 | retval = chmod(filename, dev->mode); | |
151 | if (retval) | |
152 | dbg("chmod(%s, %#o) failed with error '%s'", | |
153 | filename, dev->mode, strerror(errno)); | |
154 | ||
c19a6b30 KS |
155 | if (*dev->owner) { |
156 | char *endptr; | |
157 | unsigned long id = strtoul(dev->owner, &endptr, 10); | |
158 | if (*endptr == 0x00) | |
159 | uid = (uid_t) id; | |
10950dfe GKH |
160 | else { |
161 | struct passwd *pw = getpwnam(dev->owner); | |
162 | if (!pw) | |
f7b4eca4 | 163 | dbg("user unknown '%s'", dev->owner); |
10950dfe GKH |
164 | else |
165 | uid = pw->pw_uid; | |
166 | } | |
c19a6b30 KS |
167 | } |
168 | ||
169 | if (*dev->group) { | |
170 | char *endptr; | |
171 | unsigned long id = strtoul(dev->group, &endptr, 10); | |
172 | if (*endptr == 0x00) | |
173 | gid = (gid_t) id; | |
10950dfe GKH |
174 | else { |
175 | struct group *gr = getgrnam(dev->group); | |
176 | if (!gr) | |
f7b4eca4 | 177 | dbg("group unknown '%s'", dev->group); |
10950dfe GKH |
178 | else |
179 | gid = gr->gr_gid; | |
180 | } | |
c19a6b30 KS |
181 | } |
182 | ||
183 | if (uid || gid) { | |
184 | dbg("chown(%s, %u, %u)", filename, uid, gid); | |
185 | retval = chown(filename, uid, gid); | |
186 | if (retval) | |
3d150dfb KS |
187 | dbg("chown(%s, %u, %u) failed with error '%s'", |
188 | filename, uid, gid, strerror(errno)); | |
189 | } | |
190 | ||
3d150dfb KS |
191 | /* create symlink if requested */ |
192 | if (*dev->symlink) { | |
4763256c KS |
193 | symlinks = dev->symlink; |
194 | while (1) { | |
195 | linkname = strsep(&symlinks, " "); | |
196 | if (linkname == NULL) | |
197 | break; | |
198 | ||
199 | strncpy(filename, udev_root, sizeof(filename)); | |
200 | strncat(filename, linkname, sizeof(filename)); | |
201 | dbg("symlink '%s' to node '%s' requested", filename, dev->name); | |
202 | if (strrchr(linkname, '/')) | |
203 | create_path(filename); | |
204 | ||
205 | /* optimize relative link */ | |
206 | linktarget[0] = '\0'; | |
207 | i = 0; | |
208 | tail = 0; | |
209 | while ((dev->name[i] == linkname[i]) && dev->name[i]) { | |
210 | if (dev->name[i] == '/') | |
211 | tail = i+1; | |
212 | i++; | |
213 | } | |
214 | while (linkname[i]) { | |
215 | if (linkname[i] == '/') | |
216 | strcat(linktarget, "../"); | |
217 | i++; | |
218 | } | |
3d150dfb | 219 | |
4763256c KS |
220 | if (*linktarget == '\0') |
221 | strcpy(linktarget, "./"); | |
222 | strcat(linktarget, &dev->name[tail]); | |
3d150dfb | 223 | |
4763256c KS |
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 | } | |
c19a6b30 KS |
230 | } |
231 | ||
ea733a2f GKH |
232 | return retval; |
233 | } | |
234 | ||
d7e954a4 | 235 | static struct sysfs_class_device *get_class_dev(char *device_name) |
ea733a2f GKH |
236 | { |
237 | char dev_path[SYSFS_PATH_MAX]; | |
63dde9f8 GKH |
238 | struct sysfs_class_device *class_dev = NULL; |
239 | ||
ea733a2f GKH |
240 | strcpy(dev_path, sysfs_path); |
241 | strcat(dev_path, device_name); | |
f7b4eca4 | 242 | dbg("looking at '%s'", dev_path); |
ea733a2f GKH |
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"); | |
63dde9f8 | 248 | goto exit; |
ea733a2f | 249 | } |
f7b4eca4 | 250 | dbg("class_dev->name='%s'", class_dev->name); |
ea733a2f | 251 | |
63dde9f8 | 252 | exit: |
ea733a2f GKH |
253 | return class_dev; |
254 | } | |
255 | ||
29fd7e67 GKH |
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 | |
6089318c | 260 | static int sleep_for_dev(char *path) |
29fd7e67 GKH |
261 | { |
262 | char filename[SYSFS_PATH_MAX + 6]; | |
c332cfc7 RL |
263 | int loop = SECONDS_TO_WAIT_FOR_DEV; |
264 | int retval; | |
29fd7e67 GKH |
265 | |
266 | strcpy(filename, sysfs_path); | |
5840bc63 | 267 | strcat(filename, path); |
29fd7e67 GKH |
268 | strcat(filename, "/dev"); |
269 | ||
c332cfc7 RL |
270 | while (loop--) { |
271 | struct stat buf; | |
272 | ||
f7b4eca4 | 273 | dbg("looking for '%s'", filename); |
29fd7e67 | 274 | retval = stat(filename, &buf); |
c332cfc7 | 275 | if (!retval) |
29fd7e67 | 276 | goto exit; |
29fd7e67 | 277 | |
f7b4eca4 | 278 | /* sleep to give the kernel a chance to create the dev file */ |
29fd7e67 GKH |
279 | sleep(1); |
280 | } | |
281 | retval = -ENODEV; | |
282 | exit: | |
283 | return retval; | |
284 | } | |
285 | ||
5840bc63 | 286 | int udev_add_device(char *path, char *subsystem) |
ea733a2f | 287 | { |
30defadd | 288 | struct sysfs_class_device *class_dev = NULL; |
5840bc63 | 289 | struct udevice dev; |
ea733a2f GKH |
290 | int retval = -EINVAL; |
291 | ||
f3b04a2e GKH |
292 | memset(&dev, 0x00, sizeof(dev)); |
293 | ||
ea733a2f GKH |
294 | /* for now, the block layer is the only place where block devices are */ |
295 | if (strcmp(subsystem, "block") == 0) | |
5840bc63 | 296 | dev.type = 'b'; |
ea733a2f | 297 | else |
5840bc63 | 298 | dev.type = 'c'; |
ea733a2f | 299 | |
5840bc63 | 300 | retval = sleep_for_dev(path); |
29fd7e67 GKH |
301 | if (retval) |
302 | goto exit; | |
63dde9f8 | 303 | |
5840bc63 | 304 | class_dev = get_class_dev(path); |
ea733a2f GKH |
305 | if (class_dev == NULL) |
306 | goto exit; | |
307 | ||
30defadd | 308 | retval = get_major_minor(class_dev, &dev); |
ea733a2f | 309 | if (retval) { |
ca999860 | 310 | dbg("get_major_minor failed"); |
ea733a2f GKH |
311 | goto exit; |
312 | } | |
313 | ||
30defadd GKH |
314 | retval = namedev_name_device(class_dev, &dev); |
315 | if (retval) | |
316 | goto exit; | |
317 | ||
5840bc63 | 318 | retval = udevdb_add_dev(path, &dev); |
8e41d35d | 319 | if (retval != 0) |
5840bc63 | 320 | dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. " |
ca999860 GKH |
321 | "But remove might not work properly for this device."); |
322 | ||
f7b4eca4 | 323 | dbg("name='%s'", dev.name); |
5840bc63 | 324 | retval = create_node(&dev); |
ea733a2f | 325 | |
7ac0feeb GKH |
326 | if (retval == 0) |
327 | sysbus_send_create(&dev, path); | |
5aebfbcb | 328 | |
ea733a2f | 329 | exit: |
30defadd GKH |
330 | if (class_dev) |
331 | sysfs_close_class_device(class_dev); | |
332 | ||
ea733a2f GKH |
333 | return retval; |
334 | } |