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