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();
36 char *configfile, *routefile;
53 routefile = DEFAULTROUTE;
54 configfile = DEFAULTCONFIG;
55 stat_interval = -1; /* print at exit only */
57 /* let's use syslogd for all this junk... */
58 openlog("ipip", LOG_PID | LOG_NOWAIT, IPIP_SYSLOG);
60 (void)sprintf(progname,"ipip[%d]: ",getpid());
62 /* log our pid so that we can get kicked during log rotation */
63 if(!(pid_file = fopen(PIDLOG,"w"))) {
64 syslog(LOG_CRIT, "could not log pid to file %s",PIDLOG);
67 fprintf(pid_file,"%d\n",getpid());
70 /* Process arguments */
72 for(i=1;((i<argc)&&(oops==0));i++){
73 if(strcmp(argv[i],"-c")==0){
75 if(i<argc)configfile = argv[i];
77 } else if(strcmp(argv[i],"-r")==0){
79 if(i<argc)routefile = argv[i];
81 } else if(strcmp(argv[i],"-s")==0){
83 if(i<argc)stat_interval = atoi(argv[i]) * 60;
85 } else if(strcmp(argv[i],"-d")==0){
87 } else if(strcmp(argv[i],"-t")==0){
89 } else if(strcmp(argv[i],"-v")==0){
91 } else if(strcmp(argv[i],"-np")==0){
93 } else if(strcmp(argv[i],"-nts")==0){
95 } else if(strcmp(argv[i],"-help")==0){
101 (void)fprintf(stderr,"%sUsage: %s [-help] [-c <configfile>] [-r <routefile>] [-s <minutes>] [-np] [-nts] [-t] [-d] [-v]\n",
103 (void)printf("%sexit (syntax error)\n",progname);
108 (void)printf("%s Options are:\n",progname);
109 (void)printf("%s -help Print this summary of options\n",
111 (void)printf("%s -v Print just the version number and exit\n",
113 (void)printf("%s -c file Use the supplied filename as the config file\n",
115 (void)printf("%s -r file Use the supplied filename as the route file\n",
117 (void)printf("%s -s n Report statistics every n minutes\n",
119 (void)printf("%s -np Don't print the ipip[1234]: prefix\n",
121 (void)printf("%s -nts Don't include a timestamp in the statistics\n",
123 (void)printf("%s -t Turn on tracing of incoming packets\n",
125 (void)printf("%s -d Print out some debugging information\n",
131 if(strtol("0xffffffff",NULL,0)!=0xffffffff){
132 (void)fprintf(stderr,"%sAssertion failed: this program cannot properly read\n",progname);
133 (void)fprintf(stderr,"%sunsigned numbers from the config files. See the\n",progname);
134 (void)fprintf(stderr,"%ssource code for details. ABORTED.\n",progname);
135 (void)printf("%sexit (program error)\n",progname);
140 if(setjmp(term_env)!=0){
141 send_stats(1); /* force print of statistics */
143 syslog(LOG_CRIT, "exit (killed)");
147 (void)signal(SIGTERM, terminator);
148 (void)signal(SIGINT, terminator);
150 /* Say hello to the world */
151 syslog(LOG_DEBUG,"version %s Copyright 1991, Michael Westerhof.",VERS);
153 if(version_only!=0)exit(0);
155 /* Read the configuration file */
157 if(read_config(configfile)<0){
158 syslog(LOG_CRIT, "exit (config file error)");
162 /* Here's where we restart when we get a SIGHUP signal */
164 if(sigsetjmp(restart_env,1)==0){
166 if(setjmp(restart_env,1)==0){
168 (void)signal(SIGHUP, hupper);
171 /* make sure to close and reopen the log channel if we get a SIGHUP,
172 since it might mean the log file was just rotated! */
174 openlog("ipip", LOG_PID | LOG_NOWAIT, IPIP_SYSLOG);
176 /* Read the route tables */
178 if(read_routes(routefile)<0){
179 syslog(LOG_CRIT, "exit (routes file error)");
183 /* flush the stdout buffer before we start to run */
184 /* (void)fflush(stdout); */
190 syslog(LOG_CRIT, "exit (I/O error)");
194 /* Should never return this way... but stuff happens... */
196 syslog(LOG_CRIT, "exit (no error code?)");
203 syslog(LOG_DEBUG, "SIGHUP received");
205 siglongjmp(restart_env, 1);
207 longjmp(restart_env, 1);
214 longjmp(term_env, 1);