From 1924323af6c6e769c39e4d6716e30eb6f7391b4b Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Thu, 3 Mar 2016 10:34:48 +1100 Subject: [PATCH] ctdb-killtcp: New helper ctdb_killtcp This will allow killing of TCP connections without daemon involvement. It looks strange that the common code for daemon and helper is in the server directory. Having it in the server directory means less temporary changes to the build configuration. This code will move into the helper itself and will no longer be used by the daemon. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- ctdb/packaging/RPM/ctdb.spec.in | 1 + ctdb/tools/ctdb_killtcp.c | 131 ++++++++++++++++++++++++++++++++ ctdb/wscript | 6 ++ 3 files changed, 138 insertions(+) create mode 100644 ctdb/tools/ctdb_killtcp.c diff --git a/ctdb/packaging/RPM/ctdb.spec.in b/ctdb/packaging/RPM/ctdb.spec.in index ec26855315c..bbbf66c26b1 100644 --- a/ctdb/packaging/RPM/ctdb.spec.in +++ b/ctdb/packaging/RPM/ctdb.spec.in @@ -205,6 +205,7 @@ rm -rf $RPM_BUILD_ROOT %{_libexecdir}/ctdb/ctdb_event_helper %{_libexecdir}/ctdb/ctdb_recovery_helper %{_libexecdir}/ctdb/ctdb_natgw +%{_libexecdir}/ctdb/ctdb_killtcp %{_libexecdir}/ctdb/smnotify %dir %{_libdir} %{_libdir}/ctdb/lib* diff --git a/ctdb/tools/ctdb_killtcp.c b/ctdb/tools/ctdb_killtcp.c new file mode 100644 index 00000000000..3b1c4d927bc --- /dev/null +++ b/ctdb/tools/ctdb_killtcp.c @@ -0,0 +1,131 @@ +/* + CTDB TCP connection killing utility + + Copyright (C) Martin Schwenke 2016 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . +*/ + +#include "replace.h" +#include "system/network.h" + +#include "lib/util/debug.h" + +#include "protocol/protocol.h" + +#include "common/system.h" +#include "common/logging.h" + +#include "server/killtcp.h" + +static const char *prog; + +static int ctdb_killtcp_destructor(struct ctdb_kill_tcp *killtcp) +{ + bool *done = killtcp->destructor_data; + *done = true; + + return 0; +} + +static void usage(void) +{ + printf("usage: %s [ ]\n", prog); + exit(1); +} + +int main(int argc, char **argv) +{ + struct ctdb_connection conn; + struct ctdb_kill_tcp *killtcp = NULL; + struct tevent_context *ev = NULL; + struct TALLOC_CONTEXT *mem_ctx = NULL; + struct ctdb_connection *conns = NULL; + bool done; + int num = 0; + int i, ret; + + prog = argv[0]; + + if (argc != 2 && argc != 4) { + usage(); + } + + if (argc == 4) { + if (!parse_ip_port(argv[2], &conn.src)) { + DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[2])); + goto fail; + } + + if (!parse_ip_port(argv[3], &conn.dst)) { + DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[3])); + goto fail; + } + + conns = &conn; + num = 1; + } else { + ret = ctdb_parse_connections(stdin, mem_ctx, &num, &conns); + if (ret != 0) { + DEBUG(DEBUG_ERR, + ("Unable to parse connections [%s]\n", + strerror(ret))); + goto fail; + } + } + + mem_ctx = talloc_new(NULL); + if (mem_ctx == NULL) { + DEBUG(DEBUG_ERR, (__location__ " out of memory\n")); + goto fail; + } + + ev = tevent_context_init(mem_ctx); + if (ev == NULL) { + DEBUG(DEBUG_ERR, ("Failed to initialise tevent\n")); + goto fail; + } + + if (num == 0) { + /* No connections, done! */ + talloc_free(mem_ctx); + return 0; + } + + for (i = 0; i < num; i++) { + ret = ctdb_killtcp(ev, mem_ctx, argv[1], + &conns[i].src, &conns[i].dst, + &killtcp); + if (ret != 0) { + DEBUG(DEBUG_ERR, ("Unable to killtcp\n")); + goto fail; + } + } + + done = false; + killtcp->destructor_data = &done; + talloc_set_destructor(killtcp, ctdb_killtcp_destructor); + + while (!done) { + tevent_loop_once(ev); + } + + talloc_free(mem_ctx); + + return 0; + +fail: + TALLOC_FREE(mem_ctx); + return -1; +} diff --git a/ctdb/wscript b/ctdb/wscript index 6d310755f1d..88fe59d4dd5 100755 --- a/ctdb/wscript +++ b/ctdb/wscript @@ -420,6 +420,12 @@ def build(bld): install_path='${BINDIR}', manpages='ctdb.1') + bld.SAMBA_BINARY('ctdb_killtcp', + source='tools/ctdb_killtcp.c server/killtcp.c', + deps='''ctdb-protocol ctdb-util ctdb-system + samba-util replace''', + install_path='${CTDB_HELPER_BINDIR}') + bld.SAMBA_BINARY('ltdbtool', source='tools/ltdbtool.c', includes='include', -- 2.47.3