Merge commit 'origin/tomato-RT' into tomato-shibby
[tomato.git] / release / src / router / rc / rc.c
blob1b833befa07d96e1087decca9bf087f231c7f2a3
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();
46 #ifdef TCONFIG_USB
47 // !!TB - USB Support
48 else if (strcmp(argv[1], "usb") == 0) {
49 hotplug_usb();
51 #ifdef LINUX26
52 else if (strcmp(argv[1], "block") == 0) {
53 hotplug_usb();
55 #endif
56 #endif
58 return 0;
61 static int rc_main(int argc, char *argv[])
63 if (argc < 2) return 0;
64 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
65 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
66 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
67 return 0;
72 typedef struct {
73 const char *name;
74 int (*main)(int argc, char *argv[]);
75 } applets_t;
77 static const applets_t applets[] = {
78 { "init", init_main },
79 { "console", console_main },
80 { "rc", rc_main },
81 { "ip-up", ipup_main },
82 { "ip-down", ipdown_main },
83 #ifdef TCONFIG_IPV6
84 { "ipv6-up", ip6up_main },
85 { "ipv6-down", ip6down_main },
86 #endif
87 { "ppp_event", pppevent_main },
88 { "hotplug", hotplug_main },
89 { "redial", redial_main },
90 { "listen", listen_main },
91 { "service", service_main },
92 { "sched", sched_main },
93 { "mtd-write", mtd_write_main },
94 { "mtd-erase", mtd_unlock_erase_main },
95 { "mtd-unlock", mtd_unlock_erase_main },
96 { "buttons", buttons_main },
97 { "rcheck", rcheck_main },
98 { "dhcpc-event", dhcpc_event_main },
99 { "dhcpc-release", dhcpc_release_main },
100 { "dhcpc-renew", dhcpc_renew_main },
101 #ifdef TCONFIG_IPV6
102 { "dhcp6c-state", dhcp6c_state_main },
103 #endif
104 { "radio", radio_main },
105 { "led", led_main },
106 { "halt", reboothalt_main },
107 { "reboot", reboothalt_main },
108 { "gpio", gpio_main },
109 { "wldist", wldist_main },
110 #ifdef TCONFIG_CIFS
111 { "mount-cifs", mount_cifs_main },
112 #endif
113 #ifdef TCONFIG_DDNS
114 { "ddns-update", ddns_update_main },
115 #endif
116 #ifdef DEBUG_RCTEST
117 { "rctest", rctest_main },
118 #endif
119 {NULL, NULL}
122 int main(int argc, char **argv)
124 char *base;
125 int f;
128 Make sure std* are valid since several functions attempt to close these
129 handles. If nvram_*() runs first, nvram=0, nvram gets closed. - zzz
131 if ((f = open("/dev/null", O_RDWR)) < 3) {
132 dup(f);
133 dup(f);
135 else {
136 close(f);
139 base = strrchr(argv[0], '/');
140 base = base ? base + 1 : argv[0];
142 #if 1
143 if (strcmp(base, "rc") == 0) {
144 if (argc < 2) return 1;
145 if (strcmp(argv[1], "start") == 0) return kill(1, SIGUSR2);
146 if (strcmp(argv[1], "stop") == 0) return kill(1, SIGINT);
147 if (strcmp(argv[1], "restart") == 0) return kill(1, SIGHUP);
148 ++argv;
149 --argc;
150 base = argv[0];
152 #endif
154 #if defined(DEBUG_NOISY)
155 if (nvram_match("debug_logrc", "1")) {
156 int i;
158 cprintf("[rc %d] ", getpid());
159 for (i = 0; i < argc; ++i) {
160 cprintf("%s ", argv[i]);
162 cprintf("\n");
165 #endif
167 #if defined(DEBUG_NOISY)
168 if (nvram_match("debug_ovrc", "1")) {
169 char tmp[256];
170 char *a[32];
172 realpath(argv[0], tmp);
173 if ((strncmp(tmp, "/tmp/", 5) != 0) && (argc < 32)) {
174 sprintf(tmp, "%s%s", "/tmp/", base);
175 if (f_exists(tmp)) {
176 cprintf("[rc] override: %s\n", tmp);
177 memcpy(a, argv, argc * sizeof(a[0]));
178 a[argc] = 0;
179 a[0] = tmp;
180 execvp(tmp, a);
181 exit(0);
185 #endif
187 const applets_t *a;
188 for (a = applets; a->name; ++a) {
189 if (strcmp(base, a->name) == 0) {
190 openlog(base, LOG_PID, LOG_USER);
191 return a->main(argc, argv);
195 printf("Unknown applet: %s\n", base);
196 return 0;