New upstream version 1.2.3
[quagga-debian.git] / ospfd / ospf_main.c
1 /*
2  * OSPFd main routine.
3  *   Copyright (C) 1998, 99 Kunihiro Ishiguro, Toshiaki Takada
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 "thread.h"
28 #include "prefix.h"
29 #include "linklist.h"
30 #include "if.h"
31 #include "vector.h"
32 #include "vty.h"
33 #include "command.h"
34 #include "filter.h"
35 #include "plist.h"
36 #include "stream.h"
37 #include "log.h"
38 #include "memory.h"
39 #include "privs.h"
40 #include "sigevent.h"
41 #include "zclient.h"
42 #include "vrf.h"
43
44 #include "ospfd/ospfd.h"
45 #include "ospfd/ospf_interface.h"
46 #include "ospfd/ospf_asbr.h"
47 #include "ospfd/ospf_lsa.h"
48 #include "ospfd/ospf_lsdb.h"
49 #include "ospfd/ospf_neighbor.h"
50 #include "ospfd/ospf_dump.h"
51 #include "ospfd/ospf_zebra.h"
52 #include "ospfd/ospf_vty.h"
53
54 /* ospfd privileges */
55 zebra_capabilities_t _caps_p [] = 
56 {
57   ZCAP_NET_RAW,
58   ZCAP_BIND,
59   ZCAP_NET_ADMIN,
60 };
61
62 struct zebra_privs_t ospfd_privs =
63 {
64 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
65   .user = QUAGGA_USER,
66   .group = QUAGGA_GROUP,
67 #endif
68 #if defined(VTY_GROUP)
69   .vty_group = VTY_GROUP,
70 #endif
71   .caps_p = _caps_p,
72   .cap_num_p = array_size(_caps_p),
73   .cap_num_i = 0
74 };
75
76 /* Configuration filename and directory. */
77 char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG;
78
79 /* OSPFd options. */
80 struct option longopts[] = 
81 {
82   { "daemon",      no_argument,       NULL, 'd'},
83   { "config_file", required_argument, NULL, 'f'},
84   { "pid_file",    required_argument, NULL, 'i'},
85   { "socket",      required_argument, NULL, 'z'},
86   { "dryrun",      no_argument,       NULL, 'C'},
87   { "help",        no_argument,       NULL, 'h'},
88   { "vty_addr",    required_argument, NULL, 'A'},
89   { "vty_port",    required_argument, NULL, 'P'},
90   { "user",        required_argument, NULL, 'u'},
91   { "group",       required_argument, NULL, 'g'},
92   { "apiserver",   no_argument,       NULL, 'a'},
93   { "version",     no_argument,       NULL, 'v'},
94   { 0 }
95 };
96
97 /* OSPFd program name */
98
99 /* Master of threads. */
100 struct thread_master *master;
101
102 /* Process ID saved for use by init system */
103 const char *pid_file = PATH_OSPFD_PID;
104
105 #ifdef SUPPORT_OSPF_API
106 extern int ospf_apiserver_enable;
107 #endif /* SUPPORT_OSPF_API */
108
109 /* Help information display. */
110 static void __attribute__ ((noreturn))
111 usage (char *progname, int status)
112 {
113   if (status != 0)
114     fprintf (stderr, "Try `%s --help' for more information.\n", progname);
115   else
116     {    
117       printf ("Usage : %s [OPTION...]\n\
118 Daemon which manages OSPF.\n\n\
119 -d, --daemon       Runs in daemon mode\n\
120 -f, --config_file  Set configuration file name\n\
121 -i, --pid_file     Set process identifier file name\n\
122 -z, --socket       Set path of zebra socket\n\
123 -A, --vty_addr     Set vty's bind address\n\
124 -P, --vty_port     Set vty's port number\n\
125 -u, --user         User to run as\n\
126 -g, --group        Group to run as\n\
127 -a. --apiserver    Enable OSPF apiserver\n\
128 -v, --version      Print program version\n\
129 -C, --dryrun       Check configuration for validity and exit\n\
130 -h, --help         Display this help and exit\n\
131 \n\
132 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
133     }
134   exit (status);
135 }
136
137 /* SIGHUP handler. */
138 static void 
139 sighup (void)
140 {
141   zlog (NULL, LOG_INFO, "SIGHUP received");
142 }
143
144 /* SIGINT / SIGTERM handler. */
145 static void
146 sigint (void)
147 {
148   zlog_notice ("Terminating on signal");
149   ospf_terminate ();
150 }
151
152 /* SIGUSR1 handler. */
153 static void
154 sigusr1 (void)
155 {
156   zlog_rotate (NULL);
157 }
158
159 struct quagga_signal_t ospf_signals[] =
160 {
161   {
162     .signal = SIGHUP,
163     .handler = &sighup,
164   },
165   {
166     .signal = SIGUSR1,
167     .handler = &sigusr1,
168   },  
169   {
170     .signal = SIGINT,
171     .handler = &sigint,
172   },
173   {
174     .signal = SIGTERM,
175     .handler = &sigint,
176   },
177 };
178
179 /* OSPFd main routine. */
180 int
181 main (int argc, char **argv)
182 {
183   char *p;
184   char *vty_addr = NULL;
185   int vty_port = OSPF_VTY_PORT;
186   int daemon_mode = 0;
187   char *config_file = NULL;
188   char *progname;
189   int dryrun = 0;
190
191   /* Set umask before anything for security */
192   umask (0027);
193
194 #ifdef SUPPORT_OSPF_API
195   /* OSPF apiserver is disabled by default. */
196   ospf_apiserver_enable = 0;
197 #endif /* SUPPORT_OSPF_API */
198
199   /* get program name */
200   progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
201
202   while (1) 
203     {
204       int opt;
205
206       opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:avC", longopts, 0);
207     
208       if (opt == EOF)
209         break;
210
211       switch (opt) 
212         {
213         case 0:
214           break;
215         case 'd':
216           daemon_mode = 1;
217           break;
218         case 'f':
219           config_file = optarg;
220           break;
221         case 'A':
222           vty_addr = optarg;
223           break;
224         case 'i':
225           pid_file = optarg;
226           break;
227         case 'z':
228           zclient_serv_path_set (optarg);
229           break;
230         case 'P':
231           /* Deal with atoi() returning 0 on failure, and ospfd not
232              listening on ospfd port... */
233           if (strcmp(optarg, "0") == 0) 
234             {
235               vty_port = 0;
236               break;
237             } 
238           vty_port = atoi (optarg);
239           if (vty_port <= 0 || vty_port > 0xffff)
240             vty_port = OSPF_VTY_PORT;
241           break;
242         case 'u':
243           ospfd_privs.user = optarg;
244           break;
245         case 'g':
246           ospfd_privs.group = optarg;
247           break;
248 #ifdef SUPPORT_OSPF_API
249         case 'a':
250           ospf_apiserver_enable = 1;
251           break;
252 #endif /* SUPPORT_OSPF_API */
253         case 'v':
254           print_version (progname);
255           exit (0);
256           break;
257         case 'C':
258           dryrun = 1;
259           break;
260         case 'h':
261           usage (progname, 0);
262           break;
263         default:
264           usage (progname, 1);
265           break;
266         }
267     }
268
269   /* Invoked by a priviledged user? -- endo. */
270   if (geteuid () != 0)
271     {
272       errno = EPERM;
273       perror (progname);
274       exit (1);
275     }
276
277   zlog_default = openzlog (progname, ZLOG_OSPF,
278                            LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
279
280   /* OSPF master init. */
281   ospf_master_init ();
282
283   /* Initializations. */
284   master = om->master;
285
286   /* Library inits. */
287   zprivs_init (&ospfd_privs);
288   signal_init (master, array_size(ospf_signals), ospf_signals);
289   cmd_init (1);
290   debug_init ();
291   vty_init (master);
292   memory_init ();
293   vrf_init ();
294
295   access_list_init ();
296   prefix_list_init ();
297
298   /* OSPFd inits. */
299   ospf_if_init ();
300   ospf_zebra_init (master);
301
302   /* OSPF vty inits. */
303   ospf_vty_init ();
304   ospf_vty_show_init ();
305   ospf_vty_clear_init ();
306
307   ospf_route_map_init ();
308 #ifdef HAVE_SNMP
309   ospf_snmp_init ();
310 #endif /* HAVE_SNMP */
311   ospf_opaque_init ();
312   
313   /* Get configuration file. */
314   vty_read_config (config_file, config_default);
315
316   /* Start execution only if not in dry-run mode */
317   if (dryrun)
318     return(0);
319   
320   /* Change to the daemon program. */
321   if (daemon_mode && daemon (0, 0) < 0)
322     {
323       zlog_err("OSPFd daemon failed: %s", strerror(errno));
324       exit (1);
325     }
326
327   /* Process id file create. */
328   pid_output (pid_file);
329
330   /* Create VTY socket */
331   vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
332
333   /* Print banner. */
334   zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
335
336   thread_main (master);
337   
338   /* Not reached. */
339   return (0);
340 }
341