chiark / gitweb /
[PATCH] tell the user what mknod() we are trying to do.
[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
38 static char *get_action(void)
39 {
40         char *action;
41
42         action = getenv("ACTION");
43         return action;
44 }
45
46
47 static char *get_device(void)
48 {
49         char *device;
50
51         device = getenv("DEVPATH");
52         return device;
53 }
54
55 char **main_argv;
56 char **main_envp;
57
58 int main(int argc, char **argv, char **envp)
59 {
60         char *action;
61         char *device;
62         char *subsystem;
63         int retval = -EINVAL;
64         
65         main_argv = argv;
66         main_envp = envp;
67
68         if (argc != 2) {
69                 dbg ("unknown number of arguments");
70                 goto exit;
71         }
72
73         subsystem = argv[1];
74
75         device = get_device();
76         if (!device) {
77                 dbg ("no device?");
78                 goto exit;
79         }
80         dbg("looking at %s", device);
81
82         /* we only care about class devices and block stuff */
83         if (!strstr(device, "class") &&
84             !strstr(device, "block")) {
85                 dbg("not block or class");
86                 goto exit;
87         }
88
89         /* but we don't care about net class devices */
90         if (strcmp(subsystem, "net") == 0) {
91                 dbg("don't care about net devices");
92                 goto exit;
93         }
94
95         action = get_action();
96         if (!action) {
97                 dbg ("no action?");
98                 goto exit;
99         }
100
101         /* initialize udev database */
102         retval = udevdb_init(UDEVDB_DEFAULT);
103         if (retval != 0) {
104                 dbg("Unable to initialize database.");
105                 goto exit;
106         }
107
108         /* initialize the naming deamon */
109         namedev_init();
110
111         if (strcmp(action, "add") == 0)
112                 retval = udev_add_device(device, argv[1]);
113
114         else if (strcmp(action, "remove") == 0)
115                 retval = udev_remove_device(device, argv[1]);
116
117         else {
118                 dbg("Unknown action: %s", action);
119                 retval = -EINVAL;
120         }
121         udevdb_exit();
122
123 exit:   
124         return retval;
125 }
126