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"
41 #include "libsysfs/libsysfs.h"
42 #include "klibc_fixups.h"
45 * Right now the major/minor of a device is stored in a file called
47 * The number is stored as:
51 * The value is in decimal.
53 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");
65 if (sscanf(dev, "%u:%u", &udev->major, &udev->minor) != 2)
68 dbg("found major=%d, minor=%d", udev->major, udev->minor);
75 static int create_path(char *file)
82 strncpy(p, file, sizeof(p));
83 pos = strchr(p+1, '/');
85 pos = strchr(pos+1, '/');
89 if (stat(p, &stats)) {
90 retval = mkdir(p, 0755);
92 dbg("mkdir(%s) failed with error '%s'",
96 dbg("created '%s'", p);
104 /** Send out a signal that a device node is created
106 * @param dev udevice object
107 * @param path Sysfs path of device
109 static void sysbus_send_create(struct udevice *dev, const char *path)
112 DBusMessage* message;
113 DBusMessageIter iter;
115 if (sysbus_connection == NULL)
118 strncpy(filename, udev_root, sizeof(filename));
119 strncat(filename, dev->name, sizeof(filename));
121 /* object, interface, member */
122 message = dbus_message_new_signal("/org/kernel/udev/NodeMonitor",
123 "org.kernel.udev.NodeMonitor",
126 dbus_message_iter_init(message, &iter);
127 dbus_message_iter_append_string(&iter, filename);
128 dbus_message_iter_append_string(&iter, path);
130 if ( !dbus_connection_send(sysbus_connection, message, NULL) )
131 dbg("error sending d-bus signal");
133 dbus_message_unref(message);
135 dbus_connection_flush(sysbus_connection);
137 #endif /* USE_DBUS */
140 * we possibly want to add some symlinks here
141 * only numeric owner/group id's are supported
143 static int create_node(struct udevice *dev)
146 char linktarget[255];
155 strncpy(filename, udev_root, sizeof(filename));
156 strncat(filename, dev->name, sizeof(filename));
159 res = (dev->major << 8) | (dev->minor);
161 res = makedev(dev->major, dev->minor);
166 dev->mode |= S_IFBLK;
170 dev->mode |= S_IFCHR;
173 dev->mode |= S_IFIFO;
176 dbg("unknown node type %c\n", dev->type);
180 /* create parent directories if needed */
181 if (strrchr(dev->name, '/'))
182 create_path(filename);
184 dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
185 retval = mknod(filename, dev->mode, res);
187 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
188 filename, dev->mode, dev->major, dev->minor, strerror(errno));
190 dbg("chmod(%s, %#o)", filename, dev->mode);
191 retval = chmod(filename, dev->mode);
193 dbg("chmod(%s, %#o) failed with error '%s'",
194 filename, dev->mode, strerror(errno));
198 unsigned long id = strtoul(dev->owner, &endptr, 10);
202 struct passwd *pw = getpwnam(dev->owner);
204 dbg("user unknown '%s'", dev->owner);
212 unsigned long id = strtoul(dev->group, &endptr, 10);
216 struct group *gr = getgrnam(dev->group);
218 dbg("group unknown '%s'", dev->group);
225 dbg("chown(%s, %u, %u)", filename, uid, gid);
226 retval = chown(filename, uid, gid);
228 dbg("chown(%s, %u, %u) failed with error '%s'",
229 filename, uid, gid, strerror(errno));
233 /* create symlink if requested */
235 strncpy(filename, udev_root, sizeof(filename));
236 strncat(filename, dev->symlink, sizeof(filename));
237 dbg("symlink '%s' to node '%s' requested", filename, dev->name);
238 if (strrchr(dev->symlink, '/'))
239 create_path(filename);
241 /* optimize relative link */
242 linktarget[0] = '\0';
245 while ((dev->name[i] == dev->symlink[i]) && dev->name[i]) {
246 if (dev->name[i] == '/')
250 while (dev->symlink[i]) {
251 if (dev->symlink[i] == '/')
252 strcat(linktarget, "../");
256 if (*linktarget == '\0')
257 strcpy(linktarget, "./");
258 strcat(linktarget, &dev->name[tail]);
260 dbg("symlink(%s, %s)", linktarget, filename);
261 retval = symlink(linktarget, filename);
263 dbg("symlink(%s, %s) failed with error '%s'",
264 linktarget, filename, strerror(errno));
270 static struct sysfs_class_device *get_class_dev(char *device_name)
272 char dev_path[SYSFS_PATH_MAX];
273 struct sysfs_class_device *class_dev = NULL;
275 strcpy(dev_path, sysfs_path);
276 strcat(dev_path, device_name);
278 dbg("looking at '%s'", dev_path);
280 /* open up the sysfs class device for this thing... */
281 class_dev = sysfs_open_class_device(dev_path);
282 if (class_dev == NULL) {
283 dbg ("sysfs_open_class_device failed");
286 dbg("class_dev->name='%s'", class_dev->name);
292 /* wait for the "dev" file to show up in the directory in sysfs.
293 * If it doesn't happen in about 10 seconds, give up.
295 #define SECONDS_TO_WAIT_FOR_DEV 10
296 static int sleep_for_dev(char *path)
298 char filename[SYSFS_PATH_MAX + 6];
299 int loop = SECONDS_TO_WAIT_FOR_DEV;
302 strcpy(filename, sysfs_path);
303 strcat(filename, path);
304 strcat(filename, "/dev");
309 dbg("looking for '%s'", filename);
310 retval = stat(filename, &buf);
314 /* sleep to give the kernel a chance to create the dev file */
322 int udev_add_device(char *path, char *subsystem)
324 struct sysfs_class_device *class_dev = NULL;
326 int retval = -EINVAL;
328 memset(&dev, 0x00, sizeof(dev));
330 /* for now, the block layer is the only place where block devices are */
331 if (strcmp(subsystem, "block") == 0)
336 retval = sleep_for_dev(path);
340 class_dev = get_class_dev(path);
341 if (class_dev == NULL)
344 retval = get_major_minor(class_dev, &dev);
346 dbg("get_major_minor failed");
350 retval = namedev_name_device(class_dev, &dev);
354 retval = udevdb_add_dev(path, &dev);
356 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
357 "But remove might not work properly for this device.");
359 dbg("name='%s'", dev.name);
360 retval = create_node(&dev);
364 sysbus_send_create(&dev, path);
366 #endif /* USE_DBUS */
370 sysfs_close_class_device(class_dev);