]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_main.c
aa3db3d5b2e4328be79932788c679a323322a830
[mirror_frr.git] / pimd / pim_main.c
1 /*
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"
26 #include "privs.h"
27 #include "version.h"
28 #include <getopt.h>
29 #include "command.h"
30 #include "thread.h"
31 #include <signal.h>
32
33 #include "memory.h"
34 #include "vrf.h"
35 #include "memory_vty.h"
36 #include "filter.h"
37 #include "vty.h"
38 #include "sigevent.h"
39 #include "version.h"
40 #include "prefix.h"
41 #include "plist.h"
42 #include "vrf.h"
43
44 #include "pimd.h"
45 #include "pim_version.h"
46 #include "pim_signals.h"
47 #include "pim_zebra.h"
48
49 #ifdef PIM_ZCLIENT_DEBUG
50 extern int zclient_debug;
51 #endif
52
53 extern struct host host;
54
55 char config_default[] = SYSCONFDIR PIMD_DEFAULT_CONFIG;
56
57 struct option longopts[] = {
58 { "daemon", no_argument, NULL, 'd'},
59 { "config_file", required_argument, NULL, 'f'},
60 { "pid_file", required_argument, NULL, 'i'},
61 { "vty_addr", required_argument, NULL, 'A'},
62 { "vty_port", required_argument, NULL, 'P'},
63 { "version", no_argument, NULL, 'v'},
64 { "debug_zclient", no_argument, NULL, 'Z'},
65 { "help", no_argument, NULL, 'h'},
66 { 0 }
67 };
68
69 /* pimd privileges */
70 zebra_capabilities_t _caps_p [] =
71 {
72 ZCAP_NET_ADMIN,
73 ZCAP_SYS_ADMIN,
74 ZCAP_NET_RAW,
75 };
76
77 /* pimd privileges to run with */
78 struct zebra_privs_t pimd_privs =
79 {
80 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
81 .user = QUAGGA_USER,
82 .group = QUAGGA_GROUP,
83 #endif
84 #ifdef VTY_GROUP
85 .vty_group = VTY_GROUP,
86 #endif
87 .caps_p = _caps_p,
88 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
89 .cap_num_i = 0
90 };
91
92 char* progname;
93 const char *pid_file = PATH_PIMD_PID;
94
95 static void usage(int status)
96 {
97 if (status != 0)
98 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
99 else {
100 printf ("Usage : %s [OPTION...]\n\
101 Daemon which manages PIM.\n\n\
102 -d, --daemon Run in daemon mode\n\
103 -f, --config_file Set configuration file name\n\
104 -i, --pid_file Set process identifier file name\n\
105 -z, --socket Set path of zebra socket\n\
106 -A, --vty_addr Set vty's bind address\n\
107 -P, --vty_port Set vty's port number\n\
108 -v, --version Print program version\n\
109 "
110
111 #ifdef PIM_ZCLIENT_DEBUG
112 "\
113 -Z, --debug_zclient Enable zclient debugging\n\
114 "
115 #endif
116
117 "\
118 -h, --help Display this help and exit\n\
119 \n\
120 Report bugs to %s\n", progname, PIMD_BUG_ADDRESS);
121 }
122
123 exit (status);
124 }
125
126
127 int main(int argc, char** argv, char** envp) {
128 char *p;
129 char *vty_addr = NULL;
130 int vty_port = -1;
131 int daemon_mode = 0;
132 char *config_file = NULL;
133 char *zebra_sock_path = NULL;
134 struct thread thread;
135
136 umask(0027);
137
138 progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]);
139
140 zlog_default = openzlog(progname, ZLOG_PIM, 0,
141 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
142 zprivs_init (&pimd_privs);
143 #if defined(HAVE_CUMULUS)
144 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
145 #endif
146
147 /* this while just reads the options */
148 while (1) {
149 int opt;
150
151 opt = getopt_long (argc, argv, "df:i:z:A:P:vZh", longopts, 0);
152
153 if (opt == EOF)
154 break;
155
156 switch (opt) {
157 case 0:
158 break;
159 case 'd':
160 daemon_mode = 1;
161 break;
162 case 'f':
163 config_file = optarg;
164 break;
165 case 'i':
166 pid_file = optarg;
167 break;
168 case 'z':
169 zebra_sock_path = optarg;
170 break;
171 case 'A':
172 vty_addr = optarg;
173 break;
174 case 'P':
175 vty_port = atoi (optarg);
176 break;
177 case 'v':
178 printf(PIMD_PROGNAME " version %s\n", PIMD_VERSION);
179 print_version(QUAGGA_PROGNAME);
180 exit (0);
181 break;
182 #ifdef PIM_ZCLIENT_DEBUG
183 case 'Z':
184 zclient_debug = 1;
185 break;
186 #endif
187 case 'h':
188 usage (0);
189 break;
190 default:
191 usage (1);
192 break;
193 }
194 }
195
196 master = thread_master_create();
197
198 zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting",
199 QUAGGA_VERSION, PIMD_VERSION);
200
201 /*
202 * Initializations
203 */
204 pim_signals_init();
205 cmd_init(1);
206 vty_init(master);
207 memory_init();
208 vrf_init ();
209 access_list_init();
210 prefix_list_init ();
211 pim_route_map_init ();
212 pim_init();
213
214 /*
215 * Initialize zclient "update" and "lookup" sockets
216 */
217 pim_zebra_init(zebra_sock_path);
218
219 zlog_notice("Loading configuration - begin");
220
221 /* Get configuration file. */
222 vty_read_config(config_file, config_default);
223
224 /*
225 Starting from here zlog_* functions will log according configuration
226 */
227
228 zlog_notice("Loading configuration - end");
229
230 /* Change to the daemon program. */
231 if (daemon_mode) {
232 if (daemon(0, 0)) {
233 zlog_warn("failed to daemonize");
234 }
235 }
236
237 /* Process ID file creation. */
238 pid_output(pid_file);
239
240 /* Create pimd VTY socket */
241 if (vty_port < 0)
242 vty_port = PIMD_VTY_PORT;
243 vty_serv_sock(vty_addr, vty_port, PIM_VTYSH_PATH);
244
245 zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting, VTY interface at port TCP %d",
246 QUAGGA_VERSION, PIMD_VERSION, vty_port);
247
248 #ifdef PIM_DEBUG_BYDEFAULT
249 zlog_notice("PIM_DEBUG_BYDEFAULT: Enabling all debug commands");
250 PIM_DO_DEBUG_PIM_EVENTS;
251 PIM_DO_DEBUG_PIM_PACKETS;
252 PIM_DO_DEBUG_PIM_TRACE;
253 PIM_DO_DEBUG_IGMP_EVENTS;
254 PIM_DO_DEBUG_IGMP_PACKETS;
255 PIM_DO_DEBUG_IGMP_TRACE;
256 PIM_DO_DEBUG_ZEBRA;
257 #endif
258
259 #ifdef PIM_ZCLIENT_DEBUG
260 zlog_notice("PIM_ZCLIENT_DEBUG: zclient debugging is supported, mode is %s (see option -Z)",
261 zclient_debug ? "ON" : "OFF");
262 #endif
263
264 #ifdef PIM_CHECK_RECV_IFINDEX_SANITY
265 zlog_notice("PIM_CHECK_RECV_IFINDEX_SANITY: will match sock/recv ifindex");
266 #ifdef PIM_REPORT_RECV_IFINDEX_MISMATCH
267 zlog_notice("PIM_REPORT_RECV_IFINDEX_MISMATCH: will report sock/recv ifindex mismatch");
268 #endif
269 #endif
270
271 #ifdef PIM_UNEXPECTED_KERNEL_UPCALL
272 zlog_notice("PIM_UNEXPECTED_KERNEL_UPCALL: report unexpected kernel upcall");
273 #endif
274
275 #ifdef HAVE_CLOCK_MONOTONIC
276 zlog_notice("HAVE_CLOCK_MONOTONIC");
277 #else
278 zlog_notice("!HAVE_CLOCK_MONOTONIC");
279 #endif
280
281 while (thread_fetch(master, &thread))
282 thread_call(&thread);
283
284 zlog_err("%s %s: thread_fetch() returned NULL, exiting",
285 __FILE__, __PRETTY_FUNCTION__);
286
287 /* never reached */
288 return 0;
289 }