root/src/router/rc/check_ps.c

Revision 12224, 4.8 kB (checked in by BrainSlayer, 6 months ago)

code formating

Line 
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <wait.h>
13 #include <dlfcn.h>
14
15 #include <bcmnvram.h>
16 #include <netconf.h>
17 #include <shutils.h>
18 #include <utils.h>
19 #include <cy_conf.h>
20 #include <code_pattern.h>
21 #include <rc.h>
22 #include <wlutils.h>
23
24 struct mon {
25         char *name;             // Process name
26         int count;              // Process count, 0 means don't check
27         int type;               // LAN or WAN
28         // int (*stop) (void); // stop function
29         // int (*start) (void); // start function
30         char *nvvalue;
31         char *nvmatch;
32 };
33
34 enum { M_LAN, M_WAN };
35
36 struct mon mons[] = {
37         // {"tftpd", 1, M_LAN, stop_tftpd, start_tftpd},
38         {"upnp", 1, M_LAN, "upnp_enable", "1"},
39         {"process_monitor", 1, M_LAN},
40         {"httpd", 2, M_LAN},
41         {"udhcpd", 1, M_LAN},
42         {"dnsmasq", 1, M_LAN, "dnsmasq_enable", "1"},
43         {"dhcpfwd", 1, M_LAN, "dhcpfwd_enable", "1"},
44 #ifdef HAVE_NOCAT
45         {"splashd", 1, M_LAN, "NC_enable", "1"},
46 #endif
47 #ifdef HAVE_CHILLI
48         {"chilli", 1, M_LAN, "chilli_enable", "1"},
49 #endif
50 #ifdef HAVE_WIFIDOG
51         {"wifidog", 1, M_WAN, "wd_enable", "1"},
52 #endif
53 #ifdef HAVE_OLSRD
54         {"olsrd", 1, M_LAN, "wk_mode", "olsrd"},
55 #endif
56 #ifdef HAVE_SPUTNIK_APD
57         {"sputnik", 1, M_WAN, "apd_enable", "1"},
58 #endif
59         {NULL, 0, 0}
60 };
61
62 int search_process(char *name, int count)
63 {
64         int c = 0;
65
66         c = count_processes(name);
67         if (!c) {
68                 printf("Can't find %s\n", name);
69                 return 0;
70         } else {
71                 printf("Find %s which count is %d\n", name, c);
72                 // if(count && c != count){
73                 // cprintf("%s count is not match\n", name);
74                 // return 0;
75                 // }
76                 // else
77                 return 1;
78         }
79 }
80
81 void checknas(void)             // for broadcom v24 only
82 {
83 #if !defined(HAVE_MADWIFI) && !defined(HAVE_RT2880)
84
85         char buf[32];
86         FILE *fnas = fopen("/tmp/.nas", "r");
87
88         if (fnas == NULL)
89                 return;
90
91         fgets(buf, sizeof(buf), fnas);
92         fclose(fnas);
93
94         if (strlen(buf) != count_processes("nas"))      // restart all nas
95                 // processes
96         {
97                 eval("stopservice", "nas");
98                 eval("startservice_f", "nas");
99         }
100
101         return;
102
103 #endif
104 }
105
106 /*
107  * software wlan led control
108  */
109 void softcontrol_wlan_led(void) // done in watchdog.c for non-micro
110                                         // builds.
111 {
112 #if defined(HAVE_MICRO) && !defined(HAVE_ADM5120)
113
114         int brand;
115         int radiostate = -1;
116         int oldstate = -1;
117
118 #ifdef HAVE_MADWIFI
119         radiostate = get_radiostate("ath0");
120 #else
121         wl_ioctl(get_wdev(), WLC_GET_RADIO, &radiostate, sizeof(int));
122 #endif
123
124         if (radiostate != oldstate) {
125 #ifdef HAVE_MADWIFI
126                 if (radiostate == 1)
127 #else
128                 if (radiostate == 0)
129 #endif
130                         led_control(LED_WLAN, LED_ON);
131                 else {
132                         led_control(LED_WLAN, LED_OFF);
133 #ifndef HAVE_MADWIFI
134                         brand = getRouterBrand();
135                         /*
136                          * Disable wireless will cause diag led blink, so we want to stop
137                          * it.
138                          */
139                         if (brand == ROUTER_WRT54G)
140                                 diag_led(DIAG, STOP_LED);
141                         /*
142                          * Disable wireless will cause power led off, so we want to turn
143                          * it on.
144                          */
145                         if (brand == ROUTER_WRT54G_V8)
146                                 led_control(LED_POWER, LED_ON);
147 #endif
148                 }
149
150                 oldstate = radiostate;
151         }
152         return;
153
154 #endif
155 }
156
157 /*
158  * end software wlan led control
159  */
160
161 void checkupgrade(void)
162 {
163 #ifndef HAVE_X86
164         FILE *in = fopen("/tmp/firmware.bin", "rb");
165
166         if (in != NULL) {
167                 fclose(in);
168                 eval("rm", "/tmp/cron.d/check_ps");     // deleting cron file to
169                 // prevent double call of
170                 // this
171                 fprintf(stderr,
172                         "found firmware upgrade, flashing now, but we will wait for another 30 seconds\n");
173                 sleep(30);
174 #if defined(HAVE_WHRAG108) || defined(HAVE_TW6600) || defined(HAVE_LS5)
175                 eval("write", "/tmp/firmware.bin", "rootfs");
176 #else
177                 eval("write", "/tmp/firmware.bin", "linux");
178 #endif
179                 fprintf(stderr, "done. rebooting now\n");
180                 eval("killall", "-3", "init");
181         }
182 #endif
183 }
184
185 int do_mon(void)
186 {
187         struct mon *v;
188
189         checkupgrade();
190         checknas();
191 #ifndef HAVE_RT2880
192         softcontrol_wlan_led();
193 #endif
194         for (v = mons; v < &mons[sizeof(mons) / sizeof(struct mon)]; v++) {
195                 if (v->name == NULL)
196                         break;
197                 if (v->nvvalue && v->nvmatch) {
198                         if (!nvram_match(v->nvvalue, v->nvmatch))
199                                 continue;       // service not enabled. no need to check
200                 }
201                 printf("checking %s\n", v->name);
202
203                 if (v->type == M_WAN)
204                         if (!check_wan_link(0)) {
205                                 printf("process is wan, but wan is not up\n");
206                                 continue;
207                         }
208                 if (!search_process(v->name, v->count)) {
209
210                         printf("Maybe %s had died, we need to re-exec it\n",
211                                v->name);
212                         eval("stopservice", v->name);
213                         killall(v->name, SIGKILL);
214                         eval("startservice_f", v->name);
215                 }
216                 printf("checking for %s done\n", v->name);
217         }
218
219         return 1;
220 }
221
222 int main(int argc, char **argv)
223 {
224         pid_t pid;
225
226         if (check_action() != ACT_IDLE) {       // Don't execute during upgrading
227                 printf("check_ps: nothing to do...\n");
228                 return 1;
229         }
230
231         pid = fork();
232         switch (pid) {
233         case -1:
234                 perror("fork failed");
235                 exit(1);
236                 break;
237         case 0:
238                 do_mon();
239                 exit(0);
240                 break;
241         default:
242                 _exit(0);
243                 break;
244         }
245 }
Note: See TracBrowser for help on using the browser.