📄 simumain.c
字号:
#define WIN32_OTHER 1#include "simu112.h"#include <string.h>#include <errno.h>#ifdef WIN32_ENV#include <io.h>#endif#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <signal.h>#include <fcntl.h>#include <sys/stat.h>#include <stdarg.h>#ifndef WIN32_ENV#include <unistd.h>#include <string.h>#ifndef HPUX#include <sys/syslog.h>#endif#include <sys/types.h>#include <sys/wait.h>#include <sys/param.h>#endifextern DEVICEHEAD *device_head;extern COMPRULEDATA comp_rule_data[MAX_SWITCH + 1] = { {"UNKNOWN", 0, NULL, NULL}, {"S1240E", 1, NULL, NULL}, {"DMS100", 2, NULL, NULL}, {"DMS10", 3, NULL, NULL}, {"SP30", 4, NULL, NULL}, {"5ESS", 5, 0, 0}, {"AXE10", 6, 0, 0}, {"NEAX61", 7, NULL, NULL}, {"SP30CN", 8, NULL, NULL}, {"CC08", 9, NULL, NULL}, {"HW_AN", 10, NULL, NULL}, {"ZX_AN", 11, NULL, NULL}, {"UT_AN", 12, NULL, NULL}, {"BE_AN", 13, NULL, NULL}, {"XMZ_AN", 14, NULL, NULL}, {"EWSD", 15, NULL, NULL}, {"S1240J", 16, NULL, NULL}, {"F150", 17, NULL, NULL}, {"FST_AN", 18, NULL, NULL}, {"LX_AN", 19, NULL, NULL}, {"NEAX_RSU", 20, NULL, NULL}, {"NEC_AN", 21, NULL, NULL}, {"EWSD_OMT", 22, NULL, NULL}, {"EWSD_OMT13", 23, NULL, NULL}, {"DEMO", 24, NULL, NULL}, {"Q3_AN", 25, NULL, NULL}, {"\0", 0, NULL, NULL}};SOCKETSTRUCT so[MAX_FD];char LIC_USERNAME[61];int LIC_DRIVERNUMBER;typedef struct{ FILE *Logfp; char LogFile[20];} THISAPP;THISAPP ThisApp;sig_atomic_t Child_died = 0;void fQuitAll(int signo);static void sig_child_died(int sig);void handle_child_died(void);void make_daemon();int fBackupLog(void);int fCloseLog() ;int fLogInfo(char *fmt, ...);int fOpenLog();extern int fGetLicInfo();/*************************************************/int main(int argc, char *argv[]){ int simdrv_fd; int ret_code; struct tm *ctime; time_t lt; FILE *lp;#ifdef WIN32_OTHER int ErrorLevel = (-1); /* error level for logging */ pid_t p; struct sigaction action; int i, j; ThisApp.Logfp=NULL; strcpy(ThisApp.LogFile,"simu112.log"); fOpenLog(); fLogInfo(">>> Start simu112 <<<\n"); if (!fGetLicInfo()) fQuitAll(0); if (argc == 1) { printf("\n STSC -- Simulate Test System Control Server version %s\n", LIC_VERSION); printf(" Copyright(c) by RELTEC Comm/Tech Ltd. Corp. 2000.07 \n"); printf("\n Licensed to: %s\n", LIC_USERNAME); printf(" Maximum drivers is: %d\n\n", LIC_DRIVERNUMBER); fQuitAll(0); } fCloseLog(); make_daemon(); fOpenLog(); sigemptyset(&action.sa_mask); action.sa_flags = 0; action.sa_handler = SIG_IGN; sigaction(SIGPIPE, &action, 0); action.sa_handler = fQuitAll; sigaction(SIGUSR1, &action, NULL); sigaction(SIGTERM, &action, NULL); sigaction(SIGKILL, &action, NULL); action.sa_flags = 0; action.sa_handler = SIG_IGN; sigaction(SIGPWR, &action, NULL); action.sa_handler = sig_child_died; sigaction(SIGCHLD, &action, NULL);#endif#ifdef WIN32_ENV int err; WORD wVersionRequired; WSADATA wsaData; wVersionRequired = MAKEWORD(1, 1); err = WSAStartup(wVersionRequired, &wsaData); if (err != 0) fQuitAll(0);#endif if (argc < 3) fQuitAll(0); ret_code = loadSetTable(); if (ret_code < 0) fQuitAll(0); simdrv_fd = startSIMsrv(argv[1], atoi(argv[2])); attachSocket(simdrv_fd, attachClient, IS_HIMSELF); schedProcess(); freeSetTable();#ifdef WIN32_ENV WSACleanup(); closesocket(simdrv_fd);#else close(simdrv_fd);#endif fQuitAll(0);}#ifdef WIN32_OTHER/*:**************************************************************************** Function Name: sig_child_died Narrative: Sets the Child_died flag when a child dies. Param: int sig - signal passed to routine Return: none ***************************************************************************** */static void sig_child_died(int sig){ sig = sig; /* resolve lint warnings */ Child_died = 1;} /* end sig_child_died() *//*:**************************************************************************** Function Name: handle_child_died Narrative: A the point of entry, all we know is that a child or children have died. We need to do the following: 1.) determine which child(ren) died by testing the status of every fd opened for a child. 2.) for each fd of a dead child, close the fd, update fd's in the appropriate tables, and set up a timer event to respawn the child at 1 minute intervals until the child is up and running. Param: none Return: none ***************************************************************************** */void make_daemon(){ int i; long open_max; pid_t p; /* turn off all file permission masking */ umask(0); /* move working directory to one which won't go away */ /* the purpose of this is to move to a place where any */ /* file system can be un-mounted with us stopping this */ /* for this project we are using files in the local dir */ /* so this is not implemented *//* chdir("/"); */ /* if we are to disassociate from parent and disconnect from */ /* terminal then do it */ /* This was not implemented in TRM */ /* we will use setsid to disassociate as this in the POSIX form */ /* setsid is similar to System V setpgrp and BSD 4.3 setpgrp */ if ((p = fork()) < 0) { fprintf(stderr, "\nDaemon cannot fork first child\n"); exit(1); } else { if (p > 0) exit(0); /* parent */ } setsid(); /* insure futrue opens will not allocate controlling terminals */ sigset(SIGHUP, SIG_IGN); if ((p = fork()) < 0) { fprintf(stderr, "\nDaemon cannot fork second child\n"); exit(1); } else { if (p > 0) exit(0); /* parent */ } /* close any and all file descriptor that may be open */ /* determine current maximum with POSIX routine */ open_max = sysconf(_SC_OPEN_MAX); /* We we do not get an answer choose something reasonable */ if (open_max <= 0) open_max = 64; for (i = 0; i < (int) open_max; i++) close(i); /* now open fd 0, 1, 2 in case somebody tries to write to it */ i = open("/dev/null", O_RDWR); dup(i); dup(i);/*#ifdef IGNORE_ALL_SIG */ /* now ignore all signals */ for (i = 0; i < NSIG; i++) signal(i, SIG_IGN);/*#endif */}#endifint fOpenLog(){ if(ThisApp.Logfp!=NULL) return 0; ThisApp.Logfp = fopen(ThisApp.LogFile, "a+"); if(ThisApp.Logfp==NULL) return 0; return 1;}/******************************************* 1=Close Ok. 0=Already Close******************************************/int fCloseLog() { if(ThisApp.Logfp==NULL) return 0; fclose(ThisApp.Logfp); ThisApp.Logfp=NULL; return 1;}int fLogInfo(char *fmt, ...){ char *str; va_list ptr; time_t t; time(&t); fBackupLog(); if (ThisApp.Logfp!= NULL) { va_start(ptr, fmt); fprintf(ThisApp.Logfp, "\n%s",ctime(&t)); vfprintf(ThisApp.Logfp, fmt, ptr); va_end(ptr); fflush(ThisApp.Logfp); return 1; } return 0;}int fBackupLog(void){ struct stat statbuf; char *src,*dst,*ptr,BackupFile[20]; FILE *stream; if(ThisApp.Logfp==NULL) return 0; fstat(fileno(ThisApp.Logfp), &statbuf); if(statbuf.st_size<1000000L) return 0; sprintf(BackupFile,"%s.bak",ThisApp.LogFile); remove(BackupFile); fCloseLog(); rename(ThisApp.LogFile,BackupFile); fOpenLog(); return 1;}void fQuitAll(int signo){ fLogInfo(">>> End simu112 <<<\n\n"); fCloseLog(); exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -