Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / udevtrigger / udevtrigger.c
blob88eca2c7500b858ff962f14454057ee34369ebde
1 /*
2 * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2006 Hannes Reinecke <hare@suse.de>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <getopt.h>
27 #include <errno.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30 #include <syslog.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
34 #define PATH_SIZE 512
36 static int verbose;
37 static int dry_run;
39 void log_message(int priority, const char *format, ...)
41 va_list args;
43 va_start(args, format);
44 vsyslog(priority, format, args);
45 va_end(args);
48 #undef err
49 #define err(format, arg...) \
50 do { \
51 log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg); \
52 } while (0)
54 #undef info
55 #define info(format, arg...) \
56 do { \
57 log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg); \
58 } while (0)
60 #ifdef DEBUG
61 #undef dbg
62 #define dbg(format, arg...) \
63 do { \
64 log_message(LOG_DEBUG ,"%s: " format ,__FUNCTION__ ,## arg); \
65 } while (0)
66 #else
67 #define dbg(...) do {} while(0)
68 #endif
71 static void trigger_uevent(const char *devpath)
73 char filename[PATH_SIZE];
74 int fd;
76 strlcpy(filename, "/sys", sizeof(filename));
77 strlcat(filename, devpath, sizeof(filename));
78 strlcat(filename, "/uevent", sizeof(filename));
80 if (verbose)
81 printf("%s\n", devpath);
83 if (dry_run)
84 return;
86 fd = open(filename, O_WRONLY);
87 if (fd < 0) {
88 dbg("error on opening %s: %s\n", filename, strerror(errno));
89 return;
92 if (write(fd, "add", 3) < 0)
93 info("error on triggering %s: %s\n", filename, strerror(errno));
95 close(fd);
98 static int sysfs_resolve_link(char *devpath, size_t size)
100 char link_path[PATH_SIZE];
101 char link_target[PATH_SIZE];
102 int len;
103 int i;
104 int back;
106 strlcpy(link_path, "/sys", sizeof(link_path));
107 strlcat(link_path, devpath, sizeof(link_path));
108 len = readlink(link_path, link_target, sizeof(link_target));
109 if (len <= 0)
110 return -1;
111 link_target[len] = '\0';
112 dbg("path link '%s' points to '%s'", devpath, link_target);
114 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
116 dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
117 for (i = 0; i <= back; i++) {
118 char *pos = strrchr(devpath, '/');
120 if (pos == NULL)
121 return -1;
122 pos[0] = '\0';
124 dbg("after moving back '%s'", devpath);
125 strlcat(devpath, "/", size);
126 strlcat(devpath, &link_target[back * 3], size);
127 return 0;
131 static int device_list_insert(const char *path)
133 char filename[PATH_SIZE];
134 char devpath[PATH_SIZE];
135 struct stat statbuf;
137 dbg("add '%s'" , path);
139 /* we only have a device, if we have an uevent file */
140 strlcpy(filename, path, sizeof(filename));
141 strlcat(filename, "/uevent", sizeof(filename));
142 if (stat(filename, &statbuf) < 0)
143 return -1;
144 if (!(statbuf.st_mode & S_IWUSR))
145 return -1;
147 strlcpy(devpath, &path[4], sizeof(devpath));
149 /* resolve possible link to real target */
150 if (lstat(path, &statbuf) < 0)
151 return -1;
152 if (S_ISLNK(statbuf.st_mode))
153 if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
154 return -1;
156 trigger_uevent(devpath);
157 return 0;
161 static void scan_subsystem(const char *subsys)
163 char base[PATH_SIZE];
164 DIR *dir;
165 struct dirent *dent;
167 strlcpy(base, "/sys/", sizeof(base));
168 strlcat(base, subsys, sizeof(base));
170 dir = opendir(base);
171 if (dir != NULL) {
172 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
173 char dirname[PATH_SIZE];
174 DIR *dir2;
175 struct dirent *dent2;
177 if (dent->d_name[0] == '.')
178 continue;
180 strlcpy(dirname, base, sizeof(dirname));
181 strlcat(dirname, "/", sizeof(dirname));
182 strlcat(dirname, dent->d_name, sizeof(dirname));
183 strlcat(dirname, "/devices", sizeof(dirname));
185 /* look for devices */
186 dir2 = opendir(dirname);
187 if (dir2 != NULL) {
188 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
189 char dirname2[PATH_SIZE];
191 if (dent2->d_name[0] == '.')
192 continue;
194 strlcpy(dirname2, dirname, sizeof(dirname2));
195 strlcat(dirname2, "/", sizeof(dirname2));
196 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
197 device_list_insert(dirname2);
199 closedir(dir2);
202 closedir(dir);
206 static void scan_block(void)
208 char base[PATH_SIZE];
209 DIR *dir;
210 struct dirent *dent;
212 strlcpy(base, "/sys/block", sizeof(base));
214 dir = opendir(base);
215 if (dir != NULL) {
216 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
217 char dirname[PATH_SIZE];
218 DIR *dir2;
219 struct dirent *dent2;
221 if (dent->d_name[0] == '.')
222 continue;
224 strlcpy(dirname, base, sizeof(dirname));
225 strlcat(dirname, "/", sizeof(dirname));
226 strlcat(dirname, dent->d_name, sizeof(dirname));
227 if (device_list_insert(dirname) != 0)
228 continue;
230 /* look for partitions */
231 dir2 = opendir(dirname);
232 if (dir2 != NULL) {
233 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
234 char dirname2[PATH_SIZE];
236 if (dent2->d_name[0] == '.')
237 continue;
239 if (!strcmp(dent2->d_name,"device"))
240 continue;
242 strlcpy(dirname2, dirname, sizeof(dirname2));
243 strlcat(dirname2, "/", sizeof(dirname2));
244 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
245 device_list_insert(dirname2);
247 closedir(dir2);
250 closedir(dir);
254 static void scan_class(void)
256 char base[PATH_SIZE];
257 DIR *dir;
258 struct dirent *dent;
260 strlcpy(base, "/sys/class", sizeof(base));
262 dir = opendir(base);
263 if (dir != NULL) {
264 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
265 char dirname[PATH_SIZE];
266 DIR *dir2;
267 struct dirent *dent2;
269 if (dent->d_name[0] == '.')
270 continue;
272 strlcpy(dirname, base, sizeof(dirname));
273 strlcat(dirname, "/", sizeof(dirname));
274 strlcat(dirname, dent->d_name, sizeof(dirname));
275 dir2 = opendir(dirname);
276 if (dir2 != NULL) {
277 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
278 char dirname2[PATH_SIZE];
280 if (dent2->d_name[0] == '.')
281 continue;
283 if (!strcmp(dent2->d_name, "device"))
284 continue;
286 strlcpy(dirname2, dirname, sizeof(dirname2));
287 strlcat(dirname2, "/", sizeof(dirname2));
288 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
289 device_list_insert(dirname2);
291 closedir(dir2);
294 closedir(dir);
298 int main(int argc, char *argv[], char *envp[])
300 char base[PATH_SIZE];
301 struct stat statbuf;
302 int option;
304 openlog("udevtrigger", LOG_PID | LOG_CONS, LOG_DAEMON);
306 while (1) {
307 option = getopt(argc, argv, "vnh");
308 if (option == -1)
309 break;
311 switch (option) {
312 case 'v':
313 verbose = 1;
314 break;
315 case 'n':
316 dry_run = 1;
317 break;
318 case 'h':
319 printf("Usage: udevtrigger OPTIONS\n"
320 " -v print the list of devices while running\n"
321 " -n do not actually trigger the events\n"
322 " -h print this text\n"
323 "\n");
324 goto exit;
325 default:
326 goto exit;
331 /* if we have /sys/subsystem, forget all the old stuff */
332 scan_subsystem("bus");
333 scan_class();
335 /* scan "block" if it isn't a "class" */
336 strlcpy(base, "/sys/class/block", sizeof(base));
337 if (stat(base, &statbuf) != 0)
338 scan_block();
340 exit:
342 closelog();
343 return 0;