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; |
| 49 | extern const char *default_motd; |
| 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\ |
| 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; |
| 128 | struct thread thread; |
| 129 | |
| 130 | umask(0027); |
| 131 | |
| 132 | progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]); |
| 133 | |
| 134 | zlog_default = openzlog(progname, ZLOG_PIM, |
| 135 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 136 | |
| 137 | /* this while just reads the options */ |
| 138 | while (1) { |
| 139 | int opt; |
| 140 | |
| 141 | opt = getopt_long (argc, argv, "df:i:A:P:vZh", longopts, 0); |
| 142 | |
| 143 | if (opt == EOF) |
| 144 | break; |
| 145 | |
| 146 | switch (opt) { |
| 147 | case 0: |
| 148 | break; |
| 149 | case 'd': |
| 150 | daemon_mode = 1; |
| 151 | break; |
| 152 | case 'f': |
| 153 | config_file = optarg; |
| 154 | break; |
| 155 | case 'i': |
| 156 | pid_file = optarg; |
| 157 | break; |
| 158 | case 'A': |
| 159 | vty_addr = optarg; |
| 160 | break; |
| 161 | case 'P': |
| 162 | vty_port = atoi (optarg); |
| 163 | break; |
| 164 | case 'v': |
| 165 | printf(PIMD_PROGNAME " version %s\n", PIMD_VERSION); |
| 166 | print_version(QUAGGA_PROGNAME); |
| 167 | exit (0); |
| 168 | break; |
| 169 | #ifdef PIM_ZCLIENT_DEBUG |
| 170 | case 'Z': |
| 171 | zclient_debug = 1; |
| 172 | break; |
| 173 | #endif |
| 174 | case 'h': |
| 175 | usage (0); |
| 176 | break; |
| 177 | default: |
| 178 | usage (1); |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | master = thread_master_create(); |
| 184 | |
| 185 | /* |
| 186 | * Temporarily send zlog to stdout |
| 187 | */ |
| 188 | zlog_default->maxlvl[ZLOG_DEST_STDOUT] = zlog_default->default_lvl; |
| 189 | zlog_notice("Boot logging temporarily directed to stdout - begin"); |
| 190 | |
| 191 | zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting", |
| 192 | QUAGGA_VERSION, PIMD_VERSION); |
| 193 | |
| 194 | /* |
| 195 | * Initializations |
| 196 | */ |
Leonard Herve | 596470f | 2009-08-11 15:45:26 -0300 | [diff] [blame] | 197 | zprivs_init (&pimd_privs); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 198 | pim_signals_init(); |
| 199 | cmd_init(1); |
| 200 | vty_init(master); |
| 201 | memory_init(); |
| 202 | access_list_init(); |
| 203 | pim_init(); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * reset zlog default, then will obey configuration file |
| 207 | */ |
| 208 | zlog_notice("Boot logging temporarily directed to stdout - end"); |
| 209 | #if 0 |
| 210 | /* this would disable logging to stdout, but config has not been |
| 211 | loaded yet to reconfig the logging output */ |
| 212 | zlog_default->maxlvl[ZLOG_DEST_STDOUT] = ZLOG_DISABLED; |
| 213 | #endif |
| 214 | |
| 215 | zlog_notice("Loading configuration - begin"); |
| 216 | |
| 217 | /* Get configuration file. */ |
| 218 | vty_read_config(config_file, config_default); |
| 219 | |
| 220 | /* |
| 221 | Starting from here zlog_* functions will log according configuration |
| 222 | */ |
| 223 | |
| 224 | zlog_notice("Loading configuration - end"); |
| 225 | |
| 226 | /* Change to the daemon program. */ |
| 227 | if (daemon_mode) { |
| 228 | if (daemon(0, 0)) { |
| 229 | zlog_warn("failed to daemonize"); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* Process ID file creation. */ |
| 234 | pid_output(pid_file); |
| 235 | |
| 236 | /* Create pimd VTY socket */ |
| 237 | if (vty_port < 0) |
| 238 | vty_port = PIMD_VTY_PORT; |
| 239 | vty_serv_sock(vty_addr, vty_port, PIM_VTYSH_PATH); |
| 240 | |
| 241 | zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting, VTY interface at port TCP %d", |
| 242 | QUAGGA_VERSION, PIMD_VERSION, vty_port); |
| 243 | |
| 244 | #ifdef PIM_MOTD_VERSION |
| 245 | /* Tweak default MOTD to include pimd version */ |
| 246 | zlog_notice("PIM_MOTD_VERSION: adding pimd version to default MOTD"); |
| 247 | if (host.motd == default_motd) { |
| 248 | host.motd = |
| 249 | "\r\n\ |
| 250 | Hello, this is " QUAGGA_PROGNAME " " QUAGGA_VERSION " " PIMD_PROGNAME " " PIMD_VERSION_STR "\r\n\ |
| 251 | " QUAGGA_COPYRIGHT "\r\n\ |
| 252 | \r\n"; |
| 253 | } |
| 254 | #endif |
| 255 | |
| 256 | #ifdef PIM_DEBUG_BYDEFAULT |
| 257 | zlog_notice("PIM_DEBUG_BYDEFAULT: Enabling all debug commands"); |
| 258 | PIM_DO_DEBUG_PIM_EVENTS; |
| 259 | PIM_DO_DEBUG_PIM_PACKETS; |
| 260 | PIM_DO_DEBUG_PIM_TRACE; |
| 261 | PIM_DO_DEBUG_IGMP_EVENTS; |
| 262 | PIM_DO_DEBUG_IGMP_PACKETS; |
| 263 | PIM_DO_DEBUG_IGMP_TRACE; |
| 264 | PIM_DO_DEBUG_ZEBRA; |
| 265 | #endif |
| 266 | |
| 267 | #ifdef PIM_ZCLIENT_DEBUG |
| 268 | zlog_notice("PIM_ZCLIENT_DEBUG: zclient debugging is supported, mode is %s (see option -Z)", |
| 269 | zclient_debug ? "ON" : "OFF"); |
| 270 | #endif |
| 271 | |
| 272 | #ifdef PIM_CHECK_RECV_IFINDEX_SANITY |
| 273 | zlog_notice("PIM_CHECK_RECV_IFINDEX_SANITY: will match sock/recv ifindex"); |
| 274 | #ifdef PIM_REPORT_RECV_IFINDEX_MISMATCH |
| 275 | zlog_notice("PIM_REPORT_RECV_IFINDEX_MISMATCH: will report sock/recv ifindex mismatch"); |
| 276 | #endif |
| 277 | #endif |
| 278 | |
| 279 | #ifdef PIM_USE_QUAGGA_INET_CHECKSUM |
| 280 | zlog_notice("PIM_USE_QUAGGA_INET_CHECKSUM: using Quagga's builtin checksum"); |
| 281 | #endif |
| 282 | |
| 283 | #ifdef PIM_UNEXPECTED_KERNEL_UPCALL |
| 284 | zlog_notice("PIM_UNEXPECTED_KERNEL_UPCALL: report unexpected kernel upcall"); |
| 285 | #endif |
| 286 | |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame] | 287 | #ifdef PIM_FORCE_QUAGGA_REALTIME_STABILISED |
| 288 | zlog_notice("PIM_USE_QUAGGA_GETTIME: using Quagga's quagga_gettime"()); |
| 289 | #endif |
| 290 | |
| 291 | #ifdef PIM_GETTIME_USE_GETTIMEOFDAY |
| 292 | zlog_notice("PIM_GETTIME_USE_GETTIMEOFDAY: work-around improper monotonic clock"); |
| 293 | #endif |
| 294 | |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 295 | #ifdef HAVE_CLOCK_MONOTONIC |
| 296 | zlog_notice("HAVE_CLOCK_MONOTONIC"); |
| 297 | #else |
| 298 | zlog_notice("!HAVE_CLOCK_MONOTONIC"); |
| 299 | #endif |
| 300 | |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 301 | /* |
| 302 | Initialize zclient "update" and "lookup" sockets |
| 303 | */ |
| 304 | pim_zebra_init(); |
| 305 | |
| 306 | while (thread_fetch(master, &thread)) |
| 307 | thread_call(&thread); |
| 308 | |
| 309 | zlog_err("%s %s: thread_fetch() returned NULL, exiting", |
| 310 | __FILE__, __PRETTY_FUNCTION__); |
| 311 | |
| 312 | /* never reached */ |
| 313 | return 0; |
| 314 | } |