File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / quagga / pimd / pim_main.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Nov 2 10:09:11 2016 UTC (7 years, 8 months ago) by misho
Branches: quagga, MAIN
CVS tags: v1_0_20160315, HEAD
quagga 1.0.20160315

    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 "privs.h" 
   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 "vrf.h"
   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;
   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: 
   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: 
   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: -z, --socket         Set path of zebra socket\n\
  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;
  129:   char *zebra_sock_path = NULL;
  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:             
  143:     opt = getopt_long (argc, argv, "df:i:z:A:P:vZh", longopts, 0);
  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;
  160:     case 'z':
  161:       zebra_sock_path = optarg;
  162:       break;
  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:   zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting",
  191: 	      QUAGGA_VERSION, PIMD_VERSION);
  192: 
  193:   /* 
  194:    * Initializations
  195:    */
  196:   zprivs_init (&pimd_privs);
  197:   pim_signals_init();
  198:   cmd_init(1);
  199:   vty_init(master);
  200:   memory_init();
  201:   vrf_init();
  202:   access_list_init();
  203:   pim_init();
  204: 
  205:   /*
  206:    * Initialize zclient "update" and "lookup" sockets
  207:    */
  208:   pim_zebra_init (master, zebra_sock_path);
  209: 
  210:   zlog_notice("Loading configuration - begin");
  211: 
  212:   /* Get configuration file. */
  213:   vty_read_config(config_file, config_default);
  214: 
  215:   /*
  216:     Starting from here zlog_* functions will log according configuration
  217:    */
  218: 
  219:   zlog_notice("Loading configuration - end");
  220: 
  221:   /* Change to the daemon program. */
  222:   if (daemon_mode) {
  223:     if (daemon(0, 0)) {
  224:       zlog_warn("failed to daemonize");
  225:     }
  226:   }
  227: 
  228:   /* Process ID file creation. */
  229:   pid_output(pid_file);
  230: 
  231:   /* Create pimd VTY socket */
  232:   if (vty_port < 0)
  233:     vty_port = PIMD_VTY_PORT;
  234:   vty_serv_sock(vty_addr, vty_port, PIM_VTYSH_PATH);
  235: 
  236:   zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting, VTY interface at port TCP %d",
  237: 	      QUAGGA_VERSION, PIMD_VERSION, vty_port);
  238: 
  239: #ifdef PIM_DEBUG_BYDEFAULT
  240:   zlog_notice("PIM_DEBUG_BYDEFAULT: Enabling all debug commands");
  241:   PIM_DO_DEBUG_PIM_EVENTS;
  242:   PIM_DO_DEBUG_PIM_PACKETS;
  243:   PIM_DO_DEBUG_PIM_TRACE;
  244:   PIM_DO_DEBUG_IGMP_EVENTS;
  245:   PIM_DO_DEBUG_IGMP_PACKETS;
  246:   PIM_DO_DEBUG_IGMP_TRACE;
  247:   PIM_DO_DEBUG_ZEBRA;
  248: #endif
  249: 
  250: #ifdef PIM_ZCLIENT_DEBUG
  251:   zlog_notice("PIM_ZCLIENT_DEBUG: zclient debugging is supported, mode is %s (see option -Z)",
  252: 	      zclient_debug ? "ON" : "OFF");
  253: #endif
  254: 
  255: #ifdef PIM_CHECK_RECV_IFINDEX_SANITY
  256:   zlog_notice("PIM_CHECK_RECV_IFINDEX_SANITY: will match sock/recv ifindex");
  257: #ifdef PIM_REPORT_RECV_IFINDEX_MISMATCH
  258:   zlog_notice("PIM_REPORT_RECV_IFINDEX_MISMATCH: will report sock/recv ifindex mismatch");
  259: #endif
  260: #endif
  261: 
  262: #ifdef PIM_UNEXPECTED_KERNEL_UPCALL
  263:   zlog_notice("PIM_UNEXPECTED_KERNEL_UPCALL: report unexpected kernel upcall");
  264: #endif
  265: 
  266: #ifdef HAVE_CLOCK_MONOTONIC
  267:   zlog_notice("HAVE_CLOCK_MONOTONIC");
  268: #else
  269:   zlog_notice("!HAVE_CLOCK_MONOTONIC");
  270: #endif
  271: 
  272:   while (thread_fetch(master, &thread))
  273:     thread_call(&thread);
  274: 
  275:   zlog_err("%s %s: thread_fetch() returned NULL, exiting",
  276: 	   __FILE__, __PRETTY_FUNCTION__);
  277: 
  278:   /* never reached */
  279:   return 0;
  280: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>