| 1 | /* |
|---|
| 2 | * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors: |
|---|
| 3 | * |
|---|
| 4 | * Marek Lindner <lindner_marek@yahoo.de> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or |
|---|
| 7 | * modify it under the terms of version 2 of the GNU General Public |
|---|
| 8 | * License as published by the Free Software Foundation. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, but |
|---|
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 13 | * General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|---|
| 18 | * 02110-1301, USA |
|---|
| 19 | * |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #include <unistd.h> |
|---|
| 24 | #include <stdio.h> |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <errno.h> |
|---|
| 28 | |
|---|
| 29 | #include "main.h" |
|---|
| 30 | #include "sys.h" |
|---|
| 31 | #include "functions.h" |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | static void log_usage(void) |
|---|
| 35 | { |
|---|
| 36 | printf("Usage: batctl [options] log [logfile]\n"); |
|---|
| 37 | printf("Note: if no logfile was specified stdin is read"); |
|---|
| 38 | printf("options:\n"); |
|---|
| 39 | printf(" \t -h print this help\n"); |
|---|
| 40 | printf(" \t -n don't replace mac addresses with bat-host names\n"); |
|---|
| 41 | printf(" \t -w watch mode - read the log file continuously\n"); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | int log_print(int argc, char **argv) |
|---|
| 45 | { |
|---|
| 46 | int optchar, read_opt = USE_BAT_HOSTS | LOG_MODE; |
|---|
| 47 | int found_args = 1; |
|---|
| 48 | |
|---|
| 49 | while ((optchar = getopt(argc, argv, "hnw")) != -1) { |
|---|
| 50 | switch (optchar) { |
|---|
| 51 | case 'h': |
|---|
| 52 | log_usage(); |
|---|
| 53 | return EXIT_SUCCESS; |
|---|
| 54 | case 'n': |
|---|
| 55 | read_opt &= ~USE_BAT_HOSTS; |
|---|
| 56 | found_args += 1; |
|---|
| 57 | break; |
|---|
| 58 | case 'w': |
|---|
| 59 | read_opt |= CONT_READ; |
|---|
| 60 | found_args += 1; |
|---|
| 61 | break; |
|---|
| 62 | default: |
|---|
| 63 | log_usage(); |
|---|
| 64 | return EXIT_FAILURE; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | if (argc > found_args) |
|---|
| 69 | return read_file("", argv[found_args], read_opt); |
|---|
| 70 | else |
|---|
| 71 | return read_file("", "/proc/self/fd/0", read_opt); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | static void log_level_usage(void) |
|---|
| 75 | { |
|---|
| 76 | printf("Usage: batctl [options] loglevel \n"); |
|---|
| 77 | printf("options:\n"); |
|---|
| 78 | printf(" \t -h print this help\n"); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | int handle_loglevel(int argc, char **argv) |
|---|
| 82 | { |
|---|
| 83 | int optchar, res; |
|---|
| 84 | |
|---|
| 85 | while ((optchar = getopt(argc, argv, "h")) != -1) { |
|---|
| 86 | switch (optchar) { |
|---|
| 87 | case 'h': |
|---|
| 88 | log_level_usage(); |
|---|
| 89 | return EXIT_SUCCESS; |
|---|
| 90 | default: |
|---|
| 91 | log_level_usage(); |
|---|
| 92 | return EXIT_FAILURE; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | if (argc != 1) { |
|---|
| 97 | res = write_file(SYS_MODULE_PATH, SYS_LOG_LEVEL, argv[1]); |
|---|
| 98 | goto out; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | res = read_file(SYS_MODULE_PATH, SYS_LOG_LEVEL, SINGLE_READ | USE_READ_BUFF); |
|---|
| 102 | |
|---|
| 103 | if (res != EXIT_SUCCESS) |
|---|
| 104 | goto out; |
|---|
| 105 | |
|---|
| 106 | printf("[%c] %s (%d)\n", (line_ptr[0] == '0') ? 'x' : ' ', |
|---|
| 107 | "all debug output disabled", 0); |
|---|
| 108 | printf("[%c] %s (%d)\n", (line_ptr[0] == '1') ? 'x' : ' ', |
|---|
| 109 | "messages related to routing / flooding / broadcasting", 1); |
|---|
| 110 | printf("[%c] %s (%d)\n", (line_ptr[0] == '2') ? 'x' : ' ', |
|---|
| 111 | "messages related to route or hna added / changed / deleted", 2); |
|---|
| 112 | printf("[%c] %s (%d)\n", (line_ptr[0] == '3') ? 'x' : ' ', |
|---|
| 113 | "all debug messages", 3); |
|---|
| 114 | |
|---|
| 115 | out: |
|---|
| 116 | if (errno == ENOENT) |
|---|
| 117 | printf("To increase the log level you need to compile the module with debugging enabled (see the README)\n"); |
|---|
| 118 | |
|---|
| 119 | return res; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | void originators_usage(void) |
|---|
| 123 | { |
|---|
| 124 | printf("Usage: batctl [options] originators \n"); |
|---|
| 125 | printf("options:\n"); |
|---|
| 126 | printf(" \t -h print this help\n"); |
|---|
| 127 | printf(" \t -n don't replace mac addresses with bat-host names\n"); |
|---|
| 128 | printf(" \t -w watch mode - refresh the originator table continuously\n"); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | void trans_local_usage(void) |
|---|
| 132 | { |
|---|
| 133 | printf("Usage: batctl [options] translocal \n"); |
|---|
| 134 | printf("options:\n"); |
|---|
| 135 | printf(" \t -h print this help\n"); |
|---|
| 136 | printf(" \t -n don't replace mac addresses with bat-host names\n"); |
|---|
| 137 | printf(" \t -w watch mode - refresh the local translation table continuously\n"); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void trans_global_usage(void) |
|---|
| 141 | { |
|---|
| 142 | printf("Usage: batctl [options] transglobal \n"); |
|---|
| 143 | printf("options:\n"); |
|---|
| 144 | printf(" \t -h print this help\n"); |
|---|
| 145 | printf(" \t -n don't replace mac addresses with bat-host names\n"); |
|---|
| 146 | printf(" \t -w watch mode - refresh the global translation table continuously\n"); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | int handle_sys_table(int argc, char **argv, char *file_path, void table_usage(void)) |
|---|
| 150 | { |
|---|
| 151 | int optchar, read_opt = USE_BAT_HOSTS; |
|---|
| 152 | |
|---|
| 153 | while ((optchar = getopt(argc, argv, "hnw")) != -1) { |
|---|
| 154 | switch (optchar) { |
|---|
| 155 | case 'h': |
|---|
| 156 | table_usage(); |
|---|
| 157 | return EXIT_SUCCESS; |
|---|
| 158 | case 'n': |
|---|
| 159 | read_opt &= ~USE_BAT_HOSTS; |
|---|
| 160 | break; |
|---|
| 161 | case 'w': |
|---|
| 162 | read_opt |= CLR_CONT_READ; |
|---|
| 163 | break; |
|---|
| 164 | default: |
|---|
| 165 | table_usage(); |
|---|
| 166 | return EXIT_FAILURE; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | return read_file(SYS_BATIF_PATH, file_path, read_opt); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | void aggregation_usage(void) |
|---|
| 174 | { |
|---|
| 175 | printf("Usage: batctl [options] aggregation \n"); |
|---|
| 176 | printf("options:\n"); |
|---|
| 177 | printf(" \t -h print this help\n"); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | void bonding_usage(void) |
|---|
| 181 | { |
|---|
| 182 | printf("Usage: batctl [options] bonding \n"); |
|---|
| 183 | printf("options:\n"); |
|---|
| 184 | printf(" \t -h print this help\n"); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | int handle_sys_setting(int argc, char **argv, char *file_path, void setting_usage(void)) |
|---|
| 188 | { |
|---|
| 189 | int optchar, res; |
|---|
| 190 | char *space_ptr, *comma_char, *cmds = NULL; |
|---|
| 191 | |
|---|
| 192 | while ((optchar = getopt(argc, argv, "h")) != -1) { |
|---|
| 193 | switch (optchar) { |
|---|
| 194 | case 'h': |
|---|
| 195 | setting_usage(); |
|---|
| 196 | return EXIT_SUCCESS; |
|---|
| 197 | default: |
|---|
| 198 | setting_usage(); |
|---|
| 199 | return EXIT_FAILURE; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | if (argc == 1) |
|---|
| 204 | return read_file(SYS_BATIF_PATH, file_path, SINGLE_READ); |
|---|
| 205 | |
|---|
| 206 | res = read_file(SYS_BATIF_PATH, file_path, SEARCH_ARGS); |
|---|
| 207 | if (res != EXIT_SUCCESS) |
|---|
| 208 | return res; |
|---|
| 209 | |
|---|
| 210 | while ((space_ptr = strchr(line_ptr, ' ')) != NULL) { |
|---|
| 211 | *space_ptr = '\0'; |
|---|
| 212 | |
|---|
| 213 | if (strncmp(line_ptr, SEARCH_ARGS_TAG, strlen(SEARCH_ARGS_TAG)) == 0) { |
|---|
| 214 | cmds = space_ptr + 1; |
|---|
| 215 | goto next; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | comma_char = NULL; |
|---|
| 219 | if (line_ptr[strlen(line_ptr) - 1] == ',') { |
|---|
| 220 | comma_char = line_ptr + strlen(line_ptr) - 1; |
|---|
| 221 | *comma_char = '\0'; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | if (strcmp(line_ptr, argv[1]) == 0) |
|---|
| 225 | goto write_file; |
|---|
| 226 | |
|---|
| 227 | *space_ptr = ' '; |
|---|
| 228 | if (comma_char) |
|---|
| 229 | *comma_char = ','; |
|---|
| 230 | |
|---|
| 231 | next: |
|---|
| 232 | line_ptr = space_ptr + 1; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | if (!cmds) |
|---|
| 236 | goto write_file; |
|---|
| 237 | |
|---|
| 238 | printf("Error - the supplied argument is invalid: %s\n", argv[1]); |
|---|
| 239 | printf("The following values are allowed: %s", cmds); |
|---|
| 240 | return EXIT_FAILURE; |
|---|
| 241 | |
|---|
| 242 | write_file: |
|---|
| 243 | return write_file(SYS_BATIF_PATH, file_path, argv[1]); |
|---|
| 244 | } |
|---|