码迷,mamicode.com
首页 > 其他好文 > 详细

main.c

时间:2018-04-10 17:42:57      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:flags   AC   signed   har   stderr   trying   star   mmu   voluntary   

#include "log.h"

#include <stdlib.h> //atoi malloc
#include <string.h>
#include <stdio.h> //printf perror
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h> //for waitpid WNOHANG
#include <errno.h> /* for errno and ECHILD*/
#include <unistd.h>
int checketh(); static void init_signals(void); #define VERSION "0.6.6" int main(int argc, char** argv) { #if 0 pid_t pid, sid; /* Fork off the parent process */ pid = fork(); if(pid < 0) { exit(EXIT_FAILURE); } /* If we got a good PID, then we can exit the parent process */ if(pid > 0) { exit(EXIT_SUCCESS); } /* Change the file mode mask */ umask(0); //daemon /* create a new SID for the child process */ sid = setsid(); if(sid < 0) { /* Log any failure*/ //Log(0, "EXIT SID"); exit(EXIT_FAILURE); } /* Change the current working directory */ if ((chdir("/")) < 0) { /* Log any failure here */ //Log(0, "chdir"); exit(EXIT_FAILURE); } /* Close out the standard file descriptor */ //check this because we need print when debug close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); //daemon ends #endif //#if 0 printf("This is version %s\n", VERSION); if(process_check() != 0) { printf("ss is already running\n"); return 1; } int ret = 0; //before below two steps, init logs ret = initlog(); if(ret == 1) { printf("init log file failed\n"); return 1; } else if(ret == 2) { printf("INFO: log already created\n"); Log(0, "INFO: log already created"); } //#if 0 if (argc == 11) { port_vsp = atol(argv[3]); //ip_vsp[length+1] = ‘\0‘; S_TIME = atoi(argv[4]); D_TIME = atoi(argv[5]); U_TIME = atoi(argv[6]); A_TIME = atoi(argv[7]); //STEP = atoi(argv[9]); T_TIME = atoi(argv[8]); } else { //Log(0, "test2"); printf("e.g bss: ./ss0.6.5 192.168.1.38 9020 50000 15 200 50 2 3 80 60000 &\n"); return 1; } pthread_t pid_uart0 = -1; pthread_t pid_uart1 = -1; Log(0, "This is version %s", VERSION); Log(0, "ss connect bss %s:%d, ss setup server %d, S_TIME %dms, D_TIME %dms, U_TIME %dms, A_TIME %ds, T_TIME %ds\n", ip_bss, port_bss, port_vsp, S_TIME, D_TIME, U_TIME, A_TIME, T_TIME); /* Init the signals to catch chld/quit/etc */ init_signals(); printf("init signals\n"); err = initdata(); if(err != 0) { printf("initdata error\n"); exit(-1); } //start getinfo and communicate BSS thread if (pthread_create(&pid_commBSS, NULL, &thread_commBSS, NULL) != 0) { printf("pthread_create thread_commBSS\n"); Log(0, "pthread_create thread_commBSS"); exit(3); } else { pthread_detach(pid_commBSS); } //start timeout check thread if (pthread_create(&pid_timeout, NULL, &thread_timeout, NULL) != 0) { printf("pthread_create thread_timeout\n"); Log(0, "pthread_create thread_timeout"); exit(3); } else { pthread_detach(pid_timeout); } } int checketh() { int o_eth0 = 0; int ret = 1; o_eth0 = open(P_O_ETH0, O_RDONLY); if (o_eth0 == -1) { perror("open P_O_ETH0"); return 1; } unsigned char operstate_eth[10]; memset(operstate_eth, 0, sizeof(operstate_eth)); int length = -1; length = read(o_eth0, operstate_eth, 10); printf("operstate length %d %s\n", length, operstate_eth); if (length == -1) { perror("read o_eth0"); Log(0, "read o_eth0 error"); ret = 2; goto EXITCHK; } else if(length > 0) { if (strncmp(operstate_eth, "up", 2) == 0) { ret = 0; status_BSS = 0x55; status_VSP = 0x55; goto EXITCHK; } else if(strncmp(operstate_eth, "down", 4) == 0) { ret = 3; } } EXITCHK: if(o_eth0) close(o_eth0); return ret; } /**@internal * @brief Handles SIGCHLD signals to avoid zombie processes * * When a child process exits, it causes a SIGCHLD to be sent to the * process. This handler catches it and reaps the child process so it * can exit. Otherwise we‘d get zombie processes. */ //20160918: i think there should nerver reach here, because we didn‘t fork process. void sigchl_handler(int s) { int status; pid_t rc; Log(0, "WARNING: Handler for SIGCHLD called. Trying to reap a child"); printf("WARNING: Handler for SIGCHLD called. Trying to reap a child\n"); rc = waitpid(-1, &status, WNOHANG); Log(0, "WARNING: Handler for SIGCHLD reaped child PID %d", rc); printf("WARNING: Handler for SIGCHLD reaped child PID %d\n", rc); } /** Exits cleanly after cleaning up the firewall. * Use this function anytime you need to exit after firewall initialization. * @param s Integer that is really a boolean, true means voluntary exit, 0 means error. */ void termination_handler(int s) { Log(0, "WARNING: Handler for termination caught signal %d", s); printf("WARNING: Handler for termination caught signal %d\n", s); Log(0, "WARNING: Exiting...\n\n"); printf("WARNING: Exiting...\n"); exit(s == 0 ? 1 : 0); } /** @internal * Registers all the signal handlers */ static void init_signals(void) { struct sigaction sa; Log(0, "INFO: Initializing signal handlers"); sa.sa_handler = sigchl_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { Log(0, "ERROR: sigaction(): %s", strerror(errno)); exit(1); } /* Trap SIGPIPE */ /* This is done so that when libhttpd does a socket operation on * a disconnected socket (i.e.: Broken Pipes) we catch the signal * and do nothing. The alternative is to exit. SIGPIPE are harmless * if not desirable. */ sa.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &sa, NULL) == -1) { Log(0, "ERROR: sigaction(): %s", strerror(errno)); exit(1); } sa.sa_handler = termination_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; /* Trap SIGTERM */ if (sigaction(SIGTERM, &sa, NULL) == -1) { Log(0, "ERROR: sigaction(): %s", strerror(errno)); exit(1); } /* Trap SIGQUIT */ if (sigaction(SIGQUIT, &sa, NULL) == -1) { Log(0, "ERROR: sigaction(): %s", strerror(errno)); exit(1); } /* Trap SIGINT */ if (sigaction(SIGINT, &sa, NULL) == -1) { Log(0, "ERROR: sigaction(): %s", strerror(errno)); exit(1); } }

  

main.c

标签:flags   AC   signed   har   stderr   trying   star   mmu   voluntary   

原文地址:https://www.cnblogs.com/qiaolong/p/8780311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!