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