]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev.c
[PATCH] Initial namedev parsing of config files
[thirdparty/systemd.git] / udev.c
1 /*
2 * udev.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 *
8 *
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.
12 *
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.
17 *
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.
21 *
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include "udev.h"
32 #include "udev_version.h"
33 #include "namedev.h"
34
35
36 static char *get_action(void)
37 {
38 char *action;
39
40 action = getenv("ACTION");
41 return action;
42 }
43
44
45 static char *get_device(void)
46 {
47 static char device[255];
48 char *temp;
49
50 temp = getenv("DEVPATH");
51 if (temp == NULL)
52 return NULL;
53 strcpy(device, SYSFS_ROOT);
54 strcat(device, temp);
55
56 return device;
57 }
58
59 /*
60 * Right now the major/minor of a device is stored in a file called
61 * "dev" in sysfs.
62 * The number is stored as:
63 * MMmm
64 * MM is the major
65 * mm is the minor
66 * The value is in hex.
67 * Yes, this will probably change when we go to a bigger major/minor
68 * range, and will have to be changed at that time.
69 */
70 static int get_major_minor (char *dev, int *major, int *minor)
71 {
72 char filename[255];
73 char line[20];
74 char temp[3];
75 int fd;
76 int retval = 0;
77
78 /* add the dev file to the directory and see if it's present */
79 strncpy(filename, dev, sizeof(filename));
80 strncat(filename, DEV_FILE, sizeof(filename));
81 fd = open(filename, O_RDONLY);
82 if (fd < 0) {
83 dbg("Can't open %s", filename);
84 return -ENODEV;
85 }
86
87 /* get the major/minor */
88 retval = read(fd, line, sizeof(line));
89 if (retval < 0) {
90 dbg("read error on %s", dev);
91 goto exit;
92 }
93
94 temp[0] = line[0];
95 temp[1] = line[1];
96 temp[2] = 0x00;
97 *major = (int)strtol(&temp[0], NULL, 16);
98
99 temp[0] = line[2];
100 temp[1] = line[3];
101 temp[2] = 0x00;
102 *minor = (int)strtol(&temp[0], NULL, 16);
103
104 dbg("found major = %d, minor = %d", *major, *minor);
105
106 retval = 0;
107 exit:
108 close(fd);
109 return retval;
110 }
111
112 /*
113 * Here would go a call to the naming deamon, to get the name we want to have
114 * for this device. But for now, let's just default to whatever the kernel is
115 * calling the device as that will keep the "old-style" naming policy
116 */
117 static char *get_name(char *dev, int major, int minor)
118 {
119 static char name[100];
120 char *temp;
121
122 temp = strrchr(dev, '/');
123 if (temp == NULL)
124 return NULL;
125 strncpy(name, &temp[1], sizeof(name));
126
127 dbg("name is %s", name);
128
129 return &name[0];
130 }
131
132 /*
133 * Again, this will live in the naming deamon
134 */
135 static int get_mode(char *name, char *dev, int major, int minor)
136 {
137 /* just default everyone to rw for the world! */
138 return 0666;
139 }
140
141 /*
142 * We also want to add some permissions here, and possibly some symlinks
143 */
144 static int create_node(char *name, char type, int major, int minor, int mode)
145 {
146 char *argv[7];
147 char mode_string[100];
148 char type_string[3];
149 char major_string[20];
150 char minor_string[20];
151 char filename[255];
152 int retval = 0;
153
154 strncpy(filename, UDEV_ROOT, sizeof(filename));
155 strncat(filename, name, sizeof(filename));
156
157 snprintf(mode_string, sizeof(mode_string), "--mode=%#o", mode);
158 snprintf(type_string, sizeof(type_string), "%c", type);
159 snprintf(major_string, sizeof(major_string), "%d", major);
160 snprintf(minor_string, sizeof(minor_string), "%d", minor);
161
162 argv[0] = MKNOD;
163 argv[1] = mode_string;
164 argv[2] = filename;
165 argv[3] = type_string;
166 argv[4] = major_string;
167 argv[5] = minor_string;
168 argv[6] = NULL;
169 dbg ("executing %s %s %s %s %s %s",
170 argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
171 switch (fork()) {
172 case 0:
173 /* we are the child, so lets run the program */
174 execv (MKNOD, argv);
175 exit(0);
176 break;
177 case (-1):
178 dbg ("fork failed.");
179 retval = -EFAULT;
180 break;
181 default:
182 break;
183 }
184 return retval;
185 }
186
187 /*
188 * We also want to clean up any symlinks that were created in create_node()
189 */
190 static int delete_node(char *name)
191 {
192 char filename[255];
193
194 strncpy(filename, UDEV_ROOT, sizeof(filename));
195 strncat(filename, name, sizeof(filename));
196
197 dbg("unlinking %s", filename);
198 return unlink(filename);
199 }
200
201 static int add_device(char *device, char type)
202 {
203 char *name;
204 int major;
205 int minor;
206 int mode;
207 int retval = -EINVAL;
208
209 retval = get_major_minor(device, &major, &minor);
210 if (retval) {
211 dbg ("get_major_minor failed");
212 goto exit;
213 }
214
215 name = get_name(device, major, minor);
216 if (name == NULL) {
217 dbg ("get_name failed");
218 retval = -ENODEV;
219 goto exit;
220 }
221
222 mode = get_mode(name, device, major, minor);
223 if (mode < 0) {
224 dbg ("get_mode failed");
225 retval = -EINVAL;
226 goto exit;
227 }
228
229 return create_node(name, type, major, minor, mode);
230
231 exit:
232 return retval;
233 }
234
235 static int remove_device(char *device)
236 {
237 char *name;
238 int retval = 0;
239
240 name = get_name(device, 0, 0);
241 if (name == NULL) {
242 dbg ("get_name failed");
243 retval = -ENODEV;
244 goto exit;
245 }
246
247 return delete_node(name);
248
249 exit:
250 return retval;
251 }
252
253 int main(int argc, char *argv[])
254 {
255 char *subsystem;
256 char *action;
257 char *device;
258 char type;
259 int retval = -EINVAL;
260
261 if (argc != 2) {
262 dbg ("unknown number of arguments");
263 goto exit;
264 }
265
266 namedev_init();
267
268 /* sleep for a second or two to give the kernel a chance to
269 * create the dev file
270 */
271 sleep(2);
272
273 /* for now, the block layer is the only place where block devices are */
274 subsystem = argv[1];
275 if (strcmp(subsystem, "block") == 0)
276 type = 'b';
277 else
278 type = 'c';
279
280 action = get_action();
281 if (!action) {
282 dbg ("no action?");
283 goto exit;
284 }
285
286 device = get_device();
287 if (!device) {
288 dbg ("no device?");
289 goto exit;
290 }
291 dbg("looking at %s", device);
292
293 if (strcmp(action, "add") == 0)
294 return add_device(device, type);
295
296 if (strcmp(action, "remove") == 0)
297 return remove_device(device);
298
299 dbg("Unknown action: %s", action);
300 return -EINVAL;
301
302 retval = 0;
303 exit:
304 return retval;
305 }
306