chiark / gitweb /
[PATCH] get rid of the majority of the debug environment variables.
[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
32 #include "udev.h"
33 #include "udev_version.h"
34 #include "namedev.h"
35 #include "udevdb.h"
36 #include "libsysfs/libsysfs.h"
37
38 /* global variables */
39 char **main_argv;
40 char **main_envp;
41
42 static inline char *get_action(void)
43 {
44         char *action;
45
46         action = getenv("ACTION");
47         return action;
48 }
49
50 static inline char *get_devpath(void)
51 {
52         char *devpath;
53
54         devpath = getenv("DEVPATH");
55         return devpath;
56 }
57
58 static inline char *get_seqnum(void)
59 {
60         char *seqnum;
61
62         seqnum = getenv("SEQNUM");
63         return seqnum;
64 }
65
66 int main(int argc, char **argv, char **envp)
67 {
68         char *action;
69         char *devpath;
70         char *subsystem;
71         int retval = -EINVAL;
72         
73         main_argv = argv;
74         main_envp = envp;
75
76         dbg("version %s", UDEV_VERSION);
77
78         if (argc != 2) {
79                 dbg ("unknown number of arguments");
80                 goto exit;
81         }
82
83         subsystem = argv[1];
84
85         devpath = get_devpath();
86         if (!devpath) {
87                 dbg ("no devpath?");
88                 goto exit;
89         }
90         dbg("looking at '%s'", devpath);
91
92         /* we only care about class devices and block stuff */
93         if (!strstr(devpath, "class") &&
94             !strstr(devpath, "block")) {
95                 dbg("not a block or class device");
96                 goto exit;
97         }
98
99         /* but we don't care about net class devices */
100         if (strcmp(subsystem, "net") == 0) {
101                 dbg("don't care about net devices");
102                 goto exit;
103         }
104
105         action = get_action();
106         if (!action) {
107                 dbg ("no action?");
108                 goto exit;
109         }
110
111         /* initialize our configuration */
112         udev_init_config();
113
114         /* initialize udev database */
115         retval = udevdb_init(UDEVDB_DEFAULT);
116         if (retval != 0) {
117                 dbg("unable to initialize database");
118                 goto exit;
119         }
120
121         /* initialize the naming deamon */
122         namedev_init();
123
124         if (strcmp(action, "add") == 0)
125                 retval = udev_add_device(devpath, subsystem);
126
127         else if (strcmp(action, "remove") == 0)
128                 retval = udev_remove_device(devpath, subsystem);
129
130         else {
131                 dbg("unknown action '%s'", action);
132                 retval = -EINVAL;
133         }
134         udevdb_exit();
135
136 exit:   
137         return retval;
138 }