3 * Copyright (C) 1998, 99 Kunihiro Ishiguro, Toshiaki Takada
5 * This file is part of GNU Zebra.
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
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.
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
25 #include <lib/version.h>
39 #include "memory_vty.h"
46 #include "ospfd/ospfd.h"
47 #include "ospfd/ospf_interface.h"
48 #include "ospfd/ospf_asbr.h"
49 #include "ospfd/ospf_lsa.h"
50 #include "ospfd/ospf_lsdb.h"
51 #include "ospfd/ospf_neighbor.h"
52 #include "ospfd/ospf_dump.h"
53 #include "ospfd/ospf_zebra.h"
54 #include "ospfd/ospf_vty.h"
55 #include "ospfd/ospf_bfd.h"
57 /* ospfd privileges */
58 zebra_capabilities_t _caps_p
[] =
65 struct zebra_privs_t ospfd_privs
=
67 #if defined(FRR_USER) && defined(FRR_GROUP)
71 #if defined(VTY_GROUP)
72 .vty_group
= VTY_GROUP
,
75 .cap_num_p
= array_size(_caps_p
),
79 /* Configuration filename and directory. */
80 char config_default
[100];
83 #define OPTION_VTYSOCK 1000
84 struct option longopts
[] =
86 { "daemon", no_argument
, NULL
, 'd'},
87 { "instance", required_argument
, NULL
, 'n'},
88 { "config_file", required_argument
, NULL
, 'f'},
89 { "pid_file", required_argument
, NULL
, 'i'},
90 { "socket", required_argument
, NULL
, 'z'},
91 { "dryrun", no_argument
, NULL
, 'C'},
92 { "help", no_argument
, NULL
, 'h'},
93 { "vty_addr", required_argument
, NULL
, 'A'},
94 { "vty_port", required_argument
, NULL
, 'P'},
95 { "vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
96 { "user", required_argument
, NULL
, 'u'},
97 { "group", required_argument
, NULL
, 'g'},
98 { "apiserver", no_argument
, NULL
, 'a'},
99 { "version", no_argument
, NULL
, 'v'},
103 /* OSPFd program name */
105 /* VTY Socket prefix */
106 char vty_sock_path
[MAXPATHLEN
] = OSPF_VTYSH_PATH
;
108 /* Master of threads. */
109 struct thread_master
*master
;
111 /* Process ID saved for use by init system */
114 #ifdef SUPPORT_OSPF_API
115 extern int ospf_apiserver_enable
;
116 #endif /* SUPPORT_OSPF_API */
118 /* Help information display. */
119 static void __attribute__ ((noreturn
))
120 usage (char *progname
, int status
)
123 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
126 printf ("Usage : %s [OPTION...]\n\
127 Daemon which manages OSPF.\n\n\
128 -d, --daemon Runs in daemon mode\n\
129 -n, --instance Set the instance id\n\
130 -f, --config_file Set configuration file name\n\
131 -i, --pid_file Set process identifier file name\n\
132 -z, --socket Set path of zebra socket\n\
133 -A, --vty_addr Set vty's bind address\n\
134 -P, --vty_port Set vty's port number\n\
135 --vty_socket Override vty socket path\n\
136 -u, --user User to run as\n\
137 -g, --group Group to run as\n\
138 -a. --apiserver Enable OSPF apiserver\n\
139 -v, --version Print program version\n\
140 -C, --dryrun Check configuration for validity and exit\n\
141 -h, --help Display this help and exit\n\
143 Report bugs to %s\n", progname
, FRR_BUG_ADDRESS
);
148 /* SIGHUP handler. */
152 zlog (NULL
, LOG_INFO
, "SIGHUP received");
155 /* SIGINT / SIGTERM handler. */
159 zlog_notice ("Terminating on signal");
163 /* SIGUSR1 handler. */
170 struct quagga_signal_t ospf_signals
[] =
190 /* OSPFd main routine. */
192 main (int argc
, char **argv
)
195 char *vty_addr
= NULL
;
196 int vty_port
= OSPF_VTY_PORT
;
199 char *config_file
= NULL
;
201 u_short instance
= 0;
202 struct thread thread
;
205 /* Set umask before anything for security */
208 #ifdef SUPPORT_OSPF_API
209 /* OSPF apiserver is disabled by default. */
210 ospf_apiserver_enable
= 0;
211 #endif /* SUPPORT_OSPF_API */
213 strcpy(pid_file
, PATH_OSPFD_PID
);
215 /* get program name */
216 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
222 opt
= getopt_long (argc
, argv
, "df:i:n:z:hA:P:u:g:avC", longopts
, 0);
230 instance
= atoi(optarg
);
240 config_file
= optarg
;
246 strcpy(pid_file
,optarg
);
249 zclient_serv_path_set (optarg
);
252 /* Deal with atoi() returning 0 on failure, and ospfd not
253 listening on ospfd port... */
254 if (strcmp(optarg
, "0") == 0)
259 vty_port
= atoi (optarg
);
260 if (vty_port
<= 0 || vty_port
> 0xffff)
261 vty_port
= OSPF_VTY_PORT
;
264 set_socket_path(vty_sock_path
, OSPF_VTYSH_PATH
, optarg
, sizeof (vty_sock_path
));
267 ospfd_privs
.user
= optarg
;
270 ospfd_privs
.group
= optarg
;
272 #ifdef SUPPORT_OSPF_API
274 ospf_apiserver_enable
= 1;
276 #endif /* SUPPORT_OSPF_API */
278 print_version (progname
);
293 /* Invoked by a priviledged user? -- endo. */
301 zlog_default
= openzlog (progname
, ZLOG_OSPF
, instance
,
302 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
303 zprivs_init (&ospfd_privs
);
304 #if defined(HAVE_CUMULUS)
305 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
308 /* OSPF master init. */
311 /* Initializations. */
315 signal_init (master
, array_size(ospf_signals
), ospf_signals
);
327 ospf_zebra_init(master
, instance
);
329 /* OSPF vty inits. */
331 ospf_vty_show_init ();
332 ospf_vty_clear_init ();
337 ospf_route_map_init ();
340 #endif /* HAVE_SNMP */
343 /* Need to initialize the default ospf structure, so the interface mode
344 commands can be duly processed if they are received before 'router ospf',
345 when quagga(ospfd) is restarted */
346 if (!ospf_get_instance(instance
))
348 zlog_err("OSPF instance init failed: %s", strerror(errno
));
352 /* Get configuration file. */
354 sprintf(config_default
, "%sospfd-%d.conf", SYSCONFDIR
, instance
);
356 sprintf(config_default
, "%s%s", SYSCONFDIR
, OSPF_DEFAULT_CONFIG
);
357 vty_read_config (config_file
, config_default
);
359 /* Start execution only if not in dry-run mode */
363 /* Change to the daemon program. */
364 if (daemon_mode
&& daemon (0, 0) < 0)
366 zlog_err("OSPFd daemon failed: %s", strerror(errno
));
370 /* Create PID file */
373 char pidfile_temp
[100];
375 /* Override the single file with file including instance
376 number in case of multi-instance */
377 if (strrchr(pid_file
, '/') != NULL
)
378 /* cut of pid_file at last / char * to get directory */
379 *strrchr(pid_file
, '/') = '\0';
381 /* pid_file contains no directory - should never happen, but deal with it anyway */
382 /* throw-away all pid_file and assume it's only the filename */
385 snprintf(pidfile_temp
, sizeof(pidfile_temp
), "%s/ospfd-%d.pid", pid_file
, instance
);
386 strlcpy(pid_file
, pidfile_temp
, sizeof(pid_file
));
388 /* Process id file create. */
389 pid_output (pid_file
);
391 /* Create VTY socket */
394 /* Multi-Instance. Use only path section of vty_sock_path with new file incl instance */
395 if (strrchr(vty_sock_path
, '/') != NULL
)
397 /* cut of pid_file at last / char * to get directory */
398 *strrchr(vty_sock_path
, '/') = '\0';
402 /* pid_file contains no directory - should never happen, but deal with it anyway */
403 /* throw-away all pid_file and assume it's only the filename */
404 vty_sock_path
[0] = '\0';
406 snprintf(vty_path
, sizeof(vty_path
), "%s/ospfd-%d.vty", vty_sock_path
, instance
);
410 strlcpy(vty_path
, vty_sock_path
, sizeof(vty_path
));
412 vty_serv_sock (vty_addr
, vty_port
, vty_path
);
415 zlog_notice ("OSPFd %s starting: vty@%d, %s", FRR_VERSION
, vty_port
, vty_path
);
417 /* Fetch next active thread. */
418 while (thread_fetch (master
, &thread
))
419 thread_call (&thread
);