Tomato 1.25
[tomato.git] / release / src / router / rc / rc.c
blob2efa3ed0846e840a8224182cf672c8ae7c78b431
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "rc.h"
11 #ifdef DEBUG_RCTEST
12 // used for various testing
13 static int rctest_main(int argc, char *argv[])
15 if (argc < 2) {
16 printf("test what?\n");
18 else {
19 printf("%s\n", argv[1]);
21 if (strcmp(argv[1], "foo") == 0) {
23 else if (strcmp(argv[1], "qos") == 0) {
24 start_qos();
26 #ifdef DEBUG_IPTFILE
27 else if (strcmp(argv[1], "iptfile") == 0) {
28 create_test_iptfile();
30 #endif
31 else {
32 printf("what?\n");
35 return 0;
37 #endif
40 static int hotplug_main(int argc, char *argv[])
42 if (argc >= 2) {
43 if (strcmp(argv[1], "net") == 0) {
44 hotplug_net();
47 return 0;
50 static int rc_main(int argc, char *argv[])
52 if (argc < 2) return 0;
53 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
54 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
55 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
56 return 0;
61 typedef struct {
62 const char *name;
63 int (*main)(int argc, char *argv[]);
64 } applets_t;
66 static const applets_t applets[] = {
67 { "init", init_main },
68 { "rc", rc_main },
69 { "ip-up", ipup_main },
70 { "ip-down", ipdown_main },
71 { "set-pppoepid", set_pppoepid_main },
72 { "pppoe_down", pppoe_down_main },
73 { "ppp_event", pppevent_main },
74 { "hotplug", hotplug_main },
75 { "redial", redial_main },
76 { "listen", listen_main },
77 { "service", service_main },
78 { "sched", sched_main },
79 { "mtd-write", mtd_write_main },
80 { "mtd-erase", mtd_unlock_erase_main },
81 { "mtd-unlock", mtd_unlock_erase_main },
82 { "buttons", buttons_main },
83 { "rcheck", rcheck_main },
84 { "dhcpc-event", dhcpc_event_main },
85 { "dhcpc-release", dhcpc_release_main },
86 { "dhcpc-renew", dhcpc_renew_main },
87 { "radio", radio_main },
88 { "led", led_main },
89 { "halt", reboothalt_main },
90 { "reboot", reboothalt_main },
91 { "gpio", gpio_main },
92 { "wldist", wldist_main },
93 #ifdef TCONFIG_CIFS
94 { "mount-cifs", mount_cifs_main },
95 #endif
96 #ifdef TCONFIG_DDNS
97 { "ddns-update", ddns_update_main },
98 #endif
99 #ifdef DEBUG_RCTEST
100 { "rctest", rctest_main },
101 #endif
102 {NULL, NULL}
105 int main(int argc, char **argv)
107 char *base;
108 int f;
111 Make sure std* are valid since several functions attempt to close these
112 handles. If nvram_*() runs first, nvram=0, nvram gets closed. - zzz
114 if ((f = open("/dev/null", O_RDWR)) < 3) {
115 dup(f);
116 dup(f);
118 else {
119 close(f);
122 base = strrchr(argv[0], '/');
123 base = base ? base + 1 : argv[0];
125 #if 0
126 if (strcmp(base, "rc") == 0) {
127 if (argc < 2) return 1;
128 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
129 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
130 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
131 ++argv;
132 --argc;
133 base = argv[0];
135 #endif
137 #if defined(DEBUG_NOISY)
138 if (nvram_match("debug_logrc", "1")) {
139 int i;
141 cprintf("[rc %d] ", getpid());
142 for (i = 0; i < argc; ++i) {
143 cprintf("%s ", argv[i]);
145 cprintf("\n");
148 #endif
150 #if defined(DEBUG_NOISY)
151 if (nvram_match("debug_ovrc", "1")) {
152 char tmp[256];
153 char *a[32];
155 realpath(argv[0], tmp);
156 if ((strncmp(tmp, "/tmp/", 5) != 0) && (argc < 32)) {
157 sprintf(tmp, "%s%s", "/tmp/", base);
158 if (f_exists(tmp)) {
159 cprintf("[rc] override: %s\n", tmp);
160 memcpy(a, argv, argc * sizeof(a[0]));
161 a[argc] = 0;
162 a[0] = tmp;
163 execvp(tmp, a);
164 exit(0);
168 #endif
170 const applets_t *a;
171 for (a = applets; a->name; ++a) {
172 if (strcmp(base, a->name) == 0) {
173 openlog(base, LOG_PID, LOG_USER);
174 return a->main(argc, argv);
178 printf("Unknown applet: %s\n", base);
179 return 0;