From patchwork Sat Oct 31 18:40:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 18215 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id 5440780DE7; Sat, 31 Oct 2020 19:40:58 +0100 (CET) Received: from dvalin.narfation.org (dvalin.narfation.org [213.160.73.56]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 2C822802DD for ; Sat, 31 Oct 2020 19:40:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=narfation.org; s=20121; t=1604169654; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5yKYoGJ11hjrOs6NSySyS2f/u3AoQDqq1+soJA3BDsY=; b=dRvkiEMCi1GNn/dHVEGRGtzPUBRdDJUpK7k2iLOQmxDRugX+NdyqyXCEmuF29Fm85ys36m vLxCFdcv6RPYuKTcuVVVfEym9lPgJov3zIwCnSrSVkq+rT0b6bOU+W0C3j4JsiyEWOmIg0 P+jSuOuL+2H2Lmp2KerTI0HMv4VcSOI= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Cc: Sven Eckelmann Subject: [PATCH 1/3] batctl: Switch active routing algo list to netlink Date: Sat, 31 Oct 2020 19:40:48 +0100 Message-Id: <20201031184051.118630-1-sven@narfation.org> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1604169656; a=rsa-sha256; cv=none; b=L3YC2zosuyCaKzc5OXpuC8DdhI/kpxMXceV3GzzMmTMVGclpA31spWMzgKA8N3bN/GKSc2 PmnWHrpp67ADO3XrqIfRPcVZLTtoBaoSfpU0Cfn5uIKnMMN6AH+DD79r5TYTHR5yJ+vIn/ lFvPuK3OFbpVjLwuB2tff3waxzrcbQQ= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=narfation.org header.s=20121 header.b=dRvkiEMC; spf=pass (diktynna.open-mesh.org: domain of sven@narfation.org designates 213.160.73.56 as permitted sender) smtp.mailfrom=sven@narfation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1604169656; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:dkim-signature; bh=5yKYoGJ11hjrOs6NSySyS2f/u3AoQDqq1+soJA3BDsY=; b=asQ9CAEWZZw4y0Be+WcxhmT8y9yrVmguJvy9GKbYhfvQMUe33AY2b9kJKJjtldzUDyA7Wh 72vnJy6nnYUurwAkrZwo2oOCR1MeJ7EsXuR8s1SySJ3J6GmeRefNdS9kJrha6IlJF/Zw8v 9ck7LL3FZnYxQPr7rSbKs/bbVL2v6k8= Message-ID-Hash: QQJPWNBYTTUMY46GFF2TDPWCWQH5JK4J X-Message-ID-Hash: QQJPWNBYTTUMY46GFF2TDPWCWQH5JK4J X-MailFrom: sven@narfation.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: The sysfs support is disabled by default in batman-adv since a while and will be removed in 2021. The generic netlink interface should be used instead. But the list of active routing algorithms was only available when batman-adv was build with sysfs support. Instead of walking through (not existing) sysfs entries, query the list of batadv interfaces via rtnl and use the already existing helpers to get the routing algorithm. Signed-off-by: Sven Eckelmann --- routing_algo.c | 172 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 122 insertions(+), 50 deletions(-) diff --git a/routing_algo.c b/routing_algo.c index 7171c52..5fa360b 100644 --- a/routing_algo.c +++ b/routing_algo.c @@ -6,7 +6,6 @@ * License-Filename: LICENSES/preferred/GPL-2.0 */ -#include #include #include #include @@ -159,14 +158,130 @@ static int print_routing_algos(void) return err; } +static struct nla_policy link_policy[IFLA_MAX + 1] = { + [IFLA_IFNAME] = { .type = NLA_STRING, .maxlen = IFNAMSIZ }, +}; + +struct print_ra_interfaces_rtnl_arg { + uint8_t header_shown:1; +}; + +static int print_ra_interfaces_rtnl_parse(struct nl_msg *msg, void *arg) +{ + struct print_ra_interfaces_rtnl_arg *print_arg = arg; + struct nlattr *attrs[IFLA_MAX + 1]; + char algoname[256]; + struct ifinfomsg *ifm; + char *mesh_iface; + int ret; + + ifm = nlmsg_data(nlmsg_hdr(msg)); + ret = nlmsg_parse(nlmsg_hdr(msg), sizeof(*ifm), attrs, IFLA_MAX, + link_policy); + if (ret < 0) + goto err; + + if (!attrs[IFLA_IFNAME]) + goto err; + + mesh_iface = nla_get_string(attrs[IFLA_IFNAME]); + + ret = get_algoname_netlink(mesh_iface, algoname, sizeof(algoname)); + if (ret < 0) + goto err; + + if(!print_arg->header_shown) { + print_arg->header_shown = true; + printf("Active routing protocol configuration:\n"); + } + + printf(" * %s: %s\n", mesh_iface, algoname); + +err: + return NL_OK; +} + +static int print_ra_interfaces(void) +{ + struct print_ra_interfaces_rtnl_arg print_arg = {}; + + struct ifinfomsg rt_hdr = { + .ifi_family = IFLA_UNSPEC, + }; + struct nlattr *linkinfo; + struct nl_sock *sock; + struct nl_msg *msg; + struct nl_cb *cb; + int err = 0; + int ret; + + sock = nl_socket_alloc(); + if (!sock) + return -ENOMEM; + + ret = nl_connect(sock, NETLINK_ROUTE); + if (ret < 0) { + err = -ENOMEM; + goto err_free_sock; + } + + cb = nl_cb_alloc(NL_CB_DEFAULT); + if (!cb) { + err = -ENOMEM; + goto err_free_sock; + } + + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_ra_interfaces_rtnl_parse, + &print_arg); + + msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_REQUEST | NLM_F_DUMP); + if (!msg) { + err = -ENOMEM; + goto err_free_cb; + } + + ret = nlmsg_append(msg, &rt_hdr, sizeof(rt_hdr), NLMSG_ALIGNTO); + if (ret < 0) { + err = -ENOMEM; + goto err_free_msg; + } + + linkinfo = nla_nest_start(msg, IFLA_LINKINFO); + if (!linkinfo) { + err = -ENOMEM; + goto err_free_msg; + } + + ret = nla_put_string(msg, IFLA_INFO_KIND, "batadv"); + if (ret < 0) { + err = -ENOMEM; + goto err_free_msg; + } + nla_nest_end(msg, linkinfo); + + ret = nl_send_auto_complete(sock, msg); + if (ret < 0) + goto err_free_msg; + + nl_recvmsgs(sock, cb); + + if (print_arg.header_shown) + printf("\n"); + +err_free_msg: + nlmsg_free(msg); +err_free_cb: + nl_cb_put(cb); +err_free_sock: + nl_socket_free(sock); + + return err; +} + static int routing_algo(struct state *state __maybe_unused, int argc, char **argv) { - DIR *iface_base_dir; - struct dirent *iface_dir; int optchar; - char *path_buff; int res = EXIT_FAILURE; - int first_iface = 1; while ((optchar = getopt(argc, argv, "h")) != -1) { switch (optchar) { @@ -183,48 +298,10 @@ static int routing_algo(struct state *state __maybe_unused, int argc, char **arg if (argc == 2) { res = write_file(SYS_SELECTED_RA_PATH, "", argv[1], NULL); - goto out; - } - - path_buff = malloc(PATH_BUFF_LEN); - if (!path_buff) { - fprintf(stderr, "Error - could not allocate path buffer: out of memory ?\n"); - goto out; - } - - iface_base_dir = opendir(SYS_IFACE_PATH); - if (!iface_base_dir) { - fprintf(stderr, "Error - the directory '%s' could not be read: %s\n", - SYS_IFACE_PATH, strerror(errno)); - fprintf(stderr, "Is the batman-adv module loaded and sysfs mounted ?\n"); - goto free_buff; - } - - while ((iface_dir = readdir(iface_base_dir)) != NULL) { - snprintf(path_buff, PATH_BUFF_LEN, SYS_ROUTING_ALGO_FMT, iface_dir->d_name); - res = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS, 0, 0, 0); - if (res != EXIT_SUCCESS) - continue; - - if (line_ptr[strlen(line_ptr) - 1] == '\n') - line_ptr[strlen(line_ptr) - 1] = '\0'; - - if (first_iface) { - first_iface = 0; - printf("Active routing protocol configuration:\n"); - } - - printf(" * %s: %s\n", iface_dir->d_name, line_ptr); - - free(line_ptr); - line_ptr = NULL; + return EXIT_FAILURE; } - closedir(iface_base_dir); - free(path_buff); - - if (!first_iface) - printf("\n"); + print_ra_interfaces(); res = read_file("", SYS_SELECTED_RA_PATH, USE_READ_BUFF, 0, 0, 0); if (res != EXIT_SUCCESS) @@ -237,11 +314,6 @@ static int routing_algo(struct state *state __maybe_unused, int argc, char **arg print_routing_algos(); return EXIT_SUCCESS; - -free_buff: - free(path_buff); -out: - return res; } COMMAND(SUBCOMMAND, routing_algo, "ra", 0, NULL, From patchwork Sat Oct 31 18:40:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 18216 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id 14A6680E2F; Sat, 31 Oct 2020 19:41:02 +0100 (CET) Received: from dvalin.narfation.org (dvalin.narfation.org [IPv6:2a00:17d8:100::8b1]) by diktynna.open-mesh.org (Postfix) with ESMTPS id DC1A780E2F for ; Sat, 31 Oct 2020 19:40:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=narfation.org; s=20121; t=1604169659; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hPRLEmURpsTwI9DPd6YtsFsou2pO89CCqQJuV2GAJeg=; b=zgf4pZGHqngNsMPOzEmmvrd8dSr9NfCt8cqOLwym/4RLn2latzYzCtRCBxQdL6slMyVELU Kwok2+PfjOwnWc6PwFumo3PPBLpZAjqmrBYsS43y0aiCRdIjgHwrtjgfO7C8gFoMCGE5yE HKGstq62reKtLYro+0DpwOsPfudeEBg= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Cc: Sven Eckelmann Subject: [PATCH 2/3] batctl: Drop deprecated debugfs support Date: Sat, 31 Oct 2020 19:40:49 +0100 Message-Id: <20201031184051.118630-2-sven@narfation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201031184051.118630-1-sven@narfation.org> References: <20201031184051.118630-1-sven@narfation.org> MIME-Version: 1.0 ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1604169659; a=rsa-sha256; cv=none; b=lNVHDO833fv6j3E6V+mOs2iL6H0amCX+6i0YUoPk8Iy527EAxfHWOMjWwue4zpx/6oR9Tb 37GvLZMriFnV3zTHEfKDPPsBfdzfYXeFWaEzOKZaSWbnOBrdlPJfV85Oh5qo1biS58MHFt Dz0nbcptr2Vew38v8TPdOhwZ31ikotM= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=narfation.org header.s=20121 header.b=zgf4pZGH; spf=pass (diktynna.open-mesh.org: domain of sven@narfation.org designates 2a00:17d8:100::8b1 as permitted sender) smtp.mailfrom=sven@narfation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1604169659; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hPRLEmURpsTwI9DPd6YtsFsou2pO89CCqQJuV2GAJeg=; b=HvIdMbqOoL7P1b+YItl5tIv+78AHgQHW6aDg0rBgXhDaVVhi5237S2KvEpMoPpU16b4LWm wRr8mJD6DaJ8GNJnYXe7XCzleocxONEHom9rnTgvr8fX/gCMyuyax2Nn4Z2OZG8TB36ECw ml5qwH+6fJWe5VWqVhPIBtjGPt+8F04= Message-ID-Hash: B5WTHLU4XD4OMSEAFBO2JCR6NVCNPIRN X-Message-ID-Hash: B5WTHLU4XD4OMSEAFBO2JCR6NVCNPIRN X-MailFrom: sven@narfation.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: The debugfs support is disabled by default in batman-adv since a while and will be removed in 2021. The generic netlink interface should be used instead. The batctl support for it should be removed with the same release as the removal in batman-adv. Signed-off-by: Sven Eckelmann --- Makefile | 2 - README.rst | 10 ++-- backbonetable.c | 2 - claimtable.c | 2 - dat_cache.c | 2 - debug.c | 27 +-------- debug.h | 11 ---- debugfs.c | 140 ---------------------------------------------- debugfs.h | 25 --------- functions.c | 130 +++--------------------------------------- functions.h | 3 +- gateways.c | 2 - gw_mode.c | 6 +- icmp_helper.c | 1 - interface.c | 3 +- loglevel.c | 2 +- main.c | 2 +- man/batctl.8 | 7 +-- mcast_flags.c | 2 - nc_nodes.c | 18 ------ neighbors.c | 2 - originators.c | 2 - routing_algo.c | 20 +------ sys.c | 2 +- throughputmeter.c | 1 - transglobal.c | 2 - translocal.c | 2 - 27 files changed, 25 insertions(+), 403 deletions(-) delete mode 100644 debugfs.c delete mode 100644 debugfs.h delete mode 100644 nc_nodes.c diff --git a/Makefile b/Makefile index 780c2c0..71fdf88 100755 --- a/Makefile +++ b/Makefile @@ -13,7 +13,6 @@ export CONFIG_BATCTL_BISECT=n BINARY_NAME = batctl obj-y += bat-hosts.o -obj-y += debugfs.o obj-y += debug.o obj-y += functions.o obj-y += genl.o @@ -58,7 +57,6 @@ $(eval $(call add_command,mcast_flags,y)) $(eval $(call add_command,multicast_fanout,y)) $(eval $(call add_command,multicast_forceflood,y)) $(eval $(call add_command,multicast_mode,y)) -$(eval $(call add_command,nc_nodes,y)) $(eval $(call add_command,neighbors,y)) $(eval $(call add_command,network_coding,y)) $(eval $(call add_command,orig_interval,y)) diff --git a/README.rst b/README.rst index 4830347..9c55ad5 100644 --- a/README.rst +++ b/README.rst @@ -18,11 +18,11 @@ settings. How does it work ? ================== -batctl uses the debugfs/batman_adv/bat0/socket device provided by the B.A.T.M.A.N. -advanced kernel module to inject custom icmp packets into the data flow. That's why -ping and traceroute work almost like their IP based counterparts. Tcpdump was -designed because B.A.T.M.A.N. advanced encapsulates all traffic within batman -packets, so that the normal tcpdump would not recognize the packets. +batctl uses the raw packet sockets to inject custom icmp packets into the data +flow. That's why ping and traceroute work almost like their IP based +counterparts. Tcpdump was designed because B.A.T.M.A.N. advanced encapsulates +all traffic within batman packets, so that the normal tcpdump would not +recognize the packets. The bat-hosts file diff --git a/backbonetable.c b/backbonetable.c index 4774edf..84667c4 100644 --- a/backbonetable.c +++ b/backbonetable.c @@ -102,8 +102,6 @@ static int netlink_print_bla_backbone(struct state *state, char *orig_iface, } static struct debug_table_data batctl_debug_table_backbonetable = { - .debugfs_name = DEBUG_BACKBONETABLE, - .header_lines = 2, .netlink_fn = netlink_print_bla_backbone, }; diff --git a/claimtable.c b/claimtable.c index c4664d4..c41d9c3 100644 --- a/claimtable.c +++ b/claimtable.c @@ -107,8 +107,6 @@ static int netlink_print_bla_claim(struct state *state, char *orig_iface, } static struct debug_table_data batctl_debug_table_claimtable = { - .debugfs_name = DEBUG_CLAIMTABLE, - .header_lines = 2, .netlink_fn = netlink_print_bla_claim, }; diff --git a/dat_cache.c b/dat_cache.c index a2fee2a..1549c3d 100644 --- a/dat_cache.c +++ b/dat_cache.c @@ -123,8 +123,6 @@ static int netlink_print_dat_cache(struct state *state, char *orig_iface, } static struct debug_table_data batctl_debug_table_dat_cache = { - .debugfs_name = DEBUG_DAT_CACHE, - .header_lines = 2, .netlink_fn = netlink_print_dat_cache, }; diff --git a/debug.c b/debug.c index 66ddc3b..55520cb 100644 --- a/debug.c +++ b/debug.c @@ -13,7 +13,6 @@ #include #include "debug.h" -#include "debugfs.h" #include "functions.h" #include "netlink.h" #include "sys.h" @@ -47,8 +46,6 @@ int handle_debug_table(struct state *state, int argc, char **argv) { struct debug_table_data *debug_table = state->cmd->arg; int optchar, read_opt = USE_BAT_HOSTS; - char full_path[MAX_PATH+1]; - char *debugfs_mnt; char *orig_iface = NULL; float orig_timeout = 0.0f; float watch_interval = 1; @@ -147,25 +144,7 @@ int handle_debug_table(struct state *state, int argc, char **argv) return EXIT_FAILURE; } - debugfs_mnt = debugfs_mount(NULL); - if (!debugfs_mnt) { - fprintf(stderr, "Error - can't mount or find debugfs\n"); - return EXIT_FAILURE; - } - - if (debug_table->netlink_fn) { - err = debug_table->netlink_fn(state , orig_iface, read_opt, - orig_timeout, watch_interval); - if (err != -EOPNOTSUPP) - return err; - } - - if (orig_iface) - debugfs_make_path(DEBUG_BATIF_PATH_FMT "/", orig_iface, full_path, sizeof(full_path)); - else - debugfs_make_path(DEBUG_BATIF_PATH_FMT "/", state->mesh_iface, full_path, sizeof(full_path)); - - return read_file(full_path, debug_table->debugfs_name, - read_opt, orig_timeout, watch_interval, - debug_table->header_lines); + err = debug_table->netlink_fn(state , orig_iface, read_opt, + orig_timeout, watch_interval); + return err; } diff --git a/debug.h b/debug.h index 9d0bb92..f849d19 100644 --- a/debug.h +++ b/debug.h @@ -12,18 +12,7 @@ #include #include "main.h" -#define DEBUG_BATIF_PATH_FMT "%s/batman_adv/%s" -#define DEBUG_TRANSTABLE_GLOBAL "transtable_global" -#define DEBUG_BACKBONETABLE "bla_backbone_table" -#define DEBUG_CLAIMTABLE "bla_claim_table" -#define DEBUG_DAT_CACHE "dat_cache" -#define DEBUG_NC_NODES "nc_nodes" -#define DEBUG_MCAST_FLAGS "mcast_flags" -#define DEBUG_ROUTING_ALGOS "routing_algos" - struct debug_table_data { - const char *debugfs_name; - size_t header_lines; int (*netlink_fn)(struct state *state, char *hard_iface, int read_opt, float orig_timeout, float watch_interval); unsigned int option_unicast_only:1; diff --git a/debugfs.c b/debugfs.c deleted file mode 100644 index 5a71d65..0000000 --- a/debugfs.c +++ /dev/null @@ -1,140 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* Copyright (C) 2009 Clark Williams - * Copyright (C) 2009 Xiao Guangrong - * - * License-Filename: LICENSES/preferred/GPL-2.0 - */ - -#include "debugfs.h" -#include -#include -#include -#include -#include -#include - -#ifndef DEBUGFS_MAGIC -#define DEBUGFS_MAGIC 0x64626720 -#endif - -static int debugfs_premounted; -static char debugfs_mountpoint[MAX_PATH+1]; - -static const char *debugfs_find_mountpoint(void); -static int debugfs_valid_mountpoint(const char *debugfs); - -static const char *debugfs_known_mountpoints[] = { - "/sys/kernel/debug/", - "/debug/", - NULL, -}; - -/* construct a full path to a debugfs element */ -int debugfs_make_path(const char *fmt, const char *mesh_iface, char *buffer, - int size) -{ - if (strlen(debugfs_mountpoint) == 0) { - buffer[0] = '\0'; - return -1; - } - - return snprintf(buffer, size, fmt, debugfs_mountpoint, mesh_iface); -} - -static int debugfs_found; - -/* find the path to the mounted debugfs */ -static const char *debugfs_find_mountpoint(void) -{ - const char **ptr; - char type[100]; - FILE *fp; - - if (debugfs_found) - return (const char *)debugfs_mountpoint; - - ptr = debugfs_known_mountpoints; - while (*ptr) { - if (debugfs_valid_mountpoint(*ptr) == 0) { - debugfs_found = 1; - strncpy(debugfs_mountpoint, *ptr, - sizeof(debugfs_mountpoint)); - debugfs_mountpoint[sizeof(debugfs_mountpoint) - 1] = 0; - return debugfs_mountpoint; - } - ptr++; - } - - /* give up and parse /proc/mounts */ - fp = fopen("/proc/mounts", "r"); - if (fp == NULL) { - perror("Error - can't open /proc/mounts for read"); - return NULL; - } - - while (fscanf(fp, "%*s %" - STR(MAX_PATH) - "s %99s %*s %*d %*d\n", - debugfs_mountpoint, type) == 2) { - if (strcmp(type, "debugfs") == 0) - break; - } - fclose(fp); - - if (strcmp(type, "debugfs") != 0) - return NULL; - - debugfs_found = 1; - - return debugfs_mountpoint; -} - -/* verify that a mountpoint is actually a debugfs instance */ - -static int debugfs_valid_mountpoint(const char *debugfs) -{ - struct statfs st_fs; - - if (statfs(debugfs, &st_fs) < 0) - return -ENOENT; - else if (st_fs.f_type != (long) DEBUGFS_MAGIC) - return -ENOENT; - - return 0; -} - - -int debugfs_valid_entry(const char *path) -{ - struct stat st; - - if (stat(path, &st)) - return -errno; - - return 0; -} - -/* mount the debugfs somewhere if it's not mounted */ - -char *debugfs_mount(const char *mountpoint) -{ - /* see if it's already mounted */ - if (debugfs_find_mountpoint()) { - debugfs_premounted = 1; - return debugfs_mountpoint; - } - - /* if not mounted and no argument */ - if (mountpoint == NULL) - mountpoint = "/sys/kernel/debug"; - - if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) - return NULL; - - /* save the mountpoint */ - strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint)); - debugfs_mountpoint[sizeof(debugfs_mountpoint) - 1] = '\0'; - debugfs_found = 1; - - return debugfs_mountpoint; -} diff --git a/debugfs.h b/debugfs.h deleted file mode 100644 index 61384f9..0000000 --- a/debugfs.h +++ /dev/null @@ -1,25 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -/* Copyright (C) 2009 Clark Williams - * Copyright (C) 2009 Xiao Guangrong - * - * License-Filename: LICENSES/preferred/GPL-2.0 - */ - -#ifndef __DEBUGFS_H__ -#define __DEBUGFS_H__ - -#ifndef MAX_PATH -# define MAX_PATH 256 -#endif - -#ifndef STR -# define _STR(x) #x -# define STR(x) _STR(x) -#endif - -extern int debugfs_valid_entry(const char *path); -extern char *debugfs_mount(const char *mountpoint); -extern int debugfs_make_path(const char *fmt, const char *mesh_iface, - char *buffer, int size); - -#endif /* __DEBUGFS_H__ */ diff --git a/functions.c b/functions.c index 3223472..651fb1d 100644 --- a/functions.c +++ b/functions.c @@ -43,7 +43,6 @@ #include "bat-hosts.h" #include "sys.h" #include "debug.h" -#include "debugfs.h" #include "netlink.h" #define PATH_BUFF_LEN 400 @@ -140,14 +139,6 @@ static void file_open_problem_dbg(const char *dir, const char *full_path) fprintf(stderr, "Consult the README if you wish to learn more about compiling options into batman-adv.\n"); } -static int str_is_mcast_addr(char *addr) -{ - struct ether_addr *mac_addr = ether_aton(addr); - - return !mac_addr ? 0 : - mac_addr->ether_addr_octet[0] & 0x01; -} - static bool ether_addr_valid(const uint8_t *addr) { /* no multicast address */ @@ -171,67 +162,28 @@ static void print_inv_bool(char *line) printf("%s", line); } -int read_file(const char *dir, const char *fname, int read_opt, - float orig_timeout, float watch_interval, size_t header_lines) +int read_file(const char *dir, const char *fname, int read_opt) { - struct ether_addr *mac_addr; - struct bat_host *bat_host; int res = EXIT_FAILURE; - float last_seen; - char full_path[500], *buff_ptr, *space_ptr, extra_char; + char full_path[500]; size_t len = 0; FILE *fp = NULL; - size_t line; - - if (read_opt & USE_BAT_HOSTS) - bat_hosts_init(read_opt); snprintf(full_path, sizeof(full_path), "%s%s", dir, fname); -open: - line = 0; fp = fopen(full_path, "r"); - if (!fp) { if (!(read_opt & SILENCE_ERRORS)) file_open_problem_dbg(dir, full_path); - goto out; + return res; } - if (read_opt & CLR_CONT_READ) - /* clear screen, set cursor back to 0,0 */ - printf("\033[2J\033[0;0f"); - -read: while (getline(&line_ptr, &len, fp) != -1) { - if (line++ < header_lines && read_opt & SKIP_HEADER) - continue; - /* the buffer will be handled elsewhere */ if (read_opt & USE_READ_BUFF) break; - /* skip timed out originators */ - if (read_opt & NO_OLD_ORIGS) - if (sscanf(line_ptr, "%*s %f", &last_seen) - && (last_seen > orig_timeout)) - continue; - - /* translation table: skip multicast */ - if (line > header_lines && - read_opt & UNICAST_ONLY && - strlen(line_ptr) > strlen(" * xx:xx:xx:") && - str_is_mcast_addr(line_ptr+3)) - continue; - - /* translation table: skip unicast */ - if (line > header_lines && - read_opt & MULTICAST_ONLY && - strlen(line_ptr) > strlen(" * xx:xx:xx:") && - !str_is_mcast_addr(line_ptr+3)) - continue; - if (!(read_opt & USE_BAT_HOSTS)) { if (read_opt & INVERSE_BOOL) print_inv_bool(line_ptr); @@ -241,81 +193,13 @@ int read_file(const char *dir, const char *fname, int read_opt, continue; } - /* replace mac addresses with bat host names */ - buff_ptr = line_ptr; - - while ((space_ptr = strchr(buff_ptr, ' ')) != NULL) { - - *space_ptr = '\0'; - extra_char = '\0'; - - if (strlen(buff_ptr) == ETH_STR_LEN + 1) { - extra_char = buff_ptr[ETH_STR_LEN]; - switch (extra_char) { - case ',': - case ')': - buff_ptr[ETH_STR_LEN] = '\0'; - break; - default: - extra_char = '\0'; - break; - } - } - - if (strlen(buff_ptr) != ETH_STR_LEN) - goto print_plain_buff; - - mac_addr = ether_aton(buff_ptr); - - if (!mac_addr) - goto print_plain_buff; - - bat_host = bat_hosts_find_by_mac((char *)mac_addr); - - if (!bat_host) - goto print_plain_buff; - - /* keep table format */ - printf("%17s", bat_host->name); - - goto written; - -print_plain_buff: - printf("%s", buff_ptr); - -written: - if (extra_char != '\0') - printf("%c", extra_char); - - printf(" "); - buff_ptr = space_ptr + 1; - } - - printf("%s", buff_ptr); - } - - if (read_opt & CONT_READ) { - usleep(1000000 * watch_interval); - goto read; - } - - if (read_opt & CLR_CONT_READ) { - if (fp) - fclose(fp); - usleep(1000000 * watch_interval); - goto open; + printf("%s", line_ptr); } if (line_ptr) res = EXIT_SUCCESS; -out: - if (fp) - fclose(fp); - - if (read_opt & USE_BAT_HOSTS) - bat_hosts_free(); - + fclose(fp); return res; } @@ -391,7 +275,7 @@ int get_algoname(const char *mesh_iface, char *algoname, size_t algoname_len) } snprintf(path_buff, PATH_BUFF_LEN, SYS_ROUTING_ALGO_FMT, mesh_iface); - ret = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS, 0, 0, 0); + ret = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS); if (ret != EXIT_SUCCESS) { ret = -ENOENT; goto free_path_buf; @@ -1129,7 +1013,7 @@ static int check_mesh_iface_ownership_sysfs(struct state *state, /* check if this device actually belongs to the mesh interface */ snprintf(path_buff, sizeof(path_buff), SYS_MESH_IFACE_FMT, hard_iface); - res = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS, 0, 0, 0); + res = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS); if (res != EXIT_SUCCESS) { fprintf(stderr, "Error - the directory '%s' could not be read: %s\n", path_buff, strerror(errno)); diff --git a/functions.h b/functions.h index b2180e8..f63c438 100644 --- a/functions.h +++ b/functions.h @@ -43,8 +43,7 @@ char *ether_ntoa_long(const struct ether_addr *addr); char *get_name_by_macaddr(struct ether_addr *mac_addr, int read_opt); char *get_name_by_macstr(char *mac_str, int read_opt); int file_exists(const char *fpath); -int read_file(const char *dir, const char *path, int read_opt, - float orig_timeout, float watch_interval, size_t header_lines); +int read_file(const char *dir, const char *path, int read_opt); int write_file(const char *dir, const char *fname, const char *arg1, const char *arg2); struct ether_addr *translate_mac(const char *mesh_iface, diff --git a/gateways.c b/gateways.c index 2db4b17..31979a2 100644 --- a/gateways.c +++ b/gateways.c @@ -145,8 +145,6 @@ static int netlink_print_gateways(struct state *state, char *orig_iface, } static struct debug_table_data batctl_debug_table_gateways = { - .debugfs_name = "gateways", - .header_lines = 1, .netlink_fn = netlink_print_gateways, }; diff --git a/gw_mode.c b/gw_mode.c index beaa83f..72b5fbf 100644 --- a/gw_mode.c +++ b/gw_mode.c @@ -266,7 +266,7 @@ static int gw_read_setting(struct state *state, const char *path_buff) return EXIT_SUCCESS; /* fallback to sysfs */ - res = read_file(path_buff, SYS_GW_MODE, USE_READ_BUFF, 0, 0, 0); + res = read_file(path_buff, SYS_GW_MODE, USE_READ_BUFF); if (res != EXIT_SUCCESS) goto out; @@ -285,10 +285,10 @@ static int gw_read_setting(struct state *state, const char *path_buff) switch (gw_mode) { case BATADV_GW_MODE_CLIENT: - res = read_file(path_buff, SYS_GW_SEL, USE_READ_BUFF, 0, 0, 0); + res = read_file(path_buff, SYS_GW_SEL, USE_READ_BUFF); break; case BATADV_GW_MODE_SERVER: - res = read_file(path_buff, SYS_GW_BW, USE_READ_BUFF, 0, 0, 0); + res = read_file(path_buff, SYS_GW_BW, USE_READ_BUFF); break; default: printf("off\n"); diff --git a/icmp_helper.c b/icmp_helper.c index 0ce2d17..a167e2a 100644 --- a/icmp_helper.c +++ b/icmp_helper.c @@ -28,7 +28,6 @@ #include "batadv_packet.h" #include "debug.h" -#include "debugfs.h" #include "functions.h" #include "list.h" #include "netlink.h" diff --git a/interface.c b/interface.c index d0d9435..34381de 100644 --- a/interface.c +++ b/interface.c @@ -168,8 +168,7 @@ static int print_interfaces_rtnl_parse(struct nl_msg *msg, void *arg) if (!status) { snprintf(path_buff, sizeof(path_buff), SYS_IFACE_STATUS_FMT, ifname); - ret = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS, - 0, 0, 0); + ret = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS); if (ret != EXIT_SUCCESS) status = "\n"; else diff --git a/loglevel.c b/loglevel.c index 69f1855..1e4b690 100644 --- a/loglevel.c +++ b/loglevel.c @@ -96,7 +96,7 @@ static int log_level_read_setting(struct state *state, const char *path_buff) if (res >= 0) return EXIT_SUCCESS; - res = read_file(path_buff, SYS_LOG_LEVEL, USE_READ_BUFF, 0, 0, 0); + res = read_file(path_buff, SYS_LOG_LEVEL, USE_READ_BUFF); if (res != EXIT_SUCCESS) return res; diff --git a/main.c b/main.c index f6337d6..6ea589a 100644 --- a/main.c +++ b/main.c @@ -123,7 +123,7 @@ static void version(void) printf("batctl %s [batman-adv: ", SOURCE_VERSION); - ret = read_file("", module_ver_path, USE_READ_BUFF | SILENCE_ERRORS, 0, 0, 0); + ret = read_file("", module_ver_path, USE_READ_BUFF | SILENCE_ERRORS); if ((line_ptr) && (line_ptr[strlen(line_ptr) - 1] == '\n')) line_ptr[strlen(line_ptr) - 1] = '\0'; diff --git a/man/batctl.8 b/man/batctl.8 index 1737e17..012741c 100644 --- a/man/batctl.8 +++ b/man/batctl.8 @@ -241,9 +241,7 @@ Example 3: 16 or 0x0F .I \fBdebug tables: .IP The batman-adv kernel module comes with a variety of debug tables containing various information about the state of the mesh -seen by each individual node. These tables are exported via debugfs and easily accessible via batctl. You will need debugfs -support compiled into your kernel and preferably have mounted the debugfs to a well-known mountpoint. If debugfs is not -mounted batctl will attempt to do this step for you. +seen by each individual node. All of the debug tables support the following options: .RS 10 @@ -290,9 +288,6 @@ List of debug tables: \- dat_cache|dc (compile time option) .RE .RS 10 -\- nc_nodes|nn (compile time option) -.RE -.RS 10 \- mcast_flags|mf (compile time option) .RE .RE diff --git a/mcast_flags.c b/mcast_flags.c index 363b6e8..63a2c50 100644 --- a/mcast_flags.c +++ b/mcast_flags.c @@ -154,8 +154,6 @@ static int netlink_print_mcast_flags(struct state *state, char *orig_iface, } static struct debug_table_data batctl_debug_table_mcast_flags = { - .debugfs_name = DEBUG_MCAST_FLAGS, - .header_lines = 6, .netlink_fn = netlink_print_mcast_flags, }; diff --git a/nc_nodes.c b/nc_nodes.c deleted file mode 100644 index be4e804..0000000 --- a/nc_nodes.c +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* Copyright (C) 2009-2020 B.A.T.M.A.N. contributors: - * - * Marek Lindner - * - * License-Filename: LICENSES/preferred/GPL-2.0 - */ - -#include "debug.h" -#include "main.h" - -static struct debug_table_data batctl_debug_table_nc_nodes = { - .debugfs_name = DEBUG_NC_NODES, - .header_lines = 0, -}; - -COMMAND_NAMED(DEBUGTABLE, nc_nodes, "nn", handle_debug_table, - COMMAND_FLAG_MESH_IFACE, &batctl_debug_table_nc_nodes, ""); diff --git a/neighbors.c b/neighbors.c index dc6913a..afc65de 100644 --- a/neighbors.c +++ b/neighbors.c @@ -118,8 +118,6 @@ static int netlink_print_neighbors(struct state *state, char *orig_iface, } static struct debug_table_data batctl_debug_table_neighbors = { - .debugfs_name = "neighbors", - .header_lines = 2, .netlink_fn = netlink_print_neighbors, }; diff --git a/originators.c b/originators.c index f9bf21d..793743a 100644 --- a/originators.c +++ b/originators.c @@ -196,8 +196,6 @@ static int netlink_print_originators(struct state *state, char *orig_iface, } static struct debug_table_data batctl_debug_table_originators = { - .debugfs_name = "originators", - .header_lines = 2, .netlink_fn = netlink_print_originators, .option_timeout_interval = 1, .option_orig_iface = 1, diff --git a/routing_algo.c b/routing_algo.c index 5fa360b..0f2e1bb 100644 --- a/routing_algo.c +++ b/routing_algo.c @@ -19,7 +19,6 @@ #include "batadv_packet.h" #include "batman_adv.h" #include "debug.h" -#include "debugfs.h" #include "functions.h" #include "main.h" #include "netlink.h" @@ -133,28 +132,11 @@ static int netlink_print_routing_algos(void) return last_err; } -static int debug_print_routing_algos(void) -{ - char full_path[MAX_PATH+1]; - char *debugfs_mnt; - - debugfs_mnt = debugfs_mount(NULL); - if (!debugfs_mnt) { - fprintf(stderr, "Error - can't mount or find debugfs\n"); - return -1; - } - - debugfs_make_path(DEBUG_BATIF_PATH_FMT, "", full_path, sizeof(full_path)); - return read_file(full_path, DEBUG_ROUTING_ALGOS, 0, 0, 0, 0); -} - static int print_routing_algos(void) { int err; err = netlink_print_routing_algos(); - if (err == -EOPNOTSUPP) - err = debug_print_routing_algos(); return err; } @@ -303,7 +285,7 @@ static int routing_algo(struct state *state __maybe_unused, int argc, char **arg print_ra_interfaces(); - res = read_file("", SYS_SELECTED_RA_PATH, USE_READ_BUFF, 0, 0, 0); + res = read_file("", SYS_SELECTED_RA_PATH, USE_READ_BUFF); if (res != EXIT_SUCCESS) return EXIT_FAILURE; diff --git a/sys.c b/sys.c index c76ad15..7668a72 100644 --- a/sys.c +++ b/sys.c @@ -208,7 +208,7 @@ static int sys_read_setting(struct state *state, const char *path_buff, if (state->cmd->flags & COMMAND_FLAG_INVERSE) read_opt |= INVERSE_BOOL; - res = read_file(path_buff, sysfs_name, read_opt, 0, 0, 0); + res = read_file(path_buff, sysfs_name, read_opt); } return res; diff --git a/throughputmeter.c b/throughputmeter.c index 0ec5c31..4a233d5 100644 --- a/throughputmeter.c +++ b/throughputmeter.c @@ -32,7 +32,6 @@ #include "functions.h" #include "genl.h" #include "netlink.h" -#include "debugfs.h" static struct ether_addr *dst_mac; static char *tp_mesh_iface; diff --git a/transglobal.c b/transglobal.c index 3b23792..5aede41 100644 --- a/transglobal.c +++ b/transglobal.c @@ -136,8 +136,6 @@ static int netlink_print_transglobal(struct state *state, char *orig_iface, } static struct debug_table_data batctl_debug_table_transglobal = { - .debugfs_name = "transtable_global", - .header_lines = 2, .netlink_fn = netlink_print_transglobal, .option_unicast_only = 1, .option_multicast_only = 1, diff --git a/translocal.c b/translocal.c index 59c00c2..45e884c 100644 --- a/translocal.c +++ b/translocal.c @@ -132,8 +132,6 @@ static int netlink_print_translocal(struct state *state, char *orig_iface, } static struct debug_table_data batctl_debug_table_translocal = { - .debugfs_name = "transtable_local", - .header_lines = 2, .netlink_fn = netlink_print_translocal, .option_unicast_only = 1, .option_multicast_only = 1, From patchwork Sat Oct 31 18:40:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 18217 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id 2EDAE80BA6; Sat, 31 Oct 2020 19:41:10 +0100 (CET) Received: from dvalin.narfation.org (dvalin.narfation.org [213.160.73.56]) by diktynna.open-mesh.org (Postfix) with ESMTPS id AC72A80F6A for ; Sat, 31 Oct 2020 19:41:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=narfation.org; s=20121; t=1604169662; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PwM/wzMy/0LBLyt5LFSAfj0cRWdoyO8k52qT3o2HEXY=; b=VZQV3zZ+Iv1ir4mG4P45+O48ayIWmJnM42X8X1jACaSF1V1UvEIL10hyHnssYzqRCW4feM LyXSW6ie6Fqm2lWP2dYJ3NQcrR2BfeUhLKQbqAfjDqHUi+/dsrtse3R8MsSNpGwNZuy7Xv 8z5/x8OpwyauUv0X49JTZzsAYQ+8mzc= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Cc: Sven Eckelmann Subject: [PATCH 3/3] batctl: Drop deprecated sysfs support Date: Sat, 31 Oct 2020 19:40:50 +0100 Message-Id: <20201031184051.118630-3-sven@narfation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201031184051.118630-1-sven@narfation.org> References: <20201031184051.118630-1-sven@narfation.org> MIME-Version: 1.0 ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1604169662; a=rsa-sha256; cv=none; b=jBuJGe1Wg1i9nFlZGRXrbTkcD2OcMZlkKu1hmXSnoPXZkWMxvl5FEJHR4AKJFHJItA756v 3d1mjPxzyX+Ch6arIuqiqMpSwxPDgvS3gKGvVA4Ht16ZQTsYzQst3EhxVRtgRr6aZLD5zL h0TPRddCZL9YTfBuKTXLA9xRINePC4w= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=narfation.org header.s=20121 header.b=VZQV3zZ+; spf=pass (diktynna.open-mesh.org: domain of sven@narfation.org designates 213.160.73.56 as permitted sender) smtp.mailfrom=sven@narfation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1604169662; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PwM/wzMy/0LBLyt5LFSAfj0cRWdoyO8k52qT3o2HEXY=; b=DsTSXUto12q5A6dVXikQ7lkDN93x+hYYYmcl33E0zBMzD391uym+umAoEEsCE6qKnNcdVT ZV8zifWViqIdRyzHlYGowJmCT4FmpxZo2p6NKxiuLnii7s9Cww7zUuQbWMcR1MqKpNgqGU rjT88swiT8Xqi5yCD6WWRq8VpB/pqsc= Message-ID-Hash: 2MUWXHENSKLUL5NMT2JDAWV67IJ4AHL5 X-Message-ID-Hash: 2MUWXHENSKLUL5NMT2JDAWV67IJ4AHL5 X-MailFrom: sven@narfation.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: The sysfs support is disabled by default in batman-adv since a while and will be removed in 2021. The generic netlink interface should be used instead. The batctl support for it should be removed with the same release as the removal in batman-adv. Signed-off-by: Sven Eckelmann --- aggregation.c | 1 - ap_isolation.c | 1 - bonding.c | 1 - bridge_loop_avoidance.c | 1 - distributed_arp_table.c | 1 - elp_interval.c | 1 - fragmentation.c | 1 - functions.c | 183 ++-------------------------------------- functions.h | 5 +- gw_mode.c | 111 +++--------------------- hop_penalty.c | 2 - interface.c | 15 +--- isolation_mark.c | 1 - loglevel.c | 44 +++------- main.c | 7 +- multicast_fanout.c | 1 - multicast_forceflood.c | 1 - multicast_mode.c | 1 - network_coding.c | 1 - orig_interval.c | 1 - routing_algo.c | 38 ++++++--- sys.c | 90 +++----------------- sys.h | 9 -- throughput_override.c | 1 - 24 files changed, 71 insertions(+), 447 deletions(-) diff --git a/aggregation.c b/aggregation.c index 11327d3..5d06471 100644 --- a/aggregation.c +++ b/aggregation.c @@ -48,7 +48,6 @@ static int set_aggregated_ogms(struct state *state) } static struct settings_data batctl_settings_aggregation = { - .sysfs_name = "aggregated_ogms", .data = &aggregated_ogms, .parse = parse_simple_boolean, .netlink_get = get_aggregated_ogms, diff --git a/ap_isolation.c b/ap_isolation.c index df19072..80627e6 100644 --- a/ap_isolation.c +++ b/ap_isolation.c @@ -70,7 +70,6 @@ static int set_ap_isolation(struct state *state) } static struct settings_data batctl_settings_ap_isolation = { - .sysfs_name = "ap_isolation", .data = &ap_isolation, .parse = parse_simple_boolean, .netlink_get = get_ap_isolation, diff --git a/bonding.c b/bonding.c index db0782c..ee4d314 100644 --- a/bonding.c +++ b/bonding.c @@ -47,7 +47,6 @@ static int set_bonding(struct state *state) } static struct settings_data batctl_settings_bonding = { - .sysfs_name = "bonding", .data = &bonding, .parse = parse_simple_boolean, .netlink_get = get_bonding, diff --git a/bridge_loop_avoidance.c b/bridge_loop_avoidance.c index 6572f6a..3525a3e 100644 --- a/bridge_loop_avoidance.c +++ b/bridge_loop_avoidance.c @@ -48,7 +48,6 @@ static int set_bridge_loop_avoidance(struct state *state) } static struct settings_data batctl_settings_bridge_loop_avoidance = { - .sysfs_name = "bridge_loop_avoidance", .data = &bridge_loop_avoidance, .parse = parse_simple_boolean, .netlink_get = get_bridge_loop_avoidance, diff --git a/distributed_arp_table.c b/distributed_arp_table.c index 4e244ef..1bccba1 100644 --- a/distributed_arp_table.c +++ b/distributed_arp_table.c @@ -48,7 +48,6 @@ static int set_distributed_arp_table(struct state *state) } static struct settings_data batctl_settings_distributed_arp_table = { - .sysfs_name = "distributed_arp_table", .data = &distributed_arp_table, .parse = parse_simple_boolean, .netlink_get = get_distributed_arp_table, diff --git a/elp_interval.c b/elp_interval.c index f551b1f..f4312e7 100644 --- a/elp_interval.c +++ b/elp_interval.c @@ -98,7 +98,6 @@ static int set_elp_interval(struct state *state) } static struct settings_data batctl_settings_elp_interval = { - .sysfs_name = "elp_interval", .data = &elp_interval, .parse = parse_elp_interval, .netlink_get = get_elp_interval, diff --git a/fragmentation.c b/fragmentation.c index a8cb1e6..65e4356 100644 --- a/fragmentation.c +++ b/fragmentation.c @@ -48,7 +48,6 @@ static int set_fragmentation(struct state *state) } static struct settings_data batctl_settings_fragmentation = { - .sysfs_name = "fragmentation", .data = &fragmentation, .parse = parse_simple_boolean, .netlink_get = get_fragmentation, diff --git a/functions.c b/functions.c index 651fb1d..4df1815 100644 --- a/functions.c +++ b/functions.c @@ -117,18 +117,8 @@ int file_exists(const char *fpath) return stat(fpath, &st) == 0; } -static void file_open_problem_dbg(const char *dir, const char *full_path) +static void file_open_problem_dbg(const char *full_path) { - struct stat st; - - if (strstr(dir, "/sys/")) { - if (stat("/sys/", &st) != 0) { - fprintf(stderr, "Error - the folder '/sys/' was not found on the system\n"); - fprintf(stderr, "Please make sure that the sys filesystem is properly mounted\n"); - return; - } - } - if (!file_exists(module_ver_path)) { fprintf(stderr, "Error - batman-adv module has not been loaded\n"); return; @@ -152,29 +142,16 @@ static bool ether_addr_valid(const uint8_t *addr) return true; } -static void print_inv_bool(char *line) -{ - if (!strncmp("enabled", line, strlen("enabled"))) - printf("disabled\n"); - else if (!strncmp("disabled", line, strlen("disabled"))) - printf("enabled\n"); - else - printf("%s", line); -} - -int read_file(const char *dir, const char *fname, int read_opt) +int read_file(const char *full_path, int read_opt) { int res = EXIT_FAILURE; - char full_path[500]; size_t len = 0; FILE *fp = NULL; - snprintf(full_path, sizeof(full_path), "%s%s", dir, fname); - fp = fopen(full_path, "r"); if (!fp) { if (!(read_opt & SILENCE_ERRORS)) - file_open_problem_dbg(dir, full_path); + file_open_problem_dbg(full_path); return res; } @@ -184,15 +161,6 @@ int read_file(const char *dir, const char *fname, int read_opt) if (read_opt & USE_READ_BUFF) break; - if (!(read_opt & USE_BAT_HOSTS)) { - if (read_opt & INVERSE_BOOL) - print_inv_bool(line_ptr); - else - printf("%s", line_ptr); - - continue; - } - printf("%s", line_ptr); } @@ -203,40 +171,6 @@ int read_file(const char *dir, const char *fname, int read_opt) return res; } -int write_file(const char *dir, const char *fname, const char *arg1, - const char *arg2) -{ - int fd = -1, res = EXIT_FAILURE; - char full_path[500]; - ssize_t write_len; - - snprintf(full_path, sizeof(full_path), "%s%s", dir, fname); - - fd = open(full_path, O_WRONLY); - - if (fd < 0) { - file_open_problem_dbg(dir, full_path); - goto out; - } - - if (arg2) - write_len = dprintf(fd, "%s %s", arg1, arg2); - else - write_len = write(fd, arg1, strlen(arg1) + 1); - - if (write_len < 0) { - fprintf(stderr, "Error - can't write to file '%s': %s\n", full_path, strerror(errno)); - goto out; - } - - res = EXIT_SUCCESS; - -out: - if (fd >= 0) - close(fd); - return res; -} - struct ether_addr *translate_mac(const char *mesh_iface, const struct ether_addr *mac) { @@ -261,40 +195,7 @@ struct ether_addr *translate_mac(const char *mesh_iface, int get_algoname(const char *mesh_iface, char *algoname, size_t algoname_len) { - char *path_buff; - int ret; - - ret = get_algoname_netlink(mesh_iface, algoname, algoname_len); - if (ret != -EOPNOTSUPP) - return ret; - - path_buff = malloc(PATH_BUFF_LEN); - if (!path_buff) { - fprintf(stderr, "Error - could not allocate path buffer: out of memory ?\n"); - return -ENOMEM; - } - - snprintf(path_buff, PATH_BUFF_LEN, SYS_ROUTING_ALGO_FMT, mesh_iface); - ret = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS); - if (ret != EXIT_SUCCESS) { - ret = -ENOENT; - goto free_path_buf; - } - - if (line_ptr[strlen(line_ptr) - 1] == '\n') - line_ptr[strlen(line_ptr) - 1] = '\0'; - - strncpy(algoname, line_ptr, algoname_len); - if (algoname_len > 0) - algoname[algoname_len - 1] = '\0'; - -free_path_buf: - free(path_buff); - - free(line_ptr); - line_ptr = NULL; - - return ret; + return get_algoname_netlink(mesh_iface, algoname, algoname_len); } static int resolve_l3addr(int ai_family, const char *asc, void *l3addr) @@ -948,45 +849,16 @@ int guess_netdev_type(const char *netdev, enum selector_prefix *type) return -EINVAL; } -static int check_mesh_iface_sysfs(struct state *state) -{ - char path_buff[PATH_BUFF_LEN]; - DIR *dir; - - /* try to open the mesh sys directory */ - snprintf(path_buff, PATH_BUFF_LEN, SYS_BATIF_PATH_FMT, - state->mesh_iface); - - dir = opendir(path_buff); - if (!dir) - return -1; - - closedir(dir); - - return 0; -} - int check_mesh_iface(struct state *state) { - int ret; - state->mesh_ifindex = if_nametoindex(state->mesh_iface); if (state->mesh_ifindex == 0) return -1; - ret = check_mesh_iface_netlink(state->mesh_ifindex); - if (ret == 0) - return ret; - - ret = check_mesh_iface_sysfs(state); - if (ret == 0) - return ret; - - return -1; + return check_mesh_iface_netlink(state->mesh_ifindex); } -static int check_mesh_iface_ownership_netlink(struct state *state, - char *hard_iface) +int check_mesh_iface_ownership(struct state *state, char *hard_iface) { struct rtnl_link_iface_data link_data; unsigned int hardif_index; @@ -1005,49 +877,6 @@ static int check_mesh_iface_ownership_netlink(struct state *state, return EXIT_SUCCESS; } -static int check_mesh_iface_ownership_sysfs(struct state *state, - char *hard_iface) -{ - char path_buff[PATH_BUFF_LEN]; - int res; - - /* check if this device actually belongs to the mesh interface */ - snprintf(path_buff, sizeof(path_buff), SYS_MESH_IFACE_FMT, hard_iface); - res = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS); - if (res != EXIT_SUCCESS) { - fprintf(stderr, "Error - the directory '%s' could not be read: %s\n", - path_buff, strerror(errno)); - fprintf(stderr, "Is the batman-adv module loaded and sysfs mounted ?\n"); - return EXIT_FAILURE; - } - - if (line_ptr[strlen(line_ptr) - 1] == '\n') - line_ptr[strlen(line_ptr) - 1] = '\0'; - - if (strcmp(line_ptr, state->mesh_iface) != 0) { - fprintf(stderr, "Error - interface %s is part of batman network %s, not %s\n", - hard_iface, line_ptr, state->mesh_iface); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; -} - -int check_mesh_iface_ownership(struct state *state, char *hard_iface) -{ - int ret; - - ret = check_mesh_iface_ownership_netlink(state, hard_iface); - if (ret == EXIT_SUCCESS) - return EXIT_SUCCESS; - - ret = check_mesh_iface_ownership_sysfs(state, hard_iface); - if (ret == EXIT_SUCCESS) - return ret; - - return EXIT_FAILURE; -} - static int get_random_bytes_syscall(void *buf __maybe_unused, size_t buflen __maybe_unused) { diff --git a/functions.h b/functions.h index f63c438..923e593 100644 --- a/functions.h +++ b/functions.h @@ -43,9 +43,7 @@ char *ether_ntoa_long(const struct ether_addr *addr); char *get_name_by_macaddr(struct ether_addr *mac_addr, int read_opt); char *get_name_by_macstr(char *mac_str, int read_opt); int file_exists(const char *fpath); -int read_file(const char *dir, const char *path, int read_opt); -int write_file(const char *dir, const char *fname, const char *arg1, - const char *arg2); +int read_file(const char *full_path, int read_opt); struct ether_addr *translate_mac(const char *mesh_iface, const struct ether_addr *mac); struct ether_addr *resolve_mac(const char *asc); @@ -81,7 +79,6 @@ enum { SKIP_HEADER = 0x100, UNICAST_ONLY = 0x200, MULTICAST_ONLY = 0x400, - INVERSE_BOOL = 0x800, }; #endif diff --git a/gw_mode.c b/gw_mode.c index 72b5fbf..e48b78d 100644 --- a/gw_mode.c +++ b/gw_mode.c @@ -254,105 +254,31 @@ static int set_gw(struct state *state) NULL); } -static int gw_read_setting(struct state *state, const char *path_buff) +static int gw_read_setting(struct state *state) { - enum batadv_gw_modes gw_mode; int res; res = get_gw(state); - if (res < 0 && res != -EOPNOTSUPP) + if (res < 0) return EXIT_FAILURE; - if (res >= 0) - return EXIT_SUCCESS; - - /* fallback to sysfs */ - res = read_file(path_buff, SYS_GW_MODE, USE_READ_BUFF); - if (res != EXIT_SUCCESS) - goto out; - - if (line_ptr[strlen(line_ptr) - 1] == '\n') - line_ptr[strlen(line_ptr) - 1] = '\0'; - - if (strcmp(line_ptr, "client") == 0) - gw_mode = BATADV_GW_MODE_CLIENT; - else if (strcmp(line_ptr, "server") == 0) - gw_mode = BATADV_GW_MODE_SERVER; else - gw_mode = BATADV_GW_MODE_OFF; - - free(line_ptr); - line_ptr = NULL; - - switch (gw_mode) { - case BATADV_GW_MODE_CLIENT: - res = read_file(path_buff, SYS_GW_SEL, USE_READ_BUFF); - break; - case BATADV_GW_MODE_SERVER: - res = read_file(path_buff, SYS_GW_BW, USE_READ_BUFF); - break; - default: - printf("off\n"); - goto out; - } - - if (res != EXIT_SUCCESS) - goto out; - - if (line_ptr[strlen(line_ptr) - 1] == '\n') - line_ptr[strlen(line_ptr) - 1] = '\0'; - - switch (gw_mode) { - case BATADV_GW_MODE_CLIENT: - printf("client (selection class: %s)\n", line_ptr); - break; - case BATADV_GW_MODE_SERVER: - printf("server (announced bw: %s)\n", line_ptr); - break; - default: - goto out; - } - -out: - free(line_ptr); - line_ptr = NULL; - - return res; + return EXIT_SUCCESS; } -static int gw_write_setting(struct state *state, const char *path_buff, - int argc, char *argv[]) +static int gw_write_setting(struct state *state) { int res = EXIT_FAILURE; res = set_gw(state); - if (res < 0 && res != -EOPNOTSUPP) + if (res < 0) return EXIT_FAILURE; - if (res >= 0) + else return EXIT_SUCCESS; - - /* sysfs fallback */ - res = write_file(path_buff, SYS_GW_MODE, argv[1], NULL); - if (res != EXIT_SUCCESS) - return res; - - if (argc > 2) { - switch (gw_globals.mode) { - case BATADV_GW_MODE_CLIENT: - res = write_file(path_buff, SYS_GW_SEL, argv[2], NULL); - break; - case BATADV_GW_MODE_SERVER: - res = write_file(path_buff, SYS_GW_BW, argv[2], NULL); - break; - } - } - - return res; } static int gw_mode(struct state *state, int argc, char **argv) { int optchar, res = EXIT_FAILURE; - char *path_buff; while ((optchar = getopt(argc, argv, "h")) != -1) { switch (optchar) { @@ -365,31 +291,16 @@ static int gw_mode(struct state *state, int argc, char **argv) } } - path_buff = malloc(PATH_BUFF_LEN); - if (!path_buff) { - fprintf(stderr, "Error - could not allocate path buffer: out of memory ?\n"); - return EXIT_FAILURE; - } - - snprintf(path_buff, PATH_BUFF_LEN, SYS_BATIF_PATH_FMT, state->mesh_iface); - - if (argc == 1) { - res = gw_read_setting(state, path_buff); - goto out; - } + if (argc == 1) + return gw_read_setting(state); check_root_or_die("batctl gw_mode"); res = parse_gw(state, argc, argv); - if (res < 0) { - res = EXIT_FAILURE; - goto out; - } + if (res < 0) + return EXIT_FAILURE; - res = gw_write_setting(state, path_buff, argc, argv); -out: - free(path_buff); - return res; + return gw_write_setting(state); } COMMAND(SUBCOMMAND_MIF, gw_mode, "gw", diff --git a/hop_penalty.c b/hop_penalty.c index 5f1aabc..7430381 100644 --- a/hop_penalty.c +++ b/hop_penalty.c @@ -122,7 +122,6 @@ static int set_hop_penalty_if(struct state *state) } static struct settings_data batctl_settings_hop_penalty = { - .sysfs_name = "hop_penalty", .data = &hop_penalty, .parse = parse_hop_penalty, .netlink_get = get_hop_penalty, @@ -130,7 +129,6 @@ static struct settings_data batctl_settings_hop_penalty = { }; static struct settings_data batctl_settings_hop_penalty_if = { - .sysfs_name = NULL, .data = &hop_penalty, .parse = parse_hop_penalty, .netlink_get = get_hop_penalty_if, diff --git a/interface.c b/interface.c index 34381de..8775c59 100644 --- a/interface.c +++ b/interface.c @@ -138,7 +138,6 @@ static int print_interfaces_rtnl_parse(struct nl_msg *msg, void *arg) struct print_interfaces_rtnl_arg *print_arg = arg; char iface_status[IFACE_STATUS_LEN]; struct nlattr *attrs[IFLA_MAX + 1]; - char path_buff[PATH_BUFF_LEN]; struct ifinfomsg *ifm; char *ifname; int ret; @@ -165,21 +164,11 @@ static int print_interfaces_rtnl_parse(struct nl_msg *msg, void *arg) goto err; status = get_iface_status_netlink(master, ifm->ifi_index, iface_status); - if (!status) { - snprintf(path_buff, sizeof(path_buff), SYS_IFACE_STATUS_FMT, - ifname); - ret = read_file("", path_buff, USE_READ_BUFF | SILENCE_ERRORS); - if (ret != EXIT_SUCCESS) - status = "\n"; - else - status = line_ptr; - } + if (!status) + status = "\n"; printf("%s: %s", ifname, status); - free(line_ptr); - line_ptr = NULL; - err: return NL_OK; } diff --git a/isolation_mark.c b/isolation_mark.c index 0865de3..b0048f3 100644 --- a/isolation_mark.c +++ b/isolation_mark.c @@ -127,7 +127,6 @@ static int set_isolation_mark(struct state *state) } static struct settings_data batctl_settings_isolation_mark = { - .sysfs_name = "isolation_mark", .data = &isolation_mark, .parse = parse_isolation_mark, .netlink_get = get_isolation_mark, diff --git a/loglevel.c b/loglevel.c index 1e4b690..af7349d 100644 --- a/loglevel.c +++ b/loglevel.c @@ -86,44 +86,31 @@ static int set_log_level(struct state *state) set_attrs_log_level, NULL); } -static int log_level_read_setting(struct state *state, const char *path_buff) +static int log_level_read_setting(struct state *state) { int res; res = get_log_level(state); - if (res < 0 && res != -EOPNOTSUPP) + if (res < 0) return EXIT_FAILURE; - if (res >= 0) + else return EXIT_SUCCESS; - - res = read_file(path_buff, SYS_LOG_LEVEL, USE_READ_BUFF); - if (res != EXIT_SUCCESS) - return res; - - log_level_globals.log_level = strtol(line_ptr, (char **) NULL, 10); - - return res; } -static int log_level_write_setting(struct state *state, const char *path_buff) +static int log_level_write_setting(struct state *state) { int res; - char str[4]; res = set_log_level(state); - if (res < 0 && res != -EOPNOTSUPP) + if (res < 0) return EXIT_FAILURE; - if (res >= 0) + else return EXIT_SUCCESS; - - snprintf(str, sizeof(str), "%i", log_level_globals.log_level); - return write_file(path_buff, SYS_LOG_LEVEL, str, NULL); } static int loglevel(struct state *state, int argc, char **argv) { int optchar, res = EXIT_FAILURE; - char *path_buff; int i; log_level_globals.log_level = 0; @@ -139,14 +126,6 @@ static int loglevel(struct state *state, int argc, char **argv) } } - path_buff = malloc(PATH_BUFF_LEN); - if (!path_buff) { - fprintf(stderr, "Error - could not allocate path buffer: out of memory ?\n"); - return EXIT_FAILURE; - } - - snprintf(path_buff, PATH_BUFF_LEN, SYS_BATIF_PATH_FMT, state->mesh_iface); - if (argc != 1) { check_root_or_die("batctl loglevel"); @@ -175,17 +154,16 @@ static int loglevel(struct state *state, int argc, char **argv) log_level_globals.log_level |= BIT(7); else { log_level_usage(); - goto out; + return EXIT_FAILURE; } } - log_level_write_setting(state, path_buff); - goto out; + return log_level_write_setting(state); } - res = log_level_read_setting(state, path_buff); + res = log_level_read_setting(state); if (res != EXIT_SUCCESS) - goto out; + return res; printf("[%c] %s (%s)\n", (!log_level_globals.log_level) ? 'x' : ' ', "all debug output disabled", "none"); @@ -207,8 +185,6 @@ static int loglevel(struct state *state, int argc, char **argv) printf("[%c] %s (%s)\n", (log_level_globals.log_level & BIT(7)) ? 'x' : ' ', "messages related to throughput meter", "tp"); -out: - free(path_buff); return res; } diff --git a/main.c b/main.c index 6ea589a..54be6fb 100644 --- a/main.c +++ b/main.c @@ -123,7 +123,7 @@ static void version(void) printf("batctl %s [batman-adv: ", SOURCE_VERSION); - ret = read_file("", module_ver_path, USE_READ_BUFF | SILENCE_ERRORS); + ret = read_file(module_ver_path, USE_READ_BUFF | SILENCE_ERRORS); if ((line_ptr) && (line_ptr[strlen(line_ptr) - 1] == '\n')) line_ptr[strlen(line_ptr) - 1] = '\0'; @@ -397,10 +397,7 @@ int main(int argc, char **argv) if (cmd->flags & COMMAND_FLAG_NETLINK) { ret = netlink_create(&state); - if (ret < 0 && ret != -EOPNOTSUPP) { - /* TODO handle -EOPNOTSUPP as error when fallbacks were - * removed - */ + if (ret < 0) { fprintf(stderr, "Error - failed to connect to batadv\n"); exit(EXIT_FAILURE); diff --git a/multicast_fanout.c b/multicast_fanout.c index dcc4fc6..b43ff10 100644 --- a/multicast_fanout.c +++ b/multicast_fanout.c @@ -88,7 +88,6 @@ static int set_multicast_fanout(struct state *state) } static struct settings_data batctl_settings_multicast_fanout = { - .sysfs_name = NULL, .data = &multicast_fanout, .parse = parse_multicast_fanout, .netlink_get = get_multicast_fanout, diff --git a/multicast_forceflood.c b/multicast_forceflood.c index a33149f..e02ab9d 100644 --- a/multicast_forceflood.c +++ b/multicast_forceflood.c @@ -68,7 +68,6 @@ static int set_multicast_forceflood(struct state *state) } static struct settings_data batctl_settings_multicast_forceflood = { - .sysfs_name = "multicast_mode", .data = &multicast_forceflood, .parse = parse_simple_boolean, .netlink_get = get_multicast_forceflood, diff --git a/multicast_mode.c b/multicast_mode.c index a46cfc9..f837998 100644 --- a/multicast_mode.c +++ b/multicast_mode.c @@ -68,7 +68,6 @@ static int set_multicast_mode(struct state *state) } static struct settings_data batctl_settings_multicast_mode = { - .sysfs_name = "multicast_mode", .data = &multicast_mode, .parse = parse_simple_boolean, .netlink_get = get_multicast_mode, diff --git a/network_coding.c b/network_coding.c index a733c6b..9868e54 100644 --- a/network_coding.c +++ b/network_coding.c @@ -48,7 +48,6 @@ static int set_network_coding(struct state *state) } static struct settings_data batctl_settings_network_coding = { - .sysfs_name = "network_coding", .data = &network_coding, .parse = parse_simple_boolean, .netlink_get = get_network_coding, diff --git a/orig_interval.c b/orig_interval.c index 25e67e5..2cdad1a 100644 --- a/orig_interval.c +++ b/orig_interval.c @@ -88,7 +88,6 @@ static int set_orig_interval(struct state *state) } static struct settings_data batctl_settings_orig_interval = { - .sysfs_name = "orig_interval", .data = &orig_interval, .parse = parse_orig_interval, .netlink_get = get_orig_interval, diff --git a/routing_algo.c b/routing_algo.c index 0f2e1bb..27458ff 100644 --- a/routing_algo.c +++ b/routing_algo.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -15,6 +16,9 @@ #include #include #include +#include +#include +#include #include "batadv_packet.h" #include "batman_adv.h" @@ -73,7 +77,7 @@ static int routing_algos_callback(struct nl_msg *msg, void *arg __maybe_unused) return NL_OK; } -static int netlink_print_routing_algos(void) +static int print_routing_algos(void) { struct nl_sock *sock; struct nl_msg *msg; @@ -132,12 +136,28 @@ static int netlink_print_routing_algos(void) return last_err; } -static int print_routing_algos(void) +static int write_default_ra(const char *full_path, const char *arg1) { - int err; + ssize_t write_len; + int fd = -1; - err = netlink_print_routing_algos(); - return err; + fd = open(full_path, O_WRONLY); + if (fd < 0) { + fprintf(stderr, "Error - can't open file '%s': %s\n", full_path, + strerror(errno)); + + return EXIT_FAILURE; + } + + write_len = write(fd, arg1, strlen(arg1) + 1); + close(fd); + if (write_len < 0) { + fprintf(stderr, "Error - can't write to file '%s': %s\n", + full_path, strerror(errno)); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; } static struct nla_policy link_policy[IFLA_MAX + 1] = { @@ -278,14 +298,12 @@ static int routing_algo(struct state *state __maybe_unused, int argc, char **arg check_root_or_die("batctl routing_algo"); - if (argc == 2) { - res = write_file(SYS_SELECTED_RA_PATH, "", argv[1], NULL); - return EXIT_FAILURE; - } + if (argc == 2) + return write_default_ra(SYS_SELECTED_RA_PATH, argv[1]); print_ra_interfaces(); - res = read_file("", SYS_SELECTED_RA_PATH, USE_READ_BUFF); + res = read_file(SYS_SELECTED_RA_PATH, USE_READ_BUFF); if (res != EXIT_SUCCESS) return EXIT_FAILURE; diff --git a/sys.c b/sys.c index 7668a72..cde5002 100644 --- a/sys.c +++ b/sys.c @@ -189,63 +189,35 @@ static void settings_usage(struct state *state) fprintf(stderr, " \t -h print this help\n"); } -static int sys_read_setting(struct state *state, const char *path_buff, - const char *sysfs_name) +static int sys_read_setting(struct state *state) { struct settings_data *settings = state->cmd->arg; int res = EXIT_FAILURE; - int read_opt = NO_FLAGS; if (settings->netlink_get) { res = settings->netlink_get(state); - if (res < 0 && res != -EOPNOTSUPP) + if (res < 0) return EXIT_FAILURE; - if (res >= 0) + else return EXIT_SUCCESS; } - if (sysfs_name) { - if (state->cmd->flags & COMMAND_FLAG_INVERSE) - read_opt |= INVERSE_BOOL; - - res = read_file(path_buff, sysfs_name, read_opt); - } - return res; } -static int sys_write_setting(struct state *state, const char *path_buff, - const char *sysfs_name, int argc, char **argv) +static int sys_write_setting(struct state *state) { struct settings_data *settings = state->cmd->arg; int res = EXIT_FAILURE; - char *argv1 = argv[1]; if (settings->netlink_set) { res = settings->netlink_set(state); - if (res < 0 && res != -EOPNOTSUPP) + if (res < 0) return EXIT_FAILURE; - if (res >= 0) + else return EXIT_SUCCESS; } - if (sysfs_name) { - if (state->cmd->flags & COMMAND_FLAG_INVERSE) { - if (!strncmp("0", argv[1], strlen("0")) || - !strncmp("disable", argv[1], strlen("disable")) || - !strncmp("disabled", argv[1], strlen("disabled"))) { - argv1 = "enabled"; - } else if (!strncmp("1", argv[1], strlen("1")) || - !strncmp("enable", argv[1], strlen("enable")) || - !strncmp("enabled", argv[1], strlen("enabled"))) { - argv1 = "disabled"; - } - } - - res = write_file(path_buff, sysfs_name, - argv1, argc > 2 ? argv[2] : NULL); - } - return res; } @@ -253,7 +225,6 @@ int handle_sys_setting(struct state *state, int argc, char **argv) { struct settings_data *settings = state->cmd->arg; int optchar, res = EXIT_FAILURE; - char *path_buff; while ((optchar = getopt(argc, argv, "h")) != -1) { switch (optchar) { @@ -266,55 +237,16 @@ int handle_sys_setting(struct state *state, int argc, char **argv) } } - /* prepare the classic path */ - path_buff = malloc(PATH_BUFF_LEN); - if (!path_buff) { - fprintf(stderr, "Error - could not allocate path buffer: out of memory ?\n"); - return EXIT_FAILURE; - } - - switch (state->selector) { - case SP_NONE_OR_MESHIF: - case SP_MESHIF: - snprintf(path_buff, PATH_BUFF_LEN, SYS_BATIF_PATH_FMT, - state->mesh_iface); - break; - case SP_VLAN: - /* if the specified interface is a VLAN then change the path to - * point to the proper "vlan%{vid}" subfolder in the sysfs tree. - */ - snprintf(path_buff, PATH_BUFF_LEN, SYS_VLAN_PATH, - state->mesh_iface, state->vid); - break; - case SP_HARDIF: - /* if a hard interface was specified then change the path to - * point to the proper ${hardif}/batman-adv path in the sysfs - * tree. - */ - snprintf(path_buff, PATH_BUFF_LEN, SYS_HARDIF_PATH, - state->hard_iface); - break; - } - - if (argc == 1) { - res = sys_read_setting(state, path_buff, settings->sysfs_name); - goto out; - } + if (argc == 1) + return sys_read_setting(state); check_root_or_die("batctl"); if (settings->parse) { res = settings->parse(state, argc, argv); - if (res < 0) { - res = EXIT_FAILURE; - goto out; - } + if (res < 0) + return EXIT_FAILURE; } - res = sys_write_setting(state, path_buff, settings->sysfs_name, argc, - argv); - -out: - free(path_buff); - return res; + return sys_write_setting(state); } diff --git a/sys.h b/sys.h index b4e4bca..ac7781f 100644 --- a/sys.h +++ b/sys.h @@ -18,18 +18,9 @@ #include "batman_adv.h" #include "netlink.h" -#define SYS_BATIF_PATH_FMT "/sys/class/net/%s/mesh/" -#define SYS_IFACE_PATH "/sys/class/net" -#define SYS_IFACE_DIR SYS_IFACE_PATH"/%s/" -#define SYS_HARDIF_PATH SYS_IFACE_DIR "batman_adv/" -#define SYS_MESH_IFACE_FMT SYS_HARDIF_PATH "mesh_iface" -#define SYS_IFACE_STATUS_FMT SYS_HARDIF_PATH "iface_status" -#define SYS_VLAN_PATH SYS_IFACE_PATH"/%s/mesh/vlan%d/" -#define SYS_ROUTING_ALGO_FMT SYS_IFACE_PATH"/%s/mesh/routing_algo" #define VLAN_ID_MAX_LEN 4 struct settings_data { - const char *sysfs_name; void *data; int (*parse)(struct state *state, int argc, char *argv[]); int (*netlink_get)(struct state *state); diff --git a/throughput_override.c b/throughput_override.c index 13b5dce..f42d9de 100644 --- a/throughput_override.c +++ b/throughput_override.c @@ -101,7 +101,6 @@ static int set_throughput_override(struct state *state) } static struct settings_data batctl_settings_throughput_override = { - .sysfs_name = "throughput_override", .data = &throughput_override, .parse = parse_throughput_override, .netlink_get = get_throughput_override,