chiark / gitweb /
4bb41a869638edb48881351d51476113a89d3bec
[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 static int add_node(char *device, char type)
187 {
188         char *name;
189         int major;
190         int minor;
191         int mode;
192         int retval = -EINVAL;
193
194         retval = get_major_minor(device, &major, &minor);
195         if (retval) {
196                 dbg ("get_major_minor failed");
197                 goto exit;
198         }
199
200         name = get_name(device, major, minor);
201         if (name == NULL) {
202                 dbg ("get_name failed");
203                 retval = -ENODEV;
204                 goto exit;
205         }
206
207         mode = get_mode(name, device, major, minor);
208         if (mode < 0) {
209                 dbg ("get_mode failed");
210                 retval = -EINVAL;
211                 goto exit;
212         }
213
214         return create_node(name, type, major, minor, mode);
215
216 exit:
217         return retval;
218 }
219
220 static int remove_node(char *device)
221 {
222         return 0;
223 }
224
225 int main(int argc, char *argv[])
226 {
227         char *subsystem;
228         char *action;
229         char *device;
230         char type;
231         int retval = -EINVAL;
232         
233         if (argc != 2) {
234                 dbg ("unknown number of arguments");
235                 goto exit;
236         }
237
238         /* for now, the block layer is the only place where block devices are */
239         subsystem = argv[1];
240         if (strcmp(subsystem, "block") == 0)
241                 type = 'b';
242         else
243                 type = 'c';
244
245         action = get_action();
246         if (!action) {
247                 dbg ("no action?");
248                 goto exit;
249         }
250
251         device = get_device();
252         if (!device) {
253                 dbg ("no device?");
254                 goto exit;
255         }
256         dbg("looking at %s", device);
257
258         if (strcmp(action, "add") == 0)
259                 return add_node(device, type);
260
261         if (strcmp(action, "remove") == 0)
262                 return remove_node(device);
263
264         dbg("Unknown action: %s", action);
265         return -EINVAL;
266
267         retval = 0;
268 exit:   
269         return retval;
270 }
271