From patchwork Sat Feb 9 13:42:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 17790 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from open-mesh.org (localhost [IPv6:::1]) by open-mesh.org (Postfix) with ESMTP id 8932281C97; Sat, 9 Feb 2019 14:43:04 +0100 (CET) Authentication-Results: open-mesh.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=narfation.org header.i=@narfation.org header.b="SXZqmU83"; dkim-atps=neutral Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=79.140.41.39; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver= Received: from v3-1039.vlinux.de (narfation.org [79.140.41.39]) by open-mesh.org (Postfix) with ESMTPS id A227481C23 for ; Sat, 9 Feb 2019 14:42:38 +0100 (CET) Received: from sven-desktop.home.narfation.org (p200300C597362EFD0000000000004065.dip0.t-ipconnect.de [IPv6:2003:c5:9736:2efd::4065]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id D78C911014A; Sat, 9 Feb 2019 14:42:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1549719757; bh=HKRAMIu29U1I7juRLtI0GUEXE/8PNlxyeiOqqJ7MemQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SXZqmU83100gB0tdLnSoIYtcqho/FPBZqdlrgUCDfL7+4UuD0B2Bo7nvCvRGFI0P1 iPPwojhy5Y+Q5xdIXkSlvVp4uVOaL4u9FPytRkCjblk1CDIsDX/qzWVJhxFL60ab9o TmBItBS1RQ0C6nN5TGYf8423UDfyD3vYRfRDjamI= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Sat, 9 Feb 2019 14:42:13 +0100 Message-Id: <20190209134222.15035-12-sven@narfation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190209134222.15035-1-sven@narfation.org> References: <20190209134222.15035-1-sven@narfation.org> MIME-Version: 1.0 Subject: [B.A.T.M.A.N.] [PATCH v5 11/20] batctl: Add helper to read/write boolean configuration values X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking Errors-To: b.a.t.m.a.n-bounces@lists.open-mesh.org Sender: "B.A.T.M.A.N" Most of the available settings commands in batctl only operate on a single boolean value. Both the reading, parsing and writing of these values can mostly be handled by the same set of helper functions. Only the actual setting/getting of the correct attribute has to be handled by the actual settings command implementation. Signed-off-by: Sven Eckelmann --- functions.c | 17 +++++++++++++++++ functions.h | 1 + sys.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ sys.h | 8 ++++++++ 4 files changed, 81 insertions(+) diff --git a/functions.c b/functions.c index 9fbd548..76deab7 100644 --- a/functions.c +++ b/functions.c @@ -1169,6 +1169,23 @@ void check_root_or_die(const char *cmd) } } +int parse_bool(const char *val, bool *res) +{ + if (strcasecmp(val, "0") == 0 || + strcasecmp(val, "disable") == 0 || + strcasecmp(val, "disabled") == 0) { + *res = false; + return 0; + } else if (strcasecmp(val, "1") == 0 || + strcasecmp(val, "enable") == 0 || + strcasecmp(val, "enabled") == 0) { + *res = true; + return 0; + } + + return -EINVAL; +} + bool parse_throughput(char *buff, const char *description, uint32_t *throughput) { enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT; diff --git a/functions.h b/functions.h index bffabc6..e2bbb4e 100644 --- a/functions.h +++ b/functions.h @@ -72,6 +72,7 @@ int check_mesh_iface_ownership(char *mesh_iface, char *hard_iface); void get_random_bytes(void *buf, size_t buflen); void check_root_or_die(const char *cmd); +int parse_bool(const char *val, bool *res); bool parse_throughput(char *buff, const char *description, uint32_t *throughput); diff --git a/sys.c b/sys.c index 0972394..26c191d 100644 --- a/sys.c +++ b/sys.c @@ -47,6 +47,34 @@ const char *sysfs_param_enable[] = { NULL, }; +int parse_simple_boolean(struct state *state, int argc, char *argv[]) +{ + struct settings_data *settings = state->cmd->arg; + struct simple_boolean_data *data = settings->data; + int ret; + + if (argc != 2) { + fprintf(stderr, "Error - incorrect number of arguments (expected 1)\n"); + return -EINVAL; + } + + ret = parse_bool(argv[1], &data->val); + if (ret < 0) { + fprintf(stderr, "Error - the supplied argument is invalid: %s\n", argv[1]); + fprintf(stderr, "The following values are allowed:\n"); + fprintf(stderr, " * 0\n"); + fprintf(stderr, " * disable\n"); + fprintf(stderr, " * disabled\n"); + fprintf(stderr, " * 1\n"); + fprintf(stderr, " * enable\n"); + fprintf(stderr, " * enabled\n"); + + return ret; + } + + return 0; +} + static int sys_simple_nlerror(struct sockaddr_nl *nla __maybe_unused, struct nlmsgerr *nlerr, void *arg) { @@ -106,6 +134,33 @@ int sys_simple_nlquery(struct state *state, enum batadv_nl_commands nl_cmd, return result; } +int sys_simple_print_boolean(struct nl_msg *msg, void *arg, + enum batadv_nl_attrs attr) +{ + struct nlattr *attrs[BATADV_ATTR_MAX + 1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct genlmsghdr *ghdr; + int *result = arg; + + if (!genlmsg_valid_hdr(nlh, 0)) + return NL_OK; + + ghdr = nlmsg_data(nlh); + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + return NL_OK; + } + + if (!attrs[attr]) + return NL_OK; + + printf("%s\n", nla_get_u8(attrs[attr]) ? "enabled" : "disabled"); + + *result = 0; + return NL_STOP; +} + static void settings_usage(struct state *state) { fprintf(stderr, "Usage: batctl [options] %s|%s [parameters] %s\n", diff --git a/sys.h b/sys.h index 2f7f5f4..5d0ff1e 100644 --- a/sys.h +++ b/sys.h @@ -27,6 +27,7 @@ #include #include +#include #include "batman_adv.h" #include "netlink.h" @@ -51,10 +52,17 @@ struct settings_data { extern const char *sysfs_param_enable[]; +struct simple_boolean_data { + bool val; +}; + int handle_sys_setting(struct state *state, int argc, char **argv); +int parse_simple_boolean(struct state *state, int argc, char *argv[]); int sys_simple_nlquery(struct state *state, enum batadv_nl_commands nl_cmd, nl_recvmsg_msg_cb_t attribute_cb, nl_recvmsg_msg_cb_t callback); +int sys_simple_print_boolean(struct nl_msg *msg, void *arg, + enum batadv_nl_attrs attr); #endif