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