]>
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
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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 frr_protoname
[256] = "NONE";
40 char frr_protonameinst
[256] = "NONE";
42 char config_default
[256];
43 static char pidfile_default
[256];
44 static char vtypath_default
[256];
46 static char comb_optstr
[256];
47 static struct option comb_lo
[64];
48 static struct option
*comb_next_lo
= &comb_lo
[0];
49 static char comb_helpstr
[4096];
54 const struct option
*longopts
;
57 static void opt_extend(const struct optspec
*os
)
59 const struct option
*lo
;
61 strcat(comb_optstr
, os
->optstr
);
62 strcat(comb_helpstr
, os
->helpstr
);
63 for (lo
= os
->longopts
; lo
->name
; lo
++)
64 memcpy(comb_next_lo
++, lo
, sizeof(*lo
));
68 #define OPTION_VTYSOCK 1000
69 #define OPTION_MODULEDIR 1002
71 static const struct option lo_always
[] = {
72 {"help", no_argument
, NULL
, 'h'},
73 {"version", no_argument
, NULL
, 'v'},
74 {"daemon", no_argument
, NULL
, 'd'},
75 {"module", no_argument
, NULL
, 'M'},
76 {"vty_socket", required_argument
, NULL
, OPTION_VTYSOCK
},
77 {"moduledir", required_argument
, NULL
, OPTION_MODULEDIR
},
79 static const struct optspec os_always
= {
81 " -h, --help Display this help and exit\n"
82 " -v, --version Print program version\n"
83 " -d, --daemon Runs in daemon mode\n"
84 " -M, --module Load specified module\n"
85 " --vty_socket Override vty socket path\n"
86 " --moduledir Override modules directory\n",
90 static const struct option lo_cfg_pid_dry
[] = {
91 {"pid_file", required_argument
, NULL
, 'i'},
92 {"config_file", required_argument
, NULL
, 'f'},
93 {"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",
103 static const struct option lo_zclient
[] = {
104 {"socket", required_argument
, NULL
, 'z'},
106 static const struct optspec os_zclient
= {
107 "z:", " -z, --socket Set path of zebra socket\n", lo_zclient
};
110 static const struct option lo_vty
[] = {
111 {"vty_addr", required_argument
, NULL
, 'A'},
112 {"vty_port", required_argument
, NULL
, 'P'},
114 static const struct optspec os_vty
= {
116 " -A, --vty_addr Set vty's bind address\n"
117 " -P, --vty_port Set vty's port number\n",
121 static const struct option lo_user
[] = {{"user", required_argument
, NULL
, 'u'},
122 {"group", required_argument
, NULL
, 'g'},
124 static const struct optspec os_user
= {"u:g:",
125 " -u, --user User to run as\n"
126 " -g, --group Group to run as\n",
130 static struct frr_daemon_info
*di
= NULL
;
132 void frr_preinit(struct frr_daemon_info
*daemon
, int argc
, char **argv
)
136 /* basename(), opencoded. */
137 char *p
= strrchr(argv
[0], '/');
138 di
->progname
= p
? p
+ 1 : argv
[0];
142 opt_extend(&os_always
);
143 if (!(di
->flags
& FRR_NO_CFG_PID_DRY
))
144 opt_extend(&os_cfg_pid_dry
);
145 if (!(di
->flags
& FRR_NO_PRIVSEP
))
146 opt_extend(&os_user
);
147 if (!(di
->flags
& FRR_NO_ZCLIENT
))
148 opt_extend(&os_zclient
);
149 if (!(di
->flags
& FRR_NO_TCPVTY
))
152 snprintf(config_default
, sizeof(config_default
), "%s/%s.conf",
153 frr_sysconfdir
, di
->name
);
154 snprintf(pidfile_default
, sizeof(pidfile_default
), "%s/%s.pid",
155 frr_vtydir
, di
->name
);
157 strlcpy(frr_protoname
, di
->logname
, sizeof(frr_protoname
));
158 strlcpy(frr_protonameinst
, di
->logname
, sizeof(frr_protonameinst
));
161 void frr_opt_add(const char *optstr
, const struct option
*longopts
,
164 const struct optspec main_opts
= {optstr
, helpstr
, longopts
};
165 opt_extend(&main_opts
);
168 void frr_help_exit(int status
)
170 FILE *target
= status
? stderr
: stdout
;
173 fprintf(stderr
, "Invalid options.\n\n");
176 di
->printhelp(target
);
178 fprintf(target
, "Usage: %s [OPTION...]\n\n%s%s%s\n\n%s",
179 di
->progname
, di
->proghelp
, di
->copyright
? "\n\n" : "",
180 di
->copyright
? di
->copyright
: "", comb_helpstr
);
181 fprintf(target
, "\nReport bugs to %s\n", FRR_BUG_ADDRESS
);
185 struct option_chain
{
186 struct option_chain
*next
;
190 static struct option_chain
*modules
= NULL
, **modnext
= &modules
;
191 static int errors
= 0;
193 static int frr_opt(int opt
)
195 static int vty_port_set
= 0;
196 static int vty_addr_set
= 0;
197 struct option_chain
*oc
;
205 print_version(di
->progname
);
212 oc
= XMALLOC(MTYPE_TMP
, sizeof(*oc
));
219 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
221 di
->pid_file
= optarg
;
224 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
226 di
->config_file
= optarg
;
229 if (di
->flags
& FRR_NO_CFG_PID_DRY
)
234 if (di
->flags
& FRR_NO_ZCLIENT
)
236 zclient_serv_path_set(optarg
);
239 if (di
->flags
& FRR_NO_TCPVTY
)
243 "-A option specified more than once!\n");
248 di
->vty_addr
= optarg
;
251 if (di
->flags
& FRR_NO_TCPVTY
)
255 "-P option specified more than once!\n");
260 di
->vty_port
= strtoul(optarg
, &err
, 0);
261 if (*err
|| !*optarg
) {
263 "invalid port number \"%s\" for -P option\n",
270 if (di
->vty_sock_path
) {
272 "--vty_socket option specified more than once!\n");
276 di
->vty_sock_path
= optarg
;
278 case OPTION_MODULEDIR
:
279 if (di
->module_path
) {
281 "----moduledir option specified more than once!\n");
285 di
->module_path
= optarg
;
288 if (di
->flags
& FRR_NO_PRIVSEP
)
290 di
->privs
->user
= optarg
;
293 if (di
->flags
& FRR_NO_PRIVSEP
)
295 di
->privs
->group
= optarg
;
303 int frr_getopt(int argc
, char *const argv
[], int *longindex
)
308 comb_next_lo
->name
= NULL
;
311 opt
= getopt_long(argc
, argv
, comb_optstr
, comb_lo
, &lidx
);
316 if (opt
== -1 && errors
)
323 static struct thread_master
*master
;
324 struct thread_master
*frr_init(void)
326 struct option_chain
*oc
;
327 struct frrmod_runtime
*module
;
330 dir
= di
->module_path
? di
->module_path
: frr_moduledir
;
335 snprintf(frr_protonameinst
, sizeof(frr_protonameinst
), "%s[%u]",
336 di
->logname
, di
->instance
);
338 openzlog(di
->progname
, di
->logname
, di
->instance
,
339 LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
340 #if defined(HAVE_CUMULUS)
341 zlog_set_level(ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
344 frrmod_init(di
->module
);
346 modules
= (oc
= modules
)->next
;
347 module
= frrmod_load(oc
->arg
, dir
, moderr
, sizeof(moderr
));
349 fprintf(stderr
, "%s\n", moderr
);
352 XFREE(MTYPE_TMP
, oc
);
355 zprivs_init(di
->privs
);
357 master
= thread_master_create(NULL
);
358 signal_init(master
, di
->n_signals
, di
->signals
);
360 if (di
->flags
& FRR_LIMITED_CLI
)
370 void frr_config_fork(void)
372 hook_call(frr_late_init
, master
);
375 snprintf(config_default
, sizeof(config_default
),
376 "%s/%s-%d.conf", frr_sysconfdir
, di
->name
,
378 snprintf(pidfile_default
, sizeof(pidfile_default
),
379 "%s/%s-%d.pid", frr_vtydir
, di
->name
, di
->instance
);
382 vty_read_config(di
->config_file
, config_default
);
384 /* Don't start execution if we are in dry-run mode */
389 if (di
->daemon_mode
&& daemon(0, 0) < 0) {
390 zlog_err("Zebra daemon failed: %s", strerror(errno
));
395 di
->pid_file
= pidfile_default
;
396 pid_output(di
->pid_file
);
399 void frr_vty_serv(void)
401 /* allow explicit override of vty_path in the future
402 * (not currently set anywhere) */
405 dir
= di
->vty_sock_path
? di
->vty_sock_path
: frr_vtydir
;
408 snprintf(vtypath_default
, sizeof(vtypath_default
),
409 "%s/%s-%d.vty", dir
, di
->name
, di
->instance
);
411 snprintf(vtypath_default
, sizeof(vtypath_default
),
412 "%s/%s.vty", dir
, di
->name
);
414 di
->vty_path
= vtypath_default
;
417 vty_serv_sock(di
->vty_addr
, di
->vty_port
, di
->vty_path
);
420 void frr_run(struct thread_master
*master
)
422 char instanceinfo
[64] = "";
427 snprintf(instanceinfo
, sizeof(instanceinfo
), "instance %u ",
430 zlog_notice("%s %s starting: %svty@%d%s", di
->name
, FRR_VERSION
,
431 instanceinfo
, di
->vty_port
, di
->startinfo
);
433 struct thread thread
;
434 while (thread_fetch(master
, &thread
))
435 thread_call(&thread
);