New upstream release and new maintainer
[quagga-debian.git] / ripd / rip_main.c
1 /* RIPd main routine.
2  * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
3  *
4  * This file is part of GNU Zebra.
5  *
6  * GNU Zebra is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2, or (at your option) any
9  * later version.
10  *
11  * GNU Zebra is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GNU Zebra; see the file COPYING.  If not, write to the Free
18  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19  * 02111-1307, USA.  
20  */
21
22 #include <zebra.h>
23
24 #include <lib/version.h>
25 #include "getopt.h"
26 #include "thread.h"
27 #include "command.h"
28 #include "memory.h"
29 #include "prefix.h"
30 #include "filter.h"
31 #include "keychain.h"
32 #include "log.h"
33 #include "privs.h"
34 #include "sigevent.h"
35 #include "zclient.h"
36 #include "vrf.h"
37
38 #include "ripd/ripd.h"
39
40 /* ripd options. */
41 static struct option longopts[] = 
42 {
43   { "daemon",      no_argument,       NULL, 'd'},
44   { "config_file", required_argument, NULL, 'f'},
45   { "pid_file",    required_argument, NULL, 'i'},
46   { "socket",      required_argument, NULL, 'z'},
47   { "help",        no_argument,       NULL, 'h'},
48   { "dryrun",      no_argument,       NULL, 'C'},
49   { "vty_addr",    required_argument, NULL, 'A'},
50   { "vty_port",    required_argument, NULL, 'P'},
51   { "retain",      no_argument,       NULL, 'r'},
52   { "user",        required_argument, NULL, 'u'},
53   { "group",       required_argument, NULL, 'g'},
54   { "version",     no_argument,       NULL, 'v'},
55   { 0 }
56 };
57
58 /* ripd privileges */
59 zebra_capabilities_t _caps_p [] = 
60 {
61   ZCAP_NET_RAW,
62   ZCAP_BIND
63 };
64
65 struct zebra_privs_t ripd_privs =
66 {
67 #if defined(QUAGGA_USER)
68   .user = QUAGGA_USER,
69 #endif
70 #if defined QUAGGA_GROUP
71   .group = QUAGGA_GROUP,
72 #endif
73 #ifdef VTY_GROUP
74   .vty_group = VTY_GROUP,
75 #endif
76   .caps_p = _caps_p,
77   .cap_num_p = 2,
78   .cap_num_i = 0
79 };
80
81 /* Configuration file and directory. */
82 char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
83 char *config_file = NULL;
84
85 /* ripd program name */
86
87 /* Route retain mode flag. */
88 int retain_mode = 0;
89
90 /* RIP VTY bind address. */
91 char *vty_addr = NULL;
92
93 /* RIP VTY connection port. */
94 int vty_port = RIP_VTY_PORT;
95
96 /* Master of threads. */
97 struct thread_master *master;
98
99 /* Process ID saved for use by init system */
100 const char *pid_file = PATH_RIPD_PID;
101
102 /* Help information display. */
103 static void
104 usage (char *progname, int status)
105 {
106   if (status != 0)
107     fprintf (stderr, "Try `%s --help' for more information.\n", progname);
108   else
109     {    
110       printf ("Usage : %s [OPTION...]\n\
111 Daemon which manages RIP version 1 and 2.\n\n\
112 -d, --daemon       Runs in daemon mode\n\
113 -f, --config_file  Set configuration file name\n\
114 -i, --pid_file     Set process identifier file name\n\
115 -z, --socket       Set path of zebra socket\n\
116 -A, --vty_addr     Set vty's bind address\n\
117 -P, --vty_port     Set vty's port number\n\
118 -C, --dryrun       Check configuration for validity and exit\n\
119 -r, --retain       When program terminates, retain added route by ripd.\n\
120 -u, --user         User to run as\n\
121 -g, --group        Group to run as\n\
122 -v, --version      Print program version\n\
123 -h, --help         Display this help and exit\n\
124 \n\
125 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
126     }
127
128   exit (status);
129 }
130
131 /* SIGHUP handler. */
132 static void 
133 sighup (void)
134 {
135   zlog_info ("SIGHUP received");
136   rip_clean ();
137   rip_reset ();
138   zlog_info ("ripd restarting!");
139
140   /* Reload config file. */
141   vty_read_config (config_file, config_default);
142
143   /* Create VTY's socket */
144   vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
145
146   /* Try to return to normal operation. */
147 }
148
149 /* SIGINT handler. */
150 static void
151 sigint (void)
152 {
153   zlog_notice ("Terminating on signal");
154
155   if (! retain_mode)
156     rip_clean ();
157
158   exit (0);
159 }
160
161 /* SIGUSR1 handler. */
162 static void
163 sigusr1 (void)
164 {
165   zlog_rotate (NULL);
166 }
167
168 static struct quagga_signal_t ripd_signals[] =
169 {
170   { 
171     .signal = SIGHUP,
172     .handler = &sighup,
173   },
174   { 
175     .signal = SIGUSR1,
176     .handler = &sigusr1,
177   },
178   {
179     .signal = SIGINT,
180     .handler = &sigint,
181   },
182   {
183     .signal = SIGTERM,
184     .handler = &sigint,
185   },
186 };  
187
188 /* Main routine of ripd. */
189 int
190 main (int argc, char **argv)
191 {
192   char *p;
193   int daemon_mode = 0;
194   int dryrun = 0;
195   char *progname;
196
197   /* Set umask before anything for security */
198   umask (0027);
199
200   /* Get program name. */
201   progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
202
203   /* First of all we need logging init. */
204   zlog_default = openzlog (progname, ZLOG_RIP,
205                            LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
206
207   /* Command line option parse. */
208   while (1) 
209     {
210       int opt;
211
212       opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:rvC", longopts, 0);
213     
214       if (opt == EOF)
215         break;
216
217       switch (opt) 
218         {
219         case 0:
220           break;
221         case 'd':
222           daemon_mode = 1;
223           break;
224         case 'f':
225           config_file = optarg;
226           break;
227         case 'A':
228           vty_addr = optarg;
229           break;
230         case 'i':
231           pid_file = optarg;
232           break;
233         case 'z':
234           zclient_serv_path_set (optarg);
235           break;
236         case 'P':
237           /* Deal with atoi() returning 0 on failure, and ripd not
238              listening on rip port... */
239           if (strcmp(optarg, "0") == 0) 
240             {
241               vty_port = 0;
242               break;
243             } 
244           vty_port = atoi (optarg);
245           if (vty_port <= 0 || vty_port > 0xffff)
246             vty_port = RIP_VTY_PORT;
247           break;
248         case 'r':
249           retain_mode = 1;
250           break;
251         case 'C':
252           dryrun = 1;
253           break;
254         case 'u':
255           ripd_privs.user = optarg;
256           break;
257         case 'g':
258           ripd_privs.group = optarg;
259           break;
260         case 'v':
261           print_version (progname);
262           exit (0);
263           break;
264         case 'h':
265           usage (progname, 0);
266           break;
267         default:
268           usage (progname, 1);
269           break;
270         }
271     }
272
273   /* Prepare master thread. */
274   master = thread_master_create ();
275
276   /* Library initialization. */
277   zprivs_init (&ripd_privs);
278   signal_init (master, array_size(ripd_signals), ripd_signals);
279   cmd_init (1);
280   vty_init (master);
281   memory_init ();
282   keychain_init ();
283   vrf_init ();
284
285   /* RIP related initialization. */
286   rip_init ();
287   rip_if_init ();
288   rip_zclient_init (master);
289   rip_peer_init ();
290
291   /* Get configuration file. */
292   vty_read_config (config_file, config_default);
293
294   /* Start execution only if not in dry-run mode */
295   if(dryrun)
296     return (0);
297   
298   /* Change to the daemon program. */
299   if (daemon_mode && daemon (0, 0) < 0)
300     {
301       zlog_err("RIPd daemon failed: %s", strerror(errno));
302       exit (1);
303     }
304
305   /* Pid file create. */
306   pid_output (pid_file);
307
308   /* Create VTY's socket */
309   vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
310
311   /* Print banner. */
312   zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
313
314   /* Execute each thread. */
315   thread_main (master);
316
317   /* Not reached. */
318   return (0);
319 }