From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Subject: [B.A.T.M.A.N.] [PATCH v5 11/20] batctl: Add helper to read/write boolean configuration values
Date: Sat, 9 Feb 2019 14:42:13 +0100 [thread overview]
Message-ID: <20190209134222.15035-12-sven@narfation.org> (raw)
In-Reply-To: <20190209134222.15035-1-sven@narfation.org>
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 <sven@narfation.org>
---
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 <linux/genetlink.h>
#include <netlink/genl/genl.h>
+#include <stdbool.h>
#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
--
2.20.1
next prev parent reply other threads:[~2019-02-09 13:42 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-09 13:42 [B.A.T.M.A.N.] [PATCH v5 00/20] batctl: netlink restructuring, part 3 Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 01/20] batctl: Add support for config mcast group in event monitor Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 02/20] batctl: Don't allocate new buffer for vlan parent device Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 03/20] batctl: Automatically translate vlan to mesh_iface Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 04/20] batctl: Add settings_data hooks for netlink integration Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 05/20] batctl: Parse the arguments for gw_mode Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 06/20] batctl: Add netlink simple query helper Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 07/20] batctl: Support generic netlink for gw_mode command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 08/20] batctl: Support generic netlink for loglevel command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 09/20] batctl: Support generic netlink for isolation_mark command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 10/20] batctl: Support generic netlink for orig_interval command Sven Eckelmann
2019-02-09 13:42 ` Sven Eckelmann [this message]
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 12/20] batctl: Support generic netlink for aggregation command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 13/20] batctl: Support generic netlink for ap_isolation command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 14/20] batctl: Support generic netlink for bonding command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 15/20] batctl: Support generic netlink for bridge_loop_avoidance command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 16/20] batctl: Support generic netlink for distributed_arp_table command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 17/20] batctl: Support generic netlink for fragmentation command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 18/20] batctl: Support generic netlink for multicast_mode command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 19/20] batctl: Support generic netlink for network_coding command Sven Eckelmann
2019-02-09 13:42 ` [B.A.T.M.A.N.] [PATCH v5 20/20] batctl: Drop settings_data param lists Sven Eckelmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190209134222.15035-12-sven@narfation.org \
--to=sven@narfation.org \
--cc=b.a.t.m.a.n@lists.open-mesh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).