chiark / gitweb /
[PATCH] 007_bk version change to Makefile.
[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 #include "namedev.h"
34 #include "udevdb.h"
35 #include "libsysfs/libsysfs.h"
36
37 /* global variables */
38 char **main_argv;
39 char **main_envp;
40
41 char sysfs_path[SYSFS_PATH_MAX];
42 char *udev_config_dir = UDEV_CONFIG_DIR;
43 char *udev_root = UDEV_ROOT;
44 char udev_db_filename[PATH_MAX+NAME_MAX];
45 char udev_config_permission_filename[PATH_MAX+NAME_MAX];
46 char udev_config_filename[PATH_MAX+NAME_MAX];
47
48
49 static inline char *get_action(void)
50 {
51         char *action;
52
53         action = getenv("ACTION");
54         return action;
55 }
56
57 static inline char *get_devpath(void)
58 {
59         char *devpath;
60
61         devpath = getenv("DEVPATH");
62         return devpath;
63 }
64
65 static inline char *get_seqnum(void)
66 {
67         char *seqnum;
68
69         seqnum = getenv("SEQNUM");
70         return seqnum;
71 }
72
73 static void get_dirs(void)
74 {
75         char *temp;
76         char *udev_db = UDEV_DB;
77         char *udev_config = UDEV_CONFIG_FILE;
78         char *udev_permission = UDEV_CONFIG_PERMISSION_FILE;
79         int retval;
80
81         retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
82         if (retval)
83                 dbg("sysfs_get_mnt_path failed");
84
85         /* see if we should try to override any of the default values */
86         temp = getenv("UDEV_TEST");
87         if (temp != NULL) {
88                 /* hm testing is happening, use the specified values, if they are present */
89                 temp = getenv("SYSFS_PATH");
90                 if (temp)
91                         strncpy(sysfs_path, temp, sizeof(sysfs_path));
92                 temp = getenv("UDEV_CONFIG_DIR");
93                 if (temp)
94                         udev_config_dir = temp;
95                 temp = getenv("UDEV_ROOT");
96                 if (temp)
97                         udev_root = temp;
98                 temp = getenv("UDEV_DB");
99                 if (temp)
100                         udev_db = temp;
101                 temp = getenv("UDEV_CONFIG_FILE");
102                 if (temp)
103                         udev_config = temp;
104                 temp = getenv("UDEV_PERMISSION_FILE");
105                 if (temp)
106                         udev_permission = temp;
107         }
108         dbg("sysfs_path = %s", sysfs_path);
109
110         strncpy(udev_db_filename, udev_config_dir, sizeof(udev_db_filename));
111         strncat(udev_db_filename, udev_db, sizeof(udev_db_filename));
112
113         strncpy(udev_config_filename, udev_config_dir, sizeof(udev_config_filename));
114         strncat(udev_config_filename, udev_config, sizeof(udev_config_filename));
115         
116         strncpy(udev_config_permission_filename, udev_config_dir, sizeof(udev_config_permission_filename));
117         strncat(udev_config_permission_filename, udev_permission, sizeof(udev_config_permission_filename));
118 }
119
120 int main(int argc, char **argv, char **envp)
121 {
122         char *action;
123         char *devpath;
124         char *subsystem;
125         int retval = -EINVAL;
126         
127         main_argv = argv;
128         main_envp = envp;
129
130         dbg("version %s", UDEV_VERSION);
131
132         if (argc != 2) {
133                 dbg ("unknown number of arguments");
134                 goto exit;
135         }
136
137         subsystem = argv[1];
138
139         devpath = get_devpath();
140         if (!devpath) {
141                 dbg ("no devpath?");
142                 goto exit;
143         }
144         dbg("looking at %s", devpath);
145
146         /* we only care about class devices and block stuff */
147         if (!strstr(devpath, "class") &&
148             !strstr(devpath, "block")) {
149                 dbg("not block or class");
150                 goto exit;
151         }
152
153         /* but we don't care about net class devices */
154         if (strcmp(subsystem, "net") == 0) {
155                 dbg("don't care about net devices");
156                 goto exit;
157         }
158
159         action = get_action();
160         if (!action) {
161                 dbg ("no action?");
162                 goto exit;
163         }
164
165         /* initialize udev database */
166         get_dirs();
167         retval = udevdb_init(UDEVDB_DEFAULT);
168         if (retval != 0) {
169                 dbg("Unable to initialize database.");
170                 goto exit;
171         }
172
173         /* initialize the naming deamon */
174         namedev_init();
175
176         if (strcmp(action, "add") == 0)
177                 retval = udev_add_device(devpath, subsystem);
178
179         else if (strcmp(action, "remove") == 0)
180                 retval = udev_remove_device(devpath, subsystem);
181
182         else {
183                 dbg("Unknown action: %s", action);
184                 retval = -EINVAL;
185         }
186         udevdb_exit();
187
188 exit:   
189         return retval;
190 }
191