1 /* main.c main entrypoint
3 * $Id: main.c,v 1.5 1995/03/19 17:21:06 bdale Exp $
5 * Copyright 1991, Michael Westerhof, Sun Microsystems, Inc.
6 * This software may be freely used, distributed, or modified, providing
7 * this header is not removed.
12 #define __USE_BSD_SIGNAL
19 #include <sys/types.h>
28 sigjmp_buf restart_env;
32 static void hupper(int sig);
34 static void terminator();
41 char *configfile, *routefile;
57 routefile = DEFAULTROUTE;
58 configfile = DEFAULTCONFIG;
59 stat_interval = -1; /* print at exit only */
61 /* let's use syslogd for all this junk... */
62 openlog("ipip", LOG_PID | LOG_NOWAIT, IPIP_SYSLOG);
64 (void)sprintf(progname,"ipip[%d]: ",getpid());
66 /* log our pid so that we can get kicked during log rotation */
67 if(!(pid_file = fopen(PIDLOG,"w"))) {
68 syslog(LOG_CRIT, "could not log pid to file %s",PIDLOG);
71 fprintf(pid_file,"%d\n",getpid());
74 /* Process arguments */
76 for(i=1;((i<argc)&&(oops==0));i++){
77 if(strcmp(argv[i],"-c")==0){
79 if(i<argc)configfile = argv[i];
81 } else if(strcmp(argv[i],"-r")==0){
83 if(i<argc)routefile = argv[i];
85 } else if(strcmp(argv[i],"-s")==0){
87 if(i<argc)stat_interval = atoi(argv[i]) * 60;
89 } else if(strcmp(argv[i],"-d")==0){
91 } else if(strcmp(argv[i],"-t")==0){
93 } else if(strcmp(argv[i],"-v")==0){
95 } else if(strcmp(argv[i],"-np")==0){
97 } else if(strcmp(argv[i],"-nts")==0){
99 } else if(strcmp(argv[i],"-help")==0){
105 (void)fprintf(stderr,"%sUsage: %s [-help] [-c <configfile>] [-r <routefile>] [-s <minutes>] [-np] [-nts] [-t] [-d] [-v]\n",
107 (void)printf("%sexit (syntax error)\n",progname);
112 (void)printf("%s Options are:\n",progname);
113 (void)printf("%s -help Print this summary of options\n",
115 (void)printf("%s -v Print just the version number and exit\n",
117 (void)printf("%s -c file Use the supplied filename as the config file\n",
119 (void)printf("%s -r file Use the supplied filename as the route file\n",
121 (void)printf("%s -s n Report statistics every n minutes\n",
123 (void)printf("%s -np Don't print the ipip[1234]: prefix\n",
125 (void)printf("%s -nts Don't include a timestamp in the statistics\n",
127 (void)printf("%s -t Turn on tracing of incoming packets\n",
129 (void)printf("%s -d Print out some debugging information\n",
135 if(strtol("0xffffffff",NULL,0)!=0xffffffff){
136 (void)fprintf(stderr,"%sAssertion failed: this program cannot properly read\n",progname);
137 (void)fprintf(stderr,"%sunsigned numbers from the config files. See the\n",progname);
138 (void)fprintf(stderr,"%ssource code for details. ABORTED.\n",progname);
139 (void)printf("%sexit (program error)\n",progname);
144 if(setjmp(term_env)!=0){
145 send_stats(1); /* force print of statistics */
146 syslog(LOG_CRIT, "exit (killed)");
150 (void)signal(SIGTERM, terminator);
151 (void)signal(SIGINT, terminator);
153 /* Say hello to the world */
154 syslog(LOG_DEBUG,"version %s Copyright 1991, Michael Westerhof.",VERS);
156 if(version_only!=0)exit(0);
158 /* Read the configuration file */
160 if(read_config(configfile)<0){
161 syslog(LOG_CRIT, "exit (config file error)");
165 /* Here's where we restart when we get a SIGHUP signal */
167 if(sigsetjmp(restart_env,1)==0){
169 if(setjmp(restart_env,1)==0){
171 (void)signal(SIGHUP, hupper);
174 /* make sure to close and reopen the log channel if we get a SIGHUP,
175 since it might mean the log file was just rotated! */
177 openlog("ipip", LOG_PID | LOG_NOWAIT, IPIP_SYSLOG);
179 /* Read the route tables */
181 if(read_routes(routefile)<0){
182 syslog(LOG_CRIT, "exit (routes file error)");
186 /* flush the stdout buffer before we start to run */
187 /* (void)fflush(stdout); */
193 syslog(LOG_CRIT, "exit (I/O error)");
197 /* Should never return this way... but stuff happens... */
199 syslog(LOG_CRIT, "exit (no error code?)");
206 syslog(LOG_DEBUG, "SIGHUP received");
208 siglongjmp(restart_env, 1);
210 longjmp(restart_env, 1);
217 longjmp(term_env, 1);