From patchwork Thu Oct 25 16:22:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 17557 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 D8DC183112; Thu, 25 Oct 2018 18:23:03 +0200 (CEST) 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="O7AQTgQ1"; 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 EC0BB830CA for ; Thu, 25 Oct 2018 18:22:58 +0200 (CEST) Received: from sven-desktop.home.narfation.org (p200300C593D355EE7FED32DEE9F3DCBB.dip0.t-ipconnect.de [IPv6:2003:c5:93d3:55ee:7fed:32de:e9f3:dcbb]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id 46EE71100DC; Thu, 25 Oct 2018 18:22:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1540484578; bh=1iOftr2tDN0tKa/6MTVoKS0a4L3+U06WDFk4kY5MsHY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O7AQTgQ1rTX+ZtB0XO56bhRqU3JYxrtXsxqCVSRx2XbOtPZnTY7P33LFzlbho5UoX qtQueNmo4AhvP4dc8VQ+XzK4JiZOKvWzBskWrTIX9zAm3SZvJTOT4/XU0DNcgqukYL UmZH5YzeHNj71tm9qm59mFaetI24wj3AFPPTp0yg= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Thu, 25 Oct 2018 18:22:07 +0200 Message-Id: <20181025162245.19389-5-sven@narfation.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181025162245.19389-1-sven@narfation.org> References: <20181025162245.19389-1-sven@narfation.org> Subject: [B.A.T.M.A.N.] [PATCH v3 04/42] batctl: Move loglevel command to separate file 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" More complex commands in batctl are stored in separate files which are called like the actual command name. This makes it easier to group functionality and detect which parts belong to a more complex construct. Signed-off-by: Sven Eckelmann --- Makefile | 1 + loglevel.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ loglevel.h | 28 ++++++++++++ main.c | 3 +- sys.c | 114 ------------------------------------------------ sys.h | 1 - 6 files changed, 175 insertions(+), 116 deletions(-) create mode 100644 loglevel.c create mode 100644 loglevel.h diff --git a/Makefile b/Makefile index 6f620be..1271200 100755 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ OBJ += genl.o OBJ += hash.o OBJ += icmp_helper.o OBJ += interface.o +OBJ += loglevel.o OBJ += main.o OBJ += netlink.o OBJ += ping.o diff --git a/loglevel.c b/loglevel.c new file mode 100644 index 0000000..584cb37 --- /dev/null +++ b/loglevel.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2009-2018 B.A.T.M.A.N. contributors: + * + * Marek Lindner + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA + * + * License-Filename: LICENSES/preferred/GPL-2.0 + */ + +#include +#include +#include +#include + +#include "functions.h" +#include "main.h" +#include "sys.h" + +static void log_level_usage(void) +{ + fprintf(stderr, "Usage: batctl [options] loglevel [parameters] [level[ level[ level]]...]\n"); + fprintf(stderr, "parameters:\n"); + fprintf(stderr, " \t -h print this help\n"); + fprintf(stderr, "levels:\n"); + fprintf(stderr, " \t none Debug logging is disabled\n"); + fprintf(stderr, " \t all Print messages from all below\n"); + fprintf(stderr, " \t batman Messages related to routing / flooding / broadcasting\n"); + fprintf(stderr, " \t routes Messages related to route added / changed / deleted\n"); + fprintf(stderr, " \t tt Messages related to translation table operations\n"); + fprintf(stderr, " \t bla Messages related to bridge loop avoidance\n"); + fprintf(stderr, " \t dat Messages related to arp snooping and distributed arp table\n"); + fprintf(stderr, " \t nc Messages related to network coding\n"); + fprintf(stderr, " \t mcast Messages related to multicast\n"); + fprintf(stderr, " \t tp Messages related to throughput meter\n"); +} + +int loglevel(char *mesh_iface, int argc, char **argv) +{ + int optchar, res = EXIT_FAILURE; + int log_level = 0; + char *path_buff; + char str[4]; + int i; + + while ((optchar = getopt(argc, argv, "h")) != -1) { + switch (optchar) { + case 'h': + log_level_usage(); + return EXIT_SUCCESS; + default: + log_level_usage(); + return EXIT_FAILURE; + } + } + + 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, mesh_iface); + + if (argc != 1) { + check_root_or_die("batctl loglevel"); + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "none") == 0) { + log_level = 0; + break; + } else if (strcmp(argv[i], "all") == 0) { + log_level = 255; + break; + } else if (strcmp(argv[i], "batman") == 0) + log_level |= BIT(0); + else if (strcmp(argv[i], "routes") == 0) + log_level |= BIT(1); + else if (strcmp(argv[i], "tt") == 0) + log_level |= BIT(2); + else if (strcmp(argv[i], "bla") == 0) + log_level |= BIT(3); + else if (strcmp(argv[i], "dat") == 0) + log_level |= BIT(4); + else if (strcmp(argv[i], "nc") == 0) + log_level |= BIT(5); + else if (strcmp(argv[i], "mcast") == 0) + log_level |= BIT(6); + else if (strcmp(argv[i], "tp") == 0) + log_level |= BIT(7); + else { + log_level_usage(); + goto out; + } + } + + snprintf(str, sizeof(str), "%i", log_level); + res = write_file(path_buff, SYS_LOG_LEVEL, str, NULL); + goto out; + } + + res = read_file(path_buff, SYS_LOG_LEVEL, USE_READ_BUFF, 0, 0, 0); + + if (res != EXIT_SUCCESS) + goto out; + + log_level = strtol(line_ptr, (char **) NULL, 10); + + printf("[%c] %s (%s)\n", (!log_level) ? 'x' : ' ', + "all debug output disabled", "none"); + printf("[%c] %s (%s)\n", (log_level & BIT(0)) ? 'x' : ' ', + "messages related to routing / flooding / broadcasting", + "batman"); + printf("[%c] %s (%s)\n", (log_level & BIT(1)) ? 'x' : ' ', + "messages related to route added / changed / deleted", "routes"); + printf("[%c] %s (%s)\n", (log_level & BIT(2)) ? 'x' : ' ', + "messages related to translation table operations", "tt"); + printf("[%c] %s (%s)\n", (log_level & BIT(3)) ? 'x' : ' ', + "messages related to bridge loop avoidance", "bla"); + printf("[%c] %s (%s)\n", (log_level & BIT(4)) ? 'x' : ' ', + "messages related to arp snooping and distributed arp table", "dat"); + printf("[%c] %s (%s)\n", (log_level & BIT(5)) ? 'x' : ' ', + "messages related to network coding", "nc"); + printf("[%c] %s (%s)\n", (log_level & BIT(6)) ? 'x' : ' ', + "messages related to multicast", "mcast"); + printf("[%c] %s (%s)\n", (log_level & BIT(7)) ? 'x' : ' ', + "messages related to throughput meter", "tp"); + +out: + free(path_buff); + return res; +} diff --git a/loglevel.h b/loglevel.h new file mode 100644 index 0000000..2644f46 --- /dev/null +++ b/loglevel.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2009-2018 B.A.T.M.A.N. contributors: + * + * Marek Lindner + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA + * + * License-Filename: LICENSES/preferred/GPL-2.0 + */ + +#ifndef _BATCTL_LOGLEVEL_H +#define _BATCTL_LOGLEVEL_H + +int loglevel(char *mesh_iface, int argc, char **argv); + +#endif diff --git a/main.c b/main.c index 3a6011b..ac4bc47 100644 --- a/main.c +++ b/main.c @@ -38,6 +38,7 @@ #include "tp_meter.h" #include "bisect_iv.h" #include "statistics.h" +#include "loglevel.h" #include "functions.h" char mesh_dfl_iface[] = "bat0"; @@ -169,7 +170,7 @@ int main(int argc, char **argv) } else if ((strcmp(argv[1], "loglevel") == 0) || (strcmp(argv[1], "ll") == 0)) { - ret = handle_loglevel(mesh_iface, argc - 1, argv + 1); + ret = loglevel(mesh_iface, argc - 1, argv + 1); } else if ((strcmp(argv[1], "log") == 0) || (strcmp(argv[1], "l") == 0)) { diff --git a/sys.c b/sys.c index e0c2073..366f7a2 100644 --- a/sys.c +++ b/sys.c @@ -117,120 +117,6 @@ const struct settings_data batctl_settings[BATCTL_SETTINGS_NUM] = { }, }; -static void log_level_usage(void) -{ - fprintf(stderr, "Usage: batctl [options] loglevel [parameters] [level[ level[ level]]...]\n"); - fprintf(stderr, "parameters:\n"); - fprintf(stderr, " \t -h print this help\n"); - fprintf(stderr, "levels:\n"); - fprintf(stderr, " \t none Debug logging is disabled\n"); - fprintf(stderr, " \t all Print messages from all below\n"); - fprintf(stderr, " \t batman Messages related to routing / flooding / broadcasting\n"); - fprintf(stderr, " \t routes Messages related to route added / changed / deleted\n"); - fprintf(stderr, " \t tt Messages related to translation table operations\n"); - fprintf(stderr, " \t bla Messages related to bridge loop avoidance\n"); - fprintf(stderr, " \t dat Messages related to arp snooping and distributed arp table\n"); - fprintf(stderr, " \t nc Messages related to network coding\n"); - fprintf(stderr, " \t mcast Messages related to multicast\n"); - fprintf(stderr, " \t tp Messages related to throughput meter\n"); -} - -int handle_loglevel(char *mesh_iface, int argc, char **argv) -{ - int optchar, res = EXIT_FAILURE; - int log_level = 0; - char *path_buff; - char str[4]; - int i; - - while ((optchar = getopt(argc, argv, "h")) != -1) { - switch (optchar) { - case 'h': - log_level_usage(); - return EXIT_SUCCESS; - default: - log_level_usage(); - return EXIT_FAILURE; - } - } - - 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, mesh_iface); - - if (argc != 1) { - check_root_or_die("batctl loglevel"); - - for (i = 1; i < argc; i++) { - if (strcmp(argv[i], "none") == 0) { - log_level = 0; - break; - } else if (strcmp(argv[i], "all") == 0) { - log_level = 255; - break; - } else if (strcmp(argv[i], "batman") == 0) - log_level |= BIT(0); - else if (strcmp(argv[i], "routes") == 0) - log_level |= BIT(1); - else if (strcmp(argv[i], "tt") == 0) - log_level |= BIT(2); - else if (strcmp(argv[i], "bla") == 0) - log_level |= BIT(3); - else if (strcmp(argv[i], "dat") == 0) - log_level |= BIT(4); - else if (strcmp(argv[i], "nc") == 0) - log_level |= BIT(5); - else if (strcmp(argv[i], "mcast") == 0) - log_level |= BIT(6); - else if (strcmp(argv[i], "tp") == 0) - log_level |= BIT(7); - else { - log_level_usage(); - goto out; - } - } - - snprintf(str, sizeof(str), "%i", log_level); - res = write_file(path_buff, SYS_LOG_LEVEL, str, NULL); - goto out; - } - - res = read_file(path_buff, SYS_LOG_LEVEL, USE_READ_BUFF, 0, 0, 0); - - if (res != EXIT_SUCCESS) - goto out; - - log_level = strtol(line_ptr, (char **) NULL, 10); - - printf("[%c] %s (%s)\n", (!log_level) ? 'x' : ' ', - "all debug output disabled", "none"); - printf("[%c] %s (%s)\n", (log_level & BIT(0)) ? 'x' : ' ', - "messages related to routing / flooding / broadcasting", - "batman"); - printf("[%c] %s (%s)\n", (log_level & BIT(1)) ? 'x' : ' ', - "messages related to route added / changed / deleted", "routes"); - printf("[%c] %s (%s)\n", (log_level & BIT(2)) ? 'x' : ' ', - "messages related to translation table operations", "tt"); - printf("[%c] %s (%s)\n", (log_level & BIT(3)) ? 'x' : ' ', - "messages related to bridge loop avoidance", "bla"); - printf("[%c] %s (%s)\n", (log_level & BIT(4)) ? 'x' : ' ', - "messages related to arp snooping and distributed arp table", "dat"); - printf("[%c] %s (%s)\n", (log_level & BIT(5)) ? 'x' : ' ', - "messages related to network coding", "nc"); - printf("[%c] %s (%s)\n", (log_level & BIT(6)) ? 'x' : ' ', - "messages related to multicast", "mcast"); - printf("[%c] %s (%s)\n", (log_level & BIT(7)) ? 'x' : ' ', - "messages related to throughput meter", "tp"); - -out: - free(path_buff); - return res; -} - static void settings_usage(int setting) { fprintf(stderr, "Usage: batctl [options] %s|%s [parameters]", diff --git a/sys.h b/sys.h index 95b2a1e..5e98ab1 100644 --- a/sys.h +++ b/sys.h @@ -71,7 +71,6 @@ extern const char *sysfs_param_enable[]; extern const char *sysfs_param_server[]; extern const struct settings_data batctl_settings[BATCTL_SETTINGS_NUM]; -int handle_loglevel(char *mesh_iface, int argc, char **argv); int handle_sys_setting(char *mesh_iface, int setting, int argc, char **argv); int handle_gw_setting(char *mesh_iface, int argc, char **argv); int handle_ra_setting(int argc, char **argv);