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];
39 static char *get_action(void)
43 action = getenv("ACTION");
48 static char *get_device(void)
52 device = getenv("DEVPATH");
57 * Right now the major/minor of a device is stored in a file called
59 * The number is stored as:
63 * The value is in hex.
64 * Yes, this will probably change when we go to a bigger major/minor
65 * range, and will have to be changed at that time.
67 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
74 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
83 *major = (int)strtol(&temp[0], NULL, 16);
88 *minor = (int)strtol(&temp[0], NULL, 16);
90 dbg("found major = %d, minor = %d", *major, *minor);
97 * Here would go a call to the naming deamon, to get the name we want to have
98 * for this device. But for now, let's just default to whatever the kernel is
99 * calling the device as that will keep the "old-style" naming policy
101 static char *get_name(char *dev, int major, int minor)
103 static char name[100];
106 temp = strrchr(dev, '/');
109 strncpy(name, &temp[1], sizeof(name));
111 dbg("name is %s", name);
117 * We also want to add some permissions here, and possibly some symlinks
119 static int create_node(char *name, char type, int major, int minor, int mode)
122 char mode_string[100];
124 char major_string[20];
125 char minor_string[20];
129 strncpy(filename, UDEV_ROOT, sizeof(filename));
130 strncat(filename, name, sizeof(filename));
132 snprintf(mode_string, sizeof(mode_string), "--mode=%#o", mode);
133 snprintf(type_string, sizeof(type_string), "%c", type);
134 snprintf(major_string, sizeof(major_string), "%d", major);
135 snprintf(minor_string, sizeof(minor_string), "%d", minor);
138 argv[1] = mode_string;
140 argv[3] = type_string;
141 argv[4] = major_string;
142 argv[5] = minor_string;
144 dbg ("executing %s %s %s %s %s %s",
145 argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
148 /* we are the child, so lets run the program */
153 dbg ("fork failed.");
163 * We also want to clean up any symlinks that were created in create_node()
165 static int delete_node(char *name)
169 strncpy(filename, UDEV_ROOT, sizeof(filename));
170 strncat(filename, name, sizeof(filename));
172 dbg("unlinking %s", filename);
173 return unlink(filename);
176 struct sysfs_class_device *get_class_dev(char *device_name)
178 char dev_path[SYSFS_PATH_MAX];
179 struct sysfs_class_device *class_dev;
181 strcpy(dev_path, sysfs_path);
182 strcat(dev_path, device_name);
184 dbg("looking at %s", dev_path);
186 /* open up the sysfs class device for this thing... */
187 class_dev = sysfs_open_class_device(dev_path);
188 if (class_dev == NULL) {
189 dbg ("sysfs_open_class_device failed");
192 dbg("class_dev->name = %s", class_dev->name);
197 static int add_device(char *device, char *subsystem)
199 struct sysfs_class_device *class_dev;
200 struct device_attr attr;
206 int retval = -EINVAL;
208 /* for now, the block layer is the only place where block devices are */
209 if (strcmp(subsystem, "block") == 0)
214 class_dev = get_class_dev(device);
215 if (class_dev == NULL)
218 retval = namedev_name_device(class_dev, &attr);
222 retval = get_major_minor(class_dev, &major, &minor);
224 dbg ("get_major_minor failed");
228 sysfs_close_class_device(class_dev);
230 return create_node(attr.name, type, major, minor, attr.mode);
236 static int remove_device(char *device)
241 name = get_name(device, 0, 0);
243 dbg ("get_name failed");
248 return delete_node(name);
254 static int udev_init(void)
258 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
259 dbg("sysfs_path = %s", sysfs_path);
263 int main(int argc, char *argv[])
267 int retval = -EINVAL;
270 dbg ("unknown number of arguments");
274 device = get_device();
279 dbg("looking at %s", device);
281 /* we only care about class devices and block stuff */
282 if (!strstr(device, "class") &&
283 !strstr(device, "block")) {
284 dbg("not block or class");
288 /* sleep for a second or two to give the kernel a chance to
289 * create the dev file
296 action = get_action();
302 if (strcmp(action, "add") == 0)
303 return add_device(device, argv[1]);
305 if (strcmp(action, "remove") == 0)
306 return remove_device(device);
308 dbg("Unknown action: %s", action);