diff options
73 files changed, 1188 insertions, 2174 deletions
diff --git a/bgpd/bgp_main.c b/bgpd/bgp_main.c index 2598d66656..423c9453eb 100644 --- a/bgpd/bgp_main.c +++ b/bgpd/bgp_main.c @@ -39,7 +39,7 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  #include "queue.h"  #include "vrf.h"  #include "bfd.h" -#include "sockopt.h" +#include "libfrr.h"  #include "bgpd/bgpd.h"  #include "bgpd/bgp_attr.h" @@ -60,27 +60,13 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  #endif  /* bgpd options, we use GNU getopt library. */ -#define OPTION_VTYSOCK 1000  static const struct option longopts[] =   { -  { "daemon",      no_argument,       NULL, 'd'}, -  { "config_file", required_argument, NULL, 'f'}, -  { "pid_file",    required_argument, NULL, 'i'}, -  { "socket",      required_argument, NULL, 'z'},    { "bgp_port",    required_argument, NULL, 'p'},    { "listenon",    required_argument, NULL, 'l'}, -  { "vty_addr",    required_argument, NULL, 'A'}, -  { "vty_port",    required_argument, NULL, 'P'}, -  { "vty_socket",  required_argument, NULL, OPTION_VTYSOCK },    { "retain",      no_argument,       NULL, 'r'},    { "no_kernel",   no_argument,       NULL, 'n'},    { "ecmp",        required_argument, NULL, 'e'}, -  { "user",        required_argument, NULL, 'u'}, -  { "group",       required_argument, NULL, 'g'}, -  { "skip_runas",  no_argument,       NULL, 'S'}, -  { "version",     no_argument,       NULL, 'v'}, -  { "dryrun",      no_argument,       NULL, 'C'}, -  { "help",        no_argument,       NULL, 'h'},    { 0 }  }; @@ -112,26 +98,9 @@ static struct quagga_signal_t bgp_signals[] =    },  }; -/* Configuration file and directory. */ -char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG; - -/* VTY Socket prefix */ -char vty_sock_path[MAXPATHLEN] = BGP_VTYSH_PATH; -  /* Route retain mode flag. */  static int retain_mode = 0; -/* Manually specified configuration file name.  */ -char *config_file = NULL; - -/* Process ID saved for use by init system */ -static const char *pid_file = PATH_BGPD_PID; - -/* VTY port number and address.  */ -int vty_port = BGP_VTY_PORT; -char *vty_addr = NULL; -char *vty_sock_name; -  /* privileges */  static zebra_capabilities_t _caps_p [] =    { @@ -154,41 +123,7 @@ struct zebra_privs_t bgpd_privs =    .cap_num_i = 0,  }; -/* Help information display. */ -static void -usage (char *progname, int status) -{ -  if (status != 0) -    fprintf (stderr, "Try `%s --help' for more information.\n", progname); -  else -    {     -      printf ("Usage : %s [OPTION...]\n\n\ -Daemon which manages kernel routing table management and \ -redistribution between different routing protocols.\n\n\ --d, --daemon       Runs in daemon mode\n\ --f, --config_file  Set configuration file name\n\ --i, --pid_file     Set process identifier file name\n\ --z, --socket       Set path of zebra socket\n\ --p, --bgp_port     Set bgp protocol's port number\n\ --l, --listenon     Listen on specified address (implies -n)\n\ --A, --vty_addr     Set vty's bind address\n\ --P, --vty_port     Set vty's port number\n\ -    --vty_socket   Override vty socket path\n\ --r, --retain       When program terminates, retain added route by bgpd.\n\ --n, --no_kernel    Do not install route to kernel.\n\ --e, --ecmp         Specify ECMP to use.\n\ --u, --user         User to run as\n\ --g, --group        Group to run as\n\ --S, --skip_runas   Skip user and group run as\n\ --v, --version      Print program version\n\ --C, --dryrun       Check configuration for validity and exit\n\ --h, --help         Display this help and exit\n\ -\n\ -Report bugs to %s\n", progname, FRR_BUG_ADDRESS); -    } - -  exit (status); -} +static struct frr_daemon_info bgpd_di;  /* SIGHUP handler. */  void  @@ -202,10 +137,7 @@ sighup (void)    zlog_info ("bgpd restarting!");    /* Reload config file. */ -  vty_read_config (config_file, config_default); - -  /* Create VTY's socket */ -  vty_serv_sock (vty_addr, vty_port, vty_sock_path); +  vty_read_config (bgpd_di.config_file, config_default);    /* Try to return to normal operation. */  } @@ -219,8 +151,7 @@ sigint (void)    if (! retain_mode)      {        bgp_terminate (); -      if (bgpd_privs.user)      /* NULL if skip_runas flag set */ -        zprivs_terminate (&bgpd_privs); +      zprivs_terminate (&bgpd_privs);      }    bgp_exit (0); @@ -232,7 +163,7 @@ sigint (void)  void  sigusr1 (void)  { -  zlog_rotate (NULL); +  zlog_rotate();  }  /* @@ -309,8 +240,7 @@ bgp_exit (int status)    if (bm->master)      thread_master_free (bm->master); -  if (zlog_default) -    closezlog (zlog_default); +  closezlog ();    memset (bm, 0, sizeof (*bm)); @@ -412,33 +342,40 @@ bgp_vrf_terminate (void)    vrf_terminate ();  } +FRR_DAEMON_INFO(bgpd, BGP, +	.vty_port = BGP_VTY_PORT, + +	.proghelp = "Implementation of the BGP routing protocol.", + +	.signals = bgp_signals, +	.n_signals = array_size(bgp_signals), + +	.privs = &bgpd_privs, +) +  /* Main routine of bgpd. Treatment of argument and start bgp finite     state machine is handled at here. */  int  main (int argc, char **argv)  { -  char *p;    int opt; -  int daemon_mode = 0; -  int dryrun = 0; -  char *progname; -  struct thread thread;    int tmp_port; -  int skip_runas = 0; -  /* Set umask before anything for security */ -  umask (0027); +  int bgp_port = BGP_PORT_DEFAULT; +  char *bgp_address = NULL; -  /* Preserve name of myself. */ -  progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); - -  /* BGP master init. */ -  bgp_master_init (); +  frr_preinit(&bgpd_di, argc, argv); +  frr_opt_add("p:l:rne:", longopts, +	"  -p, --bgp_port     Set bgp protocol's port number\n" +	"  -l, --listenon     Listen on specified address (implies -n)\n" +	"  -r, --retain       When program terminates, retain added route by bgpd.\n" +	"  -n, --no_kernel    Do not install route to kernel.\n" +	"  -e, --ecmp         Specify ECMP to use.\n");    /* Command line argument treatment. */ -  while (1)  +  while (1)      { -      opt = getopt_long (argc, argv, "df:i:z:hp:l:A:P:rnu:g:vCS", longopts, 0); +      opt = frr_getopt (argc, argv, 0);        if (opt == EOF)  	break; @@ -447,28 +384,13 @@ main (int argc, char **argv)  	{  	case 0:  	  break; -	case 'd': -	  daemon_mode = 1; -	  break; -	case 'f': -	  config_file = optarg; -	  break; -        case 'i': -          pid_file = optarg; -          break; -	case 'z': -	  zclient_serv_path_set (optarg); -	  break;  	case 'p':  	  tmp_port = atoi (optarg);  	  if (tmp_port <= 0 || tmp_port > 0xffff) -	    bm->port = BGP_PORT_DEFAULT; +	    bgp_port = BGP_PORT_DEFAULT;  	  else  	    bm->port = tmp_port;  	  break; -	case 'A': -	  vty_addr = optarg; -	  break;          case 'e':            multipath_num = atoi (optarg);            if (multipath_num > MULTIPATH_NUM || multipath_num <= 0) @@ -477,107 +399,38 @@ main (int argc, char **argv)                return 1;              }            break; -	case 'P': -          /* Deal with atoi() returning 0 on failure, and bgpd not -             listening on bgp port... */ -          if (strcmp(optarg, "0") == 0)  -            { -              vty_port = 0; -              break; -            }  -          vty_port = atoi (optarg); -	  if (vty_port <= 0 || vty_port > 0xffff) -	    vty_port = BGP_VTY_PORT; -	  break; -	case OPTION_VTYSOCK: -	  set_socket_path(vty_sock_path, BGP_VTYSH_PATH, optarg, sizeof (vty_sock_path)); -	  break;  	case 'r':  	  retain_mode = 1;  	  break;  	case 'l': -	  bm->address = optarg; +	  bgp_address = optarg;  	  /* listenon implies -n */  	case 'n':  	  bgp_option_set (BGP_OPT_NO_FIB);  	  break; -	case 'u': -	  bgpd_privs.user = optarg; -	  break; -	case 'g': -	  bgpd_privs.group = optarg; -	  break; -	case 'S':   /* skip run as = override bgpd_privs */ -          skip_runas = 1; -	  break; -	case 'v': -	  print_version (progname); -	  exit (0); -	  break; -	case 'C': -	  dryrun = 1; -	  break; -	case 'h': -	  usage (progname, 0); -	  break;  	default: -	  usage (progname, 1); +	  frr_help_exit (1);  	  break;  	}      } -  zlog_default = openzlog (progname, ZLOG_BGP, 0, -			   LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); - -  if (skip_runas) -    memset (&bgpd_privs, 0, sizeof (bgpd_privs)); -  zprivs_init (&bgpd_privs); - -#if defined(HAVE_CUMULUS) -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); -#endif +  /* BGP master init. */ +  bgp_master_init (frr_init ()); +  bm->port = bgp_port; +  bm->address = bgp_address;    /* Initializations. */ -  srandom (time (NULL)); -  signal_init (bm->master, array_size(bgp_signals), bgp_signals); -  cmd_init (1); -  vty_init (bm->master); -  memory_init ();    bgp_vrf_init ();    /* BGP related initialization.  */    bgp_init (); -  /* Parse config file. */ -  vty_read_config (config_file, config_default); - -  /* Start execution only if not in dry-run mode */ -  if(dryrun) -    return(0); -   -  /* Turn into daemon if daemon_mode is set. */ -  if (daemon_mode && daemon (0, 0) < 0) -    { -      zlog_err("BGPd daemon failed: %s", strerror(errno)); -      return (1); -    } - - -  /* Process ID file creation. */ -  pid_output (pid_file); - -  /* Make bgp vty socket. */ -  vty_serv_sock (vty_addr, vty_port, vty_sock_path); - -  /* Print banner. */ -  zlog_notice ("BGPd %s starting: vty@%d, bgp@%s:%d", FRR_COPYRIGHT, -	       vty_port,  -	       (bm->address ? bm->address : "<all>"), -	       bm->port); +  snprintf (bgpd_di.startinfo, sizeof (bgpd_di.startinfo), ", bgp@%s:%d", +            (bm->address ? bm->address : "<all>"), +            bm->port); -  /* Start finite state machine, here we go! */ -  while (thread_fetch (bm->master, &thread)) -    thread_call (&thread); +  frr_config_fork (); +  frr_run (bm->master);    /* Not reached. */    return (0); diff --git a/bgpd/bgp_updgrp.h b/bgpd/bgp_updgrp.h index 6ab384369c..8f3e27bc84 100644 --- a/bgpd/bgp_updgrp.h +++ b/bgpd/bgp_updgrp.h @@ -249,7 +249,6 @@ struct update_subgroup    u_int32_t merge_checks_triggered;    uint64_t id; -  struct zlog *log;    u_int16_t sflags; diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c index 095f0fce6f..9d76b3d58b 100644 --- a/bgpd/bgpd.c +++ b/bgpd/bgpd.c @@ -7588,7 +7588,7 @@ bgp_config_write (struct vty *vty)  }  void -bgp_master_init (void) +bgp_master_init (struct thread_master *master)  {    qobj_init (); @@ -7598,7 +7598,7 @@ bgp_master_init (void)    bm->bgp = list_new ();    bm->listen_sockets = list_new ();    bm->port = BGP_PORT_DEFAULT; -  bm->master = thread_master_create (); +  bm->master = master;    bm->start_time = bgp_clock ();    bm->t_rmap_update = NULL;    bm->rmap_update_timer = RMAP_DEFAULT_UPDATE_TIMER; diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h index 99d87d785c..9ccc0e39de 100644 --- a/bgpd/bgpd.h +++ b/bgpd/bgpd.h @@ -1218,7 +1218,7 @@ extern char *peer_uptime (time_t, char *, size_t, u_char, json_object *);  extern int bgp_config_write (struct vty *);  extern void bgp_config_write_family_header (struct vty *, afi_t, safi_t, int *); -extern void bgp_master_init (void); +extern void bgp_master_init (struct thread_master *master);  extern void bgp_init (void);  extern void bgp_route_map_init (void); diff --git a/bgpd/rfapi/rfapi_vty.c b/bgpd/rfapi/rfapi_vty.c index 1f3066b5c5..c52026e8ef 100644 --- a/bgpd/rfapi/rfapi_vty.c +++ b/bgpd/rfapi/rfapi_vty.c @@ -29,6 +29,7 @@  #include "lib/memory.h"  #include "lib/routemap.h"  #include "lib/log.h" +#include "lib/log_int.h"  #include "lib/linklist.h"  #include "lib/command.h" @@ -352,7 +353,7 @@ rfapiDebugPrintf (void *dummy, const char *format, ...)  {    va_list args;    va_start (args, format); -  vzlog (NULL, LOG_DEBUG, format, args); +  vzlog (LOG_DEBUG, format, args);    va_end (args);    return 0;  } @@ -411,7 +412,7 @@ rfapiStream2Vty (        *fp = (int (*)(void *, const char *,...)) rfapiDebugPrintf;        *outstream = NULL;        *vty_newline = str_vty_newline (*vty); -      return (vzlog_test (NULL, LOG_DEBUG)); +      return (vzlog_test (LOG_DEBUG));      }    if (((uintptr_t) stream == (uintptr_t) 1) || diff --git a/configure.ac b/configure.ac index 96d3d3894d..e46e44a8b7 100755 --- a/configure.ac +++ b/configure.ac @@ -1628,30 +1628,8 @@ fi  AC_MSG_RESULT(${frr_statedir})  AC_SUBST(frr_statedir) -AC_DEFINE_UNQUOTED(PATH_ZEBRA_PID, "$frr_statedir/zebra.pid",zebra PID) -AC_DEFINE_UNQUOTED(PATH_RIPD_PID, "$frr_statedir/ripd.pid",ripd PID) -AC_DEFINE_UNQUOTED(PATH_RIPNGD_PID, "$frr_statedir/ripngd.pid",ripngd PID) -AC_DEFINE_UNQUOTED(PATH_BGPD_PID, "$frr_statedir/bgpd.pid",bgpd PID) -AC_DEFINE_UNQUOTED(PATH_OSPFD_PID, "$frr_statedir/ospfd.pid",ospfd PID) -AC_DEFINE_UNQUOTED(PATH_OSPF6D_PID, "$frr_statedir/ospf6d.pid",ospf6d PID) -AC_DEFINE_UNQUOTED(PATH_LDPD_PID, "$frr_statedir/ldpd.pid",ldpd PID)  AC_DEFINE_UNQUOTED(LDPD_SOCKET, "$frr_statedir/ldpd.sock",ldpd control socket) -AC_DEFINE_UNQUOTED(PATH_ISISD_PID, "$frr_statedir/isisd.pid",isisd PID) -AC_DEFINE_UNQUOTED(PATH_PIMD_PID, "$frr_statedir/pimd.pid",pimd PID) -AC_DEFINE_UNQUOTED(PATH_NHRPD_PID, "$frr_statedir/nhrpd.pid",nhrpd PID) -AC_DEFINE_UNQUOTED(PATH_WATCHFRR_PID, "$frr_statedir/watchfrr.pid",watchfrr PID)  AC_DEFINE_UNQUOTED(ZEBRA_SERV_PATH, "$frr_statedir/zserv.api",zebra api socket) -AC_DEFINE_UNQUOTED(ZEBRA_VTYSH_PATH, "$frr_statedir/zebra.vty",zebra vty socket) -AC_DEFINE_UNQUOTED(RIP_VTYSH_PATH, "$frr_statedir/ripd.vty",rip vty socket) -AC_DEFINE_UNQUOTED(RIPNG_VTYSH_PATH, "$frr_statedir/ripngd.vty",ripng vty socket) -AC_DEFINE_UNQUOTED(BGP_VTYSH_PATH, "$frr_statedir/bgpd.vty",bgpd vty socket) -AC_DEFINE_UNQUOTED(OSPF_VTYSH_PATH, "$frr_statedir/ospfd.vty",ospfd vty socket) -AC_DEFINE_UNQUOTED(OSPF6_VTYSH_PATH, "$frr_statedir/ospf6d.vty",ospf6d vty socket) -AC_DEFINE_UNQUOTED(LDP_VTYSH_PATH, "$frr_statedir/ldpd.vty",ldpd vty socket) -AC_DEFINE_UNQUOTED(ISIS_VTYSH_PATH, "$frr_statedir/isisd.vty",isisd vty socket) -AC_DEFINE_UNQUOTED(PIM_VTYSH_PATH, "$frr_statedir/pimd.vty",pimd vty socket) -AC_DEFINE_UNQUOTED(NHRP_VTYSH_PATH, "$frr_statedir/nhrpd.vty",nhrpd vty socket) -AC_DEFINE_UNQUOTED(WATCHFRR_VTYSH_PATH, "$frr_statedir/watchfrr.vty",watchfrr vty socket)  AC_DEFINE_UNQUOTED(DAEMON_VTY_DIR, "$frr_statedir",daemon vty directory)  dnl autoconf does this, but it does it too late... diff --git a/isisd/isis_main.c b/isisd/isis_main.c index 1b9fa68051..a6ad424ace 100644 --- a/isisd/isis_main.c +++ b/isisd/isis_main.c @@ -38,8 +38,8 @@  #include "plist.h"  #include "zclient.h"  #include "vrf.h" -#include "sockopt.h"  #include "qobj.h" +#include "libfrr.h"  #include "isisd/dict.h"  #include "isisd/isis_constants.h" @@ -82,41 +82,13 @@ struct zebra_privs_t isisd_privs = {  };  /* isisd options */ -#define OPTION_VTYSOCK 1000  struct option longopts[] = { -  {"daemon",      no_argument,       NULL, 'd'}, -  {"config_file", required_argument, NULL, 'f'}, -  {"pid_file",    required_argument, NULL, 'i'}, -  {"socket",      required_argument, NULL, 'z'}, -  {"vty_addr",    required_argument, NULL, 'A'}, -  {"vty_port",    required_argument, NULL, 'P'}, -  {"vty_socket",  required_argument, NULL, OPTION_VTYSOCK}, -  {"user",        required_argument, NULL, 'u'}, -  {"group",       required_argument, NULL, 'g'}, -  {"version",     no_argument,       NULL, 'v'}, -  {"dryrun",      no_argument,       NULL, 'C'}, -  {"help",        no_argument,       NULL, 'h'},    {0}  }; -/* Configuration file and directory. */ -char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG; -char *config_file = NULL; - -/* isisd program name. */ -char *progname; - -/* VTY Socket prefix */ -char vty_sock_path[MAXPATHLEN] = ISIS_VTYSH_PATH; - -int daemon_mode = 0; -  /* Master of threads. */  struct thread_master *master; -/* Process ID saved for use by init system */ -const char *pid_file = PATH_ISISD_PID; -  /* for reload */  char _cwd[MAXPATHLEN];  char _progpath[MAXPATHLEN]; @@ -134,36 +106,6 @@ void sigterm(void);  void sigusr1(void); -/* Help information display. */ -static void -usage (int status) -{ -  if (status != 0) -    fprintf (stderr, "Try `%s --help' for more information.\n", progname); -  else -    { -      printf ("Usage : %s [OPTION...]\n\n\ -Daemon which manages IS-IS routing\n\n\ --d, --daemon       Runs in daemon mode\n\ --f, --config_file  Set configuration file name\n\ --i, --pid_file     Set process identifier file name\n\ --z, --socket       Set path of zebra socket\n\ --A, --vty_addr     Set vty's bind address\n\ --P, --vty_port     Set vty's port number\n\ -    --vty_socket   Override vty socket path\n\ --u, --user         User to run as\n\ --g, --group        Group to run as\n\ --v, --version      Print program version\n\ --C, --dryrun       Check configuration for validity and exit\n\ --h, --help         Display this help and exit\n\ -\n\ -Report bugs to %s\n", progname, FRR_BUG_ADDRESS); -    } - -  exit (status); -} - -  void  reload ()  { @@ -213,7 +155,7 @@ void  sigusr1 (void)  {    zlog_debug ("SIGUSR1 received"); -  zlog_rotate (NULL); +  zlog_rotate();  }  struct quagga_signal_t isisd_signals[] = @@ -236,28 +178,26 @@ struct quagga_signal_t isisd_signals[] =     },  }; +FRR_DAEMON_INFO(isisd, ISIS, +	.vty_port = ISISD_VTY_PORT, + +	.proghelp = "Implementation of the IS-IS routing protocol.", +	.copyright = "Copyright (c) 2001-2002 Sampo Saaristo," +		  " Ofer Wald and Hannes Gredler", + +	.signals = isisd_signals, +	.n_signals = array_size(isisd_signals), + +	.privs = &isisd_privs, +) +  /*   * Main routine of isisd. Parse arguments and handle IS-IS state machine.   */  int  main (int argc, char **argv, char **envp)  { -  char *p; -  int opt, vty_port = ISISD_VTY_PORT; -  struct thread thread; -  char *config_file = NULL; -  char *vty_addr = NULL; -  int dryrun = 0; - -  /* Get the programname without the preceding path. */ -  progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); - -  zlog_default = openzlog (progname, ZLOG_ISIS, 0, -			   LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); -  zprivs_init (&isisd_privs); -#if defined(HAVE_CUMULUS) -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); -#endif +  int opt;    /* for reload */    _argc = argc; @@ -274,10 +214,13 @@ main (int argc, char **argv, char **envp)    else      snprintf (_progpath, sizeof (_progpath), "%s", argv[0]); +  frr_preinit (&isisd_di, argc, argv); +  frr_opt_add ("", longopts, ""); +    /* Command line argument treatment. */    while (1)      { -      opt = getopt_long (argc, argv, "df:i:z:hA:p:P:u:g:vC", longopts, 0); +      opt = frr_getopt (argc, argv, NULL);        if (opt == EOF)  	break; @@ -286,74 +229,19 @@ main (int argc, char **argv, char **envp)  	{  	case 0:  	  break; -	case 'd': -	  daemon_mode = 1; -	  break; -	case 'f': -	  config_file = optarg; -	  break; -	case 'i': -	  pid_file = optarg; -	  break; -	case 'z': -	  zclient_serv_path_set (optarg); -	  break; -	case 'A': -	  vty_addr = optarg; -	  break; -	case 'P': -	  /* Deal with atoi() returning 0 on failure, and isisd not -	     listening on isisd port... */ -	  if (strcmp (optarg, "0") == 0) -	    { -	      vty_port = 0; -	      break; -	    } -	  vty_port = atoi (optarg); -	  vty_port = (vty_port ? vty_port : ISISD_VTY_PORT); -	  break; -	case OPTION_VTYSOCK: -	  set_socket_path(vty_sock_path, ISIS_VTYSH_PATH, optarg, sizeof (vty_sock_path)); -	  break; -	case 'u': -	  isisd_privs.user = optarg; -	  break; -	case 'g': -	  isisd_privs.group = optarg; -	  break; -	case 'v': -	  printf ("ISISd version %s\n", ISISD_VERSION); -	  printf ("Copyright (c) 2001-2002 Sampo Saaristo," -		  " Ofer Wald and Hannes Gredler\n"); -	  print_version ("Zebra"); -	  exit (0); -	  break; -	case 'C': -	  dryrun = 1; -	  break; -	case 'h': -	  usage (0); -	  break;  	default: -	  usage (1); +	  frr_help_exit (1);  	  break;  	}      } +  vty_config_lockless ();    /* thread master */ -  master = thread_master_create (); - -  /* random seed from time */ -  srandom (time (NULL)); +  master = frr_init ();    /*     *  initializations     */ -  signal_init (master, array_size (isisd_signals), isisd_signals); -  cmd_init (1); -  vty_config_lockless (); -  vty_init (master); -  memory_init ();    access_list_init();    vrf_init ();    prefix_list_init(); @@ -369,34 +257,8 @@ main (int argc, char **argv, char **envp)    isis_zebra_init(master); -  /* parse config file */ -  /* this is needed three times! because we have interfaces before the areas */ -  vty_read_config (config_file, config_default); - -  /* Start execution only if not in dry-run mode */ -  if (dryrun) -    return(0); -   -  /* demonize */ -  if (daemon_mode && daemon (0, 0) < 0) -    { -      zlog_err("ISISd daemon failed: %s", strerror(errno)); -      return (1); -    } - -  /* Process ID file creation. */ -  if (pid_file[0] != '\0') -    pid_output (pid_file); - -  /* Make isis vty socket. */ -  vty_serv_sock (vty_addr, vty_port, vty_sock_path); - -  /* Print banner. */ -  zlog_notice ("Quagga-ISISd %s starting: vty@%d", FRR_VERSION, vty_port); - -  /* Start finite state machine. */ -  while (thread_fetch (master, &thread)) -    thread_call (&thread); +  frr_config_fork (); +  frr_run (master);    /* Not reached. */    exit (0); diff --git a/ldpd/ldpd.c b/ldpd/ldpd.c index 4e79f8b8ab..98ea5ca53e 100644 --- a/ldpd/ldpd.c +++ b/ldpd/ldpd.c @@ -40,8 +40,8 @@  #include "zclient.h"  #include "vrf.h"  #include "filter.h" -#include "sockopt.h"  #include "qobj.h" +#include "libfrr.h"  static void		 ldpd_shutdown(void);  static pid_t		 start_child(enum ldpd_process, char *, int, int, @@ -90,12 +90,6 @@ static pid_t		 lde_pid;  /* Master of threads. */  struct thread_master *master; -/* Process ID saved for use by init system */ -static const char *pid_file = PATH_LDPD_PID; - -/* Configuration filename and directory. */ -static char config_default[] = SYSCONFDIR LDP_DEFAULT_CONFIG; -  /* ldpd privileges */  static zebra_capabilities_t _caps_p [] =  { @@ -117,63 +111,17 @@ struct zebra_privs_t ldpd_privs =  	.cap_num_i = 0  }; -/* VTY Socket prefix */ -char vty_sock_path[MAXPATHLEN] = LDP_VTYSH_PATH; -  /* CTL Socket path */  char ctl_sock_path[MAXPATHLEN] = LDPD_SOCKET;  /* LDPd options. */ -#define OPTION_VTYSOCK 1000  #define OPTION_CTLSOCK 1001  static struct option longopts[] =  { -	{ "daemon",      no_argument,       NULL, 'd'}, -	{ "config_file", required_argument, NULL, 'f'}, -	{ "pid_file",    required_argument, NULL, 'i'}, -	{ "socket",      required_argument, NULL, 'z'}, -	{ "dryrun",      no_argument,       NULL, 'C'}, -	{ "help",        no_argument,       NULL, 'h'}, -	{ "vty_addr",    required_argument, NULL, 'A'}, -	{ "vty_port",    required_argument, NULL, 'P'}, -	{ "vty_socket",  required_argument, NULL, OPTION_VTYSOCK},  	{ "ctl_socket",  required_argument, NULL, OPTION_CTLSOCK}, -	{ "user",        required_argument, NULL, 'u'}, -	{ "group",       required_argument, NULL, 'g'}, -	{ "version",     no_argument,       NULL, 'v'},  	{ 0 }  }; -/* Help information display. */ -static void __attribute__ ((noreturn)) -usage(char *progname, int status) -{ -	if (status != 0) -		fprintf(stderr, "Try `%s --help' for more information.\n", -		    progname); -	else { -		printf("Usage : %s [OPTION...]\n\ -Daemon which manages LDP.\n\n\ --d, --daemon       Runs in daemon mode\n\ --f, --config_file  Set configuration file name\n\ --i, --pid_file     Set process identifier file name\n\ --z, --socket       Set path of zebra socket\n\ --A, --vty_addr     Set vty's bind address\n\ --P, --vty_port     Set vty's port number\n\ -    --vty_socket   Override vty socket path\n\ -    --ctl_socket   Override ctl socket path\n\ --u, --user         User to run as\n\ --g, --group        Group to run as\n\ --v, --version      Print program version\n\ --C, --dryrun       Check configuration for validity and exit\n\ --h, --help         Display this help and exit\n\ -\n\ -Report bugs to %s\n", progname, FRR_BUG_ADDRESS); -	} - -	exit(status); -} -  /* SIGHUP handler. */  static void  sighup(void) @@ -193,7 +141,7 @@ sigint(void)  static void  sigusr1(void)  { -	zlog_rotate(NULL); +	zlog_rotate();  }  static struct quagga_signal_t ldp_signals[] = @@ -216,6 +164,17 @@ static struct quagga_signal_t ldp_signals[] =  	}  }; +FRR_DAEMON_INFO(ldpd, LDP, +	.vty_port = LDP_VTY_PORT, + +	.proghelp = "Implementation of the LDP protocol.", + +	.signals = ldp_signals, +	.n_signals = array_size(ldp_signals), + +	.privs = &ldpd_privs, +) +  int  main(int argc, char *argv[])  { @@ -223,36 +182,25 @@ main(int argc, char *argv[])  	int			 lflag = 0, eflag = 0;  	int			 pipe_parent2ldpe[2], pipe_parent2ldpe_sync[2];  	int			 pipe_parent2lde[2], pipe_parent2lde_sync[2]; -	char			*p; -	char			*vty_addr = NULL; -	int			 vty_port = LDP_VTY_PORT;  	char			*ctl_sock_custom_path = NULL;  	char			*ctl_sock_name; -	int			 daemon_mode = 0;  	const char		*user = NULL;  	const char		*group = NULL; -	char			*config_file = NULL; -	char			*progname; -	struct thread		 thread; -	int			 dryrun = 0;  	ldpd_process = PROC_MAIN; -	/* Set umask before anything for security */ -	umask(0027); - -	/* get program name */ -	progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]); -  	saved_argv0 = argv[0];  	if (saved_argv0 == NULL)  		saved_argv0 = (char *)"ldpd"; +	frr_preinit(&ldpd_di, argc, argv); +	frr_opt_add("LE", longopts, +		"      --ctl_socket   Override ctl socket path\n"); +  	while (1) {  		int opt; -		opt = getopt_long(argc, argv, "df:i:z:hA:P:u:g:vCLE", -		    longopts, 0); +		opt = frr_getopt(argc, argv, NULL);  		if (opt == EOF)  			break; @@ -260,37 +208,6 @@ main(int argc, char *argv[])  		switch (opt) {  		case 0:  			break; -		case 'd': -			daemon_mode = 1; -			break; -		case 'f': -			config_file = optarg; -			break; -		case 'A': -			vty_addr = optarg; -			break; -		case 'i': -			pid_file = optarg; -			break; -		case 'z': -			zclient_serv_path_set(optarg); -			break; -		case 'P': -			/* -			 * Deal with atoi() returning 0 on failure, and ldpd -			 * not listening on ldpd port. -			 */ -			if (strcmp(optarg, "0") == 0) { -				vty_port = 0; -				break; -			} -			vty_port = atoi(optarg); -			if (vty_port <= 0 || vty_port > 0xffff) -				vty_port = LDP_VTY_PORT; -			break; -		case OPTION_VTYSOCK: -			set_socket_path(vty_sock_path, LDP_VTYSH_PATH, optarg, sizeof (vty_sock_path)); -			break;  		case OPTION_CTLSOCK:  			ctl_sock_name = strrchr(LDPD_SOCKET, '/');  			if (ctl_sock_name) @@ -310,22 +227,6 @@ main(int argc, char *argv[])  			strlcat(ctl_sock_path, ctl_sock_name,  			    sizeof(ctl_sock_path));  			break; -		case 'u': -			user = optarg; -			break; -		case 'g': -			group = optarg; -			break; -		case 'v': -			print_version(progname); -			exit(0); -			break; -		case 'C': -			dryrun = 1; -			break; -		case 'h': -			usage(progname, 0); -			break;  		case 'L':  			lflag = 1;  			break; @@ -333,24 +234,27 @@ main(int argc, char *argv[])  			eflag = 1;  			break;  		default: -			usage(progname, 1); +			frr_help_exit(1);  			break;  		}  	} +	user = ldpd_privs.user; +	group = ldpd_privs.group; +  	argc -= optind;  	argv += optind;  	if (argc > 0 || (lflag && eflag)) -		usage(progname, 1); +		frr_help_exit(1);  	/* check for root privileges  */  	if (geteuid() != 0) {  		errno = EPERM; -		perror(progname); +		perror(ldpd_di.progname);  		exit(1);  	} -	zlog_default = openzlog(progname, ZLOG_LDP, 0, +	openzlog(ldpd_di.progname, "LDP", 0,  	    LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);  	if (lflag) @@ -371,19 +275,11 @@ main(int argc, char *argv[])  	/* Get configuration file. */  	ldpd_conf = config_new_empty();  	ldp_config_reset_main(ldpd_conf, NULL); -	vty_read_config(config_file, config_default); -	/* Start execution only if not in dry-run mode */ -	if (dryrun) -		exit(0); +	frr_config_fork();  	QOBJ_REG (ldpd_conf, ldpd_conf); -	if (daemon_mode && daemon(0, 0) < 0) { -		log_warn("LDPd daemon failed"); -		exit(1); -	} -  	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_parent2ldpe) == -1)  		fatal("socketpair");  	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, @@ -418,10 +314,6 @@ main(int argc, char *argv[])  	    user, group, ctl_sock_custom_path);  	/* drop privileges */ -	if (user) -		ldpd_privs.user = user; -	if (group) -		ldpd_privs.group = group;  	zprivs_init(&ldpd_privs);  	/* setup signal handler */ @@ -471,18 +363,7 @@ main(int argc, char *argv[])  	if (ldpd_conf->ipv6.flags & F_LDPD_AF_ENABLED)  		main_imsg_send_net_sockets(AF_INET6); -	/* Process id file create. */ -	pid_output(pid_file); - -	/* Create VTY socket */ -	vty_serv_sock(vty_addr, vty_port, vty_sock_path); - -	/* Print banner. */ -	log_notice("LDPd %s starting: vty@%d", FRR_VERSION, vty_port); - -	/* Fetch next active thread. */ -	while (thread_fetch(master, &thread)) -		thread_call(&thread); +	frr_run(master);  	/* NOTREACHED */  	return (0); @@ -526,7 +407,7 @@ ldpd_shutdown(void)  	ldp_zebra_destroy();  	zprivs_terminate(&ldpd_privs);  	thread_master_free(master); -	closezlog(zlog_default); +	closezlog();  	exit(0);  } diff --git a/ldpd/log.c b/ldpd/log.c index b30604db0d..407668bb03 100644 --- a/ldpd/log.c +++ b/ldpd/log.c @@ -24,6 +24,7 @@  #include "log.h"  #include <lib/log.h> +#include <lib/log_int.h>  #include "mpls.h"  static const char * const procnames[] = { @@ -59,7 +60,7 @@ vlog(int pri, const char *fmt, va_list ap)  		ldpe_imsg_compose_parent(IMSG_LOG, pri, buf, strlen(buf) + 1);  		break;  	case PROC_MAIN: -		vzlog(NULL, pri, fmt, ap); +		vzlog(pri, fmt, ap);  		break;  	}  } diff --git a/lib/Makefile.am b/lib/Makefile.am index a9fe646938..1a8c7af42b 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -29,6 +29,7 @@ libfrr_la_SOURCES = \  	grammar_sandbox.c \  	srcdest_table.c \  	spf_backoff.c \ +	libfrr.c \  	strlcpy.c \  	strlcat.c @@ -52,10 +53,13 @@ pkginclude_HEADERS = \  	event_counter.h \  	monotime.h \  	spf_backoff.h \ -	srcdest_table.h +	srcdest_table.h \ +	libfrr.h \ +	# end  noinst_HEADERS = \ -	plist_int.h +	plist_int.h \ +	log_int.h  noinst_PROGRAMS = grammar_sandbox diff --git a/lib/command.c b/lib/command.c index 50976f2010..bfff581d9b 100644 --- a/lib/command.c +++ b/lib/command.c @@ -30,6 +30,7 @@  #include "memory.h"  #include "log.h" +#include "log_int.h"  #include <lib/version.h>  #include "thread.h"  #include "vector.h" @@ -2163,7 +2164,7 @@ DEFUN (config_logmsg,    if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)      return CMD_ERR_NO_MATCH; -  zlog(NULL, level, "%s", ((message = argv_concat(argv, argc, idx_message)) ? message : "")); +  zlog(level, "%s", ((message = argv_concat(argv, argc, idx_message)) ? message : ""));    if (message)      XFREE(MTYPE_TMP, message); @@ -2214,7 +2215,7 @@ DEFUN (show_logging,    vty_out (vty, "%s", VTY_NEWLINE);    vty_out (vty, "Protocol name: %s%s", -           zlog_proto_names[zl->protocol], VTY_NEWLINE); +           zl->protoname, VTY_NEWLINE);    vty_out (vty, "Record priority: %s%s",             (zl->record_priority ? "enabled" : "disabled"), VTY_NEWLINE);    vty_out (vty, "Timestamp precision: %d%s", @@ -2234,14 +2235,14 @@ DEFUN (config_log_stdout,    if (argc == idx_log_level)    { -    zlog_set_level (NULL, ZLOG_DEST_STDOUT, zlog_default->default_lvl); +    zlog_set_level (ZLOG_DEST_STDOUT, zlog_default->default_lvl);      return CMD_SUCCESS;    }    int level;    if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)      return CMD_ERR_NO_MATCH; -  zlog_set_level (NULL, ZLOG_DEST_STDOUT, level); +  zlog_set_level (ZLOG_DEST_STDOUT, level);    return CMD_SUCCESS;  } @@ -2253,7 +2254,7 @@ DEFUN (no_config_log_stdout,         "Cancel logging to stdout\n"         LOG_LEVEL_DESC)  { -  zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED); +  zlog_set_level (ZLOG_DEST_STDOUT, ZLOG_DISABLED);    return CMD_SUCCESS;  } @@ -2268,14 +2269,14 @@ DEFUN (config_log_monitor,    if (argc == idx_log_level)    { -    zlog_set_level (NULL, ZLOG_DEST_MONITOR, zlog_default->default_lvl); +    zlog_set_level (ZLOG_DEST_MONITOR, zlog_default->default_lvl);      return CMD_SUCCESS;    }    int level;    if ((level = level_match(argv[idx_log_level]->arg)) == ZLOG_DISABLED)      return CMD_ERR_NO_MATCH; -  zlog_set_level (NULL, ZLOG_DEST_MONITOR, level); +  zlog_set_level (ZLOG_DEST_MONITOR, level);    return CMD_SUCCESS;  } @@ -2287,7 +2288,7 @@ DEFUN (no_config_log_monitor,         "Disable terminal line (monitor) logging\n"         LOG_LEVEL_DESC)  { -  zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); +  zlog_set_level (ZLOG_DEST_MONITOR, ZLOG_DISABLED);    return CMD_SUCCESS;  } @@ -2322,7 +2323,7 @@ set_log_file(struct vty *vty, const char *fname, int loglevel)    else      fullpath = fname; -  ret = zlog_set_file (NULL, fullpath, loglevel); +  ret = zlog_set_file (fullpath, loglevel);    if (p)      XFREE (MTYPE_TMP, p); @@ -2376,7 +2377,7 @@ DEFUN (no_config_log_file,         "Logging file name\n"         "Logging level\n")  { -  zlog_reset_file (NULL); +  zlog_reset_file ();    if (host.logfile)      XFREE (MTYPE_HOST, host.logfile); @@ -2399,12 +2400,12 @@ DEFUN (config_log_syslog,      int level;      if ((level = level_match (argv[idx_log_levels]->arg)) == ZLOG_DISABLED)        return CMD_ERR_NO_MATCH; -    zlog_set_level (NULL, ZLOG_DEST_SYSLOG, level); +    zlog_set_level (ZLOG_DEST_SYSLOG, level);      return CMD_SUCCESS;    }    else    { -    zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); +    zlog_set_level (ZLOG_DEST_SYSLOG, zlog_default->default_lvl);      return CMD_SUCCESS;    }  } @@ -2418,7 +2419,7 @@ DEFUN (no_config_log_syslog,         LOG_FACILITY_DESC         LOG_LEVEL_DESC)  { -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); +  zlog_set_level (ZLOG_DEST_SYSLOG, ZLOG_DISABLED);    return CMD_SUCCESS;  } @@ -2726,7 +2727,6 @@ cmd_init (int terminal)        vrf_install_commands ();      } -  srandom(time(NULL));  #ifdef DEV_BUILD    grammar_sandbox_init(); diff --git a/lib/filter.c b/lib/filter.c index 2b9ba87137..fd73d4de73 100644 --- a/lib/filter.c +++ b/lib/filter.c @@ -1697,9 +1697,7 @@ filter_show (struct vty *vty, const char *name, afi_t afi)      return 0;    /* Print the name of the protocol */ -  if (zlog_default) -      vty_out (vty, "%s:%s", -      zlog_proto_names[zlog_default->protocol], VTY_NEWLINE); +  vty_out(vty, "%s:%s", zlog_protoname(), VTY_NEWLINE);    for (access = master->num.head; access; access = access->next)      { diff --git a/lib/grammar_sandbox_main.c b/lib/grammar_sandbox_main.c index 5deef406c1..681d4da440 100644 --- a/lib/grammar_sandbox_main.c +++ b/lib/grammar_sandbox_main.c @@ -40,11 +40,11 @@ int main(int argc, char **argv)    master = thread_master_create (); -  zlog_default = openzlog ("grammar_sandbox", ZLOG_NONE, 0, +  openzlog ("grammar_sandbox", "NONE", 0,                             LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); -  zlog_set_level (NULL, ZLOG_DEST_STDOUT, LOG_DEBUG); -  zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); +  zlog_set_level (ZLOG_DEST_SYSLOG, ZLOG_DISABLED); +  zlog_set_level (ZLOG_DEST_STDOUT, LOG_DEBUG); +  zlog_set_level (ZLOG_DEST_MONITOR, ZLOG_DISABLED);    /* Library inits. */    cmd_init (1); @@ -987,7 +987,7 @@ connected_log (struct connected *connected, char *str)        strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),  	       BUFSIZ - strlen(logbuf));      } -  zlog (NULL, LOG_INFO, "%s", logbuf); +  zlog_info("%s", logbuf);  }  /* Print if_addr structure. */ @@ -1007,7 +1007,7 @@ nbr_connected_log (struct nbr_connected *connected, char *str)  	    inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),  	    p->prefixlen); -  zlog (NULL, LOG_INFO, "%s", logbuf); +  zlog_info("%s", logbuf);  }  /* If two connected address has same prefix return 1. */ diff --git a/lib/libfrr.c b/lib/libfrr.c new file mode 100644 index 0000000000..f9ac3158a3 --- /dev/null +++ b/lib/libfrr.c @@ -0,0 +1,392 @@ +/* + * libfrr overall management functions + * + * Copyright (C) 2016  David Lamparter for NetDEF, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA  02111-1307  USA + */ + +#include <zebra.h> + +#include "libfrr.h" +#include "getopt.h" +#include "vty.h" +#include "command.h" +#include "version.h" +#include "memory_vty.h" +#include "zclient.h" + +const char frr_sysconfdir[] = SYSCONFDIR; +const char frr_vtydir[] = DAEMON_VTY_DIR; + +char config_default[256]; +static char pidfile_default[256]; +static char vtypath_default[256]; + +static char comb_optstr[256]; +static struct option comb_lo[64]; +static struct option *comb_next_lo = &comb_lo[0]; +static char comb_helpstr[4096]; + +struct optspec { +	const char *optstr; +	const char *helpstr; +	const struct option *longopts; +}; + +static void opt_extend(const struct optspec *os) +{ +	const struct option *lo; + +	strcat(comb_optstr, os->optstr); +	strcat(comb_helpstr, os->helpstr); +	for (lo = os->longopts; lo->name; lo++) +		memcpy(comb_next_lo++, lo, sizeof(*lo)); +} + + +#define OPTION_VTYSOCK 1000 + +static const struct option lo_always[] = { +	{ "help",        no_argument,       NULL, 'h' }, +	{ "version",     no_argument,       NULL, 'v' }, +	{ "daemon",      no_argument,       NULL, 'd' }, +	{ "vty_socket",  required_argument, NULL, OPTION_VTYSOCK }, +	{ NULL } +}; +static const struct optspec os_always = { +	"hvdi:", +	"  -h, --help         Display this help and exit\n" +	"  -v, --version      Print program version\n" +	"  -d, --daemon       Runs in daemon mode\n" +	"      --vty_socket   Override vty socket path\n", +	lo_always +}; + + +static const struct option lo_cfg_pid_dry[] = { +	{ "pid_file",    required_argument, NULL, 'i' }, +	{ "config_file", required_argument, NULL, 'f' }, +	{ "dryrun",      no_argument,       NULL, 'C' }, +	{ NULL } +}; +static const struct optspec os_cfg_pid_dry = { +	"f:i:C", +	"  -f, --config_file  Set configuration file name\n" +	"  -i, --pid_file     Set process identifier file name\n" +	"  -C, --dryrun       Check configuration for validity and exit\n", +	lo_cfg_pid_dry +}; + + +static const struct option lo_zclient[] = { +	{ "socket", required_argument, NULL, 'z' }, +	{ NULL } +}; +static const struct optspec os_zclient = { +	"z:", +	"  -z, --socket       Set path of zebra socket\n", +	lo_zclient +}; + + +static const struct option lo_vty[] = { +	{ "vty_addr",   required_argument, NULL, 'A'}, +	{ "vty_port",   required_argument, NULL, 'P'}, +	{ NULL } +}; +static const struct optspec os_vty = { +	"A:P:", +	"  -A, --vty_addr     Set vty's bind address\n" +	"  -P, --vty_port     Set vty's port number\n", +	lo_vty +}; + + +static const struct option lo_user[] = { +	{ "user",  required_argument, NULL, 'u'}, +	{ "group", required_argument, NULL, 'g'}, +	{ NULL } +}; +static const struct optspec os_user = { +	"u:g:", +	"  -u, --user         User to run as\n" +	"  -g, --group        Group to run as\n", +	lo_user +}; + + +static struct frr_daemon_info *di = NULL; + +void frr_preinit(struct frr_daemon_info *daemon, int argc, char **argv) +{ +	di = daemon; + +	/* basename(), opencoded. */ +	char *p = strrchr(argv[0], '/'); +	di->progname = p ? p + 1 : argv[0]; + +	umask(0027); + +	opt_extend(&os_always); +	if (!(di->flags & FRR_NO_CFG_PID_DRY)) +		opt_extend(&os_cfg_pid_dry); +	if (!(di->flags & FRR_NO_PRIVSEP)) +		opt_extend(&os_user); +	if (!(di->flags & FRR_NO_ZCLIENT)) +		opt_extend(&os_zclient); +	if (!(di->flags & FRR_NO_TCPVTY)) +		opt_extend(&os_vty); + +	snprintf(config_default, sizeof(config_default), "%s/%s.conf", +			frr_sysconfdir, di->name); +	snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s.pid", +			frr_vtydir, di->name); +} + +void frr_opt_add(const char *optstr, const struct option *longopts, +		const char *helpstr) +{ +	const struct optspec main_opts = { optstr, helpstr, longopts }; +	opt_extend(&main_opts); +} + +void frr_help_exit(int status) +{ +	FILE *target = status ? stderr : stdout; + +	if (status != 0) +		fprintf(stderr, "Invalid options.\n\n"); + +	if (di->printhelp) +		di->printhelp(target); +	else +		fprintf(target, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s", +				di->progname, +				di->proghelp, +				di->copyright ? "\n\n" : "", +				di->copyright ? di->copyright : "", +				comb_helpstr); +	fprintf(target, "\nReport bugs to %s\n", FRR_BUG_ADDRESS); +	exit(status); +} + +static int errors = 0; + +static int frr_opt(int opt) +{ +	static int vty_port_set = 0; +	static int vty_addr_set = 0; +	char *err; + +	switch (opt) { +	case 'h': +		frr_help_exit(0); +		break; +	case 'v': +		print_version(di->progname); +		exit(0); +		break; +	case 'd': +		di->daemon_mode = 1; +		break; +	case 'i': +		if (di->flags & FRR_NO_CFG_PID_DRY) +			return 1; +		di->pid_file = optarg; +		break; +	case 'f': +		if (di->flags & FRR_NO_CFG_PID_DRY) +			return 1; +		di->config_file = optarg; +		break; +	case 'C': +		if (di->flags & FRR_NO_CFG_PID_DRY) +			return 1; +		di->dryrun = 1; +		break; +	case 'z': +		if (di->flags & FRR_NO_ZCLIENT) +			return 1; +		zclient_serv_path_set(optarg); +		break; +	case 'A': +		if (di->flags & FRR_NO_TCPVTY) +			return 1; +		if (vty_addr_set) { +			fprintf(stderr, "-A option specified more than once!\n"); +			errors++; +			break; +		} +		vty_addr_set = 1; +		di->vty_addr = optarg; +		break; +	case 'P': +		if (di->flags & FRR_NO_TCPVTY) +			return 1; +		if (vty_port_set) { +			fprintf(stderr, "-P option specified more than once!\n"); +			errors++; +			break; +		} +		vty_port_set = 1; +		di->vty_port = strtoul(optarg, &err, 0); +		if (*err || !*optarg) { +			fprintf(stderr, "invalid port number \"%s\" for -P option\n", +					optarg); +			errors++; +			break; +		} +		break; +	case OPTION_VTYSOCK: +		if (di->vty_sock_path) { +			fprintf(stderr, "--vty_socket option specified more than once!\n"); +			errors++; +			break; +		} +		di->vty_sock_path = optarg; +		break; +	case 'u': +		if (di->flags & FRR_NO_PRIVSEP) +			return 1; +		di->privs->user = optarg; +		break; +	case 'g': +		if (di->flags & FRR_NO_PRIVSEP) +			return 1; +		di->privs->group = optarg; +		break; +	default: +		return 1; +	} +	return 0; +} + +int frr_getopt(int argc, char * const argv[], int *longindex) +{ +	int opt; +	int lidx; + +	comb_next_lo->name = NULL; + +	do { +		opt = getopt_long(argc, argv, comb_optstr, comb_lo, &lidx); +		if (frr_opt(opt)) +			break; +	} while (opt != -1); + +	if (opt == -1 && errors) +		frr_help_exit(1); +	if (longindex) +		*longindex = lidx; +	return opt; +} + +struct thread_master *frr_init(void) +{ +	struct thread_master *master; + +	srandom(time(NULL)); + +	openzlog (di->progname, di->logname, di->instance, +			LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); +#if defined(HAVE_CUMULUS) +	zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); +#endif + +	zprivs_init(di->privs); + +	master = thread_master_create(); +	signal_init(master, di->n_signals, di->signals); + +	if (di->flags & FRR_LIMITED_CLI) +		cmd_init(-1); +	else +		cmd_init(1); +	vty_init(master); +	memory_init(); + +	return master; +} + +void frr_config_fork(void) +{ +	if (di->instance) { +		snprintf(config_default, sizeof(config_default), "%s/%s-%d.conf", +				frr_sysconfdir, di->name, di->instance); +		snprintf(pidfile_default, sizeof(pidfile_default), "%s/%s-%d.pid", +				frr_vtydir, di->name, di->instance); +	} + +	vty_read_config(di->config_file, config_default); + +	/* Don't start execution if we are in dry-run mode */ +	if (di->dryrun) +		exit(0); + +	/* Daemonize. */ +	if (di->daemon_mode && daemon (0, 0) < 0) { +		zlog_err("Zebra daemon failed: %s", strerror(errno)); +		exit(1); +	} + +	if (!di->pid_file) +		di->pid_file = pidfile_default; +	pid_output (di->pid_file); +} + +void frr_vty_serv(void) +{ +	/* allow explicit override of vty_path in the future  +	 * (not currently set anywhere) */ +	if (!di->vty_path) { +		const char *dir; +		dir = di->vty_sock_path ? di->vty_sock_path : frr_vtydir; + +		if (di->instance) +			snprintf(vtypath_default, sizeof(vtypath_default), +					"%s/%s-%d.vty", +					dir, di->name, di->instance); +		else +			snprintf(vtypath_default, sizeof(vtypath_default), +					"%s/%s.vty", dir, di->name); + +		di->vty_path = vtypath_default; +	} + +	vty_serv_sock(di->vty_addr, di->vty_port, di->vty_path); +} + +void frr_run(struct thread_master *master) +{ +	char instanceinfo[64] = ""; + +	frr_vty_serv(); + +	if (di->instance) +		snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ", +				di->instance); + +	zlog_notice("%s %s starting: %svty@%d%s", +			di->name, +			FRR_VERSION, +			instanceinfo, +			di->vty_port, +			di->startinfo); + +	struct thread thread; +	while (thread_fetch(master, &thread)) +		thread_call(&thread); +} diff --git a/lib/libfrr.h b/lib/libfrr.h new file mode 100644 index 0000000000..d37f406f5b --- /dev/null +++ b/lib/libfrr.h @@ -0,0 +1,100 @@ +/* + * libfrr overall management functions + * + * Copyright (C) 2016  David Lamparter for NetDEF, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA  02111-1307  USA + */ + +#ifndef _ZEBRA_FRR_H +#define _ZEBRA_FRR_H + +#include "sigevent.h" +#include "privs.h" +#include "thread.h" +#include "log.h" +#include "getopt.h" + +#define FRR_NO_PRIVSEP		(1 << 0) +#define FRR_NO_TCPVTY		(1 << 1) +#define FRR_LIMITED_CLI		(1 << 2) +#define FRR_NO_CFG_PID_DRY		(1 << 3) +#define FRR_NO_ZCLIENT		(1 << 4) + +struct frr_daemon_info { +	unsigned flags; + +	const char *progname; +	const char *name; +	const char *logname; +	unsigned short instance; + +	char *vty_addr; +	int vty_port; +	char *vty_sock_path; +	bool dryrun; +	bool daemon_mode; +	const char *config_file; +	const char *pid_file; +	const char *vty_path; + +	const char *proghelp; +	void (*printhelp)(FILE *target); +	const char *copyright; +	char startinfo[128]; + +	struct quagga_signal_t *signals; +	size_t n_signals; + +	struct zebra_privs_t *privs; +}; + +/* execname is the daemon's executable (and pidfile and configfile) name, + * i.e. "zebra" or "bgpd" + * constname is the daemons source-level name, primarily for the logging ID, + * i.e. "ZEBRA" or "BGP" + * + * note that this macro is also a latch-on point for other changes (e.g. + * upcoming plugin support) that need to place some per-daemon things.  Each + * daemon should have one of these. + */ +#define FRR_DAEMON_INFO(execname, constname, ...) \ +	static struct frr_daemon_info execname ##_di = { \ +		.name = # execname, \ +		.logname = # constname, \ +		__VA_ARGS__ \ +	}; + +extern void frr_preinit(struct frr_daemon_info *daemon, +		int argc, char **argv); +extern void frr_opt_add(const char *optstr, +		const struct option *longopts, const char *helpstr); +extern int frr_getopt(int argc, char * const argv[], int *longindex); +extern void frr_help_exit(int status); + +extern struct thread_master *frr_init(void); + +extern void frr_config_fork(void); + +extern void frr_vty_serv(void); + +/* note: contains call to frr_vty_serv() */ +extern void frr_run(struct thread_master *master); + +extern char config_default[256]; +extern const char frr_sysconfdir[]; +extern const char frr_vtydir[]; + +#endif /* _ZEBRA_FRR_H */ @@ -26,6 +26,7 @@  #include "zclient.h"  #include "log.h" +#include "log_int.h"  #include "memory.h"  #include "command.h"  #ifndef SUNOS_5 @@ -42,29 +43,6 @@ static int logfile_fd = -1;	/* Used in signal handler. */  struct zlog *zlog_default = NULL; -/* - * This must be kept in the same order as the - * zlog_proto_t enum - */ -const char *zlog_proto_names[] =  -{ -  "NONE", -  "DEFAULT", -  "ZEBRA", -  "RIP", -  "BGP", -  "OSPF", -  "RIPNG", -  "OSPF6", -  "LDP", -  "ISIS", -  "PIM", -  "NHRP", -  "RFP", -  "WATCHFRR", -  NULL, -}; -  const char *zlog_priority[] =  {    "emergencies", @@ -185,16 +163,13 @@ time_print(FILE *fp, struct timestamp_control *ctl)  /* va_list version of zlog. */  void -vzlog (struct zlog *zl, int priority, const char *format, va_list args) +vzlog (int priority, const char *format, va_list args)  {    char proto_str[32];    int original_errno = errno;    struct timestamp_control tsctl;    tsctl.already_rendered = 0; - -  /* If zlog is not specified, use default one. */ -  if (zl == NULL) -    zl = zlog_default; +  struct zlog *zl = zlog_default;    /* When zlog_default is also NULL, use stderr for logging. */    if (zl == NULL) @@ -222,9 +197,9 @@ vzlog (struct zlog *zl, int priority, const char *format, va_list args)      }    if (zl->instance) -   sprintf (proto_str, "%s[%d]: ", zlog_proto_names[zl->protocol], zl->instance); +   sprintf (proto_str, "%s[%d]: ", zl->protoname, zl->instance);    else -   sprintf (proto_str, "%s: ", zlog_proto_names[zl->protocol]); +   sprintf (proto_str, "%s: ", zl->protoname);    /* File output. */    if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) @@ -265,11 +240,9 @@ vzlog (struct zlog *zl, int priority, const char *format, va_list args)  }  int  -vzlog_test (struct zlog *zl, int priority) +vzlog_test (int priority)  { -  /* If zlog is not specified, use default one. */ -  if (zl == NULL) -    zl = zlog_default; +  struct zlog *zl = zlog_default;    /* When zlog_default is also NULL, use stderr for logging. */    if (zl == NULL) @@ -456,7 +429,7 @@ zlog_signal(int signo, const char *action    time(&now);    if (zlog_default)      { -      s = str_append(LOC,zlog_proto_names[zlog_default->protocol]); +      s = str_append(LOC,zlog_default->protoname);        *s++ = ':';        *s++ = ' ';        msgstart = s; @@ -639,7 +612,7 @@ void  zlog_backtrace(int priority)  {  #ifndef HAVE_GLIBC_BACKTRACE -  zlog(NULL, priority, "No backtrace available on this platform."); +  zlog(priority, "No backtrace available on this platform.");  #else    void *array[20];    int size, i; @@ -653,29 +626,29 @@ zlog_backtrace(int priority)  	       size, (unsigned long)(array_size(array)));        return;      } -  zlog(NULL, priority, "Backtrace for %d stack frames:", size); +  zlog(priority, "Backtrace for %d stack frames:", size);    if (!(strings = backtrace_symbols(array, size)))      {        zlog_err("Cannot get backtrace symbols (out of memory?)");        for (i = 0; i < size; i++) -	zlog(NULL, priority, "[bt %d] %p",i,array[i]); +	zlog(priority, "[bt %d] %p",i,array[i]);      }    else      {        for (i = 0; i < size; i++) -	zlog(NULL, priority, "[bt %d] %s",i,strings[i]); +	zlog(priority, "[bt %d] %s",i,strings[i]);        free(strings);      }  #endif /* HAVE_GLIBC_BACKTRACE */  }  void -zlog (struct zlog *zl, int priority, const char *format, ...) +zlog (int priority, const char *format, ...)  {    va_list args;    va_start(args, format); -  vzlog (zl, priority, format, args); +  vzlog (priority, format, args);    va_end (args);  } @@ -685,7 +658,7 @@ FUNCNAME(const char *format, ...) \  { \    va_list args; \    va_start(args, format); \ -  vzlog (NULL, PRIORITY, format, args); \ +  vzlog (PRIORITY, format, args); \    va_end(args); \  } @@ -704,11 +677,11 @@ ZLOG_FUNC(zlog_debug, LOG_DEBUG)  void zlog_thread_info (int log_level)  {    if (thread_current) -    zlog(NULL, log_level, "Current thread function %s, scheduled from " +    zlog(log_level, "Current thread function %s, scheduled from "  	 "file %s, line %u", thread_current->funcname,  	 thread_current->schedfrom, thread_current->schedfrom_line);    else -    zlog(NULL, log_level, "Current thread not known/applicable"); +    zlog(log_level, "Current thread not known/applicable");  }  void @@ -720,7 +693,7 @@ _zlog_assert_failed (const char *assertion, const char *file,        ((logfile_fd = open_crashlog()) >= 0) &&        ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))      zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR; -  zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s", +  zlog(LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",         assertion,file,line,(function ? function : "?"));    zlog_backtrace(LOG_CRIT);    zlog_thread_info(LOG_CRIT); @@ -738,8 +711,8 @@ memory_oom (size_t size, const char *name)  }  /* Open log stream */ -struct zlog * -openzlog (const char *progname, zlog_proto_t protocol, u_short instance, +void +openzlog (const char *progname, const char *protoname, u_short instance,  	  int syslog_flags, int syslog_facility)  {    struct zlog *zl; @@ -748,7 +721,7 @@ openzlog (const char *progname, zlog_proto_t protocol, u_short instance,    zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));    zl->ident = progname; -  zl->protocol = protocol; +  zl->protoname = protoname;    zl->instance = instance;    zl->facility = syslog_facility;    zl->syslog_options = syslog_flags; @@ -760,13 +733,14 @@ openzlog (const char *progname, zlog_proto_t protocol, u_short instance,    zl->default_lvl = LOG_DEBUG;    openlog (progname, syslog_flags, zl->facility); -   -  return zl; +  zlog_default = zl;  }  void -closezlog (struct zlog *zl) +closezlog (void)  { +  struct zlog *zl = zlog_default; +    closelog();    if (zl->fp != NULL) @@ -776,30 +750,31 @@ closezlog (struct zlog *zl)      XFREE(MTYPE_ZLOG, zl->filename);    XFREE (MTYPE_ZLOG, zl); +  zlog_default = NULL; +} + +const char * +zlog_protoname (void) +{ +  return zlog_default ? zlog_default->protoname : "NONE";  }  /* Called from command.c. */  void -zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level) +zlog_set_level (zlog_dest_t dest, int log_level)  { -  if (zl == NULL) -    zl = zlog_default; - -  zl->maxlvl[dest] = log_level; +  zlog_default->maxlvl[dest] = log_level;  }  int -zlog_set_file (struct zlog *zl, const char *filename, int log_level) +zlog_set_file (const char *filename, int log_level)  { +  struct zlog *zl = zlog_default;    FILE *fp;    mode_t oldumask;    /* There is opend file.  */ -  zlog_reset_file (zl); - -  /* Set default zl. */ -  if (zl == NULL) -    zl = zlog_default; +  zlog_reset_file ();    /* Open file. */    oldumask = umask (0777 & ~LOGFILE_MASK); @@ -819,10 +794,9 @@ zlog_set_file (struct zlog *zl, const char *filename, int log_level)  /* Reset opend file. */  int -zlog_reset_file (struct zlog *zl) +zlog_reset_file (void)  { -  if (zl == NULL) -    zl = zlog_default; +  struct zlog *zl = zlog_default;    if (zl->fp)      fclose (zl->fp); @@ -839,13 +813,11 @@ zlog_reset_file (struct zlog *zl)  /* Reopen log file. */  int -zlog_rotate (struct zlog *zl) +zlog_rotate (void)  { +  struct zlog *zl = zlog_default;    int level; -  if (zl == NULL) -    zl = zlog_default; -    if (zl->fp)      fclose (zl->fp);    zl->fp = NULL; @@ -42,28 +42,6 @@   * please use LOG_ERR instead.   */ -/* - * This must be kept in the same order as - * zlog_proto_names[] - */ -typedef enum  -{ -  ZLOG_NONE, -  ZLOG_DEFAULT, -  ZLOG_ZEBRA, -  ZLOG_RIP, -  ZLOG_BGP, -  ZLOG_OSPF, -  ZLOG_RIPNG, -  ZLOG_OSPF6, -  ZLOG_LDP, -  ZLOG_ISIS, -  ZLOG_PIM, -  ZLOG_NHRP, -  ZLOG_RFP, -  ZLOG_WATCHFRR, -} zlog_proto_t; -  /* If maxlvl is set to ZLOG_DISABLED, then no messages will be sent     to that logging destination. */  #define ZLOG_DISABLED	(LOG_EMERG-1) @@ -77,23 +55,6 @@ typedef enum  } zlog_dest_t;  #define ZLOG_NUM_DESTS		(ZLOG_DEST_FILE+1) -struct zlog  -{ -  const char *ident;	/* daemon name (first arg to openlog) */ -  zlog_proto_t protocol; -  u_short      instance; -  int maxlvl[ZLOG_NUM_DESTS];	/* maximum priority to send to associated -  				   logging destination */ -  int default_lvl;	/* maxlvl to use if none is specified */ -  FILE *fp; -  char *filename; -  int facility;		/* as per syslog facility */ -  int record_priority;	/* should messages logged through stdio include the -  			   priority of the message? */ -  int syslog_options;	/* 2nd arg to openlog */ -  int timestamp_precision;	/* # of digits of subsecond precision */ -}; -  /* Message structure. */  struct message  { @@ -101,15 +62,14 @@ struct message    const char *str;  }; -/* Default logging strucutre. */ -extern struct zlog *zlog_default; -  /* Open zlog function */ -extern struct zlog *openzlog (const char *progname, zlog_proto_t protocol, -		              u_short instance, int syslog_options, int syslog_facility); +extern void openzlog (const char *progname, const char *protoname, +                      u_short instance, int syslog_options, int syslog_facility);  /* Close zlog function. */ -extern void closezlog (struct zlog *zl); +extern void closezlog (void); + +extern const char *zlog_protoname (void);  /* GCC have printf type attribute check.  */  #ifdef __GNUC__ @@ -118,35 +78,28 @@ extern void closezlog (struct zlog *zl);  #define PRINTF_ATTRIBUTE(a,b)  #endif /* __GNUC__ */ -/* Generic function for zlog. */ -extern void zlog (struct zlog *zl, int priority, const char *format, ...) -  PRINTF_ATTRIBUTE(3, 4); -  /* Handy zlog functions. */ -extern void vzlog (struct zlog *zl, int priority, const char *format, va_list args);  extern void zlog_err (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);  extern void zlog_warn (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);  extern void zlog_info (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);  extern void zlog_notice (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);  extern void zlog_debug (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); -extern void vzlog (struct zlog *, int , const char *, va_list ); -  extern void zlog_thread_info (int log_level);  /* Set logging level for the given destination.  If the log_level     argument is ZLOG_DISABLED, then the destination is disabled.     This function should not be used for file logging (use zlog_set_file     or zlog_reset_file instead). */ -extern void zlog_set_level (struct zlog *zl, zlog_dest_t, int log_level); +extern void zlog_set_level (zlog_dest_t, int log_level);  /* Set logging to the given filename at the specified level. */ -extern int zlog_set_file (struct zlog *zl, const char *filename, int log_level); +extern int zlog_set_file (const char *filename, int log_level);  /* Disable file logging. */ -extern int zlog_reset_file (struct zlog *zl); +extern int zlog_reset_file (void);  /* Rotate log. */ -extern int zlog_rotate (struct zlog *); +extern int zlog_rotate (void);  /* For hackey message lookup and check */  #define LOOKUP_DEF(x, y, def) mes_lookup(x, x ## _max, y, def, #x) @@ -157,9 +110,6 @@ extern const char *mes_lookup (const struct message *meslist,                                 int max, int index,                                 const char *no_item, const char *mesname); -extern const char *zlog_priority[]; -extern const char *zlog_proto_names[]; -  /* Safe version of strerror -- never returns NULL. */  extern const char *safe_strerror(int errnum); @@ -193,8 +143,7 @@ extern void zlog_hexdump(const void *mem, unsigned int len);  extern const char *zlog_sanitize(char *buf, size_t bufsz, const void *in, size_t inlen); -extern int  -vzlog_test (struct zlog *zl, int priority); +extern int vzlog_test (int priority);  /* structure useful for avoiding repeated rendering of the same timestamp */  struct timestamp_control { diff --git a/lib/log_int.h b/lib/log_int.h new file mode 100644 index 0000000000..c21d723ac6 --- /dev/null +++ b/lib/log_int.h @@ -0,0 +1,57 @@ +/* + * Zebra logging funcions. + * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro + * + * This file is part of GNU Zebra. + * + * GNU Zebra is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * GNU Zebra is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Zebra; see the file COPYING.  If not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +#ifndef _ZEBRA_LOG_PRIVATE_H +#define _ZEBRA_LOG_PRIVATE_H + +#include "log.h" + +struct zlog +{ +  const char *ident;	/* daemon name (first arg to openlog) */ +  const char *protoname; +  u_short instance; +  int maxlvl[ZLOG_NUM_DESTS];   /* maximum priority to send to associated +                                   logging destination */ +  int default_lvl;      /* maxlvl to use if none is specified */ +  FILE *fp; +  char *filename; +  int facility;         /* as per syslog facility */ +  int record_priority;  /* should messages logged through stdio include the +                           priority of the message? */ +  int syslog_options;   /* 2nd arg to openlog */ +  int timestamp_precision;      /* # of digits of subsecond precision */ +}; + +/* Default logging strucutre. */ +extern struct zlog *zlog_default; + +extern const char *zlog_priority[]; + +/* Generic function for zlog. */ +extern void vzlog (int priority, const char *format, va_list args); +extern void zlog (int priority, const char *format, ...) +  PRINTF_ATTRIBUTE(2, 3); + +#endif /* _ZEBRA_LOG_PRIVATE_H */ + + diff --git a/lib/plist.c b/lib/plist.c index 3ed5c8fc5c..3714969696 100644 --- a/lib/plist.c +++ b/lib/plist.c @@ -1175,8 +1175,7 @@ vty_show_prefix_entry (struct vty *vty, afi_t afi, struct prefix_list *plist,    struct prefix_list_entry *pentry;    /* Print the name of the protocol */ -  if (zlog_default) -      vty_out (vty, "%s: ", zlog_proto_names[zlog_default->protocol]); +  vty_out(vty, "%s: ", zlog_protoname());    if (dtype == normal_display)      { diff --git a/lib/prefix.c b/lib/prefix.c index dec22a44a3..0cc759bb7c 100644 --- a/lib/prefix.c +++ b/lib/prefix.c @@ -313,8 +313,7 @@ prefix_copy (struct prefix *dest, const struct prefix *src)      }    else      { -      zlog (NULL, LOG_ERR, "prefix_copy(): Unknown address family %d", -	      src->family); +      zlog_err("prefix_copy(): Unknown address family %d", src->family);        assert (0);      }  } diff --git a/lib/routemap.c b/lib/routemap.c index 39d9a5d375..1647ac3668 100644 --- a/lib/routemap.c +++ b/lib/routemap.c @@ -28,6 +28,7 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  #include "routemap.h"  #include "command.h"  #include "log.h" +#include "log_int.h"  #include "hash.h"  DEFINE_MTYPE_STATIC(LIB, ROUTE_MAP,          "Route map") @@ -991,7 +992,7 @@ vty_show_route_map_entry (struct vty *vty, struct route_map *map)    /* Print the name of the protocol */    if (zlog_default)    { -    vty_out (vty, "%s", zlog_proto_names[zlog_default->protocol]); +    vty_out (vty, "%s", zlog_protoname());      if (zlog_default->instance)        vty_out (vty, " %d", zlog_default->instance);    } @@ -1051,8 +1052,7 @@ vty_show_route_map (struct vty *vty, const char *name)          }        else          { -          if (zlog_default) -            vty_out (vty, "%s", zlog_proto_names[zlog_default->protocol]); +          vty_out (vty, "%s", zlog_protoname());            if (zlog_default && zlog_default->instance)              vty_out (vty, " %d", zlog_default->instance);            vty_out (vty, ": 'route-map %s' not found%s", name, VTY_NEWLINE); @@ -1614,9 +1614,8 @@ route_map_apply (struct route_map *map, struct prefix *prefix,    if (recursion > RMAP_RECURSION_LIMIT)      { -      zlog (NULL, LOG_WARNING, -            "route-map recursion limit (%d) reached, discarding route", -            RMAP_RECURSION_LIMIT); +      zlog_warn("route-map recursion limit (%d) reached, discarding route", +                RMAP_RECURSION_LIMIT);        recursion = 0;        return RMAP_DENYMATCH;      } diff --git a/lib/sockopt.c b/lib/sockopt.c index 91b0602b3a..2a9f907cb3 100644 --- a/lib/sockopt.c +++ b/lib/sockopt.c @@ -29,29 +29,6 @@  #include "sockopt.h"  #include "sockunion.h" -/* Replace the path of given defaultpath with newpath, but keep filename */ -void -set_socket_path (char *path, const char *defaultpath, char *newpath, int maxsize) -{ -  const char *sock_name; - -  sock_name = strrchr(defaultpath, '/'); -  if (sock_name) -    /* skip '/' */ -    sock_name++; -  else -    /* -     * VTYSH_PATH configured as relative path -     * during config? Should really never happen for -     * sensible config -     */ -    sock_name = defaultpath; - -  strlcpy (path, newpath, maxsize); -  strlcat (path, "/", maxsize); -  strlcat (path, sock_name, maxsize); -} -  void  setsockopt_so_recvbuf (int sock, int size)  { diff --git a/lib/sockopt.h b/lib/sockopt.h index d5724ce60f..1b7be1e49f 100644 --- a/lib/sockopt.h +++ b/lib/sockopt.h @@ -24,10 +24,6 @@  #include "sockunion.h" -/* Override (vty) socket paths, but keep the filename */ -extern void set_socket_path (char *path, const char *defaultpath, -                             char *newpath, int maxsize); -  extern void setsockopt_so_recvbuf (int sock, int size);  extern void setsockopt_so_sendbuf (const int sock, int size);  extern int getsockopt_so_sendbuf (const int sock); diff --git a/lib/sockunion.c b/lib/sockunion.c index 5b508d1bf8..9ba2ce82f6 100644 --- a/lib/sockunion.c +++ b/lib/sockunion.c @@ -148,8 +148,8 @@ sockunion_socket (const union sockunion *su)    if (sock < 0)      {        char buf[SU_ADDRSTRLEN]; -      zlog (NULL, LOG_WARNING, "Can't make socket for %s : %s", -            sockunion_log (su, buf, SU_ADDRSTRLEN), safe_strerror (errno)); +      zlog_warn("Can't make socket for %s : %s", +                sockunion_log(su, buf, SU_ADDRSTRLEN), safe_strerror(errno));        return -1;      } @@ -264,7 +264,7 @@ sockunion_stream_socket (union sockunion *su)    sock = socket (su->sa.sa_family, SOCK_STREAM, 0);    if (sock < 0) -    zlog (NULL, LOG_WARNING, "can't make socket sockunion_stream_socket"); +    zlog_warn("can't make socket sockunion_stream_socket");    return sock;  } @@ -308,8 +308,8 @@ sockunion_bind (int sock, union sockunion *su, unsigned short port,    if (ret < 0)      {        char buf[SU_ADDRSTRLEN]; -      zlog (NULL, LOG_WARNING, "can't bind socket for %s : %s", -            sockunion_log (su, buf, SU_ADDRSTRLEN), safe_strerror (errno)); +      zlog_warn("can't bind socket for %s : %s", +                sockunion_log(su, buf, SU_ADDRSTRLEN), safe_strerror(errno));      }    return ret; @@ -325,7 +325,7 @@ sockopt_reuseaddr (int sock)  		    (void *) &on, sizeof (on));    if (ret < 0)      { -      zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEADDR to socket %d", sock); +      zlog_warn("can't set sockopt SO_REUSEADDR to socket %d", sock);        return -1;      }    return 0; @@ -342,7 +342,7 @@ sockopt_reuseport (int sock)  		    (void *) &on, sizeof (on));    if (ret < 0)      { -      zlog (NULL, LOG_WARNING, "can't set sockopt SO_REUSEPORT to socket %d", sock); +      zlog_warn("can't set sockopt SO_REUSEPORT to socket %d", sock);        return -1;      }    return 0; @@ -367,7 +367,7 @@ sockopt_ttl (int family, int sock, int ttl)  			(void *) &ttl, sizeof (int));        if (ret < 0)  	{ -	  zlog (NULL, LOG_WARNING, "can't set sockopt IP_TTL %d to socket %d", ttl, sock); +	  zlog_warn("can't set sockopt IP_TTL %d to socket %d", ttl, sock);  	  return -1;  	}        return 0; @@ -379,8 +379,8 @@ sockopt_ttl (int family, int sock, int ttl)  			(void *) &ttl, sizeof (int));        if (ret < 0)  	{ -	  zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_UNICAST_HOPS %d to socket %d", -		    ttl, sock); +	  zlog_warn("can't set sockopt IPV6_UNICAST_HOPS %d to socket %d", +                    ttl, sock);  	  return -1;  	}        return 0; @@ -425,9 +425,8 @@ sockopt_minttl (int family, int sock, int minttl)      {        int ret = setsockopt (sock, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl));        if (ret < 0) -	  zlog (NULL, LOG_WARNING, -		"can't set sockopt IP_MINTTL to %d on socket %d: %s", -		minttl, sock, safe_strerror (errno)); +	  zlog_warn("can't set sockopt IP_MINTTL to %d on socket %d: %s", +                    minttl, sock, safe_strerror(errno));        return ret;      }  #endif /* IP_MINTTL */ @@ -436,9 +435,8 @@ sockopt_minttl (int family, int sock, int minttl)      {        int ret = setsockopt (sock, IPPROTO_IPV6, IPV6_MINHOPCOUNT, &minttl, sizeof(minttl));        if (ret < 0) -	  zlog (NULL, LOG_WARNING, -		"can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s", -		minttl, sock, safe_strerror (errno)); +	  zlog_warn("can't set sockopt IPV6_MINHOPCOUNT to %d on socket %d: %s", +                    minttl, sock, safe_strerror(errno));        return ret;      }  #endif @@ -459,8 +457,8 @@ sockopt_v6only (int family, int sock)  			(void *) &on, sizeof (int));        if (ret < 0)  	{ -	  zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY " -		    "to socket %d", sock); +	  zlog_warn("can't set sockopt IPV6_V6ONLY " "to socket %d", +                    sock);  	  return -1;  	}        return 0; @@ -626,8 +624,7 @@ sockunion_getpeername (int fd)    ret = getpeername (fd, (struct sockaddr *)&name, &len);    if (ret < 0)      { -      zlog (NULL, LOG_WARNING, "Can't get remote address and port: %s", -	    safe_strerror (errno)); +      zlog_warn("Can't get remote address and port: %s", safe_strerror(errno));        return NULL;      } diff --git a/lib/thread.c b/lib/thread.c index c1558a83e1..6138e79718 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -719,7 +719,8 @@ funcname_thread_add_read_write (int dir, struct thread_master *m,  #else    if (FD_ISSET (fd, fdset))      { -      zlog (NULL, LOG_WARNING, "There is already %s fd [%d]", (dir == THREAD_READ) ? "read" : "write", fd); +      zlog_warn ("There is already %s fd [%d]", +                 (dir == THREAD_READ) ? "read" : "write", fd);        return NULL;      } diff --git a/lib/thread.h b/lib/thread.h index 0489246ea6..34adcc4d09 100644 --- a/lib/thread.h +++ b/lib/thread.h @@ -246,11 +246,6 @@ extern void thread_cmd_init (void);  extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before,  					  unsigned long *cpu_time_elapsed); -/* Global variable containing a recent result from gettimeofday.  This can -   be used instead of calling gettimeofday if a recent value is sufficient. -   This is guaranteed to be refreshed before a thread is called. */ -extern struct timeval recent_time; -  /* only for use in logging functions! */  extern struct thread *thread_current; @@ -214,7 +214,7 @@ vty_time_print (struct vty *vty, int cr)    if (quagga_timestamp(0, buf, sizeof(buf)) == 0)      { -      zlog (NULL, LOG_INFO, "quagga_timestamp error"); +      zlog_info("quagga_timestamp error");        return;      }    if (cr) @@ -437,7 +437,7 @@ vty_command (struct vty *vty, char *buf)        snprintf(prompt_str, sizeof(prompt_str), cmd_prompt (vty->node), vty_str);        /* now log the command */ -      zlog(NULL, LOG_ERR, "%s%s", prompt_str, buf); +      zlog_err("%s%s", prompt_str, buf);      }    /* Split readline string up into the vector */    vline = cmd_make_strvec (buf); @@ -457,10 +457,7 @@ vty_command (struct vty *vty, char *buf)    ret = cmd_execute_command (vline, vty, NULL, 0);    /* Get the name of the protocol if any */ -  if (zlog_default) -      protocolname = zlog_proto_names[zlog_default->protocol]; -  else -      protocolname = zlog_proto_names[ZLOG_NONE]; +  protocolname = zlog_protoname();  #ifdef CONSUMED_TIME_CHECK      GETRUSAGE(&after); @@ -1856,8 +1853,8 @@ vty_accept (struct thread *thread)        if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&            (access_list_apply (acl, &p) == FILTER_DENY))          { -          zlog (NULL, LOG_INFO, "Vty connection refused from %s", -                sockunion2str (&su, buf, SU_ADDRSTRLEN)); +          zlog_info ("Vty connection refused from %s", +                     sockunion2str (&su, buf, SU_ADDRSTRLEN));            close (vty_sock);            /* continue accepting connections */ @@ -1873,8 +1870,8 @@ vty_accept (struct thread *thread)        if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&            (access_list_apply (acl, &p) == FILTER_DENY))          { -          zlog (NULL, LOG_INFO, "Vty connection refused from %s", -                sockunion2str (&su, buf, SU_ADDRSTRLEN)); +          zlog_info ("Vty connection refused from %s", +                     sockunion2str (&su, buf, SU_ADDRSTRLEN));            close (vty_sock);            /* continue accepting connections */ @@ -1888,11 +1885,11 @@ vty_accept (struct thread *thread)    ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY,                      (char *) &on, sizeof (on));    if (ret < 0) -    zlog (NULL, LOG_INFO, "can't set sockopt to vty_sock : %s", -          safe_strerror (errno)); +    zlog_info ("can't set sockopt to vty_sock : %s", +               safe_strerror (errno)); -  zlog (NULL, LOG_INFO, "Vty connection from %s", -        sockunion2str (&su, buf, SU_ADDRSTRLEN)); +  zlog_info ("Vty connection from %s", +             sockunion2str (&su, buf, SU_ADDRSTRLEN));    vty_create (vty_sock, &su); @@ -2354,7 +2351,7 @@ vty_read_file (FILE *confp)  }  static FILE * -vty_use_backup_config (char *fullpath) +vty_use_backup_config (const char *fullpath)  {    char *fullpath_sav, *fullpath_tmp;    FILE *ret = NULL; @@ -2413,12 +2410,12 @@ out_close_sav:  /* Read up configuration file from file_name. */  void -vty_read_config (char *config_file, +vty_read_config (const char *config_file,                   char *config_default_dir)  {    char cwd[MAXPATHLEN];    FILE *confp = NULL; -  char *fullpath; +  const char *fullpath;    char *tmp = NULL;    /* If -f flag specified. */ @@ -2518,7 +2515,7 @@ vty_read_config (char *config_file,  tmp_free_and_out:    if (tmp) -    XFREE (MTYPE_TMP, fullpath); +    XFREE (MTYPE_TMP, tmp);  }  /* Small utility function which output log to the VTY. */ @@ -324,7 +324,7 @@ extern void vty_reset (void);  extern struct vty *vty_new (void);  extern struct vty *vty_stdio (void (*atclose)(void));  extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); -extern void vty_read_config (char *, char *); +extern void vty_read_config (const char *, char *);  extern void vty_time_print (struct vty *, int);  extern void vty_serv_sock (const char *, unsigned short, const char *);  extern void vty_close (struct vty *); diff --git a/nhrpd/nhrp_main.c b/nhrpd/nhrp_main.c index dfb76a4081..2f6ee1c04f 100644 --- a/nhrpd/nhrp_main.c +++ b/nhrpd/nhrp_main.c @@ -19,6 +19,7 @@  #include "memory.h"  #include "memory_vty.h"  #include "command.h" +#include "libfrr.h"  #include "nhrpd.h"  #include "netlink.h" @@ -29,25 +30,9 @@ unsigned int debug_flags = 0;  struct thread_master *master;  struct timeval current_time; -static const char *pid_file = PATH_NHRPD_PID; -static char config_default[] = SYSCONFDIR NHRP_DEFAULT_CONFIG; -static char *config_file = NULL; -static char *vty_addr = NULL; -static int vty_port = NHRP_VTY_PORT; -static int do_daemonise = 0;  /* nhrpd options. */  struct option longopts[] = { -	{ "daemon",      no_argument,       NULL, 'd'}, -	{ "config_file", required_argument, NULL, 'f'}, -	{ "pid_file",    required_argument, NULL, 'i'}, -	{ "socket",      required_argument, NULL, 'z'}, -	{ "help",        no_argument,       NULL, 'h'}, -	{ "vty_addr",    required_argument, NULL, 'A'}, -	{ "vty_port",    required_argument, NULL, 'P'}, -	{ "user",        required_argument, NULL, 'u'}, -	{ "group",       required_argument, NULL, 'g'}, -	{ "version",     no_argument,       NULL, 'v'},  	{ 0 }  }; @@ -72,76 +57,19 @@ static struct zebra_privs_t nhrpd_privs = {  	.cap_num_p = ZEBRA_NUM_OF(_caps_p),  }; -static void usage(const char *progname, int status) -{ -	if (status != 0) -		fprintf(stderr, "Try `%s --help' for more information.\n", progname); -	else -		printf( -"Usage : %s [OPTION...]\n\ -Daemon which manages NHRP protocol.\n\n\ --d, --daemon       Runs in daemon mode\n\ --f, --config_file  Set configuration file name\n\ --i, --pid_file     Set process identifier file name\n\ --z, --socket       Set path of zebra socket\n\ --A, --vty_addr     Set vty's bind address\n\ --P, --vty_port     Set vty's port number\n\ --u, --user         User to run as\n\ --g, --group        Group to run as\n\ --v, --version      Print program version\n\ --h, --help         Display this help and exit\n\ -\n\ -Report bugs to %s\n", progname, FRR_BUG_ADDRESS); - -	exit(status); -} - -static void parse_arguments(const char *progname, int argc, char **argv) +static void parse_arguments(int argc, char **argv)  {  	int opt;  	while (1) { -		opt = getopt_long(argc, argv, "df:i:z:hA:P:u:g:v", longopts, 0); +		opt = frr_getopt(argc, argv, 0);  		if(opt < 0) break;  		switch (opt) {  		case 0:  			break; -		case 'd': -			do_daemonise = -1; -			break; -		case 'f': -			config_file = optarg; -			break; -		case 'i': -			pid_file = optarg; -			break; -		case 'z': -			zclient_serv_path_set(optarg); -			break; -		case 'A': -			vty_addr = optarg; -			break; -		case 'P': -			vty_port = atoi (optarg); -			if (vty_port <= 0 || vty_port > 0xffff) -				vty_port = NHRP_VTY_PORT; -			break; -		case 'u': -			nhrpd_privs.user = optarg; -			break; -		case 'g': -			nhrpd_privs.group = optarg; -			break; -		case 'v': -			print_version(progname); -			exit(0); -			break; -		case 'h': -			usage(progname, 0); -			break;  		default: -			usage(progname, 1); +			frr_help_exit(1);  			break;  		}  	} @@ -149,7 +77,7 @@ static void parse_arguments(const char *progname, int argc, char **argv)  static void nhrp_sigusr1(void)  { -	zlog_rotate(NULL); +	zlog_rotate();  }  static void nhrp_request_stop(void) @@ -169,11 +97,9 @@ static void nhrp_request_stop(void)  	/* signal_terminate(); */  	zprivs_terminate(&nhrpd_privs); -	debugf(NHRP_DEBUG_COMMON, "Remove pid file."); -	if (pid_file) unlink(pid_file);  	debugf(NHRP_DEBUG_COMMON, "Done."); -	closezlog(zlog_default); +	closezlog();  	exit(0);  } @@ -184,26 +110,26 @@ static struct quagga_signal_t sighandlers[] = {  	{ .signal = SIGTERM, .handler = &nhrp_request_stop, },  }; +FRR_DAEMON_INFO(nhrpd, NHRP, +	.vty_port = NHRP_VTY_PORT, + +	.proghelp = "Implementation of the NHRP routing protocol.", + +	.signals = sighandlers, +	.n_signals = array_size(sighandlers), + +	.privs = &nhrpd_privs, +) +  int main(int argc, char **argv)  { -	struct thread thread; -	const char *progname, *p; - -	/* Set umask before anything for security */ -	umask(0027); -	progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); -	zlog_default = openzlog(progname, ZLOG_NHRP, 0, LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -	zlog_set_level(NULL, ZLOG_DEST_STDOUT, LOG_WARNING); +	frr_preinit(&nhrpd_di, argc, argv); +	frr_opt_add("", longopts, ""); -	parse_arguments(progname, argc, argv); +	parse_arguments(argc, argv);  	/* Library inits. */ -	master = thread_master_create(); -	zprivs_init(&nhrpd_privs); -	signal_init(master, array_size(sighandlers), sighandlers); -	cmd_init(1); -	vty_init(master); -	memory_init(); +	master = frr_init();  	nhrp_interface_init();  	vrf_init();  	resolver_init(); @@ -222,28 +148,7 @@ int main(int argc, char **argv)  	nhrp_config_init(); -	/* Get zebra configuration file. */ -	zlog_set_level(NULL, ZLOG_DEST_STDOUT, do_daemonise ? ZLOG_DISABLED : LOG_DEBUG); -	vty_read_config(config_file, config_default); - -	if (do_daemonise && daemon(0, 0) < 0) { -		zlog_err("daemonise: %s", safe_strerror(errno)); -		exit (1); -	} - -	/* write pid file */ -	if (pid_output(pid_file) < 0) { -		zlog_err("error while writing pidfile"); -		exit (1); -	} - -	/* Create VTY socket */ -	vty_serv_sock(vty_addr, vty_port, NHRP_VTYSH_PATH); -	zlog_notice("nhrpd starting: vty@%d", vty_port); - -	/* Main loop */ -	while (thread_fetch(master, &thread)) -		thread_call(&thread); - +	frr_config_fork(); +	frr_run(master);  	return 0;  } diff --git a/ospf6d/ospf6_main.c b/ospf6d/ospf6_main.c index f85580fa59..54ae4645ad 100644 --- a/ospf6d/ospf6_main.c +++ b/ospf6d/ospf6_main.c @@ -39,7 +39,7 @@  #include "zclient.h"  #include "vrf.h"  #include "bfd.h" -#include "sockopt.h" +#include "libfrr.h"  #include "ospf6d.h"  #include "ospf6_top.h" @@ -52,9 +52,6 @@  /* Default configuration file name for ospf6d. */  #define OSPF6_DEFAULT_CONFIG       "ospf6d.conf" -/* VTY Socket prefix */ -char vty_sock_path[MAXPATHLEN] = OSPF6_VTYSH_PATH; -  /* Default port values. */  #define OSPF6_VTY_PORT             2606 @@ -82,68 +79,14 @@ struct zebra_privs_t ospf6d_privs =  };  /* ospf6d options, we use GNU getopt library. */ -#define OPTION_VTYSOCK 1000  struct option longopts[] =   { -  { "daemon",      no_argument,       NULL, 'd'}, -  { "config_file", required_argument, NULL, 'f'}, -  { "pid_file",    required_argument, NULL, 'i'}, -  { "socket",      required_argument, NULL, 'z'}, -  { "vty_addr",    required_argument, NULL, 'A'}, -  { "vty_port",    required_argument, NULL, 'P'}, -  { "vty_socket",  required_argument, NULL, OPTION_VTYSOCK}, -  { "user",        required_argument, NULL, 'u'}, -  { "group",       required_argument, NULL, 'g'}, -  { "version",     no_argument,       NULL, 'v'}, -  { "dryrun",      no_argument,       NULL, 'C'}, -  { "help",        no_argument,       NULL, 'h'},    { 0 }  }; -/* Configuration file and directory. */ -char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG; - -/* ospf6d program name. */ -char *progname; - -/* is daemon? */ -int daemon_mode = 0; -  /* Master of threads. */  struct thread_master *master; -/* Process ID saved for use by init system */ -const char *pid_file = PATH_OSPF6D_PID; - -/* Help information display. */ -static void -usage (char *progname, int status) -{ -  if (status != 0) -    fprintf (stderr, "Try `%s --help' for more information.\n", progname); -  else -    {     -      printf ("Usage : %s [OPTION...]\n\n\ -Daemon which manages OSPF version 3.\n\n\ --d, --daemon       Runs in daemon mode\n\ --f, --config_file  Set configuration file name\n\ --i, --pid_file     Set process identifier file name\n\ --z, --socket       Set path of zebra socket\n\ --A, --vty_addr     Set vty's bind address\n\ --P, --vty_port     Set vty's port number\n\ -    --vty_socket   Override vty socket path\n\ --u, --user         User to run as\n\ --g, --group        Group to run as\n\ --v, --version      Print program version\n\ --C, --dryrun       Check configuration for validity and exit\n\ --h, --help         Display this help and exit\n\ -\n\ -Report bugs to %s\n", progname, FRR_BUG_ADDRESS); -    } - -  exit (status); -} -  static void __attribute__ ((noreturn))  ospf6_exit (int status)  { @@ -173,8 +116,7 @@ ospf6_exit (int status)    if (master)      thread_master_free (master); -  if (zlog_default) -    closezlog (zlog_default); +  closezlog ();    exit (status);  } @@ -208,7 +150,7 @@ static void  sigusr1 (void)  {    zlog_info ("SIGUSR1 received"); -  zlog_rotate (NULL); +  zlog_rotate();  }  struct quagga_signal_t ospf6_signals[] = @@ -231,32 +173,31 @@ struct quagga_signal_t ospf6_signals[] =    },  }; +FRR_DAEMON_INFO(ospf6d, OSPF6, +	.vty_port = OSPF6_VTY_PORT, + +	.proghelp = "Implementation of the OSPFv3 routing protocol.", + +	.signals = ospf6_signals, +	.n_signals = array_size(ospf6_signals), + +	.privs = &ospf6d_privs, +) +  /* Main routine of ospf6d. Treatment of argument and starting ospf finite     state machine is handled here. */  int  main (int argc, char *argv[], char *envp[])  { -  char *p;    int opt; -  char *vty_addr = NULL; -  int vty_port = 0; -  char *config_file = NULL; -  struct thread thread; -  int dryrun = 0; - -  /* Set umask before anything for security */ -  umask (0027); -  /* Preserve name of myself. */ -  progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); - -  /* Seed random number for LSA ID */ -  srandom (time(NULL)); +  frr_preinit (&ospf6d_di, argc, argv); +  frr_opt_add ("", longopts, "");    /* Command line argument treatment. */    while (1)       { -      opt = getopt_long (argc, argv, "df:i:z:hp:A:P:u:g:vC", longopts, 0); +      opt = frr_getopt (argc, argv, NULL);        if (opt == EOF)          break; @@ -265,54 +206,8 @@ main (int argc, char *argv[], char *envp[])          {          case 0:            break; -        case 'd': -          daemon_mode = 1; -          break; -        case 'f': -          config_file = optarg; -          break; -        case 'A': -          vty_addr = optarg; -          break; -        case 'i': -          pid_file = optarg; -          break; -        case 'z': -          zclient_serv_path_set (optarg); -          break; -        case 'P': -         /* Deal with atoi() returning 0 on failure, and ospf6d not -             listening on ospf6d port... */ -          if (strcmp(optarg, "0") == 0) -            { -              vty_port = 0; -              break; -            } -          vty_port = atoi (optarg); -          if (vty_port <= 0 || vty_port > 0xffff) -            vty_port = OSPF6_VTY_PORT; -          break; -        case OPTION_VTYSOCK: -          set_socket_path(vty_sock_path, OSPF6_VTYSH_PATH, optarg, sizeof (vty_sock_path)); -          break; -        case 'u': -          ospf6d_privs.user = optarg; -          break; -	case 'g': -	  ospf6d_privs.group = optarg; -	  break; -        case 'v': -          print_version (progname); -          exit (0); -          break; -	case 'C': -	  dryrun = 1; -	  break; -        case 'h': -          usage (progname, 0); -          break;          default: -          usage (progname, 1); +          frr_help_exit (1);            break;          }      } @@ -320,27 +215,13 @@ main (int argc, char *argv[], char *envp[])    if (geteuid () != 0)      {        errno = EPERM; -      perror (progname); +      perror (ospf6d_di.progname);        exit (1);      }    /* thread master */ -  master = thread_master_create (); - -  /* Initializations. */ -  zlog_default = openzlog (progname, ZLOG_OSPF6, 0, -                           LOG_CONS|LOG_NDELAY|LOG_PID, -                           LOG_DAEMON); -  zprivs_init (&ospf6d_privs); -#if defined(HAVE_CUMULUS) -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); -#endif +  master = frr_init (); -  /* initialize zebra libraries */ -  signal_init (master, array_size(ospf6_signals), ospf6_signals); -  cmd_init (1); -  vty_init (master); -  memory_init ();    vrf_init ();    access_list_init ();    prefix_list_init (); @@ -348,37 +229,8 @@ main (int argc, char *argv[], char *envp[])    /* initialize ospf6 */    ospf6_init (); -  /* parse config file */ -  vty_read_config (config_file, config_default); - -  /* Start execution only if not in dry-run mode */ -  if (dryrun) -    return(0); -   -  if (daemon_mode && daemon (0, 0) < 0) -    { -      zlog_err("OSPF6d daemon failed: %s", strerror(errno)); -      exit (1); -    } - -  /* pid file create */ -  pid_output (pid_file); - -  /* Make ospf6 vty socket. */ -  if (!vty_port) -    vty_port = OSPF6_VTY_PORT; -  vty_serv_sock (vty_addr, vty_port, vty_sock_path); - -  /* Print start message */ -  zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d", -               FRR_VERSION, OSPF6_DAEMON_VERSION,vty_port); - -  /* Start finite state machine, here we go! */ -  while (thread_fetch (master, &thread)) -    thread_call (&thread); - -  /* Log in case thread failed */ -  zlog_warn ("Thread failed"); +  frr_config_fork (); +  frr_run (master);    /* Not reached. */    ospf6_exit (0); diff --git a/ospfd/ospf_ism.c b/ospfd/ospf_ism.c index ae6d0cdbc5..9630616acc 100644 --- a/ospfd/ospf_ism.c +++ b/ospfd/ospf_ism.c @@ -255,8 +255,7 @@ ospf_hello_timer (struct thread *thread)    oi->t_hello = NULL;    if (IS_DEBUG_OSPF (ism, ISM_TIMERS)) -    zlog (NULL, LOG_DEBUG, "ISM[%s]: Timer (Hello timer expire)", -	  IF_NAME (oi)); +    zlog_debug("ISM[%s]: Timer (Hello timer expire)", IF_NAME(oi));    /* Sending hello packet. */    ospf_hello_send (oi); @@ -276,8 +275,7 @@ ospf_wait_timer (struct thread *thread)    oi->t_wait = NULL;    if (IS_DEBUG_OSPF (ism, ISM_TIMERS)) -    zlog (NULL, LOG_DEBUG, "ISM[%s]: Timer (Wait timer expire)", -	  IF_NAME (oi)); +    zlog_debug("ISM[%s]: Timer (Wait timer expire)", IF_NAME(oi));    OSPF_ISM_EVENT_SCHEDULE (oi, ISM_WaitTimer); @@ -418,7 +416,7 @@ static int  ism_ignore (struct ospf_interface *oi)  {    if (IS_DEBUG_OSPF (ism, ISM_EVENTS)) -    zlog (NULL, LOG_DEBUG, "ISM[%s]: ism_ignore called", IF_NAME (oi)); +    zlog_debug("ISM[%s]: ism_ignore called", IF_NAME(oi));    return 0;  } @@ -539,9 +537,9 @@ ism_change_state (struct ospf_interface *oi, int state)    /* Logging change of state. */    if (IS_DEBUG_OSPF (ism, ISM_STATUS)) -    zlog (NULL, LOG_DEBUG, "ISM[%s]: State change %s -> %s", IF_NAME (oi), -	  LOOKUP (ospf_ism_state_msg, oi->state), -	  LOOKUP (ospf_ism_state_msg, state)); +    zlog_debug("ISM[%s]: State change %s -> %s", IF_NAME(oi), +               LOOKUP(ospf_ism_state_msg, oi->state), +               LOOKUP(ospf_ism_state_msg, state));    old_state = oi->state;    oi->state = state; @@ -617,9 +615,9 @@ ospf_ism_event (struct thread *thread)      next_state = ISM [oi->state][event].next_state;    if (IS_DEBUG_OSPF (ism, ISM_EVENTS)) -    zlog (NULL, LOG_DEBUG, "ISM[%s]: %s (%s)", IF_NAME (oi), -	  LOOKUP (ospf_ism_state_msg, oi->state), -	  ospf_ism_event_str[event]); +    zlog_debug("ISM[%s]: %s (%s)", IF_NAME(oi), +               LOOKUP(ospf_ism_state_msg, oi->state), +               ospf_ism_event_str[event]);    /* If state is changed. */    if (next_state != oi->state) diff --git a/ospfd/ospf_main.c b/ospfd/ospf_main.c index edb1ca470e..5705dad5d4 100644 --- a/ospfd/ospf_main.c +++ b/ospfd/ospf_main.c @@ -41,7 +41,7 @@  #include "sigevent.h"  #include "zclient.h"  #include "vrf.h" -#include "sockopt.h" +#include "libfrr.h"  #include "ospfd/ospfd.h"  #include "ospfd/ospf_interface.h" @@ -76,41 +76,19 @@ struct zebra_privs_t ospfd_privs =    .cap_num_i = 0  }; -/* Configuration filename and directory. */ -char config_default[100]; -  /* OSPFd options. */ -#define OPTION_VTYSOCK 1000  struct option longopts[] =   { -  { "daemon",      no_argument,       NULL, 'd'},    { "instance",    required_argument, NULL, 'n'}, -  { "config_file", required_argument, NULL, 'f'}, -  { "pid_file",    required_argument, NULL, 'i'}, -  { "socket",      required_argument, NULL, 'z'}, -  { "dryrun",      no_argument,       NULL, 'C'}, -  { "help",        no_argument,       NULL, 'h'}, -  { "vty_addr",    required_argument, NULL, 'A'}, -  { "vty_port",    required_argument, NULL, 'P'}, -  { "vty_socket",  required_argument, NULL, OPTION_VTYSOCK}, -  { "user",        required_argument, NULL, 'u'}, -  { "group",       required_argument, NULL, 'g'},    { "apiserver",   no_argument,       NULL, 'a'}, -  { "version",     no_argument,       NULL, 'v'},    { 0 }  };  /* OSPFd program name */ -/* VTY Socket prefix */ -char vty_sock_path[MAXPATHLEN] = OSPF_VTYSH_PATH; -  /* Master of threads. */  struct thread_master *master; -/* Process ID saved for use by init system */ -char pid_file[100]; -  #ifdef SUPPORT_OSPF_API  extern int ospf_apiserver_enable;  #endif /* SUPPORT_OSPF_API */ @@ -124,21 +102,6 @@ usage (char *progname, int status)    else      {            printf ("Usage : %s [OPTION...]\n\ -Daemon which manages OSPF.\n\n\ --d, --daemon       Runs in daemon mode\n\ --n, --instance     Set the instance id\n\ --f, --config_file  Set configuration file name\n\ --i, --pid_file     Set process identifier file name\n\ --z, --socket       Set path of zebra socket\n\ --A, --vty_addr     Set vty's bind address\n\ --P, --vty_port     Set vty's port number\n\ -    --vty_socket   Override vty socket path\n\ --u, --user         User to run as\n\ --g, --group        Group to run as\n\ --a. --apiserver    Enable OSPF apiserver\n\ --v, --version      Print program version\n\ --C, --dryrun       Check configuration for validity and exit\n\ --h, --help         Display this help and exit\n\  \n\  Report bugs to %s\n", progname, FRR_BUG_ADDRESS);      } @@ -149,7 +112,7 @@ Report bugs to %s\n", progname, FRR_BUG_ADDRESS);  static void   sighup (void)  { -  zlog (NULL, LOG_INFO, "SIGHUP received"); +  zlog_info("SIGHUP received");  }  /* SIGINT / SIGTERM handler. */ @@ -164,7 +127,7 @@ sigint (void)  static void  sigusr1 (void)  { -  zlog_rotate (NULL); +  zlog_rotate();  }  struct quagga_signal_t ospf_signals[] = @@ -187,39 +150,38 @@ struct quagga_signal_t ospf_signals[] =    },  }; +FRR_DAEMON_INFO(ospfd, OSPF, +	.vty_port = OSPF_VTY_PORT, + +	.proghelp = "Implementation of the OSPFv2 routing protocol.", + +	.signals = ospf_signals, +	.n_signals = array_size(ospf_signals), + +	.privs = &ospfd_privs, +) +  /* OSPFd main routine. */  int  main (int argc, char **argv)  { -  char *p; -  char *vty_addr = NULL; -  int vty_port = OSPF_VTY_PORT; -  char vty_path[100]; -  int daemon_mode = 0; -  char *config_file = NULL; -  char *progname;    u_short instance = 0; -  struct thread thread; -  int dryrun = 0; - -  /* Set umask before anything for security */ -  umask (0027);  #ifdef SUPPORT_OSPF_API    /* OSPF apiserver is disabled by default. */    ospf_apiserver_enable = 0;  #endif /* SUPPORT_OSPF_API */ -  strcpy(pid_file, PATH_OSPFD_PID); - -  /* get program name */ -  progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); +  frr_preinit (&ospfd_di, argc, argv); +  frr_opt_add ("n:a", longopts, +	"  -n, --instance     Set the instance id\n" +	"  -a, --apiserver    Enable OSPF apiserver\n");    while (1)       {        int opt; -      opt = getopt_long (argc, argv, "df:i:n:z:hA:P:u:g:avC", longopts, 0); +      opt = frr_getopt (argc, argv, NULL);        if (opt == EOF)  	break; @@ -227,65 +189,19 @@ main (int argc, char **argv)        switch (opt)   	{  	case 'n': -          instance = atoi(optarg); +          ospfd_di.instance = instance = atoi(optarg);            if (instance < 1)              exit(0);  	  break;  	case 0:  	  break; -	case 'd': -	  daemon_mode = 1; -	  break; -	case 'f': -	  config_file = optarg; -	  break; -	case 'A': -	  vty_addr = optarg; -	  break; -        case 'i': -          strcpy(pid_file,optarg); -          break; -	case 'z': -	  zclient_serv_path_set (optarg); -	  break; -	case 'P': -          /* Deal with atoi() returning 0 on failure, and ospfd not -             listening on ospfd port... */ -          if (strcmp(optarg, "0") == 0)  -            { -              vty_port = 0; -              break; -            }  -          vty_port = atoi (optarg); -          if (vty_port <= 0 || vty_port > 0xffff) -            vty_port = OSPF_VTY_PORT; -  	  break; -	case OPTION_VTYSOCK: -	  set_socket_path(vty_sock_path, OSPF_VTYSH_PATH, optarg, sizeof (vty_sock_path)); -	  break; -	case 'u': -	  ospfd_privs.user = optarg; -	  break; -	case 'g': -	  ospfd_privs.group = optarg; -	  break;  #ifdef SUPPORT_OSPF_API  	case 'a':  	  ospf_apiserver_enable = 1;  	  break;  #endif /* SUPPORT_OSPF_API */ -	case 'v': -	  print_version (progname); -	  exit (0); -	  break; -	case 'C': -	  dryrun = 1; -	  break; -	case 'h': -	  usage (progname, 0); -	  break;  	default: -	  usage (progname, 1); +	  frr_help_exit (1);  	  break;  	}      } @@ -294,29 +210,18 @@ main (int argc, char **argv)    if (geteuid () != 0)      {        errno = EPERM; -      perror (progname); +      perror (ospfd_di.progname);        exit (1);      } -  zlog_default = openzlog (progname, ZLOG_OSPF, instance, -			   LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -  zprivs_init (&ospfd_privs); -#if defined(HAVE_CUMULUS) -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); -#endif -    /* OSPF master init. */ -  ospf_master_init (); +  ospf_master_init (frr_init ());    /* Initializations. */    master = om->master;    /* Library inits. */ -  signal_init (master, array_size(ospf_signals), ospf_signals); -  cmd_init (1);    debug_init (); -  vty_init (master); -  memory_init ();    vrf_init ();    access_list_init (); @@ -349,74 +254,8 @@ main (int argc, char **argv)        exit (1);      } -  /* Get configuration file. */ -  if (instance) -    sprintf(config_default, "%sospfd-%d.conf", SYSCONFDIR, instance); -  else -    sprintf(config_default, "%s%s", SYSCONFDIR, OSPF_DEFAULT_CONFIG); -  vty_read_config (config_file, config_default); - -  /* Start execution only if not in dry-run mode */ -  if (dryrun) -    return(0); -   -  /* Change to the daemon program. */ -  if (daemon_mode && daemon (0, 0) < 0) -    { -      zlog_err("OSPFd daemon failed: %s", strerror(errno)); -      exit (1); -    } - -  /* Create PID file */ -  if (instance) -    { -      char pidfile_temp[100]; - -      /* Override the single file with file including instance -         number in case of multi-instance */ -      if (strrchr(pid_file, '/') != NULL) -          /* cut of pid_file at last / char * to get directory */ -          *strrchr(pid_file, '/') = '\0'; -      else -          /* pid_file contains no directory - should never happen, but deal with it anyway */ -          /* throw-away all pid_file and assume it's only the filename */ -          pid_file[0] = '\0'; - -      snprintf(pidfile_temp, sizeof(pidfile_temp), "%s/ospfd-%d.pid", pid_file, instance ); -      strlcpy(pid_file, pidfile_temp, sizeof(pid_file)); -    } -  /* Process id file create. */ -  pid_output (pid_file); - -  /* Create VTY socket */ -  if (instance) -    { -      /* Multi-Instance. Use only path section of vty_sock_path with new file incl instance */ -      if (strrchr(vty_sock_path, '/') != NULL) -        { -          /* cut of pid_file at last / char * to get directory */ -          *strrchr(vty_sock_path, '/') = '\0'; -        } -      else -        { -          /* pid_file contains no directory - should never happen, but deal with it anyway */ -          /* throw-away all pid_file and assume it's only the filename */ -          vty_sock_path[0] = '\0'; -        } -        snprintf(vty_path, sizeof(vty_path), "%s/ospfd-%d.vty", vty_sock_path, instance ); -    } -  else -    { -      strlcpy(vty_path, vty_sock_path, sizeof(vty_path)); -    } -  vty_serv_sock (vty_addr, vty_port, vty_path); - -  /* Print banner. */ -  zlog_notice ("OSPFd %s starting: vty@%d, %s", FRR_VERSION, vty_port, vty_path); - -  /* Fetch next active thread. */ -  while (thread_fetch (master, &thread)) -    thread_call (&thread); +  frr_config_fork(); +  frr_run (master);    /* Not reached. */    return (0); diff --git a/ospfd/ospf_nsm.c b/ospfd/ospf_nsm.c index ccb82bf8fe..01617055ce 100644 --- a/ospfd/ospf_nsm.c +++ b/ospfd/ospf_nsm.c @@ -63,8 +63,8 @@ ospf_inactivity_timer (struct thread *thread)    nbr->t_inactivity = NULL;    if (IS_DEBUG_OSPF (nsm, NSM_TIMERS)) -    zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (Inactivity timer expire)", -	  IF_NAME (nbr->oi), inet_ntoa (nbr->router_id)); +    zlog_debug("NSM[%s:%s]: Timer (Inactivity timer expire)", +               IF_NAME(nbr->oi), inet_ntoa(nbr->router_id));    OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_InactivityTimer); @@ -80,8 +80,8 @@ ospf_db_desc_timer (struct thread *thread)    nbr->t_db_desc = NULL;    if (IS_DEBUG_OSPF (nsm, NSM_TIMERS)) -    zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (DD Retransmit timer expire)", -	  IF_NAME (nbr->oi), inet_ntoa (nbr->src)); +    zlog_debug("NSM[%s:%s]: Timer (DD Retransmit timer expire)", +               IF_NAME(nbr->oi), inet_ntoa(nbr->src));    /* resent last send DD packet. */    assert (nbr->last_send); diff --git a/ospfd/ospf_packet.c b/ospfd/ospf_packet.c index bf78336ad5..a0cc367cd7 100644 --- a/ospfd/ospf_packet.c +++ b/ospfd/ospf_packet.c @@ -2971,9 +2971,8 @@ ospf_read (struct thread *thread)        ospf_ls_ack (iph, ospfh, ibuf, oi, length);        break;      default: -      zlog (NULL, LOG_WARNING, -	    "interface %s: OSPF packet header type %d is illegal", -	    IF_NAME (oi), ospfh->type); +      zlog_warn("interface %s: OSPF packet header type %d is illegal", +                IF_NAME(oi), ospfh->type);        break;      } @@ -3453,8 +3452,8 @@ ospf_poll_timer (struct thread *thread)    nbr_nbma->t_poll = NULL;    if (IS_DEBUG_OSPF (nsm, NSM_TIMERS)) -    zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (Poll timer expire)", -    IF_NAME (nbr_nbma->oi), inet_ntoa (nbr_nbma->addr)); +    zlog_debug("NSM[%s:%s]: Timer (Poll timer expire)", IF_NAME(nbr_nbma->oi), +               inet_ntoa(nbr_nbma->addr));    ospf_poll_send (nbr_nbma); @@ -3477,8 +3476,8 @@ ospf_hello_reply_timer (struct thread *thread)    assert (nbr->oi);    if (IS_DEBUG_OSPF (nsm, NSM_TIMERS)) -    zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (hello-reply timer expire)", -	  IF_NAME (nbr->oi), inet_ntoa (nbr->router_id)); +    zlog_debug("NSM[%s:%s]: Timer (hello-reply timer expire)", +               IF_NAME(nbr->oi), inet_ntoa(nbr->router_id));    ospf_hello_send_sub (nbr->oi, nbr->address.u.prefix4.s_addr); diff --git a/ospfd/ospf_snmp.c b/ospfd/ospf_snmp.c index 676756d6d9..4afbda8788 100644 --- a/ospfd/ospf_snmp.c +++ b/ospfd/ospf_snmp.c @@ -2658,8 +2658,8 @@ ospfTrapNbrStateChange (struct ospf_neighbor *on)    char msgbuf[16];    ospf_nbr_state_message(on, msgbuf, sizeof(msgbuf)); -  zlog (NULL, LOG_INFO, "ospfTrapNbrStateChange trap sent: %s now %s", -	inet_ntoa(on->address.u.prefix4), msgbuf); +  zlog_info("ospfTrapNbrStateChange trap sent: %s now %s", +            inet_ntoa(on->address.u.prefix4), msgbuf);    oid_copy_addr (index, &(on->address.u.prefix4), IN_ADDR_SIZE);    index[IN_ADDR_SIZE] = 0; @@ -2678,7 +2678,7 @@ ospfTrapVirtNbrStateChange (struct ospf_neighbor *on)  {    oid index[sizeof (oid) * (IN_ADDR_SIZE + 1)]; -  zlog (NULL, LOG_INFO, "ospfTrapVirtNbrStateChange trap sent"); +  zlog_info("ospfTrapVirtNbrStateChange trap sent");    oid_copy_addr (index, &(on->address.u.prefix4), IN_ADDR_SIZE);    index[IN_ADDR_SIZE] = 0; @@ -2697,9 +2697,9 @@ ospfTrapIfStateChange (struct ospf_interface *oi)  {    oid index[sizeof (oid) * (IN_ADDR_SIZE + 1)]; -  zlog (NULL, LOG_INFO, "ospfTrapIfStateChange trap sent: %s now %s", -  	inet_ntoa(oi->address->u.prefix4), -	LOOKUP(ospf_ism_state_msg, oi->state)); +  zlog_info("ospfTrapIfStateChange trap sent: %s now %s", +            inet_ntoa(oi->address->u.prefix4), +            LOOKUP(ospf_ism_state_msg, oi->state));    oid_copy_addr (index, &(oi->address->u.prefix4), IN_ADDR_SIZE);    index[IN_ADDR_SIZE] = 0; @@ -2718,7 +2718,7 @@ ospfTrapVirtIfStateChange (struct ospf_interface *oi)  {    oid index[sizeof (oid) * (IN_ADDR_SIZE + 1)]; -  zlog (NULL, LOG_INFO, "ospfTrapVirtIfStateChange trap sent"); +  zlog_info("ospfTrapVirtIfStateChange trap sent");    oid_copy_addr (index, &(oi->address->u.prefix4), IN_ADDR_SIZE);    index[IN_ADDR_SIZE] = 0; diff --git a/ospfd/ospf_vty.c b/ospfd/ospf_vty.c index 62498f4c82..ce4bc9dfaa 100644 --- a/ospfd/ospf_vty.c +++ b/ospfd/ospf_vty.c @@ -9117,7 +9117,7 @@ ospf_interface_clear (struct interface *ifp)    if (!if_is_operative (ifp)) return;    if (IS_DEBUG_OSPF (ism, ISM_EVENTS)) -    zlog (NULL, LOG_DEBUG, "ISM[%s]: clear by reset", ifp->name); +    zlog_debug("ISM[%s]: clear by reset", ifp->name);    ospf_if_reset(ifp);  } diff --git a/ospfd/ospfd.c b/ospfd/ospfd.c index 570734bd8c..7cd3903678 100644 --- a/ospfd/ospfd.c +++ b/ospfd/ospfd.c @@ -1917,11 +1917,11 @@ ospf_nbr_nbma_poll_interval_unset (struct ospf *ospf, struct in_addr addr)  }  void -ospf_master_init () +ospf_master_init (struct thread_master *master)  {    memset (&ospf_master, 0, sizeof (struct ospf_master));    om = &ospf_master;    om->ospf = list_new (); -  om->master = thread_master_create (); +  om->master = master;  } diff --git a/ospfd/ospfd.h b/ospfd/ospfd.h index 8ebd3c4ec6..a3bd0ca12f 100644 --- a/ospfd/ospfd.h +++ b/ospfd/ospfd.h @@ -576,7 +576,7 @@ extern void ospf_area_del_if (struct ospf_area *, struct ospf_interface *);  extern void ospf_route_map_init (void);  extern void ospf_snmp_init (void); -extern void ospf_master_init (void); +extern void ospf_master_init (struct thread_master *master);  extern int ospf_interface_set (struct interface *ifp, struct in_addr area_id);  extern int ospf_interface_unset (struct interface *ifp); diff --git a/pimd/pim_main.c b/pimd/pim_main.c index 2e81ac53fd..ba1c511d16 100644 --- a/pimd/pim_main.c +++ b/pimd/pim_main.c @@ -38,7 +38,7 @@  #include "prefix.h"  #include "plist.h"  #include "vrf.h" -#include "sockopt.h" +#include "libfrr.h"  #include "pimd.h"  #include "pim_version.h" @@ -50,27 +50,10 @@  extern struct host host; -char config_default[] = SYSCONFDIR PIMD_DEFAULT_CONFIG; - -/* pimd options */ -#define OPTION_VTYSOCK 1000  struct option longopts[] = { -  { "daemon",        no_argument,       NULL, 'd'}, -  { "config_file",   required_argument, NULL, 'f'}, -  { "pid_file",      required_argument, NULL, 'i'}, -  { "socket",        required_argument, NULL, 'z'}, -  { "vty_addr",      required_argument, NULL, 'A'}, -  { "vty_port",      required_argument, NULL, 'P'}, -  { "vty_socket",    required_argument, NULL, OPTION_VTYSOCK}, -  { "version",       no_argument,       NULL, 'v'}, -  { "debug_zclient", no_argument,       NULL, 'Z'}, -  { "help",          no_argument,       NULL, 'h'},    { 0 }  }; -/* VTY Socket prefix */ -char vty_sock_path[MAXPATHLEN] = PIM_VTYSH_PATH; -  /* pimd privileges */  zebra_capabilities_t _caps_p [] =   { @@ -95,101 +78,40 @@ struct zebra_privs_t pimd_privs =    .cap_num_i = 0  }; -char* progname; -const char *pid_file = PATH_PIMD_PID; +FRR_DAEMON_INFO(pimd, PIM, +	.vty_port = PIMD_VTY_PORT, -static void usage(int status) -{ -  if (status != 0) -    fprintf (stderr, "Try `%s --help' for more information.\n", progname); -  else {     -    printf ("Usage : %s [OPTION...]\n\ -Daemon which manages PIM.\n\n\ --d, --daemon         Run in daemon mode\n\ --f, --config_file    Set configuration file name\n\ --i, --pid_file       Set process identifier file name\n\ --z, --socket         Set path of zebra socket\n\ --A, --vty_addr       Set vty's bind address\n\ --P, --vty_port       Set vty's port number\n\ -    --vty_socket     Override vty socket path\n\ --v, --version        Print program version\n\ --h, --help           Display this help and exit\n\ -\n\ -Report bugs to %s\n", progname, PACKAGE_BUGREPORT); -  } +	.proghelp = "Implementation of the PIM routing protocol.", -  exit (status); -} +	.signals = pimd_signals, +	.n_signals = 4 /* XXX array_size(pimd_signals) XXX*/, +	.privs = &pimd_privs, +)  int main(int argc, char** argv, char** envp) { -  char *p; -  char *vty_addr = NULL; -  int vty_port = -1; -  int daemon_mode = 0; -  char *config_file = NULL; -  char *zebra_sock_path = NULL; -  struct thread thread; -           -  umask(0027); -  -  progname = ((p = strrchr(argv[0], '/')) ? ++p : argv[0]); - -  zlog_default = openzlog(progname, ZLOG_PIM, 0, -			  LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -  zprivs_init (&pimd_privs); -#if defined(HAVE_CUMULUS) -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); -#endif +  frr_preinit(&pimd_di, argc, argv); +  frr_opt_add("", longopts, ""); -  /* this while just reads the options */                        +  /* this while just reads the options */    while (1) {      int opt; -             -    opt = getopt_long (argc, argv, "df:i:z:A:P:vZh", longopts, 0); -                       + +    opt = frr_getopt(argc, argv, NULL); +      if (opt == EOF)        break;      switch (opt) {      case 0:        break; -    case 'd': -      daemon_mode = 1; -      break; -    case 'f': -      config_file = optarg; -      break; -    case 'i': -      pid_file = optarg; -      break; -    case 'z': -      zebra_sock_path = optarg; -      break; -    case 'A': -      vty_addr = optarg; -      break; -    case 'P': -      vty_port = atoi (optarg); -      break; -    case OPTION_VTYSOCK: -      set_socket_path(vty_sock_path, PIM_VTYSH_PATH, optarg, sizeof (vty_sock_path)); -      break; -    case 'v': -      printf(PIMD_PROGNAME " version %s\n", PIMD_VERSION); -      print_version(progname); -      exit (0); -      break; -    case 'h': -      usage (0); -      break;      default: -      usage (1); +      frr_help_exit (1);        break;      }    } -  master = thread_master_create(); +  master = frr_init();    zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting",  	      FRR_VERSION, PIMD_VERSION); @@ -197,10 +119,6 @@ int main(int argc, char** argv, char** envp) {    /*      * Initializations     */ -  pim_signals_init(); -  cmd_init(1); -  vty_init(master); -  memory_init();    vrf_init ();    access_list_init();    prefix_list_init (); @@ -214,36 +132,9 @@ int main(int argc, char** argv, char** envp) {    /*     * Initialize zclient "update" and "lookup" sockets     */ -  pim_zebra_init(zebra_sock_path); - -  zlog_notice("Loading configuration - begin"); - -  /* Get configuration file. */ -  vty_read_config(config_file, config_default); - -  /* -    Starting from here zlog_* functions will log according configuration -   */ +  pim_zebra_init(); -  zlog_notice("Loading configuration - end"); - -  /* Change to the daemon program. */ -  if (daemon_mode) { -    if (daemon(0, 0)) { -      zlog_warn("failed to daemonize"); -    } -  } - -  /* Process ID file creation. */ -  pid_output(pid_file); - -  /* Create pimd VTY socket */ -  if (vty_port < 0) -    vty_port = PIMD_VTY_PORT; -  vty_serv_sock(vty_addr, vty_port, vty_sock_path); - -  zlog_notice("Quagga %s " PIMD_PROGNAME " %s starting, VTY interface at port TCP %d", -	      FRR_VERSION, PIMD_VERSION, vty_port); +  frr_config_fork();  #ifdef PIM_DEBUG_BYDEFAULT    zlog_notice("PIM_DEBUG_BYDEFAULT: Enabling all debug commands"); @@ -267,11 +158,7 @@ int main(int argc, char** argv, char** envp) {    zlog_notice("PIM_UNEXPECTED_KERNEL_UPCALL: report unexpected kernel upcall");  #endif -  while (thread_fetch(master, &thread)) -    thread_call(&thread); - -  zlog_err("%s %s: thread_fetch() returned NULL, exiting", -	   __FILE__, __PRETTY_FUNCTION__); +  frr_run(master);    /* never reached */    return 0; diff --git a/pimd/pim_signals.c b/pimd/pim_signals.c index bd4d9e4857..053ef6a67b 100644 --- a/pimd/pim_signals.c +++ b/pimd/pim_signals.c @@ -55,10 +55,10 @@ static void pim_sigterm()  static void pim_sigusr1()  { -  zlog_rotate (NULL); +  zlog_rotate();  } -static struct quagga_signal_t pimd_signals[] = +struct quagga_signal_t pimd_signals[] =  {    {     .signal = SIGHUP, @@ -77,9 +77,3 @@ static struct quagga_signal_t pimd_signals[] =     .handler = &pim_sigterm,     },  }; - -void pim_signals_init() -{ -  signal_init(master, array_size(pimd_signals), pimd_signals); -} - diff --git a/pimd/pim_signals.h b/pimd/pim_signals.h index 7b25608c10..d7a4926d4d 100644 --- a/pimd/pim_signals.h +++ b/pimd/pim_signals.h @@ -21,6 +21,7 @@  #ifndef PIM_SIGNALS_H  #define PIM_SIGNALS_H -void pim_signals_init(void); +#include "sigevent.h" +extern struct quagga_signal_t pimd_signals[];  #endif /* PIM_SIGNALS_H */ diff --git a/pimd/pim_zebra.c b/pimd/pim_zebra.c index 70b0302806..1db6616c58 100644 --- a/pimd/pim_zebra.c +++ b/pimd/pim_zebra.c @@ -721,13 +721,10 @@ pim_zebra_connected (struct zclient *zclient)    zclient_send_reg_requests (zclient, VRF_DEFAULT);  } -void pim_zebra_init(char *zebra_sock_path) +void pim_zebra_init(void)  {    int i; -  if (zebra_sock_path) -    zclient_serv_path_set(zebra_sock_path); -  #ifdef HAVE_TCP_ZEBRA    zlog_notice("zclient update contacting ZEBRA daemon at socket TCP %s,%d", "127.0.0.1", ZEBRA_PORT);  #else diff --git a/pimd/pim_zebra.h b/pimd/pim_zebra.h index 476185def1..751a7be25d 100644 --- a/pimd/pim_zebra.h +++ b/pimd/pim_zebra.h @@ -24,7 +24,7 @@  #include "pim_igmp.h"  #include "pim_ifchannel.h" -void pim_zebra_init(char *zebra_sock_path); +void pim_zebra_init(void);  void pim_zebra_zclient_update (struct vty *vty);  void pim_scan_individual_oil (struct channel_oil *c_oil); diff --git a/pimd/pimd.c b/pimd/pimd.c index aa863fd47f..c8a0efc401 100644 --- a/pimd/pimd.c +++ b/pimd/pimd.c @@ -98,8 +98,6 @@ static void pim_free()  void pim_init()  { -  srandom(time(NULL)); -    qpim_rp_keep_alive_time = PIM_RP_KEEPALIVE_PERIOD;    pim_rp_init (); diff --git a/ripd/rip_interface.c b/ripd/rip_interface.c index 772ba49f93..4c750faf4c 100644 --- a/ripd/rip_interface.c +++ b/ripd/rip_interface.c @@ -86,8 +86,7 @@ ipv4_multicast_join (int sock,  				   ifindex);     if (ret < 0)  -    zlog (NULL, LOG_INFO, "can't setsockopt IP_ADD_MEMBERSHIP %s", -	  safe_strerror (errno)); +    zlog_info("can't setsockopt IP_ADD_MEMBERSHIP %s", safe_strerror(errno));    return ret;  } @@ -108,7 +107,7 @@ ipv4_multicast_leave (int sock,  				   ifindex);    if (ret < 0)  -    zlog (NULL, LOG_INFO, "can't setsockopt IP_DROP_MEMBERSHIP"); +    zlog_info("can't setsockopt IP_DROP_MEMBERSHIP");    return ret;  } diff --git a/ripd/rip_main.c b/ripd/rip_main.c index 58dd2df6fa..62ea6dd078 100644 --- a/ripd/rip_main.c +++ b/ripd/rip_main.c @@ -35,27 +35,14 @@  #include "sigevent.h"  #include "zclient.h"  #include "vrf.h" -#include "sockopt.h" +#include "libfrr.h"  #include "ripd/ripd.h"  /* ripd options. */ -#define OPTION_VTYSOCK 1000  static struct option longopts[] =   { -  { "daemon",      no_argument,       NULL, 'd'}, -  { "config_file", required_argument, NULL, 'f'}, -  { "pid_file",    required_argument, NULL, 'i'}, -  { "socket",      required_argument, NULL, 'z'}, -  { "help",        no_argument,       NULL, 'h'}, -  { "dryrun",      no_argument,       NULL, 'C'}, -  { "vty_addr",    required_argument, NULL, 'A'}, -  { "vty_port",    required_argument, NULL, 'P'}, -  { "vty_socket",  required_argument, NULL, OPTION_VTYSOCK},    { "retain",      no_argument,       NULL, 'r'}, -  { "user",        required_argument, NULL, 'u'}, -  { "group",       required_argument, NULL, 'g'}, -  { "version",     no_argument,       NULL, 'v'},    { 0 }  }; @@ -82,59 +69,13 @@ struct zebra_privs_t ripd_privs =    .cap_num_i = 0  }; -/* Configuration file and directory. */ -char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG; -char *config_file = NULL; - -/* ripd program name */ - -/* VTY Socket prefix */ -char vty_sock_path[MAXPATHLEN] = RIP_VTYSH_PATH; -  /* Route retain mode flag. */  int retain_mode = 0; -/* RIP VTY bind address. */ -char *vty_addr = NULL; - -/* RIP VTY connection port. */ -int vty_port = RIP_VTY_PORT; -  /* Master of threads. */  struct thread_master *master; -/* Process ID saved for use by init system */ -const char *pid_file = PATH_RIPD_PID; - -/* Help information display. */ -static void -usage (char *progname, int status) -{ -  if (status != 0) -    fprintf (stderr, "Try `%s --help' for more information.\n", progname); -  else -    {     -      printf ("Usage : %s [OPTION...]\n\ -Daemon which manages RIP version 1 and 2.\n\n\ --d, --daemon       Runs in daemon mode\n\ --f, --config_file  Set configuration file name\n\ --i, --pid_file     Set process identifier file name\n\ --z, --socket       Set path of zebra socket\n\ --A, --vty_addr     Set vty's bind address\n\ --P, --vty_port     Set vty's port number\n\ -    --vty_socket   Override vty socket path\n\ --C, --dryrun       Check configuration for validity and exit\n\ --r, --retain       When program terminates, retain added route by ripd.\n\ --u, --user         User to run as\n\ --g, --group        Group to run as\n\ --v, --version      Print program version\n\ --h, --help         Display this help and exit\n\ -\n\ -Report bugs to %s\n", progname, FRR_BUG_ADDRESS); -    } - -  exit (status); -} +static struct frr_daemon_info ripd_di;  /* SIGHUP handler. */  static void  @@ -146,10 +87,7 @@ sighup (void)    zlog_info ("ripd restarting!");    /* Reload config file. */ -  vty_read_config (config_file, config_default); - -  /* Create VTY's socket */ -  vty_serv_sock (vty_addr, vty_port, vty_sock_path); +  vty_read_config (ripd_di.config_file, config_default);    /* Try to return to normal operation. */  } @@ -170,7 +108,7 @@ sigint (void)  static void  sigusr1 (void)  { -  zlog_rotate (NULL); +  zlog_rotate();  }  static struct quagga_signal_t ripd_signals[] = @@ -193,36 +131,31 @@ static struct quagga_signal_t ripd_signals[] =    },  };   +FRR_DAEMON_INFO(ripd, RIP, +	.vty_port = RIP_VTY_PORT, + +	.proghelp = "Implementation of the RIP routing protocol.", + +	.signals = ripd_signals, +	.n_signals = array_size(ripd_signals), + +	.privs = &ripd_privs, +) +  /* Main routine of ripd. */  int  main (int argc, char **argv)  { -  char *p; -  int daemon_mode = 0; -  int dryrun = 0; -  char *progname; -  struct thread thread; - -  /* Set umask before anything for security */ -  umask (0027); - -  /* Get program name. */ -  progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); - -  /* First of all we need logging init. */ -  zlog_default = openzlog (progname, ZLOG_RIP, 0, -			   LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -  zprivs_init (&ripd_privs); -#if defined(HAVE_CUMULUS) -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); -#endif +  frr_preinit (&ripd_di, argc, argv); +  frr_opt_add ("r", longopts, +	"  -r, --retain       When program terminates, retain added route by ripd.\n");    /* Command line option parse. */    while (1)       {        int opt; -      opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:rvC", longopts, 0); +      opt = frr_getopt (argc, argv, NULL);        if (opt == EOF)  	break; @@ -231,69 +164,19 @@ main (int argc, char **argv)  	{  	case 0:  	  break; -	case 'd': -	  daemon_mode = 1; -	  break; -	case 'f': -	  config_file = optarg; -	  break; -	case 'A': -	  vty_addr = optarg; -	  break; -        case 'i': -          pid_file = optarg; -          break; -	case 'z': -	  zclient_serv_path_set (optarg); -	  break; -	case 'P': -          /* Deal with atoi() returning 0 on failure, and ripd not -             listening on rip port... */ -          if (strcmp(optarg, "0") == 0)  -            { -              vty_port = 0; -              break; -            }  -          vty_port = atoi (optarg); -          if (vty_port <= 0 || vty_port > 0xffff) -            vty_port = RIP_VTY_PORT; -	  break; -	case OPTION_VTYSOCK: -	  set_socket_path(vty_sock_path, RIP_VTYSH_PATH, optarg, sizeof (vty_sock_path)); -	  break;  	case 'r':  	  retain_mode = 1;  	  break; -	case 'C': -	  dryrun = 1; -	  break; -	case 'u': -	  ripd_privs.user = optarg; -	  break; -	case 'g': -	  ripd_privs.group = optarg; -	  break; -	case 'v': -	  print_version (progname); -	  exit (0); -	  break; -	case 'h': -	  usage (progname, 0); -	  break;  	default: -	  usage (progname, 1); +	  frr_help_exit (1);  	  break;  	}      }    /* Prepare master thread. */ -  master = thread_master_create (); +  master = frr_init ();    /* Library initialization. */ -  signal_init (master, array_size(ripd_signals), ripd_signals); -  cmd_init (1); -  vty_init (master); -  memory_init ();    keychain_init ();    vrf_init (); @@ -303,32 +186,8 @@ main (int argc, char **argv)    rip_zclient_init(master);    rip_peer_init (); -  /* Get configuration file. */ -  vty_read_config (config_file, config_default); - -  /* Start execution only if not in dry-run mode */ -  if(dryrun) -    return (0); -   -  /* Change to the daemon program. */ -  if (daemon_mode && daemon (0, 0) < 0) -    { -      zlog_err("RIPd daemon failed: %s", strerror(errno)); -      exit (1); -    } - -  /* Pid file create. */ -  pid_output (pid_file); - -  /* Create VTY's socket */ -  vty_serv_sock (vty_addr, vty_port, vty_sock_path); - -  /* Print banner. */ -  zlog_notice ("RIPd %s starting: vty@%d", FRR_VERSION, vty_port); - -  /* Execute each thread. */ -  while (thread_fetch (master, &thread)) -    thread_call (&thread); +  frr_config_fork (); +  frr_run (master);    /* Not reached. */    return (0); diff --git a/ripd/ripd.c b/ripd/ripd.c index bcf4cd3347..e0f96f9aaf 100644 --- a/ripd/ripd.c +++ b/ripd/ripd.c @@ -4034,9 +4034,6 @@ rip_routemap_update (const char *notused)  void  rip_init (void)  { -  /* Randomize for triggered update random(). */ -  srandom (time (NULL)); -    /* Install top nodes. */    install_node (&rip_node, config_write_rip); diff --git a/ripngd/ripng_main.c b/ripngd/ripng_main.c index a90dffce14..9d97df5c41 100644 --- a/ripngd/ripng_main.c +++ b/ripngd/ripng_main.c @@ -36,31 +36,14 @@  #include "privs.h"  #include "sigevent.h"  #include "vrf.h" -#include "sockopt.h" +#include "libfrr.h"  #include "ripngd/ripngd.h" -/* Configuration filename and directory. */ -char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG; -char *config_file = NULL; -  /* RIPngd options. */ -#define OPTION_VTYSOCK 1000  struct option longopts[] =   { -  { "daemon",      no_argument,       NULL, 'd'}, -  { "config_file", required_argument, NULL, 'f'}, -  { "pid_file",    required_argument, NULL, 'i'}, -  { "socket",      required_argument, NULL, 'z'}, -  { "dryrun",      no_argument,       NULL, 'C'}, -  { "help",        no_argument,       NULL, 'h'}, -  { "vty_addr",    required_argument, NULL, 'A'}, -  { "vty_port",    required_argument, NULL, 'P'}, -  { "vty_socket",  required_argument, NULL, OPTION_VTYSOCK},    { "retain",      no_argument,       NULL, 'r'}, -  { "user",        required_argument, NULL, 'u'}, -  { "group",       required_argument, NULL, 'g'}, -  { "version",     no_argument,       NULL, 'v'},    { 0 }  }; @@ -90,52 +73,13 @@ struct zebra_privs_t ripngd_privs =  /* RIPngd program name */ -/* VTY Socket prefix */ -char vty_sock_path[MAXPATHLEN] = RIPNG_VTYSH_PATH; -  /* Route retain mode flag. */  int retain_mode = 0; -/* RIPng VTY bind address. */ -char *vty_addr = NULL; - -/* RIPng VTY connection port. */ -int vty_port = RIPNG_VTY_PORT; -  /* Master of threads. */  struct thread_master *master; -/* Process ID saved for use by init system */ -const char *pid_file = PATH_RIPNGD_PID; - -/* Help information display. */ -static void -usage (char *progname, int status) -{ -  if (status != 0) -    fprintf (stderr, "Try `%s --help' for more information.\n", progname); -  else -    {     -      printf ("Usage : %s [OPTION...]\n\ -Daemon which manages RIPng.\n\n\ --d, --daemon       Runs in daemon mode\n\ --f, --config_file  Set configuration file name\n\ --i, --pid_file     Set process identifier file name\n\ --z, --socket       Set path of zebra socket\n\ --A, --vty_addr     Set vty's bind address\n\ --P, --vty_port     Set vty's port number\n\ -    --vty_socket   Override vty socket path\n\ --r, --retain       When program terminates, retain added route by ripngd.\n\ --u, --user         User to run as\n\ --g, --group        Group to run as\n\ --v, --version      Print program version\n\ --C, --dryrun       Check configuration for validity and exit\n\ --h, --help         Display this help and exit\n\ -\n\ -Report bugs to %s\n", progname, FRR_BUG_ADDRESS); -    } -  exit (status); -} +static struct frr_daemon_info ripngd_di;  /* SIGHUP handler. */  static void  @@ -146,9 +90,7 @@ sighup (void)    ripng_reset ();    /* Reload config file. */ -  vty_read_config (config_file, config_default); -  /* Create VTY's socket */ -  vty_serv_sock (vty_addr, vty_port, vty_sock_path); +  vty_read_config (ripngd_di.config_file, config_default);    /* Try to return to normal operation. */  } @@ -169,7 +111,7 @@ sigint (void)  static void  sigusr1 (void)  { -  zlog_rotate (NULL); +  zlog_rotate();  }  struct quagga_signal_t ripng_signals[] = @@ -192,35 +134,30 @@ struct quagga_signal_t ripng_signals[] =    },  }; +FRR_DAEMON_INFO(ripngd, RIPNG, +	.vty_port = RIPNG_VTY_PORT, + +	.proghelp = "Implementation of the RIPng routing protocol.", + +	.signals = ripng_signals, +	.n_signals = array_size(ripng_signals), + +	.privs = &ripngd_privs, +) +  /* RIPngd main routine. */  int  main (int argc, char **argv)  { -  char *p; -  int vty_port = RIPNG_VTY_PORT; -  int daemon_mode = 0; -  char *progname; -  struct thread thread; -  int dryrun = 0; - -  /* Set umask before anything for security */ -  umask (0027); - -  /* get program name */ -  progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); - -  zlog_default = openzlog(progname, ZLOG_RIPNG, 0, -			  LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -  zprivs_init (&ripngd_privs); -#if defined(HAVE_CUMULUS) -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); -#endif +  frr_preinit (&ripngd_di, argc, argv); +  frr_opt_add ("r", longopts, +	"  -r, --retain       When program terminates, retain added route by ripd.\n");    while (1)       {        int opt; -      opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:vC", longopts, 0); +      opt = frr_getopt (argc, argv, NULL);        if (opt == EOF)  	break; @@ -229,68 +166,18 @@ main (int argc, char **argv)  	{  	case 0:  	  break; -	case 'd': -	  daemon_mode = 1; -	  break; -	case 'f': -	  config_file = optarg; -	  break; -	case 'A': -	  vty_addr = optarg; -	  break; -        case 'i': -          pid_file = optarg; -          break; -	case 'z': -	  zclient_serv_path_set (optarg); -	  break; -	case 'P': -          /* Deal with atoi() returning 0 on failure, and ripngd not -             listening on ripngd port... */ -          if (strcmp(optarg, "0") == 0)  -            { -              vty_port = 0; -              break; -            }  -          vty_port = atoi (optarg); -          if (vty_port <= 0 || vty_port > 0xffff) -            vty_port = RIPNG_VTY_PORT; -          break; -	case OPTION_VTYSOCK: -	  set_socket_path(vty_sock_path, RIPNG_VTYSH_PATH, optarg, sizeof (vty_sock_path)); -	  break;  	case 'r':  	  retain_mode = 1;  	  break; -	case 'u': -	  ripngd_privs.user = optarg; -	  break; -	case 'g': -	  ripngd_privs.group = optarg; -	  break; -	case 'v': -	  print_version (progname); -	  exit (0); -	  break; -	case 'C': -	  dryrun = 1; -	  break; -	case 'h': -	  usage (progname, 0); -	  break;  	default: -	  usage (progname, 1); +	  frr_help_exit (1);  	  break;  	}      } -  master = thread_master_create (); +  master = frr_init ();    /* Library inits. */ -  signal_init (master, array_size(ripng_signals), ripng_signals); -  cmd_init (1); -  vty_init (master); -  memory_init ();    vrf_init ();    /* RIPngd inits. */ @@ -298,32 +185,8 @@ main (int argc, char **argv)    zebra_init(master);    ripng_peer_init (); -  /* Get configuration file. */ -  vty_read_config (config_file, config_default); - -  /* Start execution only if not in dry-run mode */ -  if(dryrun) -    return(0); -   -  /* Change to the daemon program. */ -  if (daemon_mode && daemon (0, 0) < 0) -    { -      zlog_err("RIPNGd daemon failed: %s", strerror(errno)); -      exit (1); -    } - -  /* Create VTY socket */ -  vty_serv_sock (vty_addr, vty_port, vty_sock_path); - -  /* Process id file create. */ -  pid_output (pid_file); - -  /* Print banner. */ -  zlog_notice ("RIPNGd %s starting: vty@%d", FRR_VERSION, vty_port); - -  /* Fetch next active thread. */ -  while (thread_fetch (master, &thread)) -    thread_call (&thread); +  frr_config_fork (); +  frr_run (master);    /* Not reached. */    return 0; diff --git a/ripngd/ripngd.c b/ripngd/ripngd.c index c8c18928c9..108da21c11 100644 --- a/ripngd/ripngd.c +++ b/ripngd/ripngd.c @@ -107,7 +107,7 @@ ripng_make_socket (void)    sock = socket (AF_INET6, SOCK_DGRAM, 0);    if (sock < 0)      { -      zlog (NULL, LOG_ERR, "Can't make ripng socket"); +      zlog_err("Can't make ripng socket");        return sock;      } @@ -143,7 +143,7 @@ ripng_make_socket (void)    ret = bind (sock, (struct sockaddr *) &ripaddr, sizeof (ripaddr));    if (ret < 0)    { -    zlog (NULL, LOG_ERR, "Can't bind ripng socket: %s.", safe_strerror (errno)); +    zlog_err("Can't bind ripng socket: %s.", safe_strerror(errno));      if (ripngd_privs.change (ZPRIVS_LOWER))        zlog_err ("ripng_make_socket: could not lower privs");      return ret; @@ -1448,8 +1448,7 @@ ripng_update (struct thread *t)        if (ri->ri_send == RIPNG_SEND_OFF)  	{  	  if (IS_RIPNG_DEBUG_EVENT) -	    zlog (NULL, LOG_DEBUG,  -		  "[Event] RIPng send to if %d is suppressed by config", +	    zlog_debug ("[Event] RIPng send to if %d is suppressed by config",  		 ifp->ifindex);  	  continue;  	} @@ -3081,9 +3080,6 @@ ripng_routemap_update (const char *unused)  void  ripng_init ()  { -  /* Randomize. */ -  srandom (time (NULL)); -    /* Install RIPNG_NODE. */    install_node (&cmd_ripng_node, ripng_config_write); diff --git a/tests/bgpd/test_aspath.c b/tests/bgpd/test_aspath.c index f3999cbcff..e5ff6a1377 100644 --- a/tests/bgpd/test_aspath.c +++ b/tests/bgpd/test_aspath.c @@ -1332,7 +1332,7 @@ main (void)  {    int i = 0;    qobj_init (); -  bgp_master_init (); +  bgp_master_init (thread_master_create ());    master = bm->master;    bgp_option_set (BGP_OPT_NO_LISTEN);    bgp_attr_init (); diff --git a/tests/bgpd/test_capability.c b/tests/bgpd/test_capability.c index f83dee5e1c..c3de6a16e8 100644 --- a/tests/bgpd/test_capability.c +++ b/tests/bgpd/test_capability.c @@ -650,7 +650,7 @@ main (void)    qobj_init ();    master = thread_master_create (); -  bgp_master_init (); +  bgp_master_init (master);    vrf_init ();    bgp_option_set (BGP_OPT_NO_LISTEN); diff --git a/tests/bgpd/test_mp_attr.c b/tests/bgpd/test_mp_attr.c index 883cac0cc6..6824c11fea 100644 --- a/tests/bgpd/test_mp_attr.c +++ b/tests/bgpd/test_mp_attr.c @@ -750,7 +750,7 @@ main (void)    qobj_init ();    master = thread_master_create (); -  bgp_master_init (); +  bgp_master_init (master);    vrf_init ();    bgp_option_set (BGP_OPT_NO_LISTEN);    bgp_attr_init (); diff --git a/tests/bgpd/test_mpath.c b/tests/bgpd/test_mpath.c index f9eb1534f3..a2fd66528c 100644 --- a/tests/bgpd/test_mpath.c +++ b/tests/bgpd/test_mpath.c @@ -379,7 +379,7 @@ global_test_init (void)    qobj_init ();    master = thread_master_create ();    zclient = zclient_new(master); -  bgp_master_init (); +  bgp_master_init (master);    vrf_init ();    bgp_option_set (BGP_OPT_NO_LISTEN); diff --git a/tests/lib/cli/common_cli.c b/tests/lib/cli/common_cli.c index 104352f516..56db460438 100644 --- a/tests/lib/cli/common_cli.c +++ b/tests/lib/cli/common_cli.c @@ -52,7 +52,7 @@ static void vty_do_exit(void)    cmd_terminate ();    vty_terminate ();    thread_master_free (master); -  closezlog (zlog_default); +  closezlog ();    log_memstats_stderr ("testcli");    exit (0); @@ -70,11 +70,11 @@ main (int argc, char **argv)    /* master init. */    master = thread_master_create (); -  zlog_default = openzlog ("common-cli", ZLOG_NONE, 0, -                           LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); -  zlog_set_level (NULL, ZLOG_DEST_STDOUT, ZLOG_DISABLED); -  zlog_set_level (NULL, ZLOG_DEST_MONITOR, LOG_DEBUG); +  openzlog("common-cli", "NONE", 0, LOG_CONS | LOG_NDELAY | LOG_PID, +           LOG_DAEMON); +  zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED); +  zlog_set_level(ZLOG_DEST_STDOUT, ZLOG_DISABLED); +  zlog_set_level(ZLOG_DEST_MONITOR, LOG_DEBUG);    /* Library inits. */    cmd_init (1); diff --git a/tests/lib/test_segv.c b/tests/lib/test_segv.c index 1810c5f4b2..dfc9d5f482 100644 --- a/tests/lib/test_segv.c +++ b/tests/lib/test_segv.c @@ -49,11 +49,10 @@ main (void)    master = thread_master_create ();    signal_init (master, array_size(sigs), sigs); -  zlog_default = openzlog("testsegv", ZLOG_NONE, 0, -                          LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); -  zlog_set_level (NULL, ZLOG_DEST_STDOUT, LOG_DEBUG); -  zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); +  openzlog("testsegv", "NONE", 0, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); +  zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED); +  zlog_set_level(ZLOG_DEST_STDOUT, LOG_DEBUG); +  zlog_set_level(ZLOG_DEST_MONITOR, ZLOG_DISABLED);    thread_execute (master, threadfunc, 0, 0); diff --git a/tests/lib/test_sig.c b/tests/lib/test_sig.c index 4a04240303..10bce2303e 100644 --- a/tests/lib/test_sig.c +++ b/tests/lib/test_sig.c @@ -64,13 +64,12 @@ main (void)  {    master = thread_master_create ();    signal_init (master, array_size(sigs), sigs); -   -  zlog_default = openzlog("testsig", ZLOG_NONE, 0, -                          LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED); -  zlog_set_level (NULL, ZLOG_DEST_STDOUT, LOG_DEBUG); -  zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); -   + +  openzlog("testsig", "NONE", 0, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); +  zlog_set_level(ZLOG_DEST_SYSLOG, ZLOG_DISABLED); +  zlog_set_level(ZLOG_DEST_STDOUT, LOG_DEBUG); +  zlog_set_level(ZLOG_DEST_MONITOR, ZLOG_DISABLED); +    while (thread_fetch (master, &t))      thread_call (&t); diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index eb419313e4..114022d199 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -43,6 +43,7 @@  #include "bgpd/bgp_vty.h"  #include "ns.h"  #include "vrf.h" +#include "libfrr.h"  DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CMD, "Vtysh cmd copy") @@ -58,23 +59,23 @@ struct vtysh_client    int fd;    const char *name;    int flag; -  const char *path; +  char path[MAXPATHLEN];    struct vtysh_client *next;  };  struct vtysh_client vtysh_client[] =  { -  { .fd = -1, .name = "zebra", .flag = VTYSH_ZEBRA, .path = ZEBRA_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "ripd", .flag = VTYSH_RIPD, .path = RIP_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "ripngd", .flag = VTYSH_RIPNGD, .path = RIPNG_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "ospfd", .flag = VTYSH_OSPFD, .path = OSPF_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "ospf6d", .flag = VTYSH_OSPF6D, .path = OSPF6_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "ldpd", .flag = VTYSH_LDPD, .path = LDP_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "bgpd", .flag = VTYSH_BGPD, .path = BGP_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "isisd", .flag = VTYSH_ISISD, .path = ISIS_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "pimd", .flag = VTYSH_PIMD, .path = PIM_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "nhrpd", .flag = VTYSH_NHRPD, .path = NHRP_VTYSH_PATH, .next = NULL}, -  { .fd = -1, .name = "watchfrr", .flag = VTYSH_WATCHFRR, .path = WATCHFRR_VTYSH_PATH, .next = NULL}, +  { .fd = -1, .name = "zebra",    .flag = VTYSH_ZEBRA,    .next = NULL}, +  { .fd = -1, .name = "ripd",     .flag = VTYSH_RIPD,     .next = NULL}, +  { .fd = -1, .name = "ripngd",   .flag = VTYSH_RIPNGD,   .next = NULL}, +  { .fd = -1, .name = "ospfd",    .flag = VTYSH_OSPFD,    .next = NULL}, +  { .fd = -1, .name = "ospf6d",   .flag = VTYSH_OSPF6D,   .next = NULL}, +  { .fd = -1, .name = "ldpd",     .flag = VTYSH_LDPD,     .next = NULL}, +  { .fd = -1, .name = "bgpd",     .flag = VTYSH_BGPD,     .next = NULL}, +  { .fd = -1, .name = "isisd",    .flag = VTYSH_ISISD,    .next = NULL}, +  { .fd = -1, .name = "pimd",     .flag = VTYSH_PIMD,     .next = NULL}, +  { .fd = -1, .name = "nhrpd",    .flag = VTYSH_NHRPD,    .next = NULL}, +  { .fd = -1, .name = "watchfrr", .flag = VTYSH_WATCHFRR, .next = NULL},  };  enum vtysh_write_integrated vtysh_write_integrated = WRITE_INTEGRATED_UNSPECIFIED; @@ -2866,27 +2867,12 @@ vtysh_connect (struct vtysh_client *vclient)    int sock, len;    struct sockaddr_un addr;    struct stat s_stat; -  char path[MAXPATHLEN]; +  const char *path; -  if (vty_sock_path == NULL) -    strlcpy (path, vclient->path, sizeof (path)); -  else { -    /* Different path for VTY Socket specified -       overriding the default path, but keep the filename */ -    strlcpy (path, vty_sock_path, sizeof (path)); - -    if (strrchr (vclient->path, '/') != NULL) -      strlcat (path, strrchr (vclient->path, '/'), sizeof (path)); -    else { -      /* -       * vclient->path configured as relative path during config? Should -       * really never happen for sensible config -       */ -      strlcat (path, "/", sizeof (path)); -      strlcat (path, vclient->path, sizeof (path)); -    } -  } -  path[sizeof(path)-1] = '\0'; +  if (!vclient->path[0]) +    snprintf(vclient->path, sizeof(vclient->path), "%s/%s.vty", +                    vty_sock_path, vclient->name); +  path = vclient->path;    /* Stat socket to see if we have permission to access it. */    ret = stat (path, &s_stat); @@ -2981,24 +2967,14 @@ static void  vtysh_update_all_insances(struct vtysh_client * head_client)  {    struct vtysh_client *client; -  char *ptr; -  char vty_dir[MAXPATHLEN];    DIR *dir;    struct dirent *file;    int n = 0;    if (head_client->flag != VTYSH_OSPFD) return; -  if (vty_sock_path == NULL) -    /* ls DAEMON_VTY_DIR and look for all files ending in .vty */ -    strlcpy(vty_dir, DAEMON_VTY_DIR "/", MAXPATHLEN); -  else -    { -    /* ls vty_sock_dir and look for all files ending in .vty */ -    strlcpy(vty_dir, vty_sock_path, MAXPATHLEN); -    strlcat(vty_dir, "/", MAXPATHLEN); -    } -  dir = opendir(vty_dir); +  /* ls vty_sock_dir and look for all files ending in .vty */ +  dir = opendir(vty_sock_path);    if (dir)      {        while ((file = readdir(dir)) != NULL) @@ -3009,16 +2985,15 @@ vtysh_update_all_insances(struct vtysh_client * head_client)                  {                    fprintf(stderr,                            "Parsing %s, client limit(%d) reached!\n", -                          vty_dir, n); +                          vty_sock_path, n);                    break;                  }                client = (struct vtysh_client *) malloc(sizeof(struct vtysh_client));                client->fd = -1;  	      client->name = "ospfd";                client->flag = VTYSH_OSPFD; -              ptr = (char *) malloc(100); -              sprintf(ptr, "%s%s", vty_dir, file->d_name); -	      client->path = (const char *)ptr; +              snprintf(client->path, sizeof(client->path), "%s/%s", +                              vty_sock_path, file->d_name);                client->next = NULL;                vtysh_client_sorted_insert(head_client, client);                n++; diff --git a/vtysh/vtysh.h b/vtysh/vtysh.h index 61774b7d0e..07ba8367de 100644 --- a/vtysh/vtysh.h +++ b/vtysh/vtysh.h @@ -97,6 +97,6 @@ extern int execute_flag;  extern struct vty *vty; -extern char * vty_sock_path; +extern const char * vty_sock_path;  #endif /* VTYSH_H */ diff --git a/vtysh/vtysh_main.c b/vtysh/vtysh_main.c index 3dffa05ace..bf62850e22 100644 --- a/vtysh/vtysh_main.c +++ b/vtysh/vtysh_main.c @@ -37,6 +37,7 @@  #include "memory.h"  #include "linklist.h"  #include "memory_vty.h" +#include "libfrr.h"  #include "vtysh/vtysh.h"  #include "vtysh/vtysh_user.h" @@ -54,7 +55,7 @@ char history_file[MAXPATHLEN];  int execute_flag = 0;  /* VTY Socket prefix */ -char * vty_sock_path = NULL; +const char * vty_sock_path = NULL;  /* For sigsetjmp() & siglongjmp(). */  static sigjmp_buf jmpbuf; @@ -402,6 +403,9 @@ main (int argc, char **argv, char **env)  	}      } +  if (!vty_sock_path) +    vty_sock_path = frr_vtydir; +    if (markfile + writeconfig + dryrun + boot_flag > 1)      {        fprintf (stderr, "Invalid combination of arguments.  Please specify at " diff --git a/watchfrr/watchfrr.c b/watchfrr/watchfrr.c index f61dd3858b..413a631005 100644 --- a/watchfrr/watchfrr.c +++ b/watchfrr/watchfrr.c @@ -26,6 +26,7 @@  #include <lib/version.h>  #include "command.h"  #include "memory_vty.h" +#include "libfrr.h"  #include <getopt.h>  #include <sys/un.h> @@ -202,13 +203,10 @@ static int wakeup_send_echo(struct thread *t_wakeup);  static void try_restart(struct daemon *dmn);  static void phase_check(void); -static int usage(const char *progname, int status) +static const char *progname; +static void printhelp(FILE *target)  { -	if (status != 0) -		fprintf(stderr, "Try `%s --help' for more information.\n", -			progname); -	else { -		printf("Usage : %s [OPTION...] <daemon name> ...\n\n\ +	fprintf(target, "Usage : %s [OPTION...] <daemon name> ...\n\n\  Watchdog program to monitor status of frr daemons and try to restart\n\  them if they are down or unresponsive.  It determines whether a daemon is\n\  up based on whether it can connect to the daemon's vty unix stream socket.\n\ @@ -254,7 +252,7 @@ a restart is attempted: if the time since the last restart attempt exceeds\n\  twice the -M value, then the restart delay is set to the -m value.\n\  Otherwise, the interval is doubled (but capped at the -M value).\n\n", progname, mode_str[0], progname, mode_str[1], progname, mode_str[2], progname, mode_str[3], progname, mode_str[4], progname, mode_str[2], mode_str[3]); -		printf("Options:\n\ +	fprintf(target, "Options:\n\  -d, --daemon	Run in daemon mode.  In this mode, error messages are sent\n\  		to syslog instead of stdout.\n\  -S, --statedir	Set the vty socket directory (default is %s)\n\ @@ -313,9 +311,6 @@ Otherwise, the interval is doubled (but capped at the -M value).\n\n", progname,  		passing command-line arguments with embedded spaces.\n\  -v, --version	Print program version\n\  -h, --help	Display this help and exit\n", VTYDIR, DEFAULT_LOGLEVEL, LOG_EMERG, LOG_DEBUG, LOG_DEBUG, DEFAULT_MIN_RESTART, DEFAULT_MAX_RESTART, DEFAULT_PERIOD, DEFAULT_TIMEOUT, DEFAULT_RESTART_TIMEOUT, DEFAULT_PIDFILE); -	} - -	return status;  }  static pid_t run_background(char *shell_cmd) @@ -1011,38 +1006,48 @@ struct zebra_privs_t watchfrr_privs = {  #endif  }; +static struct quagga_signal_t watchfrr_signals[] = { +	{ +		.signal = SIGINT, +		.handler = sigint, +	}, +	{ +		.signal = SIGTERM, +		.handler = sigint, +	}, +	{ +		.signal = SIGCHLD, +		.handler = sigchild, +	}, +}; + +FRR_DAEMON_INFO(watchfrr, WATCHFRR, +	.flags = FRR_NO_PRIVSEP | FRR_NO_TCPVTY | FRR_LIMITED_CLI +		| FRR_NO_CFG_PID_DRY | FRR_NO_ZCLIENT, + +	.printhelp = printhelp, +	.copyright = "Copyright 2004 Andrew J. Schorr", + +	.signals = watchfrr_signals, +	.n_signals = array_size(watchfrr_signals), + +	.privs = &watchfrr_privs, +) +  int main(int argc, char **argv)  { -	const char *progname;  	int opt; -	int daemon_mode = 0;  	const char *pidfile = DEFAULT_PIDFILE;  	const char *special = "zebra";  	const char *blankstr = NULL; -	static struct quagga_signal_t my_signals[] = { -		{ -			.signal = SIGINT, -			.handler = sigint, -		}, -		{ -			.signal = SIGTERM, -			.handler = sigint, -		}, -		{ -			.signal = SIGCHLD, -			.handler = sigchild, -		}, -	}; -	if ((progname = strrchr(argv[0], '/')) != NULL) -		progname++; -	else -		progname = argv[0]; +	frr_preinit(&watchfrr_di, argc, argv); +	progname = watchfrr_di.progname; + +	frr_opt_add("aAb:dek:l:m:M:i:p:r:R:S:s:t:T:z", longopts, "");  	gs.restart.name = "all"; -	while ((opt = -		getopt_long(argc, argv, "aAb:dek:l:m:M:i:p:r:R:S:s:t:T:zvh", -			    longopts, 0)) != EOF) { +	while ((opt = frr_getopt(argc, argv, NULL)) != EOF) {  		switch (opt) {  		case 0:  			break; @@ -1051,7 +1056,7 @@ int main(int argc, char **argv)  			    && (gs.mode != MODE_SEPARATE_RESTART)) {  				fputs("Ambiguous operating mode selected.\n",  				      stderr); -				return usage(progname, 1); +				frr_help_exit(1);  			}  			gs.mode = MODE_PHASED_ZEBRA_RESTART;  			break; @@ -1060,16 +1065,13 @@ int main(int argc, char **argv)  			    && (gs.mode != MODE_SEPARATE_RESTART)) {  				fputs("Ambiguous operating mode selected.\n",  				      stderr); -				return usage(progname, 1); +				frr_help_exit(1);  			}  			gs.mode = MODE_PHASED_ALL_RESTART;  			break;  		case 'b':  			blankstr = optarg;  			break; -		case 'd': -			daemon_mode = 1; -			break;  		case 'e':  			gs.do_ping = 0;  			break; @@ -1078,7 +1080,7 @@ int main(int argc, char **argv)  				fprintf(stderr,  					"Invalid kill command, must contain '%%s': %s\n",  					optarg); -				return usage(progname, 1); +				frr_help_exit(1);  			}  			gs.stop_command = optarg;  			break; @@ -1092,7 +1094,7 @@ int main(int argc, char **argv)  					fprintf(stderr,  						"Invalid loglevel argument: %s\n",  						optarg); -					return usage(progname, 1); +					frr_help_exit(1);  				}  			}  			break; @@ -1106,7 +1108,7 @@ int main(int argc, char **argv)  					fprintf(stderr,  						"Invalid min_restart_interval argument: %s\n",  						optarg); -					return usage(progname, 1); +					frr_help_exit(1);  				}  			}  			break; @@ -1120,7 +1122,7 @@ int main(int argc, char **argv)  					fprintf(stderr,  						"Invalid max_restart_interval argument: %s\n",  						optarg); -					return usage(progname, 1); +					frr_help_exit(1);  				}  			}  			break; @@ -1133,7 +1135,7 @@ int main(int argc, char **argv)  					fprintf(stderr,  						"Invalid interval argument: %s\n",  						optarg); -					return usage(progname, 1); +					frr_help_exit(1);  				}  				gs.period = 1000 * period;  			} @@ -1146,13 +1148,13 @@ int main(int argc, char **argv)  			    (gs.mode == MODE_SEPARATE_RESTART)) {  				fputs("Ambiguous operating mode selected.\n",  				      stderr); -				return usage(progname, 1); +				frr_help_exit(1);  			}  			if (!valid_command(optarg)) {  				fprintf(stderr,  					"Invalid restart command, must contain '%%s': %s\n",  					optarg); -				return usage(progname, 1); +				frr_help_exit(1);  			}  			gs.restart_command = optarg;  			if (gs.mode == MODE_MONITOR) @@ -1162,13 +1164,13 @@ int main(int argc, char **argv)  			if (gs.mode != MODE_MONITOR) {  				fputs("Ambiguous operating mode selected.\n",  				      stderr); -				return usage(progname, 1); +				frr_help_exit(1);  			}  			if (strchr(optarg, '%')) {  				fprintf(stderr,  					"Invalid restart-all arg, must not contain '%%s': %s\n",  					optarg); -				return usage(progname, 1); +				frr_help_exit(1);  			}  			gs.restart_command = optarg;  			gs.mode = MODE_GLOBAL_RESTART; @@ -1178,7 +1180,7 @@ int main(int argc, char **argv)  				fprintf(stderr,  					"Invalid start command, must contain '%%s': %s\n",  					optarg); -				return usage(progname, 1); +				frr_help_exit(1);  			}  			gs.start_command = optarg;  			break; @@ -1194,7 +1196,7 @@ int main(int argc, char **argv)  					fprintf(stderr,  						"Invalid timeout argument: %s\n",  						optarg); -					return usage(progname, 1); +					frr_help_exit(1);  				}  			}  			break; @@ -1208,29 +1210,23 @@ int main(int argc, char **argv)  					fprintf(stderr,  						"Invalid restart timeout argument: %s\n",  						optarg); -					return usage(progname, 1); +					frr_help_exit(1);  				}  			}  			break;  		case 'z':  			gs.unresponsive_restart = 1;  			break; -		case 'v': -			printf("%s version %s\n", progname, FRR_VERSION); -			puts("Copyright 2004 Andrew J. Schorr"); -			return 0; -		case 'h': -			return usage(progname, 0);  		default:  			fputs("Invalid option.\n", stderr); -			return usage(progname, 1); +			frr_help_exit(1);  		}  	}  	if (gs.unresponsive_restart && (gs.mode == MODE_MONITOR)) {  		fputs("Option -z requires a -r or -R restart option.\n",  		      stderr); -		return usage(progname, 1); +		frr_help_exit(1);  	}  	switch (gs.mode) {  	case MODE_MONITOR: @@ -1238,7 +1234,7 @@ int main(int argc, char **argv)  			fprintf(stderr,  				"No kill/(re)start commands needed for %s mode.\n",  				mode_str[gs.mode]); -			return usage(progname, 1); +			frr_help_exit(1);  		}  		break;  	case MODE_GLOBAL_RESTART: @@ -1247,7 +1243,7 @@ int main(int argc, char **argv)  			fprintf(stderr,  				"No start/kill commands needed in [%s] mode.\n",  				mode_str[gs.mode]); -			return usage(progname, 1); +			frr_help_exit(1);  		}  		break;  	case MODE_PHASED_ZEBRA_RESTART: @@ -1257,7 +1253,7 @@ int main(int argc, char **argv)  			fprintf(stderr,  				"Need start, kill, and restart commands in [%s] mode.\n",  				mode_str[gs.mode]); -			return usage(progname, 1); +			frr_help_exit(1);  		}  		break;  	} @@ -1276,17 +1272,22 @@ int main(int argc, char **argv)  	gs.restart.interval = gs.min_restart_interval; -	zprivs_init(&watchfrr_privs); +	master = frr_init(); + +	zlog_set_level(ZLOG_DEST_MONITOR, ZLOG_DISABLED); +	if (watchfrr_di.daemon_mode) { +		zlog_set_level(ZLOG_DEST_SYSLOG, MIN(gs.loglevel, LOG_DEBUG)); +		if (daemon (0, 0) < 0) { +			fprintf(stderr, "Watchquagga daemon failed: %s", +					strerror(errno)); +			exit (1); +		} +	} else +		zlog_set_level(ZLOG_DEST_STDOUT, MIN(gs.loglevel, LOG_DEBUG)); -	master = thread_master_create(); -	cmd_init(-1); -	memory_init(); -	vty_init(master);  	watchfrr_vty_init(); -	vty_serv_sock(NULL, 0, WATCHFRR_VTYSH_PATH); -	signal_init(master, array_size(my_signals), my_signals); -	srandom(time(NULL)); +	frr_vty_serv();  	{  		int i; @@ -1324,31 +1325,16 @@ int main(int argc, char **argv)  	}  	if (!gs.daemons) {  		fputs("Must specify one or more daemons to monitor.\n", stderr); -		return usage(progname, 1); +		frr_help_exit(1);  	}  	if (((gs.mode == MODE_PHASED_ZEBRA_RESTART) ||  	     (gs.mode == MODE_PHASED_ALL_RESTART)) && !gs.special) {  		fprintf(stderr,  			"In mode [%s], but cannot find master daemon %s\n",  			mode_str[gs.mode], special); -		return usage(progname, 1); +		frr_help_exit(1);  	} -	zlog_default = openzlog(progname, ZLOG_WATCHFRR, 0, -				LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); -	zlog_set_level(NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED); -	if (daemon_mode) { -		zlog_set_level(NULL, ZLOG_DEST_SYSLOG, -			       MIN(gs.loglevel, LOG_DEBUG)); -		if (daemon(0, 0) < 0) { -			fprintf(stderr, "Watchfrr daemon failed: %s", -				strerror(errno)); -			exit(1); -		} -	} else -		zlog_set_level(NULL, ZLOG_DEST_STDOUT, -			       MIN(gs.loglevel, LOG_DEBUG)); -  	/* Make sure we're not already running. */  	pid_output(pidfile); diff --git a/zebra/if_ioctl_solaris.c b/zebra/if_ioctl_solaris.c index f27dc89007..339e00b22c 100644 --- a/zebra/if_ioctl_solaris.c +++ b/zebra/if_ioctl_solaris.c @@ -59,7 +59,7 @@ interface_list_ioctl (int af)    char *buf = NULL;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    sock = socket (af, SOCK_DGRAM, 0);    if (sock < 0) @@ -68,7 +68,7 @@ interface_list_ioctl (int af)                   (af == AF_INET ? "AF_INET" : "AF_INET6"), safe_strerror (errno));        if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        return -1;      } @@ -80,7 +80,7 @@ calculate_lifc_len:     /* must hold privileges to enter here */    save_errno = errno;    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    if (ret < 0)      { @@ -117,7 +117,7 @@ calculate_lifc_len:     /* must hold privileges to enter here */    lifconf.lifc_buf = buf;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    ret = ioctl (sock, SIOCGLIFCONF, &lifconf); @@ -129,13 +129,13 @@ calculate_lifc_len:     /* must hold privileges to enter here */        zlog_warn ("SIOCGLIFCONF: %s", safe_strerror (errno));        if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        goto end;      }    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    /* Allocate interface. */    lifreq = lifconf.lifc_req; diff --git a/zebra/if_sysctl.c b/zebra/if_sysctl.c index 0b71c76214..8326f0e396 100644 --- a/zebra/if_sysctl.c +++ b/zebra/if_sysctl.c @@ -69,7 +69,7 @@ ifstat_update_sysctl (void)    /* Fetch interface informations into allocated buffer. */    if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)       { -      zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno)); +      zlog_warn("sysctl error by %s", safe_strerror(errno));        XFREE(MTYPE_TMP, ref);        return;      } @@ -120,7 +120,7 @@ interface_list (struct zebra_ns *zns)    /* Query buffer size. */    if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)       { -      zlog (NULL, LOG_WARNING, "sysctl() error by %s", safe_strerror (errno)); +      zlog_warn("sysctl() error by %s", safe_strerror(errno));        return;      } @@ -130,7 +130,7 @@ interface_list (struct zebra_ns *zns)    /* Fetch interface informations into allocated buffer. */    if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)       { -      zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno)); +      zlog_warn("sysctl error by %s", safe_strerror(errno));        return;      } diff --git a/zebra/ioctl.c b/zebra/ioctl.c index 1125043310..dfd69300f9 100644 --- a/zebra/ioctl.c +++ b/zebra/ioctl.c @@ -56,20 +56,20 @@ if_ioctl (u_long request, caddr_t buffer)    int err = 0;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    sock = socket (AF_INET, SOCK_DGRAM, 0);    if (sock < 0)      {        int save_errno = errno;        if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));        exit (1);      }    if ((ret = ioctl (sock, request, buffer)) < 0)      err = errno;    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    close (sock);    if (ret < 0)  @@ -88,13 +88,13 @@ if_ioctl_ipv6 (u_long request, caddr_t buffer)    int err = 0;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    sock = socket (AF_INET6, SOCK_DGRAM, 0);    if (sock < 0)      {        int save_errno = errno;        if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        zlog_err("Cannot create IPv6 datagram socket: %s",  	       safe_strerror(save_errno));        exit (1); @@ -103,7 +103,7 @@ if_ioctl_ipv6 (u_long request, caddr_t buffer)    if ((ret = ioctl (sock, request, buffer)) < 0)      err = errno;    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    close (sock);    if (ret < 0)  @@ -162,7 +162,7 @@ if_get_mtu (struct interface *ifp)    zebra_interface_up_update(ifp);  #else -  zlog (NULL, LOG_INFO, "Can't lookup mtu on this system"); +  zlog_info("Can't lookup mtu on this system");    ifp->mtu6 = ifp->mtu = -1;  #endif  } diff --git a/zebra/ioctl_solaris.c b/zebra/ioctl_solaris.c index 1de583577d..78796a8a28 100644 --- a/zebra/ioctl_solaris.c +++ b/zebra/ioctl_solaris.c @@ -53,14 +53,14 @@ if_ioctl (u_long request, caddr_t buffer)    int err;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    sock = socket (AF_INET, SOCK_DGRAM, 0);    if (sock < 0)      {        int save_errno = errno;        if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));        exit (1);      } @@ -69,7 +69,7 @@ if_ioctl (u_long request, caddr_t buffer)      err = errno;    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    close (sock); @@ -90,14 +90,14 @@ if_ioctl_ipv6 (u_long request, caddr_t buffer)    int err;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    sock = socket (AF_INET6, SOCK_DGRAM, 0);    if (sock < 0)      {        int save_errno = errno;        if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        zlog_err("Cannot create IPv6 datagram socket: %s",  	       safe_strerror(save_errno));        exit (1); @@ -107,7 +107,7 @@ if_ioctl_ipv6 (u_long request, caddr_t buffer)      err = errno;    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    close (sock); diff --git a/zebra/ipforward_sysctl.c b/zebra/ipforward_sysctl.c index be4c9cd99c..28894f4e0a 100644 --- a/zebra/ipforward_sysctl.c +++ b/zebra/ipforward_sysctl.c @@ -61,16 +61,16 @@ ipforward_on (void)    len = sizeof ipforwarding;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    if (sysctl (mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0)      {        if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        zlog_warn ("Can't set ipforwarding on");        return -1;      }    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    return ipforwarding;  } @@ -82,16 +82,16 @@ ipforward_off (void)    len = sizeof ipforwarding;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    if (sysctl (mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0)      {        if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        zlog_warn ("Can't set ipforwarding on");        return -1;      }    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    return ipforwarding;  } @@ -117,16 +117,16 @@ ipforward_ipv6 (void)    len = sizeof ip6forwarding;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    if (sysctl (mib_ipv6, MIB_SIZ, &ip6forwarding, &len, 0, 0) < 0)      {       if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        zlog_warn ("can't get ip6forwarding value");        return -1;      }    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    return ip6forwarding;  } @@ -138,16 +138,16 @@ ipforward_ipv6_on (void)    len = sizeof ip6forwarding;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    if (sysctl (mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0)      {       if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        zlog_warn ("can't get ip6forwarding value");        return -1;      }    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    return ip6forwarding;  } @@ -159,15 +159,15 @@ ipforward_ipv6_off (void)    len = sizeof ip6forwarding;    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    if (sysctl (mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0)      {        if (zserv_privs.change(ZPRIVS_LOWER)) -        zlog (NULL, LOG_ERR, "Can't lower privileges"); +        zlog_err("Can't lower privileges");        zlog_warn ("can't get ip6forwarding value");        return -1;      }    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    return ip6forwarding;  } diff --git a/zebra/kernel_netlink.c b/zebra/kernel_netlink.c index c9c2d90eac..49394bd6f8 100644 --- a/zebra/kernel_netlink.c +++ b/zebra/kernel_netlink.c @@ -143,8 +143,8 @@ netlink_recvbuf (struct nlsock *nl, uint32_t newsize)    ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &oldsize, &oldlen);    if (ret < 0)      { -      zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name, -	    safe_strerror (errno)); +      zlog_err("Can't get %s receive buffer size: %s", nl->name, +               safe_strerror(errno));        return -1;      } @@ -160,22 +160,21 @@ netlink_recvbuf (struct nlsock *nl, uint32_t newsize)  		      sizeof(nl_rcvbufsize));    if (ret < 0)      { -      zlog (NULL, LOG_ERR, "Can't set %s receive buffer size: %s", nl->name, -	    safe_strerror (errno)); +      zlog_err("Can't set %s receive buffer size: %s", nl->name, +               safe_strerror(errno));        return -1;      }    ret = getsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, &newsize, &newlen);    if (ret < 0)      { -      zlog (NULL, LOG_ERR, "Can't get %s receive buffer size: %s", nl->name, -	    safe_strerror (errno)); +      zlog_err("Can't get %s receive buffer size: %s", nl->name, +               safe_strerror(errno));        return -1;      } -  zlog (NULL, LOG_INFO, -	"Setting netlink socket receive buffer size: %u -> %u", -	oldsize, newsize); +  zlog_info("Setting netlink socket receive buffer size: %u -> %u", oldsize, +            newsize);    return 0;  } @@ -191,15 +190,14 @@ netlink_socket (struct nlsock *nl, unsigned long groups, ns_id_t ns_id)    if (zserv_privs.change (ZPRIVS_RAISE))      { -      zlog (NULL, LOG_ERR, "Can't raise privileges"); +      zlog_err("Can't raise privileges");        return -1;      }    sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);    if (sock < 0)      { -      zlog (NULL, LOG_ERR, "Can't open %s socket: %s", nl->name, -            safe_strerror (errno)); +      zlog_err("Can't open %s socket: %s", nl->name, safe_strerror(errno));        return -1;      } @@ -211,12 +209,12 @@ netlink_socket (struct nlsock *nl, unsigned long groups, ns_id_t ns_id)    ret = bind (sock, (struct sockaddr *) &snl, sizeof snl);    save_errno = errno;    if (zserv_privs.change (ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    if (ret < 0)      { -      zlog (NULL, LOG_ERR, "Can't bind %s socket to group 0x%x: %s", -            nl->name, snl.nl_groups, safe_strerror (save_errno)); +      zlog_err("Can't bind %s socket to group 0x%x: %s", nl->name, +               snl.nl_groups, safe_strerror(save_errno));        close (sock);        return -1;      } @@ -226,8 +224,7 @@ netlink_socket (struct nlsock *nl, unsigned long groups, ns_id_t ns_id)    ret = getsockname (sock, (struct sockaddr *) &snl, (socklen_t *) &namelen);    if (ret < 0 || namelen != sizeof snl)      { -      zlog (NULL, LOG_ERR, "Can't get %s socket name: %s", nl->name, -            safe_strerror (errno)); +      zlog_err("Can't get %s socket name: %s", nl->name, safe_strerror(errno));        close (sock);        return -1;      } @@ -244,7 +241,7 @@ netlink_information_fetch (struct sockaddr_nl *snl, struct nlmsghdr *h,    /* JF: Ignore messages that aren't from the kernel */    if ( snl->nl_pid != 0 )      { -      zlog ( NULL, LOG_ERR, "Ignoring message from pid %u", snl->nl_pid ); +      zlog_err("Ignoring message from pid %u", snl->nl_pid);        return 0;      } @@ -493,8 +490,7 @@ netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *,              continue;            if (errno == EWOULDBLOCK || errno == EAGAIN)              break; -          zlog (NULL, LOG_ERR, "%s recvmsg overrun: %s", -                nl->name, safe_strerror(errno)); +          zlog_err("%s recvmsg overrun: %s", nl->name, safe_strerror(errno));            /*             *  In this case we are screwed.             *  There is no good way to @@ -506,14 +502,14 @@ netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *,        if (status == 0)          { -          zlog (NULL, LOG_ERR, "%s EOF", nl->name); +          zlog_err("%s EOF", nl->name);            return -1;          }        if (msg.msg_namelen != sizeof snl)          { -          zlog (NULL, LOG_ERR, "%s sender address length error: length %d", -                nl->name, msg.msg_namelen); +          zlog_err("%s sender address length error: length %d", nl->name, +                   msg.msg_namelen);            return -1;          } @@ -558,8 +554,7 @@ netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *,                if (h->nlmsg_len < NLMSG_LENGTH (sizeof (struct nlmsgerr)))                  { -                  zlog (NULL, LOG_ERR, "%s error: message truncated", -                        nl->name); +                  zlog_err("%s error: message truncated", nl->name);                    return -1;                  } @@ -627,7 +622,7 @@ netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *,            error = (*filter) (&snl, h, zns->ns_id, startup);            if (error < 0)              { -              zlog (NULL, LOG_ERR, "%s filter function error", nl->name); +              zlog_err("%s filter function error", nl->name);                ret = error;              }          } @@ -635,13 +630,12 @@ netlink_parse_info (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *,        /* After error care. */        if (msg.msg_flags & MSG_TRUNC)          { -          zlog (NULL, LOG_ERR, "%s error: message truncated", nl->name); +          zlog_err("%s error: message truncated", nl->name);            continue;          }        if (status)          { -          zlog (NULL, LOG_ERR, "%s error: data remnant size %d", nl->name, -                status); +          zlog_err("%s error: data remnant size %d", nl->name, status);            return -1;          }      } @@ -696,11 +690,11 @@ netlink_talk (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *,    /* Send message to netlink interface. */    if (zserv_privs.change (ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    status = sendmsg (nl->sock, &msg, 0);    save_errno = errno;    if (zserv_privs.change (ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND)      { @@ -710,8 +704,7 @@ netlink_talk (int (*filter) (struct sockaddr_nl *, struct nlmsghdr *,    if (status < 0)      { -      zlog (NULL, LOG_ERR, "netlink_talk sendmsg() error: %s", -            safe_strerror (save_errno)); +      zlog_err("netlink_talk sendmsg() error: %s", safe_strerror(save_errno));        return -1;      } @@ -740,7 +733,7 @@ netlink_request (int family, int type, struct nlsock *nl)    /* Check netlink socket. */    if (nl->sock < 0)      { -      zlog (NULL, LOG_ERR, "%s socket isn't active.", nl->name); +      zlog_err("%s socket isn't active.", nl->name);        return -1;      } @@ -760,7 +753,7 @@ netlink_request (int family, int type, struct nlsock *nl)     */    if (zserv_privs.change (ZPRIVS_RAISE))      { -      zlog (NULL, LOG_ERR, "Can't raise privileges"); +      zlog_err("Can't raise privileges");        return -1;      } @@ -769,12 +762,11 @@ netlink_request (int family, int type, struct nlsock *nl)    save_errno = errno;    if (zserv_privs.change (ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    if (ret < 0)      { -      zlog (NULL, LOG_ERR, "%s sendto failed: %s", nl->name, -            safe_strerror (save_errno)); +      zlog_err("%s sendto failed: %s", nl->name, safe_strerror(save_errno));        return -1;      } diff --git a/zebra/kernel_socket.c b/zebra/kernel_socket.c index 20a0472195..1bb85a31ab 100644 --- a/zebra/kernel_socket.c +++ b/zebra/kernel_socket.c @@ -826,9 +826,8 @@ rtm_read_mesg (struct rt_msghdr *rtm,    /* rt_msghdr version check. */    if (rtm->rtm_version != RTM_VERSION)  -      zlog (NULL, LOG_WARNING, -	      "Routing message version different %d should be %d." -	      "This may cause problem\n", rtm->rtm_version, RTM_VERSION); +      zlog_warn("Routing message version different %d should be %d." "This may cause problem\n", +                rtm->rtm_version, RTM_VERSION);    /* Be sure structure is cleared */    memset (dest, 0, sizeof (union sockunion)); @@ -852,7 +851,7 @@ rtm_read_mesg (struct rt_msghdr *rtm,    /* Assert read up to the end of pointer. */    if (pnt != end)  -      zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data."); +      zlog_warn("rtm_read() doesn't read all socket data.");    return rtm->rtm_flags;  } diff --git a/zebra/main.c b/zebra/main.c index b72ce84cd6..b70ce7eda7 100644 --- a/zebra/main.c +++ b/zebra/main.c @@ -35,7 +35,7 @@  #include "privs.h"  #include "sigevent.h"  #include "vrf.h" -#include "sockopt.h" +#include "libfrr.h"  #include "zebra/rib.h"  #include "zebra/zserv.h" @@ -60,9 +60,6 @@ struct zebra_t zebrad =  /* process id. */  pid_t pid; -/* VTY Socket prefix */ -char vty_sock_path[MAXPATHLEN] = ZEBRA_VTYSH_PATH; -  /* Pacify zclient.o in libfrr, which expects this variable. */  struct thread_master *master; @@ -81,30 +78,18 @@ u_int32_t nl_rcvbufsize = 4194304;  #endif /* HAVE_NETLINK */  /* Command line options. */ -#define OPTION_VTYSOCK 1000  struct option longopts[] =   {    { "batch",        no_argument,       NULL, 'b'}, -  { "daemon",       no_argument,       NULL, 'd'},    { "allow_delete", no_argument,       NULL, 'a'},    { "keep_kernel",  no_argument,       NULL, 'k'},    { "fpm_format",   required_argument, NULL, 'F'}, -  { "config_file",  required_argument, NULL, 'f'}, -  { "pid_file",     required_argument, NULL, 'i'},    { "socket",       required_argument, NULL, 'z'}, -  { "help",         no_argument,       NULL, 'h'}, -  { "vty_addr",     required_argument, NULL, 'A'}, -  { "vty_port",     required_argument, NULL, 'P'}, -  { "vty_socket",   required_argument, NULL, OPTION_VTYSOCK },    { "ecmp",         required_argument, NULL, 'e'},    { "retain",       no_argument,       NULL, 'r'}, -  { "dryrun",       no_argument,       NULL, 'C'},  #ifdef HAVE_NETLINK    { "nl-bufsize",   required_argument, NULL, 's'},  #endif /* HAVE_NETLINK */ -  { "user",         required_argument, NULL, 'u'}, -  { "group",        required_argument, NULL, 'g'}, -  { "version",      no_argument,       NULL, 'v'},    { 0 }  }; @@ -130,54 +115,8 @@ struct zebra_privs_t zserv_privs =    .cap_num_i = 0  }; -/* Default configuration file path. */ -char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE; - -/* Process ID saved for use by init system */ -const char *pid_file = PATH_ZEBRA_PID; -  unsigned int multipath_num = MULTIPATH_NUM; -/* Help information display. */ -static void -usage (char *progname, int status) -{ -  if (status != 0) -    fprintf (stderr, "Try `%s --help' for more information.\n", progname); -  else -    {     -      printf ("Usage : %s [OPTION...]\n\n"\ -	      "Daemon which manages kernel routing table management and "\ -	      "redistribution between different routing protocols.\n\n"\ -	      "-b, --batch        Runs in batch mode\n"\ -	      "-d, --daemon       Runs in daemon mode\n"\ -	      "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \ -	      "-f, --config_file  Set configuration file name\n"\ -	      "-F, --fpm_format   Set fpm format to 'netlink' or 'protobuf'\n"\ -	      "-i, --pid_file     Set process identifier file name\n"\ -	      "-z, --socket       Set path of zebra socket\n"\ -	      "-k, --keep_kernel  Don't delete old routes which installed by "\ -				  "zebra.\n"\ -	      "-C, --dryrun       Check configuration for validity and exit\n"\ -	      "-A, --vty_addr     Set vty's bind address\n"\ -	      "-P, --vty_port     Set vty's port number\n"\ -	      "    --vty_socket   Override vty socket path\n"\ -	      "-r, --retain       When program terminates, retain added route "\ -				  "by zebra.\n"\ -	      "-u, --user         User to run as\n"\ -	      "-g, --group        Group to run as\n", progname); -#ifdef HAVE_NETLINK -      printf ("-s, --nl-bufsize   Set netlink receive buffer size\n"); -#endif /* HAVE_NETLINK */ -      printf ("-v, --version      Print program version\n"\ -	      "-h, --help         Display this help and exit\n"\ -	      "\n"\ -	      "Report bugs to %s\n", FRR_BUG_ADDRESS); -    } - -  exit (status); -} -  /* SIGHUP handler. */  static void   sighup (void) @@ -229,8 +168,7 @@ sigint (void)      work_queue_free (zebrad.lsp_process_q);    meta_queue_free (zebrad.mq);    thread_master_free (zebrad.master); -  if (zlog_default) -    closezlog (zlog_default); +  closezlog ();    exit (0);  } @@ -239,7 +177,7 @@ sigint (void)  static void  sigusr1 (void)  { -  zlog_rotate (NULL); +  zlog_rotate();  }  struct quagga_signal_t zebra_signals[] = @@ -262,44 +200,49 @@ struct quagga_signal_t zebra_signals[] =    },  }; +FRR_DAEMON_INFO(zebra, ZEBRA, +	.vty_port = ZEBRA_VTY_PORT, +	.flags = FRR_NO_ZCLIENT, + +	.proghelp = "Daemon which manages kernel routing table management " +		"and\nredistribution between different routing protocols.", + +	.signals = zebra_signals, +	.n_signals = array_size(zebra_signals), + +	.privs = &zserv_privs, +) +  /* Main startup routine. */  int  main (int argc, char **argv)  { -  char *p; -  char *vty_addr = NULL; -  int vty_port = ZEBRA_VTY_PORT; -  int dryrun = 0; -  int batch_mode = 0; -  int daemon_mode = 0; -  char *config_file = NULL; -  char *progname; -  struct thread thread; +  // int batch_mode = 0;    char *zserv_path = NULL;    char *fpm_format = NULL; -  /* Set umask before anything for security */ -  umask (0027); +  frr_preinit(&zebra_di, argc, argv); -  /* preserve my name */ -  progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); - -  zlog_default = openzlog (progname, ZLOG_ZEBRA, 0, -			   LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); -  zprivs_init (&zserv_privs); -#if defined(HAVE_CUMULUS) -  zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl); +  frr_opt_add("bakF:z:e:r" +#ifdef HAVE_NETLINK +	"s:"  #endif +	, longopts, +	"  -b, --batch        Runs in batch mode\n" +	"  -a, --allow_delete Allow other processes to delete Quagga Routes\n" +	"  -F, --fpm_format   Set fpm format to 'netlink' or 'protobuf'\n" +	"  -z, --socket       Set path of zebra socket\n" +	"  -e, --ecmp         Specify ECMP to use.\n" +	"  -k, --keep_kernel  Don't delete old routes which installed by zebra.\n" +	"  -r, --retain       When program terminates, retain added route by zebra.\n" +#ifdef HAVE_NETLINK +	"  -s, --nl-bufsize   Set netlink receive buffer size\n" +#endif /* HAVE_NETLINK */ +	); -  while (1)  +  while (1)      { -      int opt; -   -#ifdef HAVE_NETLINK   -      opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vs:C", longopts, 0); -#else -      opt = getopt_long (argc, argv, "bdakf:F:i:z:hA:P:ru:g:vC", longopts, 0); -#endif /* HAVE_NETLINK */ +      int opt = frr_getopt(argc, argv, NULL);        if (opt == EOF)  	break; @@ -309,9 +252,7 @@ main (int argc, char **argv)  	case 0:  	  break;  	case 'b': -	  batch_mode = 1; -	case 'd': -	  daemon_mode = 1; +	  // batch_mode = 1;  	  break;  	case 'a':  	  allow_delete = 1; @@ -319,18 +260,9 @@ main (int argc, char **argv)  	case 'k':  	  keep_kernel_mode = 1;  	  break; -	case 'C': -	  dryrun = 1; -	  break; -	case 'f': -	  config_file = optarg; -	  break;  	case 'F':  	  fpm_format = optarg;  	  break; -	case 'A': -	  vty_addr = optarg; -	  break;          case 'e':            multipath_num = atoi (optarg);            if (multipath_num > MULTIPATH_NUM || multipath_num <= 0) @@ -339,27 +271,9 @@ main (int argc, char **argv)                return 1;              }            break; -        case 'i': -          pid_file = optarg; -          break;  	case 'z':  	  zserv_path = optarg;  	  break; -	case 'P': -	  /* Deal with atoi() returning 0 on failure, and zebra not -	     listening on zebra port... */ -	  if (strcmp(optarg, "0") == 0)  -	    { -	      vty_port = 0; -	      break; -	    }  -	  vty_port = atoi (optarg); -	  if (vty_port <= 0 || vty_port > 0xffff) -	    vty_port = ZEBRA_VTY_PORT; -	  break; -	case OPTION_VTYSOCK: -	  set_socket_path(vty_sock_path, ZEBRA_VTYSH_PATH, optarg, sizeof (vty_sock_path)); -	  break;  	case 'r':  	  retain_mode = 1;  	  break; @@ -368,34 +282,14 @@ main (int argc, char **argv)  	  nl_rcvbufsize = atoi (optarg);  	  break;  #endif /* HAVE_NETLINK */ -	case 'u': -	  zserv_privs.user = optarg; -	  break; -	case 'g': -	  zserv_privs.group = optarg; -	  break; -	case 'v': -	  print_version (progname); -	  exit (0); -	  break; -	case 'h': -	  usage (progname, 0); -	  break;  	default: -	  usage (progname, 1); +	  frr_help_exit (1);  	  break;  	}      } -  /* Make master thread emulator. */ -  zebrad.master = thread_master_create (); - -  /* Vty related initialize. */ -  signal_init (zebrad.master, array_size(zebra_signals), zebra_signals); -  cmd_init (1);    vty_config_lockless (); -  vty_init (zebrad.master); -  memory_init (); +  zebrad.master = frr_init();    /* Zebra related initialize. */    zebra_init (); @@ -443,29 +337,11 @@ main (int argc, char **argv)    *  The notifications from kernel will show originating PID equal    *  to that after daemon() completes (if ever called).    */ -  vty_read_config (config_file, config_default); +  frr_config_fork(); -  /* Don't start execution if we are in dry-run mode */ -  if (dryrun) -    return(0); -   -  /* Clean up rib. */ +  /* Clean up rib -- before fork (?) */    /* rib_weed_tables (); */ -  /* Exit when zebra is working in batch mode. */ -  if (batch_mode) -    exit (0); - -  /* Daemonize. */ -  if (daemon_mode && daemon (0, 0) < 0) -    { -      zlog_err("Zebra daemon failed: %s", strerror(errno)); -      exit (1); -    } - -  /* Output pid of zebra. */ -  pid_output (pid_file); -    /* After we have successfully acquired the pidfile, we can be sure    *  about being the only copy of zebra process, which is submitting    *  changes to the FIB. @@ -483,14 +359,7 @@ main (int argc, char **argv)    /* This must be done only after locking pidfile (bug #403). */    zebra_zserv_socket_init (zserv_path); -  /* Make vty server socket. */ -  vty_serv_sock (vty_addr, vty_port, vty_sock_path); - -  /* Print banner. */ -  zlog_notice ("Zebra %s starting: vty@%d", FRR_VERSION, vty_port); - -  while (thread_fetch (zebrad.master, &thread)) -    thread_call (&thread); +  frr_run (zebrad.master);    /* Not reached... */    return 0; diff --git a/zebra/rt_socket.c b/zebra/rt_socket.c index a4cc4eed06..de8cc69a8e 100644 --- a/zebra/rt_socket.c +++ b/zebra/rt_socket.c @@ -398,12 +398,12 @@ kernel_route_rib (struct prefix *p, struct prefix *src_p,    if (src_p && src_p->prefixlen)      { -      zlog (NULL, LOG_ERR, "route add: IPv6 sourcedest routes unsupported!"); +      zlog_err ("route add: IPv6 sourcedest routes unsupported!");        return 1;      }    if (zserv_privs.change(ZPRIVS_RAISE)) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    if (old)      route |= kernel_rtm (RTM_DELETE, p, old); @@ -412,7 +412,7 @@ kernel_route_rib (struct prefix *p, struct prefix *src_p,      route |= kernel_rtm (RTM_ADD, p, new);    if (zserv_privs.change(ZPRIVS_LOWER)) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    return route;  } diff --git a/zebra/test_main.c b/zebra/test_main.c index dea1df1697..a80b4c87c4 100644 --- a/zebra/test_main.c +++ b/zebra/test_main.c @@ -85,7 +85,7 @@ zebra_capabilities_t _caps_p [] =  char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;  /* Process ID saved for use by init system */ -const char *pid_file = PATH_ZEBRA_PID; +const char *pid_file = "testzebra.pid";  /* Help information display. */  static void @@ -181,7 +181,7 @@ sigint (void)  static void  sigusr1 (void)  { -  zlog_rotate (NULL); +  zlog_rotate();  }  struct quagga_signal_t zebra_signals[] = @@ -222,8 +222,7 @@ main (int argc, char **argv)    /* preserve my name */    progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); -  zlog_default = openzlog (progname, ZLOG_ZEBRA, 0, -			   LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); +  openzlog(progname, "ZEBRA", 0, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);    while (1)       { diff --git a/zebra/zebra_rib.c b/zebra/zebra_rib.c index 56a5f8e582..f9734fdf7d 100644 --- a/zebra/zebra_rib.c +++ b/zebra/zebra_rib.c @@ -28,6 +28,7 @@  #include "zebra_memory.h"  #include "command.h"  #include "log.h" +#include "log_int.h"  #include "sockunion.h"  #include "linklist.h"  #include "thread.h" @@ -111,7 +112,7 @@ _rnode_zlog(const char *_func, vrf_id_t vrf_id, struct route_node *rn, int prior        snprintf(buf, sizeof(buf), "{(route_node *) NULL}");      } -  zlog (NULL, priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf); +  zlog (priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf);  }  #define rnode_debug(node, vrf_id, ...) \ diff --git a/zebra/zserv.c b/zebra/zserv.c index 60364410a8..064489acd9 100644 --- a/zebra/zserv.c +++ b/zebra/zserv.c @@ -2169,7 +2169,7 @@ zebra_serv ()    sockopt_reuseport (accept_sock);    if ( zserv_privs.change(ZPRIVS_RAISE) ) -    zlog (NULL, LOG_ERR, "Can't raise privileges"); +    zlog_err("Can't raise privileges");    ret  = bind (accept_sock, (struct sockaddr *)&addr,   	       sizeof (struct sockaddr_in)); @@ -2183,7 +2183,7 @@ zebra_serv ()      }    if ( zserv_privs.change(ZPRIVS_LOWER) ) -    zlog (NULL, LOG_ERR, "Can't lower privileges"); +    zlog_err("Can't lower privileges");    ret = listen (accept_sock, 1);    if (ret < 0)  | 
