blob: bcc165a153fb2c209ed0f0625c4ed8491292f4c7 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 */
3#include <stdio.h>
4#include <unistd.h>
5#include <string.h>
6#include <stdlib.h>
7#include <signal.h>
8#include <sys/stat.h>
9#include "rtk_arch.h"
10
11static int isDaemon=0;
12static char *pidfile=(TOP_CONFIG_DIR "/run/webs.pid");
13#define REINIT_WEB_FILE (WIFI_WPS_TMP_DIR "/reinit_web")
14
15void get_wifi_script_dir(char *path, size_t size)
16{
17 FILE *fp;
18 char *pstr;
19 struct stat statbuf;
20 int len = 0;
21
22 fp = fopen(WIFI_CONFIG_ROOT_DIR "/wifi_script_dir", "r");
23 if (fp) {
24 pstr = fgets(path, size, fp);
25 fclose(fp);
26 if (pstr) {
27 len = strlen(path);
28 if ((len > 0) && ('\n' == path[len-1])) {
29 --len;
30 path[len] = '\0';
31 }
32 if (stat(path, &statbuf) == -1) { // dir not exist
33 len = 0;
34 }
35 }
36 }
37 if (0 == len) {
38 strncpy(path, WIFI_SCRIPT_DIR, size);
39 }
40}
41
42void sigHandler_autoconf(int signo)
43{
44 char tmpbuf[256]={0};
45 struct stat status;
46 int reinit=1;
47
48printf("<<Got Sig and Will Check Do init.sh:%d>>\n",signo);
49 if (stat(REINIT_WEB_FILE, &status) == 0) { // file existed
50 unlink(REINIT_WEB_FILE);
51 reinit = 0;
52 }
53 if (reinit) { // re-init system
54 printf("<<Got Sig and Do init.sh>>\n");
55#ifdef CONFIG_NEW_SCRIPT
56 get_wifi_script_dir(tmpbuf, sizeof(tmpbuf));
57 strncat(tmpbuf, "/../my_test.sh wps_restart", sizeof(tmpbuf)-strlen(tmpbuf)-1);
58#else
59 get_wifi_script_dir(tmpbuf, sizeof(tmpbuf));
60 strncat(tmpbuf, "/init.sh", sizeof(tmpbuf)-strlen(tmpbuf)-1);
61#endif
62 system(tmpbuf);
63 }
64}
65void init_signals(void)
66{
67 struct sigaction sa;
68
69 sa.sa_flags = SA_NODEFER;
70
71 sigemptyset(&sa.sa_mask);
72
73 sa.sa_handler = sigHandler_autoconf;
74
75 sigaction(SIGUSR1, &sa, NULL);
76 //sigaction(SIGTERM, &sa, NULL);
77}
78int main(int argc, char *argv[])
79{
80 int i;
81 char line[20];
82 FILE *fp;
83
84 if( argc==2 && (!strcmp(argv[1], "version")) )
85 {
86 printf("version : %s\n",DAEMON_VERSION);
87 return 0;
88 }
89
90 for(i=1; i<argc; i++)
91 {
92 if(argv[i][0]!='-')
93 {
94 fprintf(stderr, "%s: Unknown option\n", argv[i]);
95 }
96 else switch(argv[i][1])
97 {
98 case 'x':
99 isDaemon = 1;
100 break;
101 default:
102 fprintf(stderr, "%s: Unknown option\n", argv[i]);
103 }
104 }
105 if(isDaemon==1){
106 if (daemon(0, 1) == -1) {
107 perror("webs fork error");
108 return 0;
109 }
110 }
111 init_signals();
112 setsid();
113 sprintf(line, "%d\n", getpid());
114 if ((fp = fopen(pidfile, "w")) == NULL) {
115 printf("open file error:%s\n",pidfile);
116 return -1;
117 }
118 fwrite(line, strlen(line), 1, fp);
119 fclose(fp);
120
121 for (;;) {
122 sleep(5);
123 }
124 return 0;
125}
126
127
128