]> git.proxmox.com Git - mirror_frr.git/blob - watchfrr/watchfrr_vty.c
Merge pull request #12219 from cscarpitta/feature/srv6-usid-behavior-support
[mirror_frr.git] / watchfrr / watchfrr_vty.c
1 /*
2 * watchfrr CLI functions.
3 *
4 * Copyright (C) 2016 David Lamparter for NetDEF, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
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
19 */
20
21 #include <zebra.h>
22 #include <sys/wait.h>
23
24 #include "memory.h"
25 #include "log.h"
26 #include "log_vty.h"
27 #include "vty.h"
28 #include "command.h"
29
30 #include "watchfrr.h"
31
32 pid_t integrated_write_pid;
33 static int integrated_result_fd;
34
35 DEFUN(config_write_integrated,
36 config_write_integrated_cmd,
37 "write integrated",
38 "Write running configuration to memory, network, or terminal\n"
39 "Write integrated all-daemon frr.conf file\n")
40 {
41 pid_t child;
42 sigset_t oldmask, sigmask;
43
44 const char *e_inprog = "Configuration write already in progress.";
45 const char *e_dmn = "Not all daemons are up, cannot write config.";
46
47 if (integrated_write_pid != -1) {
48 vty_out(vty, "%% %s\n", e_inprog);
49 return CMD_WARNING;
50 }
51
52 /* check that all daemons are up before clobbering config */
53 if (!check_all_up()) {
54 vty_out(vty, "%% %s\n", e_dmn);
55 /*
56 * vtysh interprets this return value to mean that it should
57 * not try to write the config itself
58 */
59 return CMD_WARNING_CONFIG_FAILED;
60 }
61
62 fflush(stdout);
63 fflush(stderr);
64
65 /* need to temporarily block SIGCHLD because it could arrive between
66 * fork() call and setting the integrated_write_pid variable. This
67 * would mean the completion call gets lost and this hangs forever.
68 */
69 sigemptyset(&oldmask);
70 sigemptyset(&sigmask);
71 sigaddset(&sigmask, SIGCHLD);
72 sigprocmask(SIG_BLOCK, &sigmask, &oldmask);
73
74 child = fork();
75 if (child == -1) {
76 vty_out(vty, "%% configuration write fork() failed: %s.\n",
77 safe_strerror(errno));
78 sigprocmask(SIG_SETMASK, &oldmask, NULL);
79 return CMD_WARNING;
80 }
81 if (child != 0) {
82 /* note: the VTY won't write a command return value to vtysh;
83 * the
84 * session temporarily enters an intentional "hang" state. This
85 * is
86 * to make sure latency in vtysh doing the config write (several
87 * seconds is not rare to see) does not interfere with
88 * watchfrr's
89 * supervisor job.
90 *
91 * The fd is duplicated here so we don't need to hold a vty
92 * pointer
93 * (which could become invalid in the meantime).
94 */
95 integrated_write_pid = child;
96 integrated_result_fd = dup(vty->wfd);
97 sigprocmask(SIG_SETMASK, &oldmask, NULL);
98 return CMD_SUSPEND;
99 }
100
101 /* redirect stdout/stderr to vty session. Note vty->wfd is marked
102 * CLOEXEC, but dup2 will clear that flag. */
103 dup2(vty->wfd, 1);
104 dup2(vty->wfd, 2);
105
106 /* don't allow the user to pass parameters, we're root here!
107 * should probably harden vtysh at some point too... */
108 if (pathspace)
109 execl(VTYSH_BIN_PATH, "vtysh", "-N", pathspace, "-w", NULL);
110 else
111 execl(VTYSH_BIN_PATH, "vtysh", "-w", NULL);
112
113 /* unbuffered write; we just messed with stdout... */
114 char msg[512];
115 snprintf(msg, sizeof(msg), "error executing %s: %s\n", VTYSH_BIN_PATH,
116 safe_strerror(errno));
117 write(1, msg, strlen(msg));
118 exit(1);
119 }
120
121 DEFUN_NOSH (show_debugging_watchfrr,
122 show_debugging_watchfrr_cmd,
123 "show debugging [watchfrr]",
124 SHOW_STR
125 DEBUG_STR
126 WATCHFRR_STR)
127 {
128 cmd_show_lib_debugs(vty);
129
130 return CMD_SUCCESS;
131 }
132
133 DEFUN (show_watchfrr,
134 show_watchfrr_cmd,
135 "show watchfrr",
136 SHOW_STR
137 WATCHFRR_STR)
138 {
139 watchfrr_status(vty);
140 return CMD_SUCCESS;
141 }
142
143 /* we don't have the other logging commands since watchfrr only accepts
144 * log config through command line options
145 */
146 DEFUN_NOSH (show_logging,
147 show_logging_cmd,
148 "show logging",
149 SHOW_STR
150 "Show current logging configuration\n")
151 {
152 log_show_syslog(vty);
153 return CMD_SUCCESS;
154 }
155
156 #include "watchfrr/watchfrr_vty_clippy.c"
157
158 DEFPY (watchfrr_ignore_daemon,
159 watchfrr_ignore_daemon_cmd,
160 "[no] watchfrr ignore DAEMON$dname",
161 NO_STR
162 "Watchfrr Specific sub-command\n"
163 "Ignore a specified daemon when it does not respond to echo request\n"
164 "The daemon to ignore\n")
165 {
166 watchfrr_set_ignore_daemon(vty, dname, no ? false : true );
167
168 return CMD_SUCCESS;
169 }
170
171 void integrated_write_sigchld(int status)
172 {
173 uint8_t reply[4] = {0, 0, 0, CMD_WARNING};
174
175 if (WIFEXITED(status)) {
176 zlog_info("configuration write completed with exit code %d",
177 WEXITSTATUS(status));
178 reply[3] = WEXITSTATUS(status);
179 } else if (WIFSIGNALED(status)) {
180 zlog_warn("configuration write terminated by signal %d",
181 WTERMSIG(status));
182 } else {
183 zlog_warn("configuration write terminated");
184 }
185
186 if (reply[3] != CMD_SUCCESS) {
187 /* failure might be silent in vtysh without this */
188 static const char msg[] = "% Configuration write failed.\n";
189 write(integrated_result_fd, msg, strlen(msg));
190 }
191
192 /* don't care about failures here, if the connection is broken the
193 * return value will just be lost. */
194 write(integrated_result_fd, reply, sizeof(reply));
195 close(integrated_result_fd);
196
197 integrated_write_pid = -1;
198 }
199
200 void watchfrr_vty_init(void)
201 {
202 integrated_write_pid = -1;
203 install_element(ENABLE_NODE, &config_write_integrated_cmd);
204 install_element(ENABLE_NODE, &show_debugging_watchfrr_cmd);
205
206 install_element(ENABLE_NODE, &watchfrr_ignore_daemon_cmd);
207
208 install_element(CONFIG_NODE, &show_debugging_watchfrr_cmd);
209 install_element(VIEW_NODE, &show_watchfrr_cmd);
210 install_element(VIEW_NODE, &show_logging_cmd);
211 }