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