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"
35 static char *get_action(void)
39 action = getenv("ACTION");
44 static char *get_device(void)
46 static char device[255];
49 temp = getenv("DEVPATH");
52 strcpy(device, SYSFS_ROOT);
59 * Right now the major/minor of a device is stored in a file called
61 * The number is stored as:
65 * The value is in hex.
66 * Yes, this will probably change when we go to a bigger major/minor
67 * range, and will have to be changed at that time.
69 static int get_major_minor (char *dev, int *major, int *minor)
77 /* add the dev file to the directory and see if it's present */
78 strncpy(filename, dev, sizeof(filename));
79 strncat(filename, DEV_FILE, sizeof(filename));
80 fd = open(filename, O_RDONLY);
82 dbg("Can't open %s", filename);
86 /* get the major/minor */
87 retval = read(fd, line, sizeof(line));
89 dbg("read error on %s", dev);
96 *major = (int)strtol(&temp[0], NULL, 16);
101 *minor = (int)strtol(&temp[0], NULL, 16);
103 dbg("found major = %d, minor = %d", *major, *minor);
112 * Here would go a call to the naming deamon, to get the name we want to have
113 * for this device. But for now, let's just default to whatever the kernel is
114 * calling the device as that will keep the "old-style" naming policy
116 static char *get_name(char *dev, int major, int minor)
118 static char name[100];
121 temp = strrchr(dev, '/');
124 strncpy(name, &temp[1], sizeof(name));
126 dbg("name is %s", name);
132 * Again, this will live in the naming deamon
134 static int get_mode(char *name, char *dev, int major, int minor)
136 /* just default everyone to rw for the world! */
141 * We also want to add some permissions here, and possibly some symlinks
143 static int create_node(char *name, char type, int major, int minor, int mode)
146 char mode_string[100];
148 char major_string[20];
149 char minor_string[20];
153 strncpy(filename, UDEV_ROOT, sizeof(filename));
154 strncat(filename, name, sizeof(filename));
156 snprintf(mode_string, sizeof(mode_string), "--mode=%#o", mode);
157 snprintf(type_string, sizeof(type_string), "%c", type);
158 snprintf(major_string, sizeof(major_string), "%d", major);
159 snprintf(minor_string, sizeof(minor_string), "%d", minor);
162 argv[1] = mode_string;
164 argv[3] = type_string;
165 argv[4] = major_string;
166 argv[5] = minor_string;
168 dbg ("executing %s %s %s %s %s %s",
169 argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
172 /* we are the child, so lets run the program */
177 dbg ("fork failed.");
187 * We also want to clean up any symlinks that were created in create_node()
189 static int delete_node(char *name)
193 strncpy(filename, UDEV_ROOT, sizeof(filename));
194 strncat(filename, name, sizeof(filename));
196 dbg("unlinking %s", filename);
197 return unlink(filename);
200 static int add_device(char *device, char type)
206 int retval = -EINVAL;
208 retval = get_major_minor(device, &major, &minor);
210 dbg ("get_major_minor failed");
214 name = get_name(device, major, minor);
216 dbg ("get_name failed");
221 mode = get_mode(name, device, major, minor);
223 dbg ("get_mode failed");
228 return create_node(name, type, major, minor, mode);
234 static int remove_device(char *device)
239 name = get_name(device, 0, 0);
241 dbg ("get_name failed");
246 return delete_node(name);
252 int main(int argc, char *argv[])
258 int retval = -EINVAL;
261 dbg ("unknown number of arguments");
265 /* for now, the block layer is the only place where block devices are */
267 if (strcmp(subsystem, "block") == 0)
272 action = get_action();
278 device = get_device();
283 dbg("looking at %s", device);
285 if (strcmp(action, "add") == 0)
286 return add_device(device, type);
288 if (strcmp(action, "remove") == 0)
289 return remove_device(device);
291 dbg("Unknown action: %s", action);