]>
git.proxmox.com Git - mirror_frr.git/blob - lib/libfrr.c
2 * libfrr overall management functions
4 * Copyright (C) 2016 David Lamparter for NetDEF, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "memory_vty.h"
31 const char frr_sysconfdir
[] = SYSCONFDIR
;
32 const char frr_vtydir
[] = DAEMON_VTY_DIR
;
34 char config_default
[256];
35 static char pidfile_default
[256];
36 static char vtypath_default
[256];
38 static char comb_optstr
[256];
39 static struct option comb_lo
[64];
40 static struct option
*comb_next_lo
= &comb_lo
[0];
41 static char comb_helpstr
[4096];
46 const struct option
*longopts
;
49 static void opt_extend(const struct optspec
*os
)
51 const struct option
*lo
;
53 strcat(comb_optstr
, os
->optstr
);
54 strcat(comb_helpstr
, os
->helpstr
);
55 for (lo
= os
->longopts
; lo
->name
; lo
++)
56 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
60 #define OPTION_VTYSOCK 1000
62 static const struct option lo_always
[] = {
63 { "help", no_argument
, NULL
, 'h' },
64 { "version", no_argument
, NULL
, 'v' },
65 { "daemon", no_argument
, NULL
, 'd' },
66 { "vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
69 static const struct optspec os_always
= {
71 " -h, --help Display this help and exit\n"
72 " -v, --version Print program version\n"
73 " -d, --daemon Runs in daemon mode\n"
74 " --vty_socket Override vty socket path\n",
79 static const struct option lo_cfg_pid_dry
[] = {
80 { "pid_file", required_argument
, NULL
, 'i' },
81 { "config_file", required_argument
, NULL
, 'f' },
82 { "dryrun", no_argument
, NULL
, 'C' },
85 static const struct optspec os_cfg_pid_dry
= {
87 " -f, --config_file Set configuration file name\n"
88 " -i, --pid_file Set process identifier file name\n"
89 " -C, --dryrun Check configuration for validity and exit\n",
94 static const struct option lo_zclient
[] = {
95 { "socket", required_argument
, NULL
, 'z' },
98 static const struct optspec os_zclient
= {
100 " -z, --socket Set path of zebra socket\n",
105 static const struct option lo_vty
[] = {
106 { "vty_addr", required_argument
, NULL
, 'A'},
107 { "vty_port", required_argument
, NULL
, 'P'},
110 static const struct optspec os_vty
= {
112 " -A, --vty_addr Set vty's bind address\n"
113 " -P, --vty_port Set vty's port number\n",
118 static const struct option lo_user
[] = {
119 { "user", required_argument
, NULL
, 'u'},
120 { "group", required_argument
, NULL
, 'g'},
123 static const struct optspec os_user
= {
125 " -u, --user User to run as\n"
126 " -g, --group Group to run as\n",
131 static struct frr_daemon_info
*di
= NULL
;
133 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
137 /* basename(), opencoded. */
138 char *p
= strrchr(argv
[0], '/');
139 di
->progname
= p
? p
+ 1 : argv
[0];
143 opt_extend(&os_always
);
144 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
145 opt_extend(&os_cfg_pid_dry
);
146 if (!(di
->flags
& FRR_NO_PRIVSEP
))
147 opt_extend(&os_user
);
148 if (!(di
->flags
& FRR_NO_ZCLIENT
))
149 opt_extend(&os_zclient
);
150 if (!(di
->flags
& FRR_NO_TCPVTY
))
153 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
154 frr_sysconfdir
, di
->name
);
155 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
156 frr_vtydir
, di
->name
);
159 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
162 const struct optspec main_opts
= { optstr
, helpstr
, longopts
};
163 opt_extend(&main_opts
);
166 void frr_help_exit(int status
)
168 FILE *target
= status
? stderr
: stdout
;
171 fprintf(stderr
, "Invalid options.\n\n");
174 di
->printhelp(target
);
176 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
179 di
->copyright
? "\n\n" : "",
180 di
->copyright
? di
->copyright
: "",
182 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
186 static int errors
= 0;
188 static int frr_opt(int opt
)
190 static int vty_port_set
= 0;
191 static int vty_addr_set
= 0;
199 print_version(di
->progname
);
206 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
208 di
->pid_file
= optarg
;
211 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
213 di
->config_file
= optarg
;
216 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
221 if (di
->flags
& FRR_NO_ZCLIENT
)
223 zclient_serv_path_set(optarg
);
226 if (di
->flags
& FRR_NO_TCPVTY
)
229 fprintf(stderr
, "-A option specified more than once!\n");
234 di
->vty_addr
= optarg
;
237 if (di
->flags
& FRR_NO_TCPVTY
)
240 fprintf(stderr
, "-P option specified more than once!\n");
245 di
->vty_port
= strtoul(optarg
, &err
, 0);
246 if (*err
|| !*optarg
) {
247 fprintf(stderr
, "invalid port number \"%s\" for -P option\n",
254 if (di
->vty_sock_path
) {
255 fprintf(stderr
, "--vty_socket option specified more than once!\n");
259 di
->vty_sock_path
= optarg
;
262 if (di
->flags
& FRR_NO_PRIVSEP
)
264 di
->privs
->user
= optarg
;
267 if (di
->flags
& FRR_NO_PRIVSEP
)
269 di
->privs
->group
= optarg
;
277 int frr_getopt(int argc
, char * const argv
[], int *longindex
)
282 comb_next_lo
->name
= NULL
;
285 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
290 if (opt
== -1 && errors
)
297 struct thread_master
*frr_init(void)
299 struct thread_master
*master
;
303 zlog_default
= openzlog (di
->progname
, di
->log_id
, di
->instance
,
304 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
305 #if defined(HAVE_CUMULUS)
306 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
309 zprivs_init(di
->privs
);
311 master
= thread_master_create();
312 signal_init(master
, di
->n_signals
, di
->signals
);
314 if (di
->flags
& FRR_LIMITED_CLI
)
324 void frr_config_fork(void)
327 snprintf(config_default
, sizeof(config_default
), "%s/%s-%d.conf",
328 frr_sysconfdir
, di
->name
, di
->instance
);
329 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s-%d.pid",
330 frr_vtydir
, di
->name
, di
->instance
);
333 vty_read_config(di
->config_file
, config_default
);
335 /* Don't start execution if we are in dry-run mode */
340 if (di
->daemon_mode
&& daemon (0, 0) < 0) {
341 zlog_err("Zebra daemon failed: %s", strerror(errno
));
346 di
->pid_file
= pidfile_default
;
347 pid_output (di
->pid_file
);
350 void frr_vty_serv(void)
352 /* allow explicit override of vty_path in the future
353 * (not currently set anywhere) */
356 dir
= di
->vty_sock_path
? di
->vty_sock_path
: frr_vtydir
;
359 snprintf(vtypath_default
, sizeof(vtypath_default
),
361 dir
, di
->name
, di
->instance
);
363 snprintf(vtypath_default
, sizeof(vtypath_default
),
364 "%s/%s.vty", dir
, di
->name
);
366 di
->vty_path
= vtypath_default
;
369 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);