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