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