Imported Debian version 1.1.6
[debian/ipip] / main.c
1 /* main.c     main entrypoint
2  *
3  * $Id: main.c,v 1.5 1995/03/19 17:21:06 bdale Exp $
4  *
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.
8  *
9  */
10
11 #ifdef LINUX
12 #define __USE_BSD_SIGNAL
13 #endif
14 #include "ipip.h"
15 #include "version.h"
16 #include <stdio.h>
17 #include <time.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <signal.h>
22 #include <setjmp.h>
23 #include <syslog.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26
27 #ifdef LINUX
28 sigjmp_buf restart_env;
29 #else
30 jmp_buf restart_env;
31 #endif
32 static void hupper(int sig);
33 jmp_buf term_env;
34 static void terminator();
35
36 char *configfile, *routefile;
37 int version_only;
38
39 void
40 main(argc, argv)
41         int argc;
42         char *argv[];
43 {
44         int n, i, oops, help;
45         time_t now;
46         FILE *pid_file;
47
48         debugd = 0;
49         debugt = 0;
50         help = 0;
51         no_timestamp = 0;
52         version_only = 0;
53         routefile = DEFAULTROUTE;
54         configfile = DEFAULTCONFIG;
55         stat_interval = -1;             /* print at exit only */
56
57         /* let's use syslogd for all this junk... */
58         openlog("ipip", LOG_PID | LOG_NOWAIT, IPIP_SYSLOG);
59
60         (void)sprintf(progname,"ipip[%d]: ",getpid());
61
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);
65                 exit(1);
66         }
67         fprintf(pid_file,"%d\n",getpid());
68         fclose(pid_file);
69
70         /* Process arguments */
71         oops = 0;
72         for(i=1;((i<argc)&&(oops==0));i++){
73                 if(strcmp(argv[i],"-c")==0){            
74                         i++;
75                         if(i<argc)configfile = argv[i];
76                         else oops++;
77                 } else if(strcmp(argv[i],"-r")==0){
78                         i++;
79                         if(i<argc)routefile = argv[i];
80                         else oops++;
81                 } else if(strcmp(argv[i],"-s")==0){
82                         i++;
83                         if(i<argc)stat_interval = atoi(argv[i]) * 60;
84                         else oops++;
85                 } else if(strcmp(argv[i],"-d")==0){
86                         debugd++;
87                 } else if(strcmp(argv[i],"-t")==0){
88                         debugt++;
89                 } else if(strcmp(argv[i],"-v")==0){
90                         version_only++;
91                 } else if(strcmp(argv[i],"-np")==0){
92                         progname[0] = '\0';
93                 } else if(strcmp(argv[i],"-nts")==0){
94                         no_timestamp++;
95                 } else if(strcmp(argv[i],"-help")==0){
96                         help++;
97                 } else oops++;
98         }
99
100         if(oops){
101                 (void)fprintf(stderr,"%sUsage: %s [-help] [-c <configfile>] [-r <routefile>] [-s <minutes>] [-np] [-nts] [-t] [-d] [-v]\n",
102                         progname,argv[0]);
103                 (void)printf("%sexit (syntax error)\n",progname);
104                 exit(1);
105         }
106
107         if(help){
108                 (void)printf("%s Options are:\n",progname);
109                 (void)printf("%s  -help      Print this summary of options\n",
110                         progname);
111                 (void)printf("%s  -v         Print just the version number and exit\n",
112                         progname);
113                 (void)printf("%s  -c file    Use the supplied filename as the config file\n",
114                         progname);
115                 (void)printf("%s  -r file    Use the supplied filename as the route file\n",
116                         progname);
117                 (void)printf("%s  -s n       Report statistics every n minutes\n",
118                         progname);
119                 (void)printf("%s  -np        Don't print the ipip[1234]: prefix\n",
120                         progname);
121                 (void)printf("%s  -nts       Don't include a timestamp in the statistics\n",
122                         progname);
123                 (void)printf("%s  -t         Turn on tracing of incoming packets\n",
124                         progname);
125                 (void)printf("%s  -d         Print out some debugging information\n",
126                         progname);
127                 exit(0);
128         }
129
130 #ifdef NO_STRTOUL
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);
136                 exit(2);
137         }
138 #endif
139
140         if(setjmp(term_env)!=0){
141                 send_stats(1);          /* force print of statistics */
142                 now = time(NULL);
143                 syslog(LOG_CRIT, "exit (killed)");
144                 exit(0);
145         }
146
147         (void)signal(SIGTERM, terminator);
148         (void)signal(SIGINT, terminator);
149
150         /* Say hello to the world */
151         syslog(LOG_DEBUG,"version %s Copyright 1991, Michael Westerhof.",VERS);
152
153         if(version_only!=0)exit(0);
154
155         /* Read the configuration file */
156
157         if(read_config(configfile)<0){
158                 syslog(LOG_CRIT, "exit (config file error)");
159                 exit(1);
160         }
161
162         /* Here's where we restart when we get a SIGHUP signal */
163 #ifdef LINUX
164         if(sigsetjmp(restart_env,1)==0){
165 #else
166         if(setjmp(restart_env,1)==0){
167 #endif
168                 (void)signal(SIGHUP, hupper);
169         }
170
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!  */
173         closelog();
174         openlog("ipip", LOG_PID | LOG_NOWAIT, IPIP_SYSLOG);
175
176         /* Read the route tables */
177
178         if(read_routes(routefile)<0){
179                 syslog(LOG_CRIT, "exit (routes file error)");
180                 exit(1);
181         }
182
183         /* flush the stdout buffer before we start to run */
184 /*      (void)fflush(stdout);   */
185
186         /* And run it! */
187
188         n = run_it();
189         if(n<0){
190                 syslog(LOG_CRIT, "exit (I/O error)");
191                 exit(1);
192         }
193
194         /* Should never return this way... but stuff happens... */
195         send_stats(1);
196         syslog(LOG_CRIT, "exit (no error code?)");
197         exit(0);
198 }
199
200 static void
201 hupper(int sig)
202 {
203         syslog(LOG_DEBUG, "SIGHUP received");
204 #ifdef LINUX
205         siglongjmp(restart_env, 1);
206 #else
207         longjmp(restart_env, 1);
208 #endif
209 }
210
211 static void
212 terminator()
213 {
214         longjmp(term_env, 1);
215 }