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>
36 #include <sysfs/libsysfs.h>
39 #include "udev_version.h"
40 #include "udev_dbus.h"
44 #include "klibc_fixups.h"
47 * Right now the major/minor of a device is stored in a file called
49 * The number is stored as:
53 * The value is in decimal.
55 static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev)
58 struct sysfs_attribute *attr = NULL;
60 attr = sysfs_get_classdev_attr(class_dev, "dev");
63 dbg("dev='%s'", attr->value);
65 if (sscanf(attr->value, "%u:%u", &udev->major, &udev->minor) != 2)
67 dbg("found major=%d, minor=%d", udev->major, udev->minor);
74 static int create_path(char *file)
81 strncpy(p, file, sizeof(p));
82 pos = strchr(p+1, '/');
84 pos = strchr(pos+1, '/');
88 if (stat(p, &stats)) {
89 retval = mkdir(p, 0755);
91 dbg("mkdir(%s) failed with error '%s'",
95 dbg("created '%s'", p);
102 static int make_node(char *filename, int major, int minor, unsigned int mode, uid_t uid, gid_t gid)
106 retval = mknod(filename, mode, makedev(major, minor));
108 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
109 filename, mode, major, minor, strerror(errno));
113 dbg("chmod(%s, %#o)", filename, mode);
114 retval = chmod(filename, mode);
116 dbg("chmod(%s, %#o) failed with error '%s'",
117 filename, mode, strerror(errno));
121 if (uid != 0 || gid != 0) {
122 dbg("chown(%s, %u, %u)", filename, uid, gid);
123 retval = chown(filename, uid, gid);
125 dbg("chown(%s, %u, %u) failed with error '%s'",
126 filename, uid, gid, strerror(errno));
134 static int create_node(struct udevice *dev, int fake)
138 char linktarget[255];
139 char partitionname[255];
148 strncpy(filename, udev_root, sizeof(filename));
149 strncat(filename, dev->name, sizeof(filename));
153 dev->mode |= S_IFBLK;
157 dev->mode |= S_IFCHR;
160 dev->mode |= S_IFIFO;
163 dbg("unknown node type %c\n", dev->type);
167 /* create parent directories if needed */
168 if (strrchr(dev->name, '/'))
169 create_path(filename);
171 if (dev->owner[0] != '\0') {
173 unsigned long id = strtoul(dev->owner, &endptr, 10);
174 if (endptr[0] == '\0')
177 struct passwd *pw = getpwnam(dev->owner);
179 dbg("specified user unknown '%s'", dev->owner);
185 if (dev->group[0] != '\0') {
187 unsigned long id = strtoul(dev->group, &endptr, 10);
188 if (endptr[0] == '\0')
191 struct group *gr = getgrnam(dev->group);
193 dbg("specified group unknown '%s'", dev->group);
200 info("creating device node '%s'", filename);
201 make_node(filename, dev->major, dev->minor, dev->mode, uid, gid);
203 info("creating device node '%s', major = '%d', minor = '%d', "
204 "mode = '%#o', uid = '%d', gid = '%d'", filename,
205 dev->major, dev->minor, (mode_t)dev->mode, uid, gid);
208 /* create partitions if requested */
209 if (dev->partitions > 0) {
210 info("creating device partition nodes '%s[1-%i]'", filename, dev->partitions);
212 for (i = 1; i <= dev->partitions; i++) {
213 sprintf(partitionname, "%s%i", filename, i);
214 make_node(partitionname, dev->major,
215 dev->minor + i, dev->mode, uid, gid);
220 /* create symlink if requested */
221 if (dev->symlink[0] != '\0') {
222 symlinks = dev->symlink;
224 linkname = strsep(&symlinks, " ");
225 if (linkname == NULL || linkname[0] == '\0')
228 strncpy(filename, udev_root, sizeof(filename));
229 strncat(filename, linkname, sizeof(filename));
230 dbg("symlink '%s' to node '%s' requested", filename, dev->name);
232 if (strrchr(linkname, '/'))
233 create_path(filename);
235 /* optimize relative link */
236 linktarget[0] = '\0';
239 while ((dev->name[i] == linkname[i]) && dev->name[i]) {
240 if (dev->name[i] == '/')
244 while (linkname[i] != '\0') {
245 if (linkname[i] == '/')
246 strcat(linktarget, "../");
250 if (linktarget[0] == '\0')
251 strcpy(linktarget, "./");
252 strcat(linktarget, &dev->name[tail]);
254 /* unlink existing files to ensure that our symlink is created */
255 if (!fake && (lstat(filename, &stats) == 0)) {
256 if ((stats.st_mode & S_IFMT) != S_IFDIR) {
257 if (unlink(filename))
258 dbg("unlink(%s) failed with error '%s'",
259 filename, strerror(errno));
263 dbg("symlink(%s, %s)", linktarget, filename);
265 retval = symlink(linktarget, filename);
267 dbg("symlink(%s, %s) failed with error '%s'",
268 linktarget, filename, strerror(errno));
276 static struct sysfs_class_device *get_class_dev(char *device_name)
278 char dev_path[SYSFS_PATH_MAX];
279 struct sysfs_class_device *class_dev = NULL;
281 strcpy(dev_path, sysfs_path);
282 strcat(dev_path, device_name);
283 dbg("looking at '%s'", dev_path);
285 /* open up the sysfs class device for this thing... */
286 class_dev = sysfs_open_class_device_path(dev_path);
287 if (class_dev == NULL) {
288 dbg ("sysfs_open_class_device_path failed");
291 dbg("class_dev->name='%s'", class_dev->name);
297 /* wait for the "dev" file to show up in the directory in sysfs.
298 * If it doesn't happen in about 10 seconds, give up.
300 #define SECONDS_TO_WAIT_FOR_DEV 10
301 static int sleep_for_dev(char *path)
303 char filename[SYSFS_PATH_MAX + 6];
304 int loop = SECONDS_TO_WAIT_FOR_DEV;
307 strcpy(filename, sysfs_path);
308 strcat(filename, path);
309 strcat(filename, "/dev");
314 dbg("looking for '%s'", filename);
315 retval = stat(filename, &buf);
319 /* sleep to give the kernel a chance to create the dev file */
327 int udev_add_device(char *path, char *subsystem, int fake)
329 struct sysfs_class_device *class_dev = NULL;
331 int retval = -EINVAL;
333 memset(&dev, 0x00, sizeof(dev));
335 /* for now, the block layer is the only place where block devices are */
336 if (strcmp(subsystem, "block") == 0)
341 retval = sleep_for_dev(path);
345 class_dev = get_class_dev(path);
346 if (class_dev == NULL)
349 retval = get_major_minor(class_dev, &dev);
351 dbg("get_major_minor failed");
355 retval = namedev_name_device(class_dev, &dev);
360 retval = udevdb_add_dev(path, &dev);
362 dbg("udevdb_add_dev failed, but we are going to try "
363 "to create the node anyway. But remove might not "
364 "work properly for this device.");
367 dbg("name='%s'", dev.name);
368 retval = create_node(&dev, fake);
370 if ((retval == 0) && (!fake))
371 sysbus_send_create(&dev, path);
375 sysfs_close_class_device(class_dev);