summaryrefslogtreecommitdiffstats
path: root/watchfrr/watchfrr_vty.c
blob: 95a7962ec6c457ad3c69435a3a51db67ee666388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * watchfrr CLI functions.
 *
 * Copyright (C) 2016  David Lamparter for NetDEF, Inc.
 */

#include <zebra.h>

#include <signal.h>
#include <sys/wait.h>

#include "memory.h"
#include "log.h"
#include "log_vty.h"
#include "vty.h"
#include "command.h"

#include "watchfrr.h"

#include "lib/config_paths.h"

pid_t integrated_write_pid;
static int integrated_result_fd;

DEFUN(config_write_integrated,
      config_write_integrated_cmd,
      "write integrated",
      "Write running configuration to memory, network, or terminal\n"
      "Write integrated all-daemon frr.conf file\n")
{
	pid_t child;
	sigset_t oldmask, sigmask;

	const char *e_inprog = "Configuration write already in progress.";
	const char *e_dmn = "Not all daemons are up, cannot write config.";

	if (integrated_write_pid != -1) {
		vty_out(vty, "%% %s\n", e_inprog);
		return CMD_WARNING;
	}

	/* check that all daemons are up before clobbering config */
	if (!check_all_up()) {
		vty_out(vty, "%% %s\n", e_dmn);
		/*
		 * vtysh interprets this return value to mean that it should
		 * not try to write the config itself
		 */
		return CMD_WARNING_CONFIG_FAILED;
	}

	fflush(stdout);
	fflush(stderr);

	/* need to temporarily block SIGCHLD because it could arrive between
	 * fork() call and setting the integrated_write_pid variable.  This
	 * would mean the completion call gets lost and this hangs forever.
	 */
	sigemptyset(&oldmask);
	sigemptyset(&sigmask);
	sigaddset(&sigmask, SIGCHLD);
	sigprocmask(SIG_BLOCK, &sigmask, &oldmask);

	child = fork();
	if (child == -1) {
		vty_out(vty, "%% configuration write fork() failed: %s.\n",
			safe_strerror(errno));
		sigprocmask(SIG_SETMASK, &oldmask, NULL);
		return CMD_WARNING;
	}
	if (child != 0) {
		/* note: the VTY won't write a command return value to vtysh;
		 * the
		 * session temporarily enters an intentional "hang" state.  This
		 * is
		 * to make sure latency in vtysh doing the config write (several
		 * seconds is not rare to see) does not interfere with
		 * watchfrr's
		 * supervisor job.
		 *
		 * The fd is duplicated here so we don't need to hold a vty
		 * pointer
		 * (which could become invalid in the meantime).
		 */
		integrated_write_pid = child;
		integrated_result_fd = dup(vty->wfd);
		sigprocmask(SIG_SETMASK, &oldmask, NULL);
		return CMD_SUSPEND;
	}

	/* redirect stdout/stderr to vty session.  Note vty->wfd is marked
	 * CLOEXEC, but dup2 will clear that flag. */
	dup2(vty->wfd, 1);
	dup2(vty->wfd, 2);

	/* don't allow the user to pass parameters, we're root here!
	 * should probably harden vtysh at some point too... */
	if (pathspace)
		execl(VTYSH_BIN_PATH, "vtysh", "-N", pathspace, "-w", NULL);
	else
		execl(VTYSH_BIN_PATH, "vtysh", "-w", NULL);

	/* unbuffered write; we just messed with stdout... */
	char msg[512];
	snprintf(msg, sizeof(msg), "error executing %s: %s\n", VTYSH_BIN_PATH,
		 safe_strerror(errno));
	write(1, msg, strlen(msg));
	exit(1);
}

DEFUN_NOSH (show_debugging_watchfrr,
            show_debugging_watchfrr_cmd,
            "show debugging [watchfrr]",
            SHOW_STR
            DEBUG_STR
            WATCHFRR_STR)
{
	cmd_show_lib_debugs(vty);

	return CMD_SUCCESS;
}

DEFUN (show_watchfrr,
       show_watchfrr_cmd,
       "show watchfrr",
       SHOW_STR
       WATCHFRR_STR)
{
	watchfrr_status(vty);
	return CMD_SUCCESS;
}

/* we don't have the other logging commands since watchfrr only accepts
 * log config through command line options
 */
DEFUN_NOSH (show_logging,
	    show_logging_cmd,
	    "show logging",
	    SHOW_STR
	    "Show current logging configuration\n")
{
	log_show_syslog(vty);
	return CMD_SUCCESS;
}

#include "watchfrr/watchfrr_vty_clippy.c"

DEFPY (watchfrr_ignore_daemon,
       watchfrr_ignore_daemon_cmd,
       "[no] watchfrr ignore DAEMON$dname",
       NO_STR
       "Watchfrr Specific sub-command\n"
       "Ignore a specified daemon when it does not respond to echo request\n"
       "The daemon to ignore\n")
{
	watchfrr_set_ignore_daemon(vty, dname, no ? false : true );

	return CMD_SUCCESS;
}

void integrated_write_sigchld(int status)
{
	uint8_t reply[4] = {0, 0, 0, CMD_WARNING};

	if (WIFEXITED(status)) {
		zlog_info("configuration write completed with exit code %d",
			  WEXITSTATUS(status));
		reply[3] = WEXITSTATUS(status);
	} else if (WIFSIGNALED(status)) {
		zlog_warn("configuration write terminated by signal %d",
			  WTERMSIG(status));
	} else {
		zlog_warn("configuration write terminated");
	}

	if (reply[3] != CMD_SUCCESS) {
		/* failure might be silent in vtysh without this */
		static const char msg[] = "% Configuration write failed.\n";
		write(integrated_result_fd, msg, strlen(msg));
	}

	/* don't care about failures here, if the connection is broken the
	 * return value will just be lost. */
	write(integrated_result_fd, reply, sizeof(reply));
	close(integrated_result_fd);

	integrated_write_pid = -1;
}

void watchfrr_vty_init(void)
{
	integrated_write_pid = -1;
	install_element(ENABLE_NODE, &config_write_integrated_cmd);
	install_element(ENABLE_NODE, &show_debugging_watchfrr_cmd);

	install_element(ENABLE_NODE, &watchfrr_ignore_daemon_cmd);

	install_element(CONFIG_NODE, &show_debugging_watchfrr_cmd);
	install_element(VIEW_NODE, &show_watchfrr_cmd);
	install_element(VIEW_NODE, &show_logging_cmd);
}