New upstream version 1.2.3
[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   char *config_file = NULL;
240   char *vty_addr = NULL;
241   int dryrun = 0;
242
243   /* Get the programname without the preceding path. */
244   progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
245
246   zlog_default = openzlog (progname, ZLOG_ISIS,
247                            LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
248
249   /* for reload */
250   _argc = argc;
251   _argv = argv;
252   _envp = envp;
253   getcwd (_cwd, sizeof (_cwd));
254   if (*argv[0] == '.')
255     snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
256   else
257     snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
258
259   /* Command line argument treatment. */
260   while (1)
261     {
262       opt = getopt_long (argc, argv, "df:i:z:hA:p:P:u:g:vC", longopts, 0);
263
264       if (opt == EOF)
265         break;
266
267       switch (opt)
268         {
269         case 0:
270           break;
271         case 'd':
272           daemon_mode = 1;
273           break;
274         case 'f':
275           config_file = optarg;
276           break;
277         case 'i':
278           pid_file = optarg;
279           break;
280         case 'z':
281           zclient_serv_path_set (optarg);
282           break;
283         case 'A':
284           vty_addr = optarg;
285           break;
286         case 'P':
287           /* Deal with atoi() returning 0 on failure, and isisd not
288              listening on isisd port... */
289           if (strcmp (optarg, "0") == 0)
290             {
291               vty_port = 0;
292               break;
293             }
294           vty_port = atoi (optarg);
295           vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
296           break;
297         case 'u':
298           isisd_privs.user = optarg;
299           break;
300         case 'g':
301           isisd_privs.group = optarg;
302           break;
303         case 'v':
304           printf ("ISISd version %s\n", ISISD_VERSION);
305           printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
306                   " Ofer Wald and Hannes Gredler\n");
307           print_version ("Zebra");
308           exit (0);
309           break;
310         case 'C':
311           dryrun = 1;
312           break;
313         case 'h':
314           usage (0);
315           break;
316         default:
317           usage (1);
318           break;
319         }
320     }
321
322   /* thread master */
323   master = thread_master_create ();
324
325   /* random seed from time */
326   srandom (time (NULL));
327
328   /*
329    *  initializations
330    */
331   zprivs_init (&isisd_privs);
332   signal_init (master, array_size (isisd_signals), isisd_signals);
333   cmd_init (1);
334   vty_init (master);
335   memory_init ();
336   access_list_init();
337   vrf_init ();
338   prefix_list_init();
339   isis_init ();
340   isis_circuit_init ();
341   isis_spf_cmds_init ();
342   isis_redist_init ();
343   isis_route_map_init();
344   isis_mpls_te_init();
345
346   /* create the global 'isis' instance */
347   isis_new (1);
348
349   isis_zebra_init (master);
350
351   /* parse config file */
352   /* this is needed three times! because we have interfaces before the areas */
353   vty_read_config (config_file, config_default);
354
355   /* Start execution only if not in dry-run mode */
356   if (dryrun)
357     return(0);
358   
359   /* demonize */
360   if (daemon_mode && daemon (0, 0) < 0)
361     {
362       zlog_err("IS-IS daemon failed: %s", strerror(errno));
363       exit (1);
364     }
365
366   /* Process ID file creation. */
367   if (pid_file[0] != '\0')
368     pid_output (pid_file);
369
370   /* Make isis vty socket. */
371   vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
372
373   /* Print banner. */
374   zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
375
376   /* Start finite state machine. */
377   thread_main (master);
378
379   /* Not reached. */
380   exit (0);
381 }