[commits] r1623 - in trunk/batctl: . man

postmaster at open-mesh.net postmaster at open-mesh.net
Mon Apr 5 22:51:20 CEST 2010


Author: marek
Date: 2010-04-05 22:50:49 +0200 (Mon, 05 Apr 2010)
New Revision: 1623

Removed:
   trunk/batctl/proc.c
   trunk/batctl/proc.h
Modified:
   trunk/batctl/Makefile
   trunk/batctl/functions.c
   trunk/batctl/functions.h
   trunk/batctl/main.c
   trunk/batctl/man/batctl.8
   trunk/batctl/sys.c
   trunk/batctl/sys.h
Log:
batctl: adapt batctl to new sysfs interface handling

Signed-off-by: Marek Lindner <lindner_marek at yahoo.de>

Modified: trunk/batctl/Makefile
===================================================================
--- trunk/batctl/Makefile	2010-04-05 20:50:43 UTC (rev 1622)
+++ trunk/batctl/Makefile	2010-04-05 20:50:49 UTC (rev 1623)
@@ -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 sys.c ping.c traceroute.c tcpdump.c list-batman.c hash.c vis.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 vis.h $(EXTRA_MODULES_H)
+SRC_C = main.c bat-hosts.c functions.c sys.c ping.c traceroute.c tcpdump.c list-batman.c hash.c vis.c $(EXTRA_MODULES_C)
+SRC_H = main.h bat-hosts.h functions.h sys.h ping.h traceroute.h tcpdump.h list-batman.h hash.h allocate.h vis.h $(EXTRA_MODULES_H)
 SRC_O = $(SRC_C:.c=.o)
 
 PACKAGE_NAME = batctl

Modified: trunk/batctl/functions.c
===================================================================
--- trunk/batctl/functions.c	2010-04-05 20:50:43 UTC (rev 1622)
+++ trunk/batctl/functions.c	2010-04-05 20:50:49 UTC (rev 1623)
@@ -164,7 +164,8 @@
 	fp = fopen(full_path, "r");
 
 	if (!fp) {
-		printf("Error - can't open file '%s': %s\n", full_path, strerror(errno));
+		if (!(read_opt & SILENCE_ERRORS))
+			printf("Error - can't open file '%s': %s\n", full_path, strerror(errno));
 		goto out;
 	}
 

Modified: trunk/batctl/functions.h
===================================================================
--- trunk/batctl/functions.h	2010-04-05 20:50:43 UTC (rev 1622)
+++ trunk/batctl/functions.h	2010-04-05 20:50:49 UTC (rev 1623)
@@ -48,4 +48,5 @@
 	LOG_MODE = 0x08,
 	USE_READ_BUFF = 0x10,
 	SEARCH_ARGS = 0x20,
+	SILENCE_ERRORS = 0x40,
 };

Modified: trunk/batctl/main.c
===================================================================
--- trunk/batctl/main.c	2010-04-05 20:50:43 UTC (rev 1622)
+++ trunk/batctl/main.c	2010-04-05 20:50:49 UTC (rev 1623)
@@ -29,7 +29,6 @@
 #include <string.h>
 
 #include "main.h"
-#include "proc.h"
 #include "sys.h"
 #include "ping.h"
 #include "traceroute.h"
@@ -41,7 +40,7 @@
 void print_usage(void) {
 	printf("Usage: batctl [options] commands \n");
 	printf("commands:\n");
-	printf(" \tinterface|if   [none|interface]  \tdisplay or modify the interface settings\n");
+	printf(" \tinterface|if   [add|del iface(s)]\tdisplay or modify the interface settings\n");
 	printf(" \toriginators|o                    \tdisplay the originator table\n");
 	printf(" \tinterval|it    [orig_interval]   \tdisplay or modify the originator interval (in ms)\n");
 	printf(" \tloglevel|ll    [level]           \tdisplay or modify the log level\n");

Modified: trunk/batctl/man/batctl.8
===================================================================
--- trunk/batctl/man/batctl.8	2010-04-05 20:50:43 UTC (rev 1622)
+++ trunk/batctl/man/batctl.8	2010-04-05 20:50:49 UTC (rev 1623)
@@ -53,10 +53,8 @@
 .br
 .TP
 .I \fBcommands:
-.IP "\fBinterface\fP|\fBif\fP     [\fBnone\fP|\fBinterface\fP]"
-If no parameter is given the current interface settings are displayed
-otherwise the parameter(s) are added as new interfaces. Use the "none"
-keyword to deactivate all interfaces.
+.IP "\fBinterface\fP|\fBif\fP     [\fBadd\fP|\fBdel iface(s)\fP]"
+If no parameter is given or the first parameter is neither "add" nor "del" the current interface settings are displayed. In order to add or delete interfaces specify "add" or "del" as first argument and append the interface names you wish to add or delete.
 .br
 .IP "\fBoriginators\fP|\fBo\fP    [\fB\-b\fP][\fB\-n\fP]"
 Once started batctl will display the list of announced gateways in the network. Use the "\-w" option to let batctl refresh the list every second. If "\-n" is given batctl will not replace the MAC addresses with bat\-host names in the output.

Deleted: trunk/batctl/proc.c
===================================================================
--- trunk/batctl/proc.c	2010-04-05 20:50:43 UTC (rev 1622)
+++ trunk/batctl/proc.c	2010-04-05 20:50:49 UTC (rev 1623)
@@ -1,114 +0,0 @@
-/*
- * Copyright (C) 2009 B.A.T.M.A.N. contributors:
- *
- * Marek Lindner <lindner_marek at 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 <sys/time.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "main.h"
-#include "proc.h"
-#include "functions.h"
-
-static void interface_usage(void)
-{
-	printf("Usage: batctl interface [options] [none|interface] \n");
-	printf("options:\n");
-	printf(" \t -h print this help\n");
-}
-
-int interface(int argc, char **argv)
-{
-	int i, res, optchar;
-
-	while ((optchar = getopt(argc, argv, "h")) != -1) {
-		switch (optchar) {
-		case 'h':
-			interface_usage();
-			return EXIT_SUCCESS;
-		default:
-			interface_usage();
-			return EXIT_FAILURE;
-		}
-	}
-
-	if (argc == 1)
-		return read_file(PROC_ROOT_PATH, PROC_INTERFACES, SINGLE_READ);
-
-	for (i = 1; i < argc; i++) {
-		if (strcmp(argv[i], "none") == 0)
-			res = write_file(PROC_ROOT_PATH, PROC_INTERFACES, "", NULL);
-		else
-			res = write_file(PROC_ROOT_PATH, PROC_INTERFACES, argv[i], NULL);
-
-		if (res != EXIT_SUCCESS)
-			return res;
-	}
-
-	return EXIT_SUCCESS;
-}
-
-int handle_table(int argc, char **argv, char *file_path, void table_usage(void))
-{
-	int optchar, read_opt = USE_BAT_HOSTS;
-
-	while ((optchar = getopt(argc, argv, "hnw")) != -1) {
-		switch (optchar) {
-		case 'h':
-			table_usage();
-			return EXIT_SUCCESS;
-		case 'n':
-			read_opt &= ~USE_BAT_HOSTS;
-			break;
-		case 'w':
-			read_opt |= CLR_CONT_READ;
-			break;
-		default:
-			table_usage();
-			return EXIT_FAILURE;
-		}
-	}
-
-	return read_file(PROC_ROOT_PATH, file_path, read_opt);
-}
-
-int handle_proc_setting(int argc, char **argv, char *file_path, void setting_usage(void))
-{
-	int optchar;
-
-	while ((optchar = getopt(argc, argv, "h")) != -1) {
-		switch (optchar) {
-		case 'h':
-			setting_usage();
-			return EXIT_SUCCESS;
-		default:
-			setting_usage();
-			return EXIT_FAILURE;
-		}
-	}
-
-	if (argc == 1)
-		return read_file(PROC_ROOT_PATH, file_path, SINGLE_READ);
-
-	return write_file(PROC_ROOT_PATH, file_path, argv[1], NULL);
-}

Deleted: trunk/batctl/proc.h
===================================================================
--- trunk/batctl/proc.h	2010-04-05 20:50:43 UTC (rev 1622)
+++ trunk/batctl/proc.h	2010-04-05 20:50:49 UTC (rev 1623)
@@ -1,28 +0,0 @@
-/*
- * Copyright (C) 2009 B.A.T.M.A.N. contributors:
- *
- * Marek Lindner <lindner_marek at 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 PROC_ROOT_PATH "/proc/net/batman-adv/"
-#define PROC_INTERFACES "interfaces"
-
-int interface(int argc, char **argv);
-
-int handle_table(int argc, char **argv, char *file_path, void table_usage(void));
-int handle_proc_setting(int argc, char **argv, char *file_path, void setting_usage(void));

Modified: trunk/batctl/sys.c
===================================================================
--- trunk/batctl/sys.c	2010-04-05 20:50:43 UTC (rev 1622)
+++ trunk/batctl/sys.c	2010-04-05 20:50:49 UTC (rev 1623)
@@ -25,12 +25,135 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <dirent.h>
 
 #include "main.h"
 #include "sys.h"
 #include "functions.h"
 
+#define PATH_BUFF_LEN 200
 
+static void interface_usage(void)
+{
+	printf("Usage: batctl interface [options] [add|del iface(s)] \n");
+	printf("options:\n");
+	printf(" \t -h print this help\n");
+}
+
+static int print_interfaces(void)
+{
+	DIR *iface_base_dir;
+	struct dirent *iface_dir;
+	char *path_buff;
+	int res;
+
+	path_buff = malloc(PATH_BUFF_LEN);
+	if (!path_buff) {
+		printf("Error - could not allocate path buffer: out of memory ?\n");
+		goto err;
+	}
+
+	iface_base_dir = opendir(SYS_IFACE_PATH);
+	if (!iface_base_dir) {
+		printf("Error - the directory '%s' could not be read: %s\n",
+		       SYS_IFACE_PATH, strerror(errno));
+		printf("Is the batman-adv module loaded and sysfs mounted ?\n");
+		goto err_buff;
+	}
+
+	while ((iface_dir = readdir(iface_base_dir)) != NULL) {
+		snprintf(path_buff, PATH_BUFF_LEN, SYS_MESH_IFACE_FMT, iface_dir->d_name);
+		res = read_file("", path_buff, SINGLE_READ | USE_READ_BUFF | SILENCE_ERRORS);
+		if (res != EXIT_SUCCESS)
+			continue;
+
+		if (line_ptr[strlen(line_ptr) - 1] == '\n')
+			line_ptr[strlen(line_ptr) - 1] = '\0';
+
+		if (strcmp(line_ptr, "status: none") == 0)
+			goto free_line;
+
+		free(line_ptr);
+		line_ptr = NULL;
+
+		snprintf(path_buff, PATH_BUFF_LEN, SYS_IFACE_STATUS_FMT, iface_dir->d_name);
+		res = read_file("", path_buff, SINGLE_READ | USE_READ_BUFF | SILENCE_ERRORS);
+		if (res != EXIT_SUCCESS) {
+			printf("<error reading status>\n");
+			continue;
+		}
+
+		printf("%s: %s", iface_dir->d_name, line_ptr);
+
+free_line:
+		free(line_ptr);
+		line_ptr = NULL;
+	}
+
+	free(path_buff);
+	closedir(iface_base_dir);
+	return EXIT_SUCCESS;
+
+err_buff:
+	free(path_buff);
+err:
+	return EXIT_FAILURE;
+}
+
+int interface(int argc, char **argv)
+{
+	char *path_buff;
+	int i, res, optchar;
+
+	while ((optchar = getopt(argc, argv, "h")) != -1) {
+		switch (optchar) {
+		case 'h':
+			interface_usage();
+			return EXIT_SUCCESS;
+		default:
+			interface_usage();
+			return EXIT_FAILURE;
+		}
+	}
+
+	if (argc == 1)
+		return print_interfaces();
+
+	if ((strcmp(argv[1], "add") != 0) && (strcmp(argv[1], "a") != 0) &&
+	    (strcmp(argv[1], "del") != 0) && (strcmp(argv[1], "d") != 0)) {
+		printf("Error - unknown argument specified: %s\n", argv[1]);
+		interface_usage();
+		goto err;
+	}
+
+	path_buff = malloc(PATH_BUFF_LEN);
+	if (!path_buff) {
+		printf("Error - could not allocate path buffer: out of memory ?\n");
+		goto err;
+	}
+
+	for (i = 2; i < argc; i++) {
+		snprintf(path_buff, PATH_BUFF_LEN, SYS_MESH_IFACE_FMT, argv[i]);
+
+		if (argv[1][0] == 'a')
+			res = write_file("", path_buff, "bat0", NULL);
+		else
+			res = write_file("", path_buff, "none", NULL);
+
+		if (res != EXIT_SUCCESS)
+			goto err_buff;
+	}
+
+	free(path_buff);
+	return EXIT_SUCCESS;
+
+err_buff:
+	free(path_buff);
+err:
+	return EXIT_FAILURE;
+}
+
 static void log_usage(void)
 {
 	printf("Usage: batctl [options] log [logfile]\n");

Modified: trunk/batctl/sys.h
===================================================================
--- trunk/batctl/sys.h	2010-04-05 20:50:43 UTC (rev 1622)
+++ trunk/batctl/sys.h	2010-04-05 20:50:49 UTC (rev 1623)
@@ -34,6 +34,9 @@
 #define SYS_VIS_MODE "vis_mode"
 #define SYS_VIS_DATA "vis_data"
 #define SYS_ORIG_INTERVAL "orig_interval"
+#define SYS_IFACE_PATH "/sys/class/net"
+#define SYS_MESH_IFACE_FMT SYS_IFACE_PATH"/%s/batman_adv/mesh_iface"
+#define SYS_IFACE_STATUS_FMT SYS_IFACE_PATH"/%s/batman_adv/iface_status"
 
 void originators_usage(void);
 void trans_local_usage(void);
@@ -45,6 +48,7 @@
 void vis_mode_usage(void);
 void orig_interval_usage(void);
 int log_print(int argc, char **argv);
+int interface(int argc, char **argv);
 int handle_loglevel(int argc, char **argv);
 int handle_sys_table(int argc, char **argv, char *file_path, void table_usage(void));
 int handle_sys_setting(int argc, char **argv, char *file_path, void setting_usage(void));



More information about the commits mailing list