6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
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.
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.
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.
31 #include <sys/types.h>
38 #include "udev_version.h"
39 #include "udev_dbus.h"
42 #include "libsysfs/libsysfs.h"
43 #include "klibc_fixups.h"
46 * Right now the major/minor of a device is stored in a file called
48 * The number is stored as:
52 * The value is in decimal.
54 static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev)
59 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
64 if (sscanf(dev, "%u:%u", &udev->major, &udev->minor) != 2)
66 dbg("found major=%d, minor=%d", udev->major, udev->minor);
73 static int create_path(char *file)
80 strncpy(p, file, sizeof(p));
81 pos = strchr(p+1, '/');
83 pos = strchr(pos+1, '/');
87 if (stat(p, &stats)) {
88 retval = mkdir(p, 0755);
90 dbg("mkdir(%s) failed with error '%s'",
94 dbg("created '%s'", p);
101 static int create_node(struct udevice *dev)
104 char linktarget[255];
112 strncpy(filename, udev_root, sizeof(filename));
113 strncat(filename, dev->name, sizeof(filename));
116 res = (dev->major << 8) | (dev->minor);
118 res = makedev(dev->major, dev->minor);
123 dev->mode |= S_IFBLK;
127 dev->mode |= S_IFCHR;
130 dev->mode |= S_IFIFO;
133 dbg("unknown node type %c\n", dev->type);
137 /* create parent directories if needed */
138 if (strrchr(dev->name, '/'))
139 create_path(filename);
141 dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
142 retval = mknod(filename, dev->mode, res);
144 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
145 filename, dev->mode, dev->major, dev->minor, strerror(errno));
147 dbg("chmod(%s, %#o)", filename, dev->mode);
148 retval = chmod(filename, dev->mode);
150 dbg("chmod(%s, %#o) failed with error '%s'",
151 filename, dev->mode, strerror(errno));
155 unsigned long id = strtoul(dev->owner, &endptr, 10);
159 struct passwd *pw = getpwnam(dev->owner);
161 dbg("user unknown '%s'", dev->owner);
169 unsigned long id = strtoul(dev->group, &endptr, 10);
173 struct group *gr = getgrnam(dev->group);
175 dbg("group unknown '%s'", dev->group);
182 dbg("chown(%s, %u, %u)", filename, uid, gid);
183 retval = chown(filename, uid, gid);
185 dbg("chown(%s, %u, %u) failed with error '%s'",
186 filename, uid, gid, strerror(errno));
190 /* create symlink if requested */
192 strncpy(filename, udev_root, sizeof(filename));
193 strncat(filename, dev->symlink, sizeof(filename));
194 dbg("symlink '%s' to node '%s' requested", filename, dev->name);
195 if (strrchr(dev->symlink, '/'))
196 create_path(filename);
198 /* optimize relative link */
199 linktarget[0] = '\0';
202 while ((dev->name[i] == dev->symlink[i]) && dev->name[i]) {
203 if (dev->name[i] == '/')
207 while (dev->symlink[i]) {
208 if (dev->symlink[i] == '/')
209 strcat(linktarget, "../");
213 if (*linktarget == '\0')
214 strcpy(linktarget, "./");
215 strcat(linktarget, &dev->name[tail]);
217 dbg("symlink(%s, %s)", linktarget, filename);
218 retval = symlink(linktarget, filename);
220 dbg("symlink(%s, %s) failed with error '%s'",
221 linktarget, filename, strerror(errno));
227 static struct sysfs_class_device *get_class_dev(char *device_name)
229 char dev_path[SYSFS_PATH_MAX];
230 struct sysfs_class_device *class_dev = NULL;
232 strcpy(dev_path, sysfs_path);
233 strcat(dev_path, device_name);
234 dbg("looking at '%s'", dev_path);
236 /* open up the sysfs class device for this thing... */
237 class_dev = sysfs_open_class_device(dev_path);
238 if (class_dev == NULL) {
239 dbg ("sysfs_open_class_device failed");
242 dbg("class_dev->name='%s'", class_dev->name);
248 /* wait for the "dev" file to show up in the directory in sysfs.
249 * If it doesn't happen in about 10 seconds, give up.
251 #define SECONDS_TO_WAIT_FOR_DEV 10
252 static int sleep_for_dev(char *path)
254 char filename[SYSFS_PATH_MAX + 6];
255 int loop = SECONDS_TO_WAIT_FOR_DEV;
258 strcpy(filename, sysfs_path);
259 strcat(filename, path);
260 strcat(filename, "/dev");
265 dbg("looking for '%s'", filename);
266 retval = stat(filename, &buf);
270 /* sleep to give the kernel a chance to create the dev file */
278 int udev_add_device(char *path, char *subsystem)
280 struct sysfs_class_device *class_dev = NULL;
282 int retval = -EINVAL;
284 memset(&dev, 0x00, sizeof(dev));
286 /* for now, the block layer is the only place where block devices are */
287 if (strcmp(subsystem, "block") == 0)
292 retval = sleep_for_dev(path);
296 class_dev = get_class_dev(path);
297 if (class_dev == NULL)
300 retval = get_major_minor(class_dev, &dev);
302 dbg("get_major_minor failed");
306 retval = namedev_name_device(class_dev, &dev);
310 retval = udevdb_add_dev(path, &dev);
312 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
313 "But remove might not work properly for this device.");
315 dbg("name='%s'", dev.name);
316 retval = create_node(&dev);
319 sysbus_send_create(&dev, path);
323 sysfs_close_class_device(class_dev);