Author: marek Date: 2009-11-14 06:11:37 +0000 (Sat, 14 Nov 2009) New Revision: 1483
Added: trunk/batctl/sys.c trunk/batctl/sys.h Modified: trunk/batctl/Makefile trunk/batctl/functions.c trunk/batctl/functions.h trunk/batctl/main.c trunk/batctl/main.h trunk/batctl/proc.c trunk/batctl/proc.h trunk/batman-adv-kernelland/Makefile.kbuild Log: [batctl adapt batctl to the new logging mechanisms
Modified: trunk/batctl/Makefile =================================================================== --- trunk/batctl/Makefile 2009-11-14 06:11:32 UTC (rev 1482) +++ trunk/batctl/Makefile 2009-11-14 06:11:37 UTC (rev 1483) @@ -26,7 +26,7 @@ endif
CC = gcc -CFLAGS += -pedantic -Wall -W -g3 -std=gnu99 +CFLAGS += -pedantic -Wall -W -g3 -std=gnu99 -Os EXTRA_CFLAGS = -DREVISION_VERSION=$(REVISION_VERSION) LDFLAGS +=
@@ -39,8 +39,8 @@ EXTRA_MODULES_C := bisect.c EXTRA_MODULES_H := bisect.h
-SRC_C = main.c bat-hosts.c functions.c proc.c ping.c traceroute.c tcpdump.c list-batman.c hash.c $(EXTRA_MODULES_C) -SRC_H = main.h bat-hosts.h functions.h proc.h ping.h traceroute.h tcpdump.h list-batman.h hash.h allocate.h $(EXTRA_MODULES_H) +SRC_C = main.c bat-hosts.c functions.c proc.c sys.c ping.c traceroute.c tcpdump.c list-batman.c hash.c $(EXTRA_MODULES_C) +SRC_H = main.h bat-hosts.h functions.h proc.h sys.h ping.h traceroute.h tcpdump.h list-batman.h hash.h allocate.h $(EXTRA_MODULES_H) SRC_O = $(SRC_C:.c=.o)
PACKAGE_NAME = batctl
Modified: trunk/batctl/functions.c =================================================================== --- trunk/batctl/functions.c 2009-11-14 06:11:32 UTC (rev 1482) +++ trunk/batctl/functions.c 2009-11-14 06:11:37 UTC (rev 1483) @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors: * * Andreas Langer a.langer@q-dsl.de, Marek Lindner lindner_marek@yahoo.de @@ -38,6 +38,7 @@
static struct timeval start_time; static char *host_name; +char read_buff[10];
void start_timer(void) { @@ -98,43 +99,74 @@ return get_name_by_macaddr(mac_addr, read_opt); }
-static int check_proc_dir(void) +static int check_proc_dir(char *dir) { struct stat st;
- if (stat("/proc", &st) != 0) { + if (stat("/proc/", &st) != 0) { printf("Error - the folder '/proc' was not found on the system\n"); printf("Please make sure that the proc filesystem is properly mounted\n"); return EXIT_FAILURE; }
- if (stat(PROC_ROOT_PATH, &st) == 0) - return EXIT_SUCCESS; + if (stat(dir, &st) == 0) + return EXIT_SUCCESS;
- printf("Error - the folder '%s' was not found within the proc filesystem\n", PROC_ROOT_PATH); + printf("Error - the folder '%s' was not found within the proc filesystem\n", dir); printf("Please make sure that the batman-adv kernel module is loaded\n"); return EXIT_FAILURE; }
-int read_proc_file(char *path, int read_opt) +static int check_sys_dir(char *dir) { + struct stat st; + + if (stat("/sys/", &st) != 0) { + printf("Error - the folder '/sys/' was not found on the system\n"); + printf("Please make sure that the sys filesystem is properly mounted\n"); + return EXIT_FAILURE; + } + + if (stat(dir, &st) == 0) + return EXIT_SUCCESS; + + printf("Error - the folder '%s' was not found within the sys filesystem\n", dir); + printf("Please make sure that the batman-adv kernel module is loaded\n"); + return EXIT_FAILURE; +} + +int read_file(char *dir, char *fname, int read_opt) +{ struct ether_addr *mac_addr; struct bat_host *bat_host; int fd = 0, res = EXIT_FAILURE, fd_opts; - unsigned int bytes_written; - char full_path[500], buff[1500], *buff_ptr, *cr_ptr, *space_ptr, extra_char; - ssize_t read_len; + unsigned int bytes_written, read_len; + char full_path[500], *read_ptr, lbuff[1500], *buff_ptr, *cr_ptr, *space_ptr, extra_char; + ssize_t data_read_len;
if (read_opt & USE_BAT_HOSTS) bat_hosts_init();
- if (check_proc_dir() != EXIT_SUCCESS) - goto out; + if (strstr(dir, "/proc/")) { + if (check_proc_dir(dir) != EXIT_SUCCESS) + goto out; + } else if (strstr(dir, "/sys/")) { + if (check_sys_dir(dir) != EXIT_SUCCESS) + goto out; + }
- strncpy(full_path, PROC_ROOT_PATH, strlen(PROC_ROOT_PATH)); - full_path[strlen(PROC_ROOT_PATH)] = '\0'; - strncat(full_path, path, sizeof(full_path) - strlen(full_path)); + strncpy(full_path, dir, strlen(dir)); + full_path[strlen(dir)] = '\0'; + strncat(full_path, fname, sizeof(full_path) - strlen(full_path));
+ if (read_opt & USE_READ_BUFF) { + read_ptr = read_buff; + read_len = sizeof(read_buff); + } else { + read_ptr = lbuff; + read_len = sizeof(lbuff); + } + open: fd = open(full_path, O_RDONLY);
@@ -152,9 +184,9 @@
read: while (1) { - read_len = read(fd, buff, sizeof(buff)); + data_read_len = read(fd, read_ptr, read_len);
- if (read_len < 0) { + if (data_read_len < 0) { /* file was empty */ if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break; @@ -163,18 +195,21 @@ goto out; }
- if (read_len == 0) + if (data_read_len == 0) break;
- buff[read_len] = '\0'; + read_ptr[data_read_len] = '\0';
+ if (read_opt & USE_READ_BUFF) + break; + if (!(read_opt & USE_BAT_HOSTS)) { - printf("%s", buff); + printf("%s", read_ptr); goto check_eof; }
/* replace mac addresses with bat host names */ - buff_ptr = buff; + buff_ptr = read_ptr; bytes_written = 0;
while ((cr_ptr = strchr(buff_ptr, '\n')) != NULL) { @@ -230,11 +265,11 @@
}
- if (bytes_written != (size_t)read_len) + if (bytes_written != (size_t)data_read_len) printf("%s", buff_ptr);
check_eof: - if (sizeof(buff) != (size_t)read_len) + if (read_len != (size_t)data_read_len) break; }
@@ -262,18 +297,23 @@ return res; }
-int write_proc_file(char *path, char *value) +int write_file(char *dir, char *fname, char *value) { int fd = 0, res = EXIT_FAILURE; char full_path[500]; ssize_t write_len;
- if (check_proc_dir() != EXIT_SUCCESS) - goto out; + if (strstr(dir, "/proc/")) { + if (check_proc_dir(dir) != EXIT_SUCCESS) + goto out; + } else if (strstr(dir, "/sys/")) { + if (check_sys_dir(dir) != EXIT_SUCCESS) + goto out; + }
- strncpy(full_path, PROC_ROOT_PATH, strlen(PROC_ROOT_PATH)); - full_path[strlen(PROC_ROOT_PATH)] = '\0'; - strncat(full_path, path, sizeof(full_path) - strlen(full_path)); + strncpy(full_path, dir, strlen(dir)); + full_path[strlen(dir)] = '\0'; + strncat(full_path, fname, sizeof(full_path) - strlen(full_path));
fd = open(full_path, O_WRONLY);
Modified: trunk/batctl/functions.h =================================================================== --- trunk/batctl/functions.h 2009-11-14 06:11:32 UTC (rev 1482) +++ trunk/batctl/functions.h 2009-11-14 06:11:37 UTC (rev 1483) @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors: * * Andreas Langer a.langer@q-dsl.de, Marek Lindner lindner_marek@yahoo.de @@ -32,13 +32,16 @@ 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 read_proc_file(char *path, int read_opt); -int write_proc_file(char *path, char *value); +int read_file(char *dir, char *path, int read_opt); +int write_file(char *dir, char *path, char *value);
+extern char read_buff[10]; + enum { SINGLE_READ = 0x00, CONT_READ = 0x01, CLR_CONT_READ = 0x02, USE_BAT_HOSTS = 0x04, LOG_MODE = 0x08, + USE_READ_BUFF = 0x10, };
Modified: trunk/batctl/main.c =================================================================== --- trunk/batctl/main.c 2009-11-14 06:11:32 UTC (rev 1482) +++ trunk/batctl/main.c 2009-11-14 06:11:37 UTC (rev 1483) @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors: * * Andreas Langer a.langer@q-dsl.de, Marek Lindner lindner_marek@yahoo.de @@ -30,6 +30,7 @@
#include "main.h" #include "proc.h" +#include "sys.h" #include "ping.h" #include "traceroute.h" #include "tcpdump.h" @@ -108,7 +109,7 @@
} else if ((strcmp(argv[1], "loglevel") == 0) || (strcmp(argv[1], "ll") == 0)) {
- ret = handle_setting(argc - 1, argv + 1, PROC_LOG_LEVEL, log_level_usage); + ret = handle_loglevel(argc - 1, argv + 1);
} else if ((strcmp(argv[1], "log") == 0) || (strcmp(argv[1], "l") == 0)) {
@@ -116,15 +117,15 @@
} else if ((strcmp(argv[1], "interval") == 0) || (strcmp(argv[1], "it") == 0)) {
- ret = handle_setting(argc - 1, argv + 1, PROC_ORIG_INTERVAL, orig_interval_usage); + ret = handle_proc_setting(argc - 1, argv + 1, PROC_ORIG_INTERVAL, orig_interval_usage);
} else if ((strcmp(argv[1], "visformat") == 0) || (strcmp(argv[1], "vf") == 0)) {
- ret = handle_setting(argc - 1, argv + 1, PROC_VIS_FORMAT, vis_format_usage); + ret = handle_proc_setting(argc - 1, argv + 1, PROC_VIS_FORMAT, vis_format_usage);
} else if ((strcmp(argv[1], "aggregation") == 0) || (strcmp(argv[1], "ag") == 0)) {
- ret = handle_setting(argc - 1, argv + 1, PROC_AGGR, aggregation_usage); + ret = handle_proc_setting(argc - 1, argv + 1, PROC_AGGR, aggregation_usage);
} else if ((strcmp(argv[1], "bisect") == 0)) {
Modified: trunk/batctl/main.h =================================================================== --- trunk/batctl/main.h 2009-11-14 06:11:32 UTC (rev 1482) +++ trunk/batctl/main.h 2009-11-14 06:11:37 UTC (rev 1483) @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors: * * Andreas Langer a.langer@q-dsl.de, Marek Lindner lindner_marek@yahoo.de @@ -24,4 +24,3 @@ #define SOURCE_VERSION "0.2.1-beta" /*put exactly one distinct word inside the string like "0.3-pre-alpha" or "0.3-rc1" or "0.3" */
#define BAT_DEVICE "/dev/batman-adv" -#define PROC_ROOT_PATH "/proc/net/batman-adv/"
Modified: trunk/batctl/proc.c =================================================================== --- trunk/batctl/proc.c 2009-11-14 06:11:32 UTC (rev 1482) +++ trunk/batctl/proc.c 2009-11-14 06:11:37 UTC (rev 1483) @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2009 B.A.T.M.A.N. contributors: * * Marek Lindner lindner_marek@yahoo.de @@ -20,7 +20,6 @@ */
- #include <sys/time.h> #include <stdio.h> #include <stdlib.h> @@ -54,13 +53,13 @@ }
if (argc == 1) - return read_proc_file(PROC_INTERFACES, SINGLE_READ); + return read_file(PROC_ROOT_PATH, PROC_INTERFACES, SINGLE_READ);
for (i = 1; i < argc; i++) { if (strcmp(argv[i], "none") == 0) - res = write_proc_file(PROC_INTERFACES, ""); + res = write_file(PROC_ROOT_PATH, PROC_INTERFACES, ""); else - res = write_proc_file(PROC_INTERFACES, argv[i]); + res = write_file(PROC_ROOT_PATH, PROC_INTERFACES, argv[i]);
if (res != EXIT_SUCCESS) return res; @@ -69,39 +68,6 @@ return EXIT_SUCCESS; }
-static void log_usage(void) -{ - printf("Usage: batctl [options] log \n"); - printf("options:\n"); - printf(" \t -b batch mode - read the log file once and quit\n"); - printf(" \t -h print this help\n"); - printf(" \t -n don't replace mac addresses with bat-host names\n"); -} - -int log_print(int argc, char **argv) -{ - int optchar, read_opt = CONT_READ | USE_BAT_HOSTS | LOG_MODE; - - while ((optchar = getopt(argc, argv, "bhn")) != -1) { - switch (optchar) { - case 'b': - read_opt &= ~CONT_READ; - break; - case 'h': - log_usage(); - return EXIT_SUCCESS; - case 'n': - read_opt &= ~USE_BAT_HOSTS; - break; - default: - log_usage(); - return EXIT_FAILURE; - } - } - - return read_proc_file(PROC_LOG, read_opt); -} - void originators_usage(void) { printf("Usage: batctl [options] originators \n"); @@ -136,13 +102,6 @@ printf(" \t -h print this help\n"); }
-void log_level_usage(void) -{ - printf("Usage: batctl [options] loglevel \n"); - printf("options:\n"); - printf(" \t -h print this help\n"); -} - void vis_format_usage(void) { printf("Usage: batctl [options] visformat \n"); @@ -178,10 +137,10 @@ } }
- return read_proc_file(file_path, read_opt); + return read_file(PROC_ROOT_PATH, file_path, read_opt); }
-int handle_setting(int argc, char **argv, char *file_path, void setting_usage(void)) +int handle_proc_setting(int argc, char **argv, char *file_path, void setting_usage(void)) { int optchar;
@@ -197,7 +156,7 @@ }
if (argc == 1) - return read_proc_file(file_path, SINGLE_READ); + return read_file(PROC_ROOT_PATH, file_path, SINGLE_READ);
- return write_proc_file(file_path, argv[1]); + return write_file(PROC_ROOT_PATH, file_path, argv[1]); }
Modified: trunk/batctl/proc.h =================================================================== --- trunk/batctl/proc.h 2009-11-14 06:11:32 UTC (rev 1482) +++ trunk/batctl/proc.h 2009-11-14 06:11:37 UTC (rev 1483) @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2009 B.A.T.M.A.N. contributors: * * Marek Lindner lindner_marek@yahoo.de @@ -21,11 +21,10 @@
+#define PROC_ROOT_PATH "/proc/net/batman-adv/" #define PROC_INTERFACES "interfaces" #define PROC_ORIGINATORS "originators" #define PROC_ORIG_INTERVAL "orig_interval" -#define PROC_LOG_LEVEL "log_level" -#define PROC_LOG "log" #define PROC_GATEWAYS "gateways" #define PROC_TRANSTABLE_LOCAL "transtable_local" #define PROC_TRANSTABLE_GLOBAL "transtable_global" @@ -35,14 +34,12 @@
int interface(int argc, char **argv); -int log_print(int argc, char **argv);
void originators_usage(void); void trans_local_usage(void); void trans_global_usage(void); void orig_interval_usage(void); -void log_level_usage(void); void vis_format_usage(void); void aggregation_usage(void); int handle_table(int argc, char **argv, char *file_path, void table_usage(void)); -int handle_setting(int argc, char **argv, char *file_path, void setting_usage(void)); +int handle_proc_setting(int argc, char **argv, char *file_path, void setting_usage(void));
Added: trunk/batctl/sys.c =================================================================== --- trunk/batctl/sys.c (rev 0) +++ trunk/batctl/sys.c 2009-11-14 06:11:37 UTC (rev 1483) @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2009 B.A.T.M.A.N. contributors: + * + * Marek Lindner lindner_marek@yahoo.de + * + * 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 + * + */ + + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#include "main.h" +#include "sys.h" +#include "functions.h" + + +static void log_usage(void) +{ + printf("Usage: batctl [options] log [logfile]\n"); + printf("Note: if no logfile was specified stdin is read"); + printf("options:\n"); + printf(" \t -b batch mode - read the log file once and quit\n"); + printf(" \t -h print this help\n"); + printf(" \t -n don't replace mac addresses with bat-host names\n"); +} + +int log_print(int argc, char **argv) +{ + int optchar, read_opt = CONT_READ | USE_BAT_HOSTS | LOG_MODE; + int found_args = 1; + + while ((optchar = getopt(argc, argv, "bhn")) != -1) { + switch (optchar) { + case 'b': + read_opt &= ~CONT_READ; + found_args += 1; + break; + case 'h': + log_usage(); + return EXIT_SUCCESS; + case 'n': + read_opt &= ~USE_BAT_HOSTS; + found_args += 1; + break; + default: + log_usage(); + return EXIT_FAILURE; + } + } + + if (argc > found_args) + return read_file("", argv[found_args], read_opt); + else + return read_file("", "/dev/stdin", read_opt); +} + +static void log_level_usage(void) +{ + printf("Usage: batctl [options] loglevel \n"); + printf("options:\n"); + printf(" \t -h print this help\n"); +} + +int handle_loglevel(int argc, char **argv) +{ + int optchar, res; + + while ((optchar = getopt(argc, argv, "h")) != -1) { + switch (optchar) { + case 'h': + log_level_usage(); + return EXIT_SUCCESS; + default: + log_level_usage(); + return EXIT_FAILURE; + } + } + + if (argc != 1) { + res = write_file(SYS_ROOT_PATH, SYS_LOG_LEVEL, argv[1]); + goto out; + } + + res = read_file(SYS_ROOT_PATH, SYS_LOG_LEVEL, SINGLE_READ | USE_READ_BUFF); + + if (res != EXIT_SUCCESS) + goto out; + + printf("[%c] %s (%d)\n", (read_buff[0] == '0') ? 'x' : ' ', + "all debug output disabled", 0); + printf("[%c] %s (%d)\n", (read_buff[0] == '1') ? 'x' : ' ', + "messages related to routing / flooding / broadcasting", 1); + printf("[%c] %s (%d)\n", (read_buff[0] == '2') ? 'x' : ' ', + "messages related to route or hna added / changed / deleted", 2); + printf("[%c] %s (%d)\n", (read_buff[0] == '3') ? 'x' : ' ', + "all debug messages", 3); + +out: + if (errno == EBADF) + printf("To increase the log level you need to compile the module with debugging enabled (see the README)\n"); + + return res; +}
Copied: trunk/batctl/sys.h (from rev 1482, trunk/batctl/main.h) =================================================================== --- trunk/batctl/sys.h (rev 0) +++ trunk/batctl/sys.h 2009-11-14 06:11:37 UTC (rev 1483) @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2009 B.A.T.M.A.N. contributors: + * + * Marek Lindner lindner_marek@yahoo.de + * + * 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 + * + */ + + +#define SYS_ROOT_PATH "/sys/module/batman_adv/" +#define SYS_LOG_LEVEL "parameters/debug" +#define SYS_LOG "log" + + +int log_print(int argc, char **argv); +int handle_loglevel(int argc, char **argv);
Modified: trunk/batman-adv-kernelland/Makefile.kbuild =================================================================== --- trunk/batman-adv-kernelland/Makefile.kbuild 2009-11-14 06:11:32 UTC (rev 1482) +++ trunk/batman-adv-kernelland/Makefile.kbuild 2009-11-14 06:11:37 UTC (rev 1483) @@ -25,7 +25,7 @@ -include $(TOPDIR)/Rules.make endif
-#EXTRA_CFLAGS += -DCONFIG_BATMAN_DEBUG +# EXTRA_CFLAGS += -DCONFIG_BATMAN_DEBUG
ifneq ($(REVISION),) EXTRA_CFLAGS += -DREVISION_VERSION="r$(REVISION)"