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(); |
| 204 | sort_node(); |
| 205 | |
| 206 | /* |
| 207 | * reset zlog default, then will obey configuration file |
| 208 | */ |
| 209 | zlog_notice("Boot logging temporarily directed to stdout - end"); |
| 210 | #if 0 |
| 211 | /* this would disable logging to stdout, but config has not been |
| 212 | loaded yet to reconfig the logging output */ |
| 213 | zlog_default->maxlvl[ZLOG_DEST_STDOUT] = ZLOG_DISABLED; |
| 214 | #endif |
| 215 | |
| 216 | zlog_notice("Loading configuration - begin"); |
| 217 | |
| 218 | /* Get configuration file. */ |
| 219 | vty_read_config(config_file, config_default); |
| 220 | |
| 221 | /* |
| 222 | Starting from here zlog_* functions will log according configuration |
| 223 | */ |
| 224 | |
| 225 | zlog_notice("Loading configuration - end"); |
| 226 | |
| 227 | /* Change to the daemon program. */ |
| 228 | if (daemon_mode) { |
| 229 | if (daemon(0, 0)) { |
| 230 | zlog_warn("failed to daemonize"); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /* Process ID file creation. */ |
| 235 | pid_output(pid_file); |
| 236 | |
| 237 | /* Create pimd VTY socket */ |
| 238 | if (vty_port < 0) |
| 239 | vty_port = PIMD_VTY_PORT; |
| 240 | vty_serv_sock(vty_addr, vty_port, PIM_VTYSH_PATH); |
| 241 | |
| 242 | zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting, VTY interface at port TCP %d", |
| 243 | QUAGGA_VERSION, PIMD_VERSION, vty_port); |
| 244 | |
| 245 | #ifdef PIM_MOTD_VERSION |
| 246 | /* Tweak default MOTD to include pimd version */ |
| 247 | zlog_notice("PIM_MOTD_VERSION: adding pimd version to default MOTD"); |
| 248 | if (host.motd == default_motd) { |
| 249 | host.motd = |
| 250 | "\r\n\ |
| 251 | Hello, this is " QUAGGA_PROGNAME " " QUAGGA_VERSION " " PIMD_PROGNAME " " PIMD_VERSION_STR "\r\n\ |
| 252 | " QUAGGA_COPYRIGHT "\r\n\ |
| 253 | \r\n"; |
| 254 | } |
| 255 | #endif |
| 256 | |
| 257 | #ifdef PIM_DEBUG_BYDEFAULT |
| 258 | zlog_notice("PIM_DEBUG_BYDEFAULT: Enabling all debug commands"); |
| 259 | PIM_DO_DEBUG_PIM_EVENTS; |
| 260 | PIM_DO_DEBUG_PIM_PACKETS; |
| 261 | PIM_DO_DEBUG_PIM_TRACE; |
| 262 | PIM_DO_DEBUG_IGMP_EVENTS; |
| 263 | PIM_DO_DEBUG_IGMP_PACKETS; |
| 264 | PIM_DO_DEBUG_IGMP_TRACE; |
| 265 | PIM_DO_DEBUG_ZEBRA; |
| 266 | #endif |
| 267 | |
| 268 | #ifdef PIM_ZCLIENT_DEBUG |
| 269 | zlog_notice("PIM_ZCLIENT_DEBUG: zclient debugging is supported, mode is %s (see option -Z)", |
| 270 | zclient_debug ? "ON" : "OFF"); |
| 271 | #endif |
| 272 | |
| 273 | #ifdef PIM_CHECK_RECV_IFINDEX_SANITY |
| 274 | zlog_notice("PIM_CHECK_RECV_IFINDEX_SANITY: will match sock/recv ifindex"); |
| 275 | #ifdef PIM_REPORT_RECV_IFINDEX_MISMATCH |
| 276 | zlog_notice("PIM_REPORT_RECV_IFINDEX_MISMATCH: will report sock/recv ifindex mismatch"); |
| 277 | #endif |
| 278 | #endif |
| 279 | |
| 280 | #ifdef PIM_USE_QUAGGA_INET_CHECKSUM |
| 281 | zlog_notice("PIM_USE_QUAGGA_INET_CHECKSUM: using Quagga's builtin checksum"); |
| 282 | #endif |
| 283 | |
| 284 | #ifdef PIM_UNEXPECTED_KERNEL_UPCALL |
| 285 | zlog_notice("PIM_UNEXPECTED_KERNEL_UPCALL: report unexpected kernel upcall"); |
| 286 | #endif |
| 287 | |
| 288 | /* |
| 289 | Initialize zclient "update" and "lookup" sockets |
| 290 | */ |
| 291 | pim_zebra_init(); |
| 292 | |
| 293 | while (thread_fetch(master, &thread)) |
| 294 | thread_call(&thread); |
| 295 | |
| 296 | zlog_err("%s %s: thread_fetch() returned NULL, exiting", |
| 297 | __FILE__, __PRETTY_FUNCTION__); |
| 298 | |
| 299 | /* never reached */ |
| 300 | return 0; |
| 301 | } |