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.
32 #include "udev_version.h"
34 #include "libsysfs/libsysfs.h"
37 static char sysfs_path[SYSFS_PATH_MAX];
40 * Right now the major/minor of a device is stored in a file called
42 * The number is stored as:
46 * The value is in hex.
47 * Yes, this will probably change when we go to a bigger major/minor
48 * range, and will have to be changed at that time.
50 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
56 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
62 if (sscanf(dev, "%u:%u", major, minor) != 2)
65 dbg("found major = %d, minor = %d", *major, *minor);
73 * We also want to add some permissions here, and possibly some symlinks
75 static int create_node(char *name, char type, int major, int minor, int mode)
78 char mode_string[100];
80 char major_string[20];
81 char minor_string[20];
85 strncpy(filename, UDEV_ROOT, sizeof(filename));
86 strncat(filename, name, sizeof(filename));
88 snprintf(mode_string, sizeof(mode_string), "--mode=%#o", mode);
89 snprintf(type_string, sizeof(type_string), "%c", type);
90 snprintf(major_string, sizeof(major_string), "%d", major);
91 snprintf(minor_string, sizeof(minor_string), "%d", minor);
94 argv[1] = mode_string;
96 argv[3] = type_string;
97 argv[4] = major_string;
98 argv[5] = minor_string;
100 dbg ("executing %s %s %s %s %s %s",
101 argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
104 /* we are the child, so lets run the program */
109 dbg ("fork failed.");
118 struct sysfs_class_device *get_class_dev(char *device_name)
120 char dev_path[SYSFS_PATH_MAX];
121 struct sysfs_class_device *class_dev;
123 strcpy(dev_path, sysfs_path);
124 strcat(dev_path, device_name);
126 dbg("looking at %s", dev_path);
128 /* open up the sysfs class device for this thing... */
129 class_dev = sysfs_open_class_device(dev_path);
130 if (class_dev == NULL) {
131 dbg ("sysfs_open_class_device failed");
134 dbg("class_dev->name = %s", class_dev->name);
139 static int udev_init(void)
143 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
144 dbg("sysfs_path = %s", sysfs_path);
148 int udev_add_device(char *device, char *subsystem)
150 struct sysfs_class_device *class_dev;
151 struct device_attr attr;
157 int retval = -EINVAL;
159 /* sleep for a second or two to give the kernel a chance to
160 * create the dev file
166 /* for now, the block layer is the only place where block devices are */
167 if (strcmp(subsystem, "block") == 0)
172 class_dev = get_class_dev(device);
173 if (class_dev == NULL)
176 retval = namedev_name_device(class_dev, &attr);
180 retval = get_major_minor(class_dev, &major, &minor);
182 dbg ("get_major_minor failed");
186 sysfs_close_class_device(class_dev);
188 return create_node(attr.name, type, major, minor, attr.mode);