]>
Commit | Line | Data |
---|---|---|
f0083e3d GKH |
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> | |
85511f02 GKH |
26 | #include <stdio.h> |
27 | #include <fcntl.h> | |
28 | #include <unistd.h> | |
29 | #include <errno.h> | |
e8baccca | 30 | #include <ctype.h> |
85511f02 | 31 | |
f0083e3d GKH |
32 | #include "udev.h" |
33 | #include "udev_version.h" | |
2232cac8 | 34 | #include "namedev.h" |
dbfc520c | 35 | #include "udevdb.h" |
19dc5d4c | 36 | #include "libsysfs/libsysfs.h" |
f0083e3d | 37 | |
c056c514 GKH |
38 | /* global variables */ |
39 | char **main_argv; | |
40 | char **main_envp; | |
41 | ||
c056c514 | 42 | static inline char *get_action(void) |
f0083e3d GKH |
43 | { |
44 | char *action; | |
45 | ||
46 | action = getenv("ACTION"); | |
47 | return action; | |
48 | } | |
49 | ||
c056c514 GKH |
50 | static inline char *get_devpath(void) |
51 | { | |
52 | char *devpath; | |
53 | ||
54 | devpath = getenv("DEVPATH"); | |
55 | return devpath; | |
56 | } | |
f0083e3d | 57 | |
c056c514 | 58 | static inline char *get_seqnum(void) |
f0083e3d | 59 | { |
c056c514 | 60 | char *seqnum; |
f0083e3d | 61 | |
c056c514 GKH |
62 | seqnum = getenv("SEQNUM"); |
63 | return seqnum; | |
f0083e3d GKH |
64 | } |
65 | ||
c27e6911 | 66 | int main(int argc, char **argv, char **envp) |
f0083e3d | 67 | { |
f0083e3d | 68 | char *action; |
c056c514 | 69 | char *devpath; |
ea733a2f | 70 | char *subsystem; |
85511f02 | 71 | int retval = -EINVAL; |
f0083e3d | 72 | |
c27e6911 PM |
73 | main_argv = argv; |
74 | main_envp = envp; | |
75 | ||
2e5a8b99 GKH |
76 | dbg("version %s", UDEV_VERSION); |
77 | ||
f0083e3d GKH |
78 | if (argc != 2) { |
79 | dbg ("unknown number of arguments"); | |
85511f02 | 80 | goto exit; |
f0083e3d GKH |
81 | } |
82 | ||
ea733a2f GKH |
83 | subsystem = argv[1]; |
84 | ||
c056c514 GKH |
85 | devpath = get_devpath(); |
86 | if (!devpath) { | |
87 | dbg ("no devpath?"); | |
19dc5d4c GKH |
88 | goto exit; |
89 | } | |
f7b4eca4 | 90 | dbg("looking at '%s'", devpath); |
2232cac8 | 91 | |
19dc5d4c | 92 | /* we only care about class devices and block stuff */ |
c056c514 GKH |
93 | if (!strstr(devpath, "class") && |
94 | !strstr(devpath, "block")) { | |
f7b4eca4 | 95 | dbg("not a block or class device"); |
19dc5d4c GKH |
96 | goto exit; |
97 | } | |
af815f88 | 98 | |
ea733a2f GKH |
99 | /* but we don't care about net class devices */ |
100 | if (strcmp(subsystem, "net") == 0) { | |
101 | dbg("don't care about net devices"); | |
102 | goto exit; | |
103 | } | |
f0083e3d GKH |
104 | |
105 | action = get_action(); | |
106 | if (!action) { | |
107 | dbg ("no action?"); | |
85511f02 | 108 | goto exit; |
f0083e3d GKH |
109 | } |
110 | ||
e8baccca GKH |
111 | /* initialize our configuration */ |
112 | udev_init_config(); | |
113 | ||
dbfc520c DS |
114 | /* initialize udev database */ |
115 | retval = udevdb_init(UDEVDB_DEFAULT); | |
116 | if (retval != 0) { | |
f7b4eca4 | 117 | dbg("unable to initialize database"); |
dbfc520c DS |
118 | goto exit; |
119 | } | |
120 | ||
ea733a2f GKH |
121 | /* initialize the naming deamon */ |
122 | namedev_init(); | |
123 | ||
3f4967a1 | 124 | if (strcmp(action, "add") == 0) |
c056c514 | 125 | retval = udev_add_device(devpath, subsystem); |
85511f02 | 126 | |
dbfc520c | 127 | else if (strcmp(action, "remove") == 0) |
c056c514 | 128 | retval = udev_remove_device(devpath, subsystem); |
85511f02 | 129 | |
dbfc520c | 130 | else { |
f7b4eca4 | 131 | dbg("unknown action '%s'", action); |
dbfc520c DS |
132 | retval = -EINVAL; |
133 | } | |
134 | udevdb_exit(); | |
85511f02 | 135 | |
85511f02 GKH |
136 | exit: |
137 | return retval; | |
f0083e3d | 138 | } |