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)
57 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
66 *major = (int)strtol(&temp[0], NULL, 16);
71 *minor = (int)strtol(&temp[0], NULL, 16);
73 dbg("found major = %d, minor = %d", *major, *minor);
80 * We also want to add some permissions here, and possibly some symlinks
82 static int create_node(char *name, char type, int major, int minor, int mode)
85 char mode_string[100];
87 char major_string[20];
88 char minor_string[20];
92 strncpy(filename, UDEV_ROOT, sizeof(filename));
93 strncat(filename, name, sizeof(filename));
95 snprintf(mode_string, sizeof(mode_string), "--mode=%#o", mode);
96 snprintf(type_string, sizeof(type_string), "%c", type);
97 snprintf(major_string, sizeof(major_string), "%d", major);
98 snprintf(minor_string, sizeof(minor_string), "%d", minor);
101 argv[1] = mode_string;
103 argv[3] = type_string;
104 argv[4] = major_string;
105 argv[5] = minor_string;
107 dbg ("executing %s %s %s %s %s %s",
108 argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
111 /* we are the child, so lets run the program */
116 dbg ("fork failed.");
125 struct sysfs_class_device *get_class_dev(char *device_name)
127 char dev_path[SYSFS_PATH_MAX];
128 struct sysfs_class_device *class_dev;
130 strcpy(dev_path, sysfs_path);
131 strcat(dev_path, device_name);
133 dbg("looking at %s", dev_path);
135 /* open up the sysfs class device for this thing... */
136 class_dev = sysfs_open_class_device(dev_path);
137 if (class_dev == NULL) {
138 dbg ("sysfs_open_class_device failed");
141 dbg("class_dev->name = %s", class_dev->name);
146 static int udev_init(void)
150 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
151 dbg("sysfs_path = %s", sysfs_path);
155 int udev_add_device(char *device, char *subsystem)
157 struct sysfs_class_device *class_dev;
158 struct device_attr attr;
164 int retval = -EINVAL;
166 /* sleep for a second or two to give the kernel a chance to
167 * create the dev file
173 /* for now, the block layer is the only place where block devices are */
174 if (strcmp(subsystem, "block") == 0)
179 class_dev = get_class_dev(device);
180 if (class_dev == NULL)
183 retval = namedev_name_device(class_dev, &attr);
187 retval = get_major_minor(class_dev, &major, &minor);
189 dbg ("get_major_minor failed");
193 sysfs_close_class_device(class_dev);
195 return create_node(attr.name, type, major, minor, attr.mode);