File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / quagga / ripngd / ripng_main.c
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 17:26:11 2012 UTC (12 years, 4 months ago) by misho
CVS tags: MAIN, HEAD
Initial revision

    1: /*
    2:  * RIPngd main routine.
    3:  * Copyright (C) 1998, 1999 Kunihiro Ishiguro
    4:  *
    5:  * This file is part of GNU Zebra.
    6:  *
    7:  * GNU Zebra is free software; you can redistribute it and/or modify it
    8:  * under the terms of the GNU General Public License as published by the
    9:  * Free Software Foundation; either version 2, or (at your option) any
   10:  * later version.
   11:  *
   12:  * GNU Zebra is distributed in the hope that it will be useful, but
   13:  * WITHOUT ANY WARRANTY; without even the implied warranty of
   14:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15:  * General Public License for more details.
   16:  *
   17:  * You should have received a copy of the GNU General Public License
   18:  * along with GNU Zebra; see the file COPYING.  If not, write to the Free
   19:  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   20:  * 02111-1307, USA.  
   21:  */
   22: 
   23: #include <zebra.h>
   24: 
   25: #include <lib/version.h>
   26: #include "getopt.h"
   27: #include "vector.h"
   28: #include "vty.h"
   29: #include "command.h"
   30: #include "memory.h"
   31: #include "thread.h"
   32: #include "log.h"
   33: #include "prefix.h"
   34: #include "if.h"
   35: #include "privs.h"
   36: #include "sigevent.h"
   37: 
   38: #include "ripngd/ripngd.h"
   39: 
   40: /* Configuration filename and directory. */
   41: char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG;
   42: char *config_file = NULL;
   43: 
   44: /* RIPngd options. */
   45: struct option longopts[] = 
   46: {
   47:   { "daemon",      no_argument,       NULL, 'd'},
   48:   { "config_file", required_argument, NULL, 'f'},
   49:   { "pid_file",    required_argument, NULL, 'i'},
   50:   { "dryrun",      no_argument,       NULL, 'C'},
   51:   { "help",        no_argument,       NULL, 'h'},
   52:   { "vty_addr",    required_argument, NULL, 'A'},
   53:   { "vty_port",    required_argument, NULL, 'P'},
   54:   { "retain",      no_argument,       NULL, 'r'},
   55:   { "user",        required_argument, NULL, 'u'},
   56:   { "group",       required_argument, NULL, 'g'},
   57:   { "version",     no_argument,       NULL, 'v'},
   58:   { 0 }
   59: };
   60: 
   61: /* ripngd privileges */
   62: zebra_capabilities_t _caps_p [] = 
   63: {
   64:   ZCAP_NET_RAW,
   65:   ZCAP_BIND
   66: };
   67: 
   68: struct zebra_privs_t ripngd_privs =
   69: {
   70: #if defined(QUAGGA_USER)
   71:   .user = QUAGGA_USER,
   72: #endif
   73: #if defined QUAGGA_GROUP
   74:   .group = QUAGGA_GROUP,
   75: #endif
   76: #ifdef VTY_GROUP
   77:   .vty_group = VTY_GROUP,
   78: #endif
   79:   .caps_p = _caps_p,
   80:   .cap_num_p = 2,
   81:   .cap_num_i = 0
   82: };
   83: 
   84: 
   85: /* RIPngd program name */
   86: 
   87: /* Route retain mode flag. */
   88: int retain_mode = 0;
   89: 
   90: /* RIPng VTY bind address. */
   91: char *vty_addr = NULL;
   92: 
   93: /* RIPng VTY connection port. */
   94: int vty_port = RIPNG_VTY_PORT;
   95: 
   96: /* Master of threads. */
   97: struct thread_master *master;
   98: 
   99: /* Process ID saved for use by init system */
  100: const char *pid_file = PATH_RIPNGD_PID;
  101: 
  102: /* Help information display. */
  103: static void
  104: usage (char *progname, int status)
  105: {
  106:   if (status != 0)
  107:     fprintf (stderr, "Try `%s --help' for more information.\n", progname);
  108:   else
  109:     {    
  110:       printf ("Usage : %s [OPTION...]\n\
  111: Daemon which manages RIPng.\n\n\
  112: -d, --daemon       Runs in daemon mode\n\
  113: -f, --config_file  Set configuration file name\n\
  114: -i, --pid_file     Set process identifier file name\n\
  115: -A, --vty_addr     Set vty's bind address\n\
  116: -P, --vty_port     Set vty's port number\n\
  117: -r, --retain       When program terminates, retain added route by ripngd.\n\
  118: -u, --user         User to run as\n\
  119: -g, --group        Group to run as\n\
  120: -v, --version      Print program version\n\
  121: -C, --dryrun       Check configuration for validity and exit\n\
  122: -h, --help         Display this help and exit\n\
  123: \n\
  124: Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
  125:     }
  126:   exit (status);
  127: }
  128: 
  129: /* SIGHUP handler. */
  130: static void 
  131: sighup (void)
  132: {
  133:   zlog_info ("SIGHUP received");
  134:   ripng_clean ();
  135:   ripng_reset ();
  136: 
  137:   /* Reload config file. */
  138:   vty_read_config (config_file, config_default);
  139:   /* Create VTY's socket */
  140:   vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
  141: 
  142:   /* Try to return to normal operation. */
  143: }
  144: 
  145: /* SIGINT handler. */
  146: static void
  147: sigint (void)
  148: {
  149:   zlog_notice ("Terminating on signal");
  150: 
  151:   if (! retain_mode)
  152:     ripng_clean ();
  153: 
  154:   exit (0);
  155: }
  156: 
  157: /* SIGUSR1 handler. */
  158: static void
  159: sigusr1 (void)
  160: {
  161:   zlog_rotate (NULL);
  162: }
  163: 
  164: struct quagga_signal_t ripng_signals[] =
  165: {
  166:   { 
  167:     .signal = SIGHUP, 
  168:     .handler = &sighup,
  169:   },
  170:   {
  171:     .signal = SIGUSR1,
  172:     .handler = &sigusr1,
  173:   },
  174:   {
  175:     .signal = SIGINT,
  176:     .handler = &sigint,
  177:   },
  178:   {
  179:     .signal = SIGTERM,
  180:     .handler = &sigint,
  181:   },
  182: };
  183: 
  184: /* RIPngd main routine. */
  185: int
  186: main (int argc, char **argv)
  187: {
  188:   char *p;
  189:   int vty_port = RIPNG_VTY_PORT;
  190:   int daemon_mode = 0;
  191:   char *progname;
  192:   struct thread thread;
  193:   int dryrun = 0;
  194: 
  195:   /* Set umask before anything for security */
  196:   umask (0027);
  197: 
  198:   /* get program name */
  199:   progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
  200: 
  201:   zlog_default = openzlog(progname, ZLOG_RIPNG,
  202: 			  LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
  203: 
  204:   while (1) 
  205:     {
  206:       int opt;
  207: 
  208:       opt = getopt_long (argc, argv, "df:i:hA:P:u:g:vC", longopts, 0);
  209:     
  210:       if (opt == EOF)
  211: 	break;
  212: 
  213:       switch (opt) 
  214: 	{
  215: 	case 0:
  216: 	  break;
  217: 	case 'd':
  218: 	  daemon_mode = 1;
  219: 	  break;
  220: 	case 'f':
  221: 	  config_file = optarg;
  222: 	  break;
  223: 	case 'A':
  224: 	  vty_addr = optarg;
  225: 	  break;
  226:         case 'i':
  227:           pid_file = optarg;
  228:           break; 
  229: 	case 'P':
  230:           /* Deal with atoi() returning 0 on failure, and ripngd not
  231:              listening on ripngd port... */
  232:           if (strcmp(optarg, "0") == 0) 
  233:             {
  234:               vty_port = 0;
  235:               break;
  236:             } 
  237:           vty_port = atoi (optarg);
  238:           if (vty_port <= 0 || vty_port > 0xffff)
  239:             vty_port = RIPNG_VTY_PORT;
  240:           break;
  241: 	case 'r':
  242: 	  retain_mode = 1;
  243: 	  break;
  244: 	case 'u':
  245: 	  ripngd_privs.user = optarg;
  246: 	  break;
  247: 	case 'g':
  248: 	  ripngd_privs.group = optarg;
  249: 	  break;
  250: 	case 'v':
  251: 	  print_version (progname);
  252: 	  exit (0);
  253: 	  break;
  254: 	case 'C':
  255: 	  dryrun = 1;
  256: 	  break;
  257: 	case 'h':
  258: 	  usage (progname, 0);
  259: 	  break;
  260: 	default:
  261: 	  usage (progname, 1);
  262: 	  break;
  263: 	}
  264:     }
  265: 
  266:   master = thread_master_create ();
  267: 
  268:   /* Library inits. */
  269:   zprivs_init (&ripngd_privs);
  270:   signal_init (master, Q_SIGC(ripng_signals), ripng_signals);
  271:   cmd_init (1);
  272:   vty_init (master);
  273:   memory_init ();
  274: 
  275:   /* RIPngd inits. */
  276:   ripng_init ();
  277:   zebra_init ();
  278:   ripng_peer_init ();
  279: 
  280:   /* Sort all installed commands. */
  281:   sort_node ();
  282: 
  283:   /* Get configuration file. */
  284:   vty_read_config (config_file, config_default);
  285: 
  286:   /* Start execution only if not in dry-run mode */
  287:   if(dryrun)
  288:     return(0);
  289:   
  290:   /* Change to the daemon program. */
  291:   if (daemon_mode && daemon (0, 0) < 0)
  292:     {
  293:       zlog_err("RIPNGd daemon failed: %s", strerror(errno));
  294:       exit (1);
  295:     }
  296: 
  297:   /* Create VTY socket */
  298:   vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
  299: 
  300:   /* Process id file create. */
  301:   pid_output (pid_file);
  302: 
  303:   /* Print banner. */
  304:   zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
  305: 
  306:   /* Fetch next active thread. */
  307:   while (thread_fetch (master, &thread))
  308:     thread_call (&thread);
  309: 
  310:   /* Not reached. */
  311:   return 0;
  312: }

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