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