]>
Commit | Line | Data |
---|---|---|
95c4aff2 | 1 | /* |
896014f4 DL |
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 | |
95c4aff2 DL |
19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | #include <sys/wait.h> | |
23 | ||
24 | #include "memory.h" | |
25 | #include "log.h" | |
26 | #include "vty.h" | |
27 | #include "command.h" | |
28 | ||
9473e340 | 29 | #include "watchfrr.h" |
95c4aff2 DL |
30 | |
31 | pid_t integrated_write_pid; | |
32 | static int integrated_result_fd; | |
33 | ||
a6810074 DL |
34 | DEFUN(config_write_integrated, |
35 | config_write_integrated_cmd, | |
36 | "write integrated", | |
37 | "Write running configuration to memory, network, or terminal\n" | |
e20dc2ba | 38 | "Write integrated all-daemon frr.conf file\n") |
95c4aff2 DL |
39 | { |
40 | pid_t child; | |
41 | sigset_t oldmask, sigmask; | |
42 | ||
43 | if (integrated_write_pid != -1) { | |
d62a17ae | 44 | vty_out(vty, "%% configuration write already in progress.\n"); |
95c4aff2 DL |
45 | return CMD_WARNING; |
46 | } | |
47 | ||
48 | fflush(stdout); | |
49 | fflush(stderr); | |
50 | ||
51 | /* need to temporarily block SIGCHLD because it could arrive between | |
52 | * fork() call and setting the integrated_write_pid variable. This | |
53 | * would mean the completion call gets lost and this hangs forever. | |
54 | */ | |
55 | sigemptyset(&oldmask); | |
56 | sigemptyset(&sigmask); | |
57 | sigaddset(&sigmask, SIGCHLD); | |
58 | sigprocmask(SIG_BLOCK, &sigmask, &oldmask); | |
59 | ||
60 | child = fork(); | |
61 | if (child == -1) { | |
d62a17ae | 62 | vty_out(vty, "%% configuration write fork() failed: %s.\n", |
96ade3ed | 63 | safe_strerror(errno)); |
95c4aff2 DL |
64 | sigprocmask(SIG_SETMASK, &oldmask, NULL); |
65 | return CMD_WARNING; | |
66 | } | |
67 | if (child != 0) { | |
d62a17ae | 68 | /* note: the VTY won't write a command return value to vtysh; |
69 | * the | |
70 | * session temporarily enters an intentional "hang" state. This | |
71 | * is | |
a6810074 | 72 | * to make sure latency in vtysh doing the config write (several |
d62a17ae | 73 | * seconds is not rare to see) does not interfere with |
74 | * watchfrr's | |
a6810074 DL |
75 | * supervisor job. |
76 | * | |
d62a17ae | 77 | * The fd is duplicated here so we don't need to hold a vty |
78 | * pointer | |
a6810074 DL |
79 | * (which could become invalid in the meantime). |
80 | */ | |
95c4aff2 DL |
81 | integrated_write_pid = child; |
82 | integrated_result_fd = dup(vty->wfd); | |
83 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | |
84 | return CMD_SUSPEND; | |
85 | } | |
86 | ||
87 | /* redirect stdout/stderr to vty session. Note vty->wfd is marked | |
88 | * CLOEXEC, but dup2 will clear that flag. */ | |
89 | dup2(vty->wfd, 1); | |
90 | dup2(vty->wfd, 2); | |
91 | ||
92 | /* don't allow the user to pass parameters, we're root here! | |
93 | * should probably harden vtysh at some point too... */ | |
94 | execl(VTYSH_BIN_PATH, "vtysh", "-w", NULL); | |
95 | ||
96 | /* unbuffered write; we just messed with stdout... */ | |
97 | char msg[512]; | |
d62a17ae | 98 | snprintf(msg, sizeof(msg), "error executing %s: %s\n", VTYSH_BIN_PATH, |
99 | safe_strerror(errno)); | |
95c4aff2 DL |
100 | write(1, msg, strlen(msg)); |
101 | exit(1); | |
102 | } | |
103 | ||
dff13b6f DW |
104 | DEFUN_NOSH (show_debugging_watchfrr, |
105 | show_debugging_watchfrr_cmd, | |
106 | "show debugging [watchfrr]", | |
107 | SHOW_STR | |
108 | DEBUG_STR | |
109 | WATCHFRR_STR) | |
110 | { | |
111 | return CMD_SUCCESS; | |
112 | } | |
113 | ||
95c4aff2 DL |
114 | void integrated_write_sigchld(int status) |
115 | { | |
d62a17ae | 116 | uint8_t reply[4] = {0, 0, 0, CMD_WARNING}; |
95c4aff2 DL |
117 | |
118 | if (WIFEXITED(status)) { | |
119 | zlog_info("configuration write completed with exit code %d", | |
a6810074 | 120 | WEXITSTATUS(status)); |
95c4aff2 DL |
121 | reply[3] = WEXITSTATUS(status); |
122 | } else if (WIFSIGNALED(status)) { | |
123 | zlog_warn("configuration write terminated by signal %d", | |
a6810074 | 124 | WTERMSIG(status)); |
95c4aff2 DL |
125 | } else { |
126 | zlog_warn("configuration write terminated"); | |
127 | } | |
128 | ||
c10c5926 DL |
129 | if (reply[3] != CMD_SUCCESS) { |
130 | /* failure might be silent in vtysh without this */ | |
131 | static const char msg[] = "% Configuration write failed.\n"; | |
132 | write(integrated_result_fd, msg, strlen(msg)); | |
133 | } | |
134 | ||
95c4aff2 DL |
135 | /* don't care about failures here, if the connection is broken the |
136 | * return value will just be lost. */ | |
137 | write(integrated_result_fd, reply, sizeof(reply)); | |
138 | close(integrated_result_fd); | |
139 | ||
140 | integrated_write_pid = -1; | |
141 | } | |
142 | ||
9473e340 | 143 | void watchfrr_vty_init(void) |
95c4aff2 DL |
144 | { |
145 | integrated_write_pid = -1; | |
146 | install_element(ENABLE_NODE, &config_write_integrated_cmd); | |
dff13b6f DW |
147 | install_element(ENABLE_NODE, &show_debugging_watchfrr_cmd); |
148 | install_element(CONFIG_NODE, &show_debugging_watchfrr_cmd); | |
95c4aff2 | 149 | } |