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