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