blob: ea70a8fcf4226d996c6ad577b51db9cb6112eb45 [file] [log] [blame]
Everton Marques871dbcf2009-08-11 15:43:05 -03001/*
2 PIM for Quagga
3 Copyright (C) 2008 Everton da Silva Marques
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 MA 02110-1301 USA
19
20 $QuaggaId: $Format:%an, %ai, %h$ $
21*/
22
23#include <zebra.h>
24
25#include "log.h"
Leonard Herve596470f2009-08-11 15:45:26 -030026#include "privs.h"
Everton Marques871dbcf2009-08-11 15:43:05 -030027#include "version.h"
28#include <getopt.h>
29#include "command.h"
30#include "thread.h"
31#include <signal.h>
32
33#include "memory.h"
Feng Lu126215c2015-05-22 11:39:58 +020034#include "vrf.h"
Everton Marques871dbcf2009-08-11 15:43:05 -030035#include "filter.h"
36#include "vty.h"
37#include "sigevent.h"
38#include "version.h"
Donald Sharp456cb632016-06-02 02:37:52 -040039#include "prefix.h"
40#include "plist.h"
Everton Marques871dbcf2009-08-11 15:43:05 -030041
42#include "pimd.h"
43#include "pim_version.h"
44#include "pim_signals.h"
45#include "pim_zebra.h"
46
47#ifdef PIM_ZCLIENT_DEBUG
48extern int zclient_debug;
49#endif
50
51extern struct host host;
Everton Marques871dbcf2009-08-11 15:43:05 -030052
53char config_default[] = SYSCONFDIR PIMD_DEFAULT_CONFIG;
54
55struct option longopts[] = {
56 { "daemon", no_argument, NULL, 'd'},
57 { "config_file", required_argument, NULL, 'f'},
58 { "pid_file", required_argument, NULL, 'i'},
59 { "vty_addr", required_argument, NULL, 'A'},
60 { "vty_port", required_argument, NULL, 'P'},
61 { "version", no_argument, NULL, 'v'},
62 { "debug_zclient", no_argument, NULL, 'Z'},
63 { "help", no_argument, NULL, 'h'},
64 { 0 }
65};
66
Leonard Herve596470f2009-08-11 15:45:26 -030067/* pimd privileges */
68zebra_capabilities_t _caps_p [] =
69{
70 ZCAP_NET_ADMIN,
71 ZCAP_SYS_ADMIN,
72 ZCAP_NET_RAW,
73};
74
75/* pimd privileges to run with */
76struct zebra_privs_t pimd_privs =
77{
78#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
79 .user = QUAGGA_USER,
80 .group = QUAGGA_GROUP,
81#endif
82#ifdef VTY_GROUP
83 .vty_group = VTY_GROUP,
84#endif
85 .caps_p = _caps_p,
86 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
87 .cap_num_i = 0
88};
89
Everton Marques871dbcf2009-08-11 15:43:05 -030090char* progname;
91const char *pid_file = PATH_PIMD_PID;
92
93static void usage(int status)
94{
95 if (status != 0)
96 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
97 else {
98 printf ("Usage : %s [OPTION...]\n\
99Daemon which manages PIM.\n\n\
100-d, --daemon Run in daemon mode\n\
101-f, --config_file Set configuration file name\n\
102-i, --pid_file Set process identifier file name\n\
Everton Marques1f298942014-08-21 15:47:28 -0300103-z, --socket Set path of zebra socket\n\
Everton Marques871dbcf2009-08-11 15:43:05 -0300104-A, --vty_addr Set vty's bind address\n\
105-P, --vty_port Set vty's port number\n\
106-v, --version Print program version\n\
107"
108
109#ifdef PIM_ZCLIENT_DEBUG
110"\
111-Z, --debug_zclient Enable zclient debugging\n\
112"
113#endif
114
115"\
116-h, --help Display this help and exit\n\
117\n\
118Report bugs to %s\n", progname, PIMD_BUG_ADDRESS);
119 }
120
121 exit (status);
122}
123
124
125int main(int argc, char** argv, char** envp) {
126 char *p;
127 char *vty_addr = NULL;
128 int vty_port = -1;
129 int daemon_mode = 0;
130 char *config_file = NULL;
Everton Marques1f298942014-08-21 15:47:28 -0300131 char *zebra_sock_path = NULL;
Everton Marques871dbcf2009-08-11 15:43:05 -0300132 struct thread thread;
133
134 umask(0027);
135
136 progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
137
138 zlog_default = openzlog(progname, ZLOG_PIM,
139 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
140
141 /* this while just reads the options */
142 while (1) {
143 int opt;
144
Everton Marques1f298942014-08-21 15:47:28 -0300145 opt = getopt_long (argc, argv, "df:i:z:A:P:vZh", longopts, 0);
Everton Marques871dbcf2009-08-11 15:43:05 -0300146
147 if (opt == EOF)
148 break;
149
150 switch (opt) {
151 case 0:
152 break;
153 case 'd':
154 daemon_mode = 1;
155 break;
156 case 'f':
157 config_file = optarg;
158 break;
159 case 'i':
160 pid_file = optarg;
161 break;
Everton Marques1f298942014-08-21 15:47:28 -0300162 case 'z':
163 zebra_sock_path = optarg;
164 break;
Everton Marques871dbcf2009-08-11 15:43:05 -0300165 case 'A':
166 vty_addr = optarg;
167 break;
168 case 'P':
169 vty_port = atoi (optarg);
170 break;
171 case 'v':
172 printf(PIMD_PROGNAME " version %s\n", PIMD_VERSION);
173 print_version(QUAGGA_PROGNAME);
174 exit (0);
175 break;
176#ifdef PIM_ZCLIENT_DEBUG
177 case 'Z':
178 zclient_debug = 1;
179 break;
180#endif
181 case 'h':
182 usage (0);
183 break;
184 default:
185 usage (1);
186 break;
187 }
188 }
189
190 master = thread_master_create();
191
Everton Marques871dbcf2009-08-11 15:43:05 -0300192 zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting",
193 QUAGGA_VERSION, PIMD_VERSION);
194
195 /*
196 * Initializations
197 */
Leonard Herve596470f2009-08-11 15:45:26 -0300198 zprivs_init (&pimd_privs);
Everton Marques871dbcf2009-08-11 15:43:05 -0300199 pim_signals_init();
200 cmd_init(1);
201 vty_init(master);
202 memory_init();
Feng Lu126215c2015-05-22 11:39:58 +0200203 vrf_init();
Everton Marques871dbcf2009-08-11 15:43:05 -0300204 access_list_init();
Donald Sharp456cb632016-06-02 02:37:52 -0400205 prefix_list_init ();
206 pim_route_map_init ();
Everton Marques871dbcf2009-08-11 15:43:05 -0300207 pim_init();
Everton Marques871dbcf2009-08-11 15:43:05 -0300208
209 /*
Donald Sharpe472b8a2015-09-08 15:19:55 -0400210 * Initialize zclient "update" and "lookup" sockets
Donald Sharpd6326892015-01-19 16:50:24 -0200211 */
Donald Sharp71252932015-09-24 09:25:19 -0400212 pim_zebra_init (master, zebra_sock_path);
Donald Sharpd6326892015-01-19 16:50:24 -0200213
Everton Marques871dbcf2009-08-11 15:43:05 -0300214 zlog_notice("Loading configuration - begin");
215
216 /* Get configuration file. */
217 vty_read_config(config_file, config_default);
218
219 /*
220 Starting from here zlog_* functions will log according configuration
221 */
222
223 zlog_notice("Loading configuration - end");
224
225 /* Change to the daemon program. */
226 if (daemon_mode) {
227 if (daemon(0, 0)) {
228 zlog_warn("failed to daemonize");
229 }
230 }
231
232 /* Process ID file creation. */
233 pid_output(pid_file);
234
235 /* Create pimd VTY socket */
236 if (vty_port < 0)
237 vty_port = PIMD_VTY_PORT;
238 vty_serv_sock(vty_addr, vty_port, PIM_VTYSH_PATH);
239
240 zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting, VTY interface at port TCP %d",
241 QUAGGA_VERSION, PIMD_VERSION, vty_port);
242
Everton Marques871dbcf2009-08-11 15:43:05 -0300243#ifdef PIM_DEBUG_BYDEFAULT
244 zlog_notice("PIM_DEBUG_BYDEFAULT: Enabling all debug commands");
245 PIM_DO_DEBUG_PIM_EVENTS;
246 PIM_DO_DEBUG_PIM_PACKETS;
247 PIM_DO_DEBUG_PIM_TRACE;
248 PIM_DO_DEBUG_IGMP_EVENTS;
249 PIM_DO_DEBUG_IGMP_PACKETS;
250 PIM_DO_DEBUG_IGMP_TRACE;
251 PIM_DO_DEBUG_ZEBRA;
252#endif
253
254#ifdef PIM_ZCLIENT_DEBUG
255 zlog_notice("PIM_ZCLIENT_DEBUG: zclient debugging is supported, mode is %s (see option -Z)",
256 zclient_debug ? "ON" : "OFF");
257#endif
258
259#ifdef PIM_CHECK_RECV_IFINDEX_SANITY
260 zlog_notice("PIM_CHECK_RECV_IFINDEX_SANITY: will match sock/recv ifindex");
261#ifdef PIM_REPORT_RECV_IFINDEX_MISMATCH
262 zlog_notice("PIM_REPORT_RECV_IFINDEX_MISMATCH: will report sock/recv ifindex mismatch");
263#endif
264#endif
265
Everton Marques871dbcf2009-08-11 15:43:05 -0300266#ifdef PIM_UNEXPECTED_KERNEL_UPCALL
267 zlog_notice("PIM_UNEXPECTED_KERNEL_UPCALL: report unexpected kernel upcall");
268#endif
269
Everton Marquesf9e05e52010-03-11 11:17:33 -0300270#ifdef HAVE_CLOCK_MONOTONIC
271 zlog_notice("HAVE_CLOCK_MONOTONIC");
272#else
273 zlog_notice("!HAVE_CLOCK_MONOTONIC");
274#endif
275
Everton Marques871dbcf2009-08-11 15:43:05 -0300276 while (thread_fetch(master, &thread))
277 thread_call(&thread);
278
279 zlog_err("%s %s: thread_fetch() returned NULL, exiting",
280 __FILE__, __PRETTY_FUNCTION__);
281
282 /* never reached */
283 return 0;
284}