]>
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"
33 DEFINE_HOOK(frr_late_init
, (struct thread_master
*tm
), (tm
))
35 const char frr_sysconfdir
[] = SYSCONFDIR
;
36 const char frr_vtydir
[] = DAEMON_VTY_DIR
;
37 const char frr_moduledir
[] = MODULE_PATH
;
39 char config_default
[256];
40 static char pidfile_default
[256];
41 static char vtypath_default
[256];
43 static char comb_optstr
[256];
44 static struct option comb_lo
[64];
45 static struct option
*comb_next_lo
= &comb_lo
[0];
46 static char comb_helpstr
[4096];
51 const struct option
*longopts
;
54 static void opt_extend(const struct optspec
*os
)
56 const struct option
*lo
;
58 strcat(comb_optstr
, os
->optstr
);
59 strcat(comb_helpstr
, os
->helpstr
);
60 for (lo
= os
->longopts
; lo
->name
; lo
++)
61 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
65 #define OPTION_VTYSOCK 1000
66 #define OPTION_MODULEDIR 1002
68 static const struct option lo_always
[] = {
69 { "help", no_argument
, NULL
, 'h' },
70 { "version", no_argument
, NULL
, 'v' },
71 { "daemon", no_argument
, NULL
, 'd' },
72 { "module", no_argument
, NULL
, 'M' },
73 { "vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
74 { "moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
77 static const struct optspec os_always
= {
79 " -h, --help Display this help and exit\n"
80 " -v, --version Print program version\n"
81 " -d, --daemon Runs in daemon mode\n"
82 " -M, --module Load specified module\n"
83 " --vty_socket Override vty socket path\n"
84 " --moduledir Override modules directory\n",
89 static const struct option lo_cfg_pid_dry
[] = {
90 { "pid_file", required_argument
, NULL
, 'i' },
91 { "config_file", required_argument
, NULL
, 'f' },
92 { "dryrun", no_argument
, NULL
, 'C' },
95 static const struct optspec os_cfg_pid_dry
= {
97 " -f, --config_file Set configuration file name\n"
98 " -i, --pid_file Set process identifier file name\n"
99 " -C, --dryrun Check configuration for validity and exit\n",
104 static const struct option lo_zclient
[] = {
105 { "socket", required_argument
, NULL
, 'z' },
108 static const struct optspec os_zclient
= {
110 " -z, --socket Set path of zebra socket\n",
115 static const struct option lo_vty
[] = {
116 { "vty_addr", required_argument
, NULL
, 'A'},
117 { "vty_port", required_argument
, NULL
, 'P'},
120 static const struct optspec os_vty
= {
122 " -A, --vty_addr Set vty's bind address\n"
123 " -P, --vty_port Set vty's port number\n",
128 static const struct option lo_user
[] = {
129 { "user", required_argument
, NULL
, 'u'},
130 { "group", required_argument
, NULL
, 'g'},
133 static const struct optspec os_user
= {
135 " -u, --user User to run as\n"
136 " -g, --group Group to run as\n",
141 static struct frr_daemon_info
*di
= NULL
;
143 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
147 /* basename(), opencoded. */
148 char *p
= strrchr(argv
[0], '/');
149 di
->progname
= p
? p
+ 1 : argv
[0];
153 opt_extend(&os_always
);
154 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
155 opt_extend(&os_cfg_pid_dry
);
156 if (!(di
->flags
& FRR_NO_PRIVSEP
))
157 opt_extend(&os_user
);
158 if (!(di
->flags
& FRR_NO_ZCLIENT
))
159 opt_extend(&os_zclient
);
160 if (!(di
->flags
& FRR_NO_TCPVTY
))
163 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
164 frr_sysconfdir
, di
->name
);
165 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
166 frr_vtydir
, di
->name
);
169 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
172 const struct optspec main_opts
= { optstr
, helpstr
, longopts
};
173 opt_extend(&main_opts
);
176 void frr_help_exit(int status
)
178 FILE *target
= status
? stderr
: stdout
;
181 fprintf(stderr
, "Invalid options.\n\n");
184 di
->printhelp(target
);
186 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
189 di
->copyright
? "\n\n" : "",
190 di
->copyright
? di
->copyright
: "",
192 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
196 struct option_chain
{
197 struct option_chain
*next
;
201 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
202 static int errors
= 0;
204 static int frr_opt(int opt
)
206 static int vty_port_set
= 0;
207 static int vty_addr_set
= 0;
208 struct option_chain
*oc
;
216 print_version(di
->progname
);
223 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
230 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
232 di
->pid_file
= optarg
;
235 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
237 di
->config_file
= optarg
;
240 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
245 if (di
->flags
& FRR_NO_ZCLIENT
)
247 zclient_serv_path_set(optarg
);
250 if (di
->flags
& FRR_NO_TCPVTY
)
253 fprintf(stderr
, "-A option specified more than once!\n");
258 di
->vty_addr
= optarg
;
261 if (di
->flags
& FRR_NO_TCPVTY
)
264 fprintf(stderr
, "-P option specified more than once!\n");
269 di
->vty_port
= strtoul(optarg
, &err
, 0);
270 if (*err
|| !*optarg
) {
271 fprintf(stderr
, "invalid port number \"%s\" for -P option\n",
278 if (di
->vty_sock_path
) {
279 fprintf(stderr
, "--vty_socket option specified more than once!\n");
283 di
->vty_sock_path
= optarg
;
285 case OPTION_MODULEDIR
:
286 if (di
->module_path
) {
287 fprintf(stderr
, "----moduledir option specified more than once!\n");
291 di
->module_path
= optarg
;
294 if (di
->flags
& FRR_NO_PRIVSEP
)
296 di
->privs
->user
= optarg
;
299 if (di
->flags
& FRR_NO_PRIVSEP
)
301 di
->privs
->group
= optarg
;
309 int frr_getopt(int argc
, char * const argv
[], int *longindex
)
314 comb_next_lo
->name
= NULL
;
317 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
322 if (opt
== -1 && errors
)
329 static struct thread_master
*master
;
330 struct thread_master
*frr_init(void)
332 struct option_chain
*oc
;
333 struct frrmod_runtime
*module
;
336 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
340 openzlog (di
->progname
, di
->logname
, di
->instance
,
341 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
342 #if defined(HAVE_CUMULUS)
343 zlog_set_level (ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
346 frrmod_init(di
->module
);
348 modules
= (oc
= modules
)->next
;
349 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
351 fprintf(stderr
, "%s\n", moderr
);
354 XFREE(MTYPE_TMP
, oc
);
357 zprivs_init(di
->privs
);
359 master
= thread_master_create();
360 signal_init(master
, di
->n_signals
, di
->signals
);
362 if (di
->flags
& FRR_LIMITED_CLI
)
372 void frr_config_fork(void)
374 hook_call(frr_late_init
, master
);
377 snprintf(config_default
, sizeof(config_default
), "%s/%s-%d.conf",
378 frr_sysconfdir
, di
->name
, di
->instance
);
379 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s-%d.pid",
380 frr_vtydir
, di
->name
, di
->instance
);
383 vty_read_config(di
->config_file
, config_default
);
385 /* Don't start execution if we are in dry-run mode */
390 if (di
->daemon_mode
&& daemon (0, 0) < 0) {
391 zlog_err("Zebra daemon failed: %s", strerror(errno
));
396 di
->pid_file
= pidfile_default
;
397 pid_output (di
->pid_file
);
400 void frr_vty_serv(void)
402 /* allow explicit override of vty_path in the future
403 * (not currently set anywhere) */
406 dir
= di
->vty_sock_path
? di
->vty_sock_path
: frr_vtydir
;
409 snprintf(vtypath_default
, sizeof(vtypath_default
),
411 dir
, di
->name
, di
->instance
);
413 snprintf(vtypath_default
, sizeof(vtypath_default
),
414 "%s/%s.vty", dir
, di
->name
);
416 di
->vty_path
= vtypath_default
;
419 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
422 void frr_run(struct thread_master
*master
)
424 char instanceinfo
[64] = "";
429 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
432 zlog_notice("%s %s starting: %svty@%d%s",
439 struct thread thread
;
440 while (thread_fetch(master
, &thread
))
441 thread_call(&thread
);