]>
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
},
76 static const struct optspec os_always
= {
78 " -h, --help Display this help and exit\n"
79 " -v, --version Print program version\n"
80 " -d, --daemon Runs in daemon mode\n"
81 " -M, --module Load specified module\n"
82 " --vty_socket Override vty socket path\n"
83 " --moduledir Override modules directory\n",
87 static const struct option lo_cfg_pid_dry
[] = {
88 {"pid_file", required_argument
, NULL
, 'i'},
89 {"config_file", required_argument
, NULL
, 'f'},
90 {"dryrun", no_argument
, NULL
, 'C'},
92 static const struct optspec os_cfg_pid_dry
= {
94 " -f, --config_file Set configuration file name\n"
95 " -i, --pid_file Set process identifier file name\n"
96 " -C, --dryrun Check configuration for validity and exit\n",
100 static const struct option lo_zclient
[] = {
101 {"socket", required_argument
, NULL
, 'z'},
103 static const struct optspec os_zclient
= {
104 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
107 static const struct option lo_vty
[] = {
108 {"vty_addr", required_argument
, NULL
, 'A'},
109 {"vty_port", required_argument
, NULL
, 'P'},
111 static const struct optspec os_vty
= {
113 " -A, --vty_addr Set vty's bind address\n"
114 " -P, --vty_port Set vty's port number\n",
118 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
119 {"group", required_argument
, NULL
, 'g'},
121 static const struct optspec os_user
= {"u:g:",
122 " -u, --user User to run as\n"
123 " -g, --group Group to run as\n",
127 static struct frr_daemon_info
*di
= NULL
;
129 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
133 /* basename(), opencoded. */
134 char *p
= strrchr(argv
[0], '/');
135 di
->progname
= p
? p
+ 1 : argv
[0];
139 opt_extend(&os_always
);
140 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
141 opt_extend(&os_cfg_pid_dry
);
142 if (!(di
->flags
& FRR_NO_PRIVSEP
))
143 opt_extend(&os_user
);
144 if (!(di
->flags
& FRR_NO_ZCLIENT
))
145 opt_extend(&os_zclient
);
146 if (!(di
->flags
& FRR_NO_TCPVTY
))
149 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
150 frr_sysconfdir
, di
->name
);
151 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
152 frr_vtydir
, di
->name
);
155 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
158 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
159 opt_extend(&main_opts
);
162 void frr_help_exit(int status
)
164 FILE *target
= status
? stderr
: stdout
;
167 fprintf(stderr
, "Invalid options.\n\n");
170 di
->printhelp(target
);
172 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
173 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
174 di
->copyright
? di
->copyright
: "", comb_helpstr
);
175 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
179 struct option_chain
{
180 struct option_chain
*next
;
184 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
185 static int errors
= 0;
187 static int frr_opt(int opt
)
189 static int vty_port_set
= 0;
190 static int vty_addr_set
= 0;
191 struct option_chain
*oc
;
199 print_version(di
->progname
);
206 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
213 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
215 di
->pid_file
= optarg
;
218 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
220 di
->config_file
= optarg
;
223 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
228 if (di
->flags
& FRR_NO_ZCLIENT
)
230 zclient_serv_path_set(optarg
);
233 if (di
->flags
& FRR_NO_TCPVTY
)
237 "-A option specified more than once!\n");
242 di
->vty_addr
= optarg
;
245 if (di
->flags
& FRR_NO_TCPVTY
)
249 "-P option specified more than once!\n");
254 di
->vty_port
= strtoul(optarg
, &err
, 0);
255 if (*err
|| !*optarg
) {
257 "invalid port number \"%s\" for -P option\n",
264 if (di
->vty_sock_path
) {
266 "--vty_socket option specified more than once!\n");
270 di
->vty_sock_path
= optarg
;
272 case OPTION_MODULEDIR
:
273 if (di
->module_path
) {
275 "----moduledir option specified more than once!\n");
279 di
->module_path
= optarg
;
282 if (di
->flags
& FRR_NO_PRIVSEP
)
284 di
->privs
->user
= optarg
;
287 if (di
->flags
& FRR_NO_PRIVSEP
)
289 di
->privs
->group
= optarg
;
297 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
302 comb_next_lo
->name
= NULL
;
305 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
310 if (opt
== -1 && errors
)
317 static struct thread_master
*master
;
318 struct thread_master
*frr_init(void)
320 struct option_chain
*oc
;
321 struct frrmod_runtime
*module
;
324 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
328 openzlog(di
->progname
, di
->logname
, di
->instance
,
329 LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
330 #if defined(HAVE_CUMULUS)
331 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
334 frrmod_init(di
->module
);
336 modules
= (oc
= modules
)->next
;
337 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
339 fprintf(stderr
, "%s\n", moderr
);
342 XFREE(MTYPE_TMP
, oc
);
345 zprivs_init(di
->privs
);
347 master
= thread_master_create();
348 signal_init(master
, di
->n_signals
, di
->signals
);
350 if (di
->flags
& FRR_LIMITED_CLI
)
360 void frr_config_fork(void)
362 hook_call(frr_late_init
, master
);
365 snprintf(config_default
, sizeof(config_default
),
366 "%s/%s-%d.conf", frr_sysconfdir
, di
->name
,
368 snprintf(pidfile_default
, sizeof(pidfile_default
),
369 "%s/%s-%d.pid", frr_vtydir
, di
->name
, di
->instance
);
372 vty_read_config(di
->config_file
, config_default
);
374 /* Don't start execution if we are in dry-run mode */
379 if (di
->daemon_mode
&& daemon(0, 0) < 0) {
380 zlog_err("Zebra daemon failed: %s", strerror(errno
));
385 di
->pid_file
= pidfile_default
;
386 pid_output(di
->pid_file
);
389 void frr_vty_serv(void)
391 /* allow explicit override of vty_path in the future
392 * (not currently set anywhere) */
395 dir
= di
->vty_sock_path
? di
->vty_sock_path
: frr_vtydir
;
398 snprintf(vtypath_default
, sizeof(vtypath_default
),
399 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
401 snprintf(vtypath_default
, sizeof(vtypath_default
),
402 "%s/%s.vty", dir
, di
->name
);
404 di
->vty_path
= vtypath_default
;
407 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
410 void frr_run(struct thread_master
*master
)
412 char instanceinfo
[64] = "";
417 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
420 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
421 instanceinfo
, di
->vty_port
, di
->startinfo
);
423 struct thread thread
;
424 while (thread_fetch(master
, &thread
))
425 thread_call(&thread
);