chiark / gitweb /
[PATCH] small cut n paste error fix
[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 #include <ctype.h>
31 #include <signal.h>
32 #include <stdarg.h>
33
34 #include "udev.h"
35 #include "udev_version.h"
36 #include "udev_dbus.h"
37 #include "logging.h"
38 #include "namedev.h"
39 #include "udevdb.h"
40 #include "libsysfs/libsysfs.h"
41
42 /* global variables */
43 char **main_argv;
44 char **main_envp;
45
46 static void sig_handler(int signum)
47 {
48         dbg("caught signal %d", signum);
49         switch (signum) {
50                 case SIGINT:
51                 case SIGTERM:
52                 case SIGKILL:
53                         sysbus_disconnect();
54                         udevdb_exit();
55                         exit(20 + signum);
56                         break;
57                 default:
58                         dbg("unhandled signal");
59         }
60 }
61
62 static inline char *get_action(void)
63 {
64         char *action;
65
66         action = getenv("ACTION");
67         return action;
68 }
69
70 static inline char *get_devpath(void)
71 {
72         char *devpath;
73
74         devpath = getenv("DEVPATH");
75         return devpath;
76 }
77
78 static inline char *get_seqnum(void)
79 {
80         char *seqnum;
81
82         seqnum = getenv("SEQNUM");
83         return seqnum;
84 }
85
86 static void print_record(char *path, struct udevice *dev)
87 {
88         printf("P: %s\n", path);
89         printf("N: %s\n", dev->name);
90         printf("S: %s\n", dev->symlink);
91         printf("O: %s\n", dev->owner);
92         printf("G: %s\n", dev->group);
93         printf("\n");
94 }
95
96 enum query_type {
97         NONE,
98         NAME,
99         SYMLINK,
100         OWNER,
101         GROUP
102 };
103
104 static inline int udev_user(int argc, char **argv)
105 {
106         static const char short_options[] = "dp:q:rVh";
107         int option;
108         int retval = -EINVAL;
109         struct udevice dev;
110         int root = 0;
111         enum query_type query = NONE;
112         char result[NAME_SIZE] = "";
113         char path[NAME_SIZE] = "";
114
115         /* get command line options */
116         while (1) {
117                 option = getopt(argc, argv, short_options);
118                 if (option == -1)
119                         break;
120
121                 dbg("option '%c'", option);
122                 switch (option) {
123                 case 'p':
124                         dbg("udev path: %s\n", optarg);
125                         strfieldcpy(path, optarg);
126                         break;
127
128                 case 'q':
129                         dbg("udev query: %s\n", optarg);
130
131                         if (strcmp(optarg, "name") == 0) {
132                                 query = NAME;
133                                 break;
134                         }
135
136                         if (strcmp(optarg, "symlink") == 0) {
137                                 query = SYMLINK;
138                                 break;
139                         }
140
141                         if (strcmp(optarg, "owner") == 0) {
142                                 query = OWNER;
143                                 break;
144                         }
145
146                         if (strcmp(optarg, "group") == 0) {
147                                 query = GROUP;
148                                 break;
149                         }
150
151                         printf("unknown query type\n");
152                         return -EINVAL;
153
154                 case 'r':
155                         root = 1;
156                         break;
157
158                 case 'd':
159                         retval = udevdb_open_ro();
160                         if (retval != 0) {
161                                 printf("unable to open udev database\n");
162                                 return -EACCES;
163                         }
164                         retval = udevdb_dump(print_record);
165                         udevdb_exit();
166                         return retval;
167
168                 case 'V':
169                         printf("udev, version %s\n", UDEV_VERSION);
170                         return 0;
171
172                 case 'h':
173                         retval = 0;
174                 case '?':
175                 default:
176                         goto help;
177                 }
178         }
179
180         /* process options */
181         if (query != NONE) {
182                 if (path[0] == '\0') {
183                         printf("query needs device path specified\n");
184                         return -EINVAL;
185                 }
186
187                 retval = udevdb_open_ro();
188                 if (retval != 0) {
189                         printf("unable to open udev database\n");
190                         return -EACCES;
191                 }
192                 retval = udevdb_get_dev(path, &dev);
193                 if (retval == 0) {
194                         switch(query) {
195                         case NAME:
196                                 if (root)
197                                 strfieldcpy(result, udev_root);
198                                 strncat(result, dev.name, sizeof(result));
199                                 break;
200
201                         case SYMLINK:
202                                 strfieldcpy(result, dev.symlink);
203                                 break;
204
205                         case GROUP:
206                                 strfieldcpy(result, dev.group);
207                                 break;
208
209                         case OWNER:
210                                 strfieldcpy(result, dev.owner);
211                                 break;
212
213                         default:
214                                 break;
215                         }
216                         printf("%s\n", result);
217                 } else {
218                         printf("device not found in udev database\n");
219                 }
220                 udevdb_exit();
221                 return retval;
222         }
223
224         if (root) {
225                 printf("%s\n", udev_root);
226                 return 0;
227         }
228
229 help:
230         printf("Usage: [-pqrdVh]\n"
231                "  -q TYPE  query database for the specified value:\n"
232                "             'name'    name of device node\n"
233                "             'symlink' pointing to node\n"
234                "             'owner'   of node\n"
235                "             'group'   of node\n"
236                "  -p PATH  sysfs device path used for query\n"
237                "  -r       print udev root\n"
238                "  -d       dump whole database\n"
239                "  -V       print udev version\n"
240                "  -h       print this help text\n"
241                "\n");
242         return retval;
243 }
244
245 static char *subsystem_blacklist[] = {
246         "net",
247         "scsi_host",
248         "scsi_device",
249         "usb_host",
250         "pci_bus",
251         "",
252 };
253
254 static inline int udev_hotplug(int argc, char **argv)
255 {
256         char *action;
257         char *devpath;
258         char *subsystem;
259         int retval = -EINVAL;
260         int i;
261
262         subsystem = argv[1];
263
264         devpath = get_devpath();
265         if (!devpath) {
266                 dbg ("no devpath?");
267                 goto exit;
268         }
269         dbg("looking at '%s'", devpath);
270
271         /* we only care about class devices and block stuff */
272         if (!strstr(devpath, "class") &&
273             !strstr(devpath, "block")) {
274                 dbg("not a block or class device");
275                 goto exit;
276         }
277
278         /* skip blacklisted subsystems */
279         i = 0;
280         while (subsystem_blacklist[i][0] != '\0') {
281                 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
282                         dbg("don't care about '%s' devices", subsystem);
283                         goto exit;
284                 }
285                 i++;
286         }
287
288         action = get_action();
289         if (!action) {
290                 dbg ("no action?");
291                 goto exit;
292         }
293
294         /* connect to the system message bus */
295         sysbus_connect();
296
297         /* initialize udev database */
298         retval = udevdb_init(UDEVDB_DEFAULT);
299         if (retval != 0) {
300                 dbg("unable to initialize database");
301                 goto exit_sysbus;
302         }
303
304         /* set up a default signal handler for now */
305         signal(SIGINT, sig_handler);
306         signal(SIGTERM, sig_handler);
307         signal(SIGKILL, sig_handler);
308
309         /* initialize the naming deamon */
310         namedev_init();
311
312         if (strcmp(action, "add") == 0)
313                 retval = udev_add_device(devpath, subsystem);
314
315         else if (strcmp(action, "remove") == 0)
316                 retval = udev_remove_device(devpath, subsystem);
317
318         else {
319                 dbg("unknown action '%s'", action);
320                 retval = -EINVAL;
321         }
322         udevdb_exit();
323
324 exit_sysbus:
325         /* disconnect from the system message bus */
326         sysbus_disconnect();
327
328 exit:
329         if (retval > 0)
330                 retval = 0;
331
332         return -retval;
333 }
334
335 int main(int argc, char **argv, char **envp)
336 {
337         int retval;
338         main_argv = argv;
339         main_envp = envp;
340
341         dbg("version %s", UDEV_VERSION);
342
343         /* initialize our configuration */
344         udev_init_config();
345
346         if (argc == 2 && argv[1][0] != '-') {
347                 dbg("called by hotplug");
348                 retval = udev_hotplug(argc, argv);
349         } else {
350                 dbg("called by user");
351                 retval = udev_user(argc, argv);
352         }
353
354         return retval;
355 }
356
357