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