Import Debian changes 1.2.2-1
[quagga-debian.git] / isisd / isis_main.c
1 /*
2  * IS-IS Rout(e)ing protocol - isis_main.c
3  *
4  * Copyright (C) 2001,2002   Sampo Saaristo
5  *                           Tampere University of Technology      
6  *                           Institute of Communications Engineering
7  *
8  * This program is free software; you can redistribute it and/or modify it 
9  * under the terms of the GNU General Public Licenseas published by the Free 
10  * Software Foundation; either version 2 of the License, or (at your option) 
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,but WITHOUT 
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16  * more details.
17
18  * You should have received a copy of the GNU General Public License along 
19  * with this program; if not, write to the Free Software Foundation, Inc., 
20  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #include <zebra.h>
24
25 #include "getopt.h"
26 #include "thread.h"
27 #include "log.h"
28 #include <lib/version.h>
29 #include "command.h"
30 #include "vty.h"
31 #include "memory.h"
32 #include "stream.h"
33 #include "if.h"
34 #include "privs.h"
35 #include "sigevent.h"
36 #include "filter.h"
37 #include "plist.h"
38 #include "zclient.h"
39 #include "vrf.h"
40
41 #include "isisd/dict.h"
42 #include "include-netbsd/iso.h"
43 #include "isisd/isis_constants.h"
44 #include "isisd/isis_common.h"
45 #include "isisd/isis_flags.h"
46 #include "isisd/isis_circuit.h"
47 #include "isisd/isisd.h"
48 #include "isisd/isis_dynhn.h"
49 #include "isisd/isis_spf.h"
50 #include "isisd/isis_route.h"
51 #include "isisd/isis_routemap.h"
52 #include "isisd/isis_zebra.h"
53 #include "isisd/isis_tlv.h"
54 #include "isisd/isis_te.h"
55
56 /* Default configuration file name */
57 #define ISISD_DEFAULT_CONFIG "isisd.conf"
58 /* Default vty port */
59 #define ISISD_VTY_PORT       2608
60
61 /* isisd privileges */
62 zebra_capabilities_t _caps_p[] = {
63   ZCAP_NET_RAW,
64   ZCAP_BIND
65 };
66
67 struct zebra_privs_t isisd_privs = {
68 #if defined(QUAGGA_USER)
69   .user = QUAGGA_USER,
70 #endif
71 #if defined QUAGGA_GROUP
72   .group = QUAGGA_GROUP,
73 #endif
74 #ifdef VTY_GROUP
75   .vty_group = VTY_GROUP,
76 #endif
77   .caps_p = _caps_p,
78   .cap_num_p = sizeof (_caps_p) / sizeof (*_caps_p),
79   .cap_num_i = 0
80 };
81
82 /* isisd options */
83 struct option longopts[] = {
84   {"daemon",      no_argument,       NULL, 'd'},
85   {"config_file", required_argument, NULL, 'f'},
86   {"pid_file",    required_argument, NULL, 'i'},
87   {"socket",      required_argument, NULL, 'z'},
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   {"version",     no_argument,       NULL, 'v'},
93   {"dryrun",      no_argument,       NULL, 'C'},
94   {"help",        no_argument,       NULL, 'h'},
95   {0}
96 };
97
98 /* Configuration file and directory. */
99 char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
100 char *config_file = NULL;
101
102 /* isisd program name. */
103 char *progname;
104
105 int daemon_mode = 0;
106
107 /* Master of threads. */
108 struct thread_master *master;
109
110 /* Process ID saved for use by init system */
111 const char *pid_file = PATH_ISISD_PID;
112
113 /* for reload */
114 char _cwd[MAXPATHLEN];
115 char _progpath[MAXPATHLEN];
116 int _argc;
117 char **_argv;
118 char **_envp;
119
120 /*
121  * Prototypes.
122  */
123 void reload(void);
124 void sighup(void);
125 void sigint(void);
126 void sigterm(void);
127 void sigusr1(void);
128
129
130 /* Help information display. */
131 static void
132 usage (int status)
133 {
134   if (status != 0)
135     fprintf (stderr, "Try `%s --help' for more information.\n", progname);
136   else
137     {
138       printf ("Usage : %s [OPTION...]\n\n\
139 Daemon which manages IS-IS routing\n\n\
140 -d, --daemon       Runs in daemon mode\n\
141 -f, --config_file  Set configuration file name\n\
142 -i, --pid_file     Set process identifier file name\n\
143 -z, --socket       Set path of zebra socket\n\
144 -A, --vty_addr     Set vty's bind address\n\
145 -P, --vty_port     Set vty's port number\n\
146 -u, --user         User to run as\n\
147 -g, --group        Group to run as\n\
148 -v, --version      Print program version\n\
149 -C, --dryrun       Check configuration for validity and exit\n\
150 -h, --help         Display this help and exit\n\
151 \n\
152 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
153     }
154
155   exit (status);
156 }
157
158
159 void
160 reload ()
161 {
162   zlog_debug ("Reload");
163   /* FIXME: Clean up func call here */
164   vty_reset ();
165   (void) isisd_privs.change (ZPRIVS_RAISE);
166   execve (_progpath, _argv, _envp);
167   zlog_err ("Reload failed: cannot exec %s: %s", _progpath,
168       safe_strerror (errno));
169 }
170
171 static void
172 terminate (int i)
173 {
174   exit (i);
175 }
176
177 /*
178  * Signal handlers
179  */
180
181 void
182 sighup (void)
183 {
184   zlog_debug ("SIGHUP received");
185   reload ();
186
187   return;
188 }
189
190 void
191 sigint (void)
192 {
193   zlog_notice ("Terminating on signal SIGINT");
194   terminate (0);
195 }
196
197 void
198 sigterm (void)
199 {
200   zlog_notice ("Terminating on signal SIGTERM");
201   terminate (0);
202 }
203
204 void
205 sigusr1 (void)
206 {
207   zlog_debug ("SIGUSR1 received");
208   zlog_rotate (NULL);
209 }
210
211 struct quagga_signal_t isisd_signals[] =
212 {
213   {
214    .signal = SIGHUP,
215    .handler = &sighup,
216    },
217   {
218    .signal = SIGUSR1,
219    .handler = &sigusr1,
220    },
221   {
222    .signal = SIGINT,
223    .handler = &sigint,
224    },
225   {
226    .signal = SIGTERM,
227    .handler = &sigterm,
228    },
229 };
230
231 /*
232  * Main routine of isisd. Parse arguments and handle IS-IS state machine.
233  */
234 int
235 main (int argc, char **argv, char **envp)
236 {
237   char *p;
238   int opt, vty_port = ISISD_VTY_PORT;
239   struct thread thread;
240   char *config_file = NULL;
241   char *vty_addr = NULL;
242   int dryrun = 0;
243
244   /* Get the programname without the preceding path. */
245   progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
246
247   zlog_default = openzlog (progname, ZLOG_ISIS,
248                            LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
249
250   /* for reload */
251   _argc = argc;
252   _argv = argv;
253   _envp = envp;
254   getcwd (_cwd, sizeof (_cwd));
255   if (*argv[0] == '.')
256     snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
257   else
258     snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
259
260   /* Command line argument treatment. */
261   while (1)
262     {
263       opt = getopt_long (argc, argv, "df:i:z:hA:p:P:u:g:vC", longopts, 0);
264
265       if (opt == EOF)
266         break;
267
268       switch (opt)
269         {
270         case 0:
271           break;
272         case 'd':
273           daemon_mode = 1;
274           break;
275         case 'f':
276           config_file = optarg;
277           break;
278         case 'i':
279           pid_file = optarg;
280           break;
281         case 'z':
282           zclient_serv_path_set (optarg);
283           break;
284         case 'A':
285           vty_addr = optarg;
286           break;
287         case 'P':
288           /* Deal with atoi() returning 0 on failure, and isisd not
289              listening on isisd port... */
290           if (strcmp (optarg, "0") == 0)
291             {
292               vty_port = 0;
293               break;
294             }
295           vty_port = atoi (optarg);
296           vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
297           break;
298         case 'u':
299           isisd_privs.user = optarg;
300           break;
301         case 'g':
302           isisd_privs.group = optarg;
303           break;
304         case 'v':
305           printf ("ISISd version %s\n", ISISD_VERSION);
306           printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
307                   " Ofer Wald and Hannes Gredler\n");
308           print_version ("Zebra");
309           exit (0);
310           break;
311         case 'C':
312           dryrun = 1;
313           break;
314         case 'h':
315           usage (0);
316           break;
317         default:
318           usage (1);
319           break;
320         }
321     }
322
323   /* thread master */
324   master = thread_master_create ();
325
326   /* random seed from time */
327   srandom (time (NULL));
328
329   /*
330    *  initializations
331    */
332   zprivs_init (&isisd_privs);
333   signal_init (master, array_size (isisd_signals), isisd_signals);
334   cmd_init (1);
335   vty_init (master);
336   memory_init ();
337   access_list_init();
338   vrf_init ();
339   prefix_list_init();
340   isis_init ();
341   isis_circuit_init ();
342   isis_spf_cmds_init ();
343   isis_redist_init ();
344   isis_route_map_init();
345   isis_mpls_te_init();
346
347   /* create the global 'isis' instance */
348   isis_new (1);
349
350   isis_zebra_init (master);
351
352   /* parse config file */
353   /* this is needed three times! because we have interfaces before the areas */
354   vty_read_config (config_file, config_default);
355
356   /* Start execution only if not in dry-run mode */
357   if (dryrun)
358     return(0);
359   
360   /* demonize */
361   if (daemon_mode && daemon (0, 0) < 0)
362     {
363       zlog_err("IS-IS daemon failed: %s", strerror(errno));
364       exit (1);
365     }
366
367   /* Process ID file creation. */
368   if (pid_file[0] != '\0')
369     pid_output (pid_file);
370
371   /* Make isis vty socket. */
372   vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
373
374   /* Print banner. */
375   zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
376
377   /* Start finite state machine. */
378   while (thread_fetch (master, &thread))
379     thread_call (&thread);
380
381   /* Not reached. */
382   exit (0);
383 }