Commit f5a0f6cf authored by Daniel Salzman's avatar Daniel Salzman

utils: use new control interface

parent 6133d468
......@@ -226,8 +226,8 @@ src/knot/conf/tools.c
src/knot/conf/tools.h
src/knot/ctl/commands.c
src/knot/ctl/commands.h
src/knot/ctl/remote.c
src/knot/ctl/remote.h
src/knot/ctl/process.c
src/knot/ctl/process.h
src/knot/dnssec/context.c
src/knot/dnssec/context.h
src/knot/dnssec/nsec-chain.c
......@@ -466,8 +466,6 @@ src/utils/knotc/commands.h
src/utils/knotc/estimator.c
src/utils/knotc/estimator.h
src/utils/knotc/main.c
src/utils/knotc/remote.c
src/utils/knotc/remote.h
src/utils/knotd/main.c
src/utils/knsupdate/knsupdate_exec.c
src/utils/knsupdate/knsupdate_exec.h
......
......@@ -404,15 +404,21 @@ Configuration of the server control interface.
.ft C
control:
listen: STR
timeout: TIME
.ft P
.fi
.UNINDENT
.UNINDENT
.SS listen
.sp
A UNIX socket path where the server listens for remote control commands.
A UNIX socket path where the server listens for control commands.
.sp
\fIDefault:\fP \fI\%rundir\fP/knot.sock
.SS timeout
.sp
Maximum time the control socket operations can take. Set 0 for infinity.
.sp
\fIDefault:\fP 5
.SH REMOTE SECTION
.sp
Definitions of remote servers for outgoing connections (source of a zone
......
......@@ -46,7 +46,10 @@ The default configuration database, if exists, has a preference to the default
configuration file.
.TP
\fB\-s\fP, \fB\-\-socket\fP \fIpath\fP
Use a remote control UNIX socket path (default is \fB@run_dir@/knot.sock\fP).
Use a control UNIX socket path (default is \fB@run_dir@/knot.sock\fP).
.TP
\fB\-t\fP, \fB\-\-timeout\fP \fIseconds\fP
Use a control timeout in seconds. Set 0 for infinity (default is 5).
.TP
\fB\-f\fP, \fB\-\-force\fP
Forced operation. Overrides some checks.
......
......@@ -23,7 +23,10 @@ Parameters
configuration file.
**-s**, **--socket** *path*
Use a remote control UNIX socket path (default is :file:`@run_dir@/knot.sock`).
Use a control UNIX socket path (default is :file:`@run_dir@/knot.sock`).
**-t**, **--timeout** *seconds*
Use a control timeout in seconds. Set 0 for infinity (default is 5).
**-f**, **--force**
Forced operation. Overrides some checks.
......
......@@ -453,16 +453,26 @@ Configuration of the server control interface.
control:
listen: STR
timeout: TIME
.. _control_listen:
listen
------
A UNIX socket path where the server listens for remote control commands.
A UNIX socket path where the server listens for control commands.
*Default:* :ref:`rundir<server_rundir>`/knot.sock
.. _control_timeout:
timeout
-------
Maximum time the control socket operations can take. Set 0 for infinity.
*Default:* 5
.. _Remote section:
Remote section
......
......@@ -211,8 +211,6 @@ knotc_SOURCES = \
utils/knotc/commands.h \
utils/knotc/estimator.c \
utils/knotc/estimator.h \
utils/knotc/remote.c \
utils/knotc/remote.h \
utils/knotc/main.c
knotd_SOURCES = \
......@@ -245,8 +243,8 @@ libknotd_la_SOURCES = \
knot/conf/tools.h \
knot/ctl/commands.c \
knot/ctl/commands.h \
knot/ctl/remote.c \
knot/ctl/remote.h \
knot/ctl/process.c \
knot/ctl/process.h \
knot/dnssec/context.c \
knot/dnssec/context.h \
knot/dnssec/nsec-chain.c \
......
......@@ -98,6 +98,7 @@ static const yp_item_t desc_server[] = {
static const yp_item_t desc_control[] = {
{ C_LISTEN, YP_TSTR, YP_VSTR = { "knot.sock" } },
{ C_TIMEOUT, YP_TINT, YP_VINT = { 0, INT32_MAX, 5, YP_STIME } },
{ C_COMMENT, YP_TSTR, YP_VNONE },
{ NULL }
};
......
......@@ -78,6 +78,7 @@
#define C_TCP_REPLY_TIMEOUT "\x11""tcp-reply-timeout"
#define C_TCP_WORKERS "\x0B""tcp-workers"
#define C_TIMER_DB "\x08""timer-db"
#define C_TIMEOUT "\x07""timeout"
#define C_TPL "\x08""template"
#define C_UDP_WORKERS "\x0B""udp-workers"
#define C_USER "\x04""user"
......
This diff is collapsed.
/* Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -13,53 +13,80 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file
*
* \brief Server control commands.
*
* \addtogroup ctl
* @{
*/
#pragma once
#include "libknot/libknot.h"
#include "knot/server/server.h"
#define CMDARGS_ALLOC_BLOCK KNOT_WIRE_MAX_PKTSIZE
/*! Control commands. */
typedef enum {
CTL_NONE,
#define KNOT_CTL_STATUS "status"
#define KNOT_CTL_STOP "stop"
#define KNOT_CTL_RELOAD "reload"
CTL_STATUS,
CTL_STOP,
CTL_RELOAD,
#define KNOT_CTL_ZONE_STATUS "zone-status"
#define KNOT_CTL_ZONE_RELOAD "zone-reload"
#define KNOT_CTL_ZONE_REFRESH "zone-refresh"
#define KNOT_CTL_ZONE_RETRANSFER "zone-retransfer"
#define KNOT_CTL_ZONE_FLUSH "zone-flush"
#define KNOT_CTL_ZONE_SIGN "zone-sign"
CTL_ZONE_STATUS,
CTL_ZONE_RELOAD,
CTL_ZONE_REFRESH,
CTL_ZONE_RETRANSFER,
CTL_ZONE_FLUSH,
CTL_ZONE_SIGN,
#define KNOT_CTL_CONF_LIST "conf-list"
#define KNOT_CTL_CONF_READ "conf-read"
#define KNOT_CTL_CONF_BEGIN "conf-begin"
#define KNOT_CTL_CONF_COMMIT "conf-commit"
#define KNOT_CTL_CONF_ABORT "conf-abort"
#define KNOT_CTL_CONF_DIFF "conf-diff"
#define KNOT_CTL_CONF_GET "conf-get"
#define KNOT_CTL_CONF_SET "conf-set"
#define KNOT_CTL_CONF_UNSET "conf-unset"
CTL_CONF_LIST,
CTL_CONF_READ,
CTL_CONF_BEGIN,
CTL_CONF_COMMIT,
CTL_CONF_ABORT,
CTL_CONF_DIFF,
CTL_CONF_GET,
CTL_CONF_SET,
CTL_CONF_UNSET,
} ctl_cmd_t;
/*! \brief Remote command structure. */
/*! Control command parameters. */
typedef struct {
const knot_rrset_t *arg;
unsigned argc;
knot_rcode_t rc;
char *response;
size_t response_size;
size_t response_max;
} remote_cmdargs_t;
knot_ctl_t *ctl;
knot_ctl_type_t type;
knot_ctl_data_t data;
server_t *server;
} ctl_args_t;
/*! \brief Callback prototype for remote commands. */
typedef int (*remote_cmdf_t)(server_t *, remote_cmdargs_t *);
/*!
* Returns a string equivalent of the command.
*
* \param[in] cmd Command.
*
* \return Command string or NULL.
*/
const char* ctl_cmd_to_str(ctl_cmd_t cmd);
/*! \brief Remote command table item. */
typedef struct {
const char *name;
remote_cmdf_t f;
} remote_cmd_t;
/*!
* Returns a command corresponding to the string.
*
* \param[in] cmd_str Command string.
*
* \return Command.
*/
ctl_cmd_t ctl_str_to_cmd(const char *cmd_str);
/*!
* Executes a control command.
*
* \param[in] cmd Control command.
* \param[in] args Command arguments.
*
* \return Error code, KNOT_EOK if successful.
*/
int ctl_exec(ctl_cmd_t cmd, ctl_args_t *args);
/*! \brief Table of remote commands. */
extern const remote_cmd_t remote_cmd_tbl[];
/*! @} */
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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 <http://www.gnu.org/licenses/>.
*/
#include "knot/common/log.h"
#include "knot/ctl/commands.h"
#include "knot/ctl/process.h"
#include "libknot/error.h"
int ctl_process(knot_ctl_t *ctl, server_t *server)
{
if (ctl == NULL || server == NULL) {
return KNOT_EINVAL;
}
ctl_args_t args = {
.ctl = ctl,
.type = KNOT_CTL_TYPE_END,
.server = server
};
// Strip redundant/unprocessed data units in the current block.
bool strip = false;
while (true) {
// Receive data unit.
int ret = knot_ctl_receive(args.ctl, &args.type, &args.data);
if (ret != KNOT_EOK) {
log_debug("control, failed to receive (%s)",
knot_strerror(ret));
return ret;
}
// Decide what to do.
switch (args.type) {
case KNOT_CTL_TYPE_DATA:
// Leading data unit with a command name.
if (!strip) {
// Set to strip unprocessed data unit.
strip = true;
break;
}
// FALLTHROUGH
case KNOT_CTL_TYPE_EXTRA:
// All non-first data units should be parsed in a callback.
// Ignore if probable previous error.
continue;
case KNOT_CTL_TYPE_BLOCK:
strip = false;
continue;
case KNOT_CTL_TYPE_END:
return KNOT_EOF;
default:
assert(0);
}
const char *cmd_name = args.data[KNOT_CTL_IDX_CMD];
ctl_cmd_t cmd = ctl_str_to_cmd(cmd_name);
if (cmd != CTL_NONE) {
log_info("control, received command '%s'", cmd_name);
} else if (cmd_name != NULL){
log_debug("control, invalid command '%s'", cmd_name);
continue;
} else {
log_debug("control, empty command");
continue;
}
// Execute the command.
int cmd_ret = ctl_exec(cmd, &args);
switch (cmd_ret) {
case KNOT_EOK:
strip = false;
case KNOT_CTL_ESTOP:
break;
default:
log_debug("control, command '%s' (%s)", cmd_name,
knot_strerror(cmd_ret));
break;
}
// Finalize the answer block.
ret = knot_ctl_send(ctl, KNOT_CTL_TYPE_BLOCK, NULL);
if (ret != KNOT_EOK) {
log_debug("control, failed to reply (%s)",
knot_strerror(ret));
}
// Stop if required.
if (cmd_ret == KNOT_CTL_ESTOP) {
// Finalize the answer message.
ret = knot_ctl_send(ctl, KNOT_CTL_TYPE_END, NULL);
if (ret != KNOT_EOK) {
log_debug("control, failed to reply (%s)",
knot_strerror(ret));
}
return cmd_ret;
}
}
}
/* Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -12,9 +12,29 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
/*!
* \file
*
* \brief Server control processing.
*
* \addtogroup ctl
* @{
*/
#pragma once
int cmd_remote(const char *socket, const char *cmd, uint16_t rrt,
int argc, char *argv[]);
#include "libknot/libknot.h"
#include "knot/server/server.h"
/*!
* Processes incoming control commands.
*
* \param[in] ctl Control context.
* \param[in] server Server instance.
*
* \return Error code, KNOT_EOK if successful.
*/
int ctl_process(knot_ctl_t *ctl, server_t *server);
/*! @} */
This diff is collapsed.
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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 <http://www.gnu.org/licenses/>.
*/
/*!
* \file remote.h
*
* \author Marek Vavrusa <marek.vavrusa@nic.cz>
*
* \brief Functions for remote control interface.
*
* \addtogroup ctl
* @{
*/
#pragma once
#include "libknot/libknot.h"
#include "knot/server/server.h"
/*!
* \brief Bind RC interface.
*
* \param path Control UNIX socket path.
*
* \retval socket if passed.
* \retval knot_error else.
*/
int remote_bind(const char *path);
/*!
* \brief Unbind from RC interface and close socket.
*
* \note Breaks all pending connections.
*
* \param socket Interface socket.
*/
void remote_unbind(int sock);
/*!
* \brief Start a RC connection with remote.
*
* \param r RC interface socket.
* \param buf Buffer for RC command.
* \param buflen Maximum buffer size.
*
* \return client TCP socket if success.
* \return KNOT_ECONNREFUSED if fails to receive command.
*/
int remote_recv(int sock, uint8_t *buf, size_t *buflen);
/*!
* \brief Parse a RC command.
*
* \param pkt Query packet.
*
* \retval KNOT_EOK on success.
* \retval knot_error else.
*/
int remote_parse(knot_pkt_t *pkt);
/*!
* \brief Execute command and prepare answer for client.
*
* \param fd Remote client
* \param s Server instance.
* \param pkt Parsed RC command.
* \param rwire Buffer for response.
* \param rlen Maximum buffer size for response.
*
* \retval KNOT_EOK on success.
* \retval knot_error else.
*/
int remote_answer(int sock, server_t *s, knot_pkt_t *pkt);
/*!
* \brief Accept new client, receive command, process it and send response.
*
* \note This should be used as a high-level API for workers.
*
* \param server Server instance.
* \param sock RC interface socket.
* \param buf Buffer for commands/responses.
* \param buflen Maximum buffer size.
*
* \retval KNOT_EOK on success.
* \retval knot_error else.
*/
int remote_process(server_t *server, int sock, uint8_t *buf, size_t buflen);
/* Functions for creating RC packets. */
/*!
* \brief Build a RC command packet.
*
* \param query Command name, f.e. 'reload'.
*
* \retval KNOT_EOK on success.
* \retval knot_error else.
*/
knot_pkt_t* remote_query(const char *query);
/*!
* \brief Initialize a rrset with the given name and type.
*
* \param rr Output rrset.
* \param owner RRset owner.
* \param type RRset type.
*
* \return KNOT_E*.
*/
int remote_build_rr(knot_rrset_t *rr, const char *owner, uint16_t type);
/*!
* \brief Create a TXT rdata.
*
* \param rr Output rrset.
* \param str Text string.
* \param str_len Text string length.
* \param index Rdata index (ensures correct argument position).
*
* \return KNOT_E*.
*/
int remote_create_txt(knot_rrset_t *rr, const char *str, size_t str_len,
uint16_t index);
/*!
* \brief Create a NS rdata.
*
* \param rr Output rrset.
* \param name Domain name as a string.
*
* \return KNOT_E*
*/
int remote_create_ns(knot_rrset_t *rr, const char *name);
/*!
* \brief Print TXT rdata to stdout.
*
* \param rrset TXT rrset.
* \param pos Rdata position in the rrset.
*
* \return KNOT_E*
*/
int remote_print_txt(const knot_rrset_t *rrset, uint16_t pos);
/*!
* \brief Extracts TXT rdata into buffer.
*
* \param rrset TXT rrset.
* \param pos Rdata position in the rrset.
* \param out_len Output rdata blob length (optional).
*
* \return Rdata blob or NULL.
*/
uint8_t *remote_get_txt(const knot_rrset_t *rr, uint16_t pos, size_t *out_len);
/*! @} */
This diff is collapsed.
/* Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -24,36 +24,44 @@
#pragma once
#include "libknot/control/control.h"
#include "knot/ctl/commands.h"
/*! \brief Command parameter flags. */
typedef enum {
CMD_FNONE = 0, /*!< Empty flag. */
CMD_FFORCE = 1 << 0 /*!< Forced operation. */
CMD_FFORCE = 1 << 0, /*!< Forced operation. */
} cmd_flag_t;
/*! \brief Command condition flags. */
typedef enum {
CMD_CONF_FNONE = 0, /*!< Empty flag. */
CMD_CONF_FREAD = 1 << 0, /*!< Required read access to config or confdb. */
CMD_CONF_FWRITE = 1 << 1 /*!< Required write access to confdb. */
CMD_CONF_FNONE = 0, /*!< Empty flag. */
CMD_CONF_FREAD = 1 << 0, /*!< Required read access to config or confdb. */
CMD_CONF_FWRITE = 1 << 1, /*!< Required write access to confdb. */
CMD_CONF_FOPT_ITEM = 1 << 2, /*!< Optional item argument. */
CMD_CONF_FREQ_ITEM = 1 << 3, /*!< Required item argument. */
CMD_CONF_FOPT_DATA = 1 << 4, /*!< Optional item data argument. */
} cmd_conf_flag_t;
struct cmd_desc;
typedef struct cmd_desc cmd_desc_t;
/*! \brief Command callback arguments. */
typedef struct {
char *socket;
const cmd_desc_t *desc;
knot_ctl_t *ctl;
int argc;
char **argv;
cmd_flag_t flags;
} cmd_args_t;
/*! \brief Command callback prototype. */
typedef int (*cmd_t)(cmd_args_t *args);
/*! \brief Command callback description. */
typedef struct {
struct cmd_desc {
const char *name;
cmd_t cmd;
int (*fcn)(cmd_args_t *);
ctl_cmd_t cmd;
cmd_conf_flag_t flags;
} cmd_desc_t;
};
/*! \brief Old command name translation. */
typedef struct {
......
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -18,9 +18,7 @@
#include "utils/knotc/estimator.h"
#include "knot/zone/node.h"
#include "libknot/errcode.h"
#include "libknot/dname.h"
#include "libknot/descriptor.h"
#include "libknot/libknot.h"
#include "contrib/macros.h"
#include "contrib/string.h"
#include "contrib/ucw/lists.h"
......@@ -31,7 +29,7 @@ enum estim_consts {
MALLOC_MIN = MALLOC_OVERHEAD * 3 // minimum size of malloc'd chunk
};
typedef struct type_list_item {
typedef struct {
node_t n;
uint16_t type;
} type_list_item_t;
......
/* Copyright (C) 2013 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -14,13 +14,11 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file estimator.h
*
* \author Jan Kadlec <jan.kadlec@nic.cz>
* \file
*
* \brief Memory estimation for zone files.
*
* \addtogroup config
* \addtogroup knot_utils
* @{
*/
......@@ -29,19 +27,16 @@
#include "contrib/hat-trie/hat-trie.h"
#include "zscanner/scanner.h"
// Mutiplicative constant, needed because of malloc's fragmentation
static const double ESTIMATE_MAGIC = 1.0;
/*!
* \brief Memory estimation context.
*/
typedef struct zone_estim {
typedef struct {
hattrie_t *node_table; /*!< Same trie is in actual zone. */
size_t rdata_size; /*!< Estimated RDATA size. */
size_t dname_size; /*!< Estimated DNAME size. */
size_t node_size; /*!< Estimated node size. */
size_t htable_size; /*!< Estimated ahtable size. */
size_t record_count; /*!< Total record count for zone. */
size_t rdata_size; /*!< Estimated RDATA size. */
size_t dname_size; /*!< Estimated DNAME size. */
size_t node_size; /*!< Estimated node size. */
size_t htable_size; /*!< Estimated ahtable size. */
size_t record_count; /*!< Total record count for zone. */
} zone_estim_t;
/*!
......@@ -53,7 +48,7 @@ typedef struct zone_estim {
* \retval Alloc'd data on succes.
* \retval NULL on error.
*/
void *estimator_malloc(void* ctx, size_t len);
void *estimator_malloc(void *ctx, size_t len);
/*!
* \brief Size counting free wrapper.
......
/* Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
/* Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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
......@@ -18,15 +18,17 @@
#include <stdio.h>
#include <sys/stat.h>
#include "dnssec/crypto.h"
#include "knot/common/log.h"
#include "knot/conf/conf.h"
#include "libknot/libknot.h"
#include "utils/knotc/commands.h"
#include "utils/common/params.h"
#include "utils/common/strtonum.h"
#define PROGRAM_NAME "knotc"
#define DEFAULT_CTL_TIMEOUT 5
static void print_help(void)
{
printf("Usage: %s [parameters] <action> [action_args]\n"
......@@ -36,15 +38,18 @@ static void print_help(void)
" (default %s)\n"
" -C, --confdb <dir> Use a binary configuration database directory.\n"
" (default %s)\n"
" -s, --socket <path> Use a remote control UNIX socket path.\n"
" -s, --socket <path> Use a control UNIX socket path.\n"
" (default %s)\n"
" -t, --timeout <sec> Use a control socket timeout in seconds.\n"
" (default %u seconds)\n"
" -f, --force Forced operation. Overrides some checks.\n"
" -v, --verbose Enable debug output.\n"
" -h, --help Print the program help.\n"
" -V, --version Print the program version.\n"
"\n"
"Actions:\n",