chiark / gitweb /
[PATCH] make config files, sysfs root, and udev root configurable from config 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
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;
43 char *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 *udev_test;
76         char *temp;
77         int retval;
78
79         udev_test = getenv("UDEV_TEST");
80         if (udev_test == NULL) {
81                 /* normal operation, use the compiled in defaults */
82                 udev_config_dir = UDEV_CONFIG_DIR;
83                 udev_root = UDEV_ROOT;
84                 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
85                 dbg("sysfs_path = %s", sysfs_path);
86                 if (retval)
87                         dbg("sysfs_get_mnt_path failed");
88
89         } else {
90                 /* hm testing is happening, use the specified values */
91                 temp = getenv("UDEV_SYSFS_PATH");
92                 strncpy(sysfs_path, temp, sizeof(sysfs_path));
93                 udev_config_dir = getenv("UDEV_CONFIG_DIR");
94                 udev_root = getenv("UDEV_ROOT");
95         }
96
97         strncpy(udev_db_filename, udev_config_dir, sizeof(udev_db_filename));
98         strncat(udev_db_filename, UDEV_DB, sizeof(udev_db_filename));
99
100         strncpy(udev_config_filename, udev_config_dir, sizeof(udev_config_filename));
101         strncat(udev_config_filename, NAMEDEV_CONFIG_FILE, sizeof(udev_config_filename));
102         
103         strncpy(udev_config_permission_filename, udev_config_dir, sizeof(udev_config_permission_filename));
104         strncat(udev_config_permission_filename, NAMEDEV_CONFIG_PERMISSION_FILE, sizeof(udev_config_permission_filename));
105 }
106
107 int main(int argc, char **argv, char **envp)
108 {
109         char *action;
110         char *devpath;
111         char *subsystem;
112         int retval = -EINVAL;
113         
114         main_argv = argv;
115         main_envp = envp;
116
117         dbg("version %s", UDEV_VERSION);
118
119         if (argc != 2) {
120                 dbg ("unknown number of arguments");
121                 goto exit;
122         }
123
124         subsystem = argv[1];
125
126         devpath = get_devpath();
127         if (!devpath) {
128                 dbg ("no devpath?");
129                 goto exit;
130         }
131         dbg("looking at %s", devpath);
132
133         /* we only care about class devices and block stuff */
134         if (!strstr(devpath, "class") &&
135             !strstr(devpath, "block")) {
136                 dbg("not block or class");
137                 goto exit;
138         }
139
140         /* but we don't care about net class devices */
141         if (strcmp(subsystem, "net") == 0) {
142                 dbg("don't care about net devices");
143                 goto exit;
144         }
145
146         action = get_action();
147         if (!action) {
148                 dbg ("no action?");
149                 goto exit;
150         }
151
152         /* initialize udev database */
153         get_dirs();
154         retval = udevdb_init(UDEVDB_DEFAULT);
155         if (retval != 0) {
156                 dbg("Unable to initialize database.");
157                 goto exit;
158         }
159
160         /* initialize the naming deamon */
161         namedev_init();
162
163         if (strcmp(action, "add") == 0)
164                 retval = udev_add_device(devpath, subsystem);
165
166         else if (strcmp(action, "remove") == 0)
167                 retval = udev_remove_device(devpath, subsystem);
168
169         else {
170                 dbg("Unknown action: %s", action);
171                 retval = -EINVAL;
172         }
173         udevdb_exit();
174
175 exit:   
176         return retval;
177 }
178