Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 1 | /* Test implementation for osmo_tdef VTY configuration API. */ |
| 2 | /* |
| 3 | * (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
| 9 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #define _GNU_SOURCE |
| 27 | #include <getopt.h> |
| 28 | #include <signal.h> |
| 29 | #include <limits.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include <osmocom/core/application.h> |
| 33 | |
| 34 | #include <osmocom/vty/command.h> |
| 35 | #include <osmocom/vty/misc.h> |
| 36 | #include <osmocom/vty/telnet_interface.h> |
| 37 | |
| 38 | #include <osmocom/core/tdef.h> |
| 39 | #include <osmocom/vty/tdef_vty.h> |
| 40 | |
| 41 | #include <stdlib.h> |
| 42 | |
| 43 | #include "config.h" |
| 44 | |
| 45 | void *root_ctx = NULL; |
| 46 | |
| 47 | /* ------------------- HERE IS THE INTERESTING TDEF RELEVANT PART ------------------- */ |
| 48 | |
| 49 | /* This example keeps a separate list of timers for each instance of a dynamically allocated instance of a VTY node, |
| 50 | * for example of keeping separate timers for each BTS in a BSC. |
| 51 | */ |
| 52 | |
| 53 | static const struct osmo_tdef bts_default_tdefs[] = { |
| 54 | { .T=1111, .default_val=2, .desc="Dynamic Duo" }, |
| 55 | { .T=2222, .default_val=1, .desc="BATMAN" }, |
| 56 | { .T=3333, .default_val=12, .desc="Dadadadadadadadadadadada" }, |
| 57 | { .T=4444, .default_val=500, .unit=OSMO_TDEF_MS, .desc="POW!" }, |
| 58 | {} |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | /* Boilerplate dynamic VTY node ... */ |
| 63 | |
| 64 | enum tdef_vty_test_nodes { |
| 65 | MEMBER_NODE = _LAST_OSMOVTY_NODE + 1, |
| 66 | }; |
| 67 | |
| 68 | static struct cmd_node member_node = { |
| 69 | MEMBER_NODE, |
| 70 | "%s(config-member)# ", |
| 71 | 1, |
| 72 | }; |
| 73 | |
| 74 | struct member { |
| 75 | struct llist_head entry; |
| 76 | char name[23]; |
| 77 | struct osmo_tdef *tdefs; |
| 78 | }; |
| 79 | |
| 80 | LLIST_HEAD(all_members); |
| 81 | |
| 82 | struct member *member_alloc(const char *name) |
| 83 | { |
| 84 | struct member *m = talloc_zero(root_ctx, struct member); |
| 85 | osmo_strlcpy(m->name, name, sizeof(m->name)); |
| 86 | |
| 87 | /* DYNAMIC TDEF COPIES */ |
| 88 | m->tdefs = (struct osmo_tdef*)talloc_size(m, sizeof(bts_default_tdefs)); |
| 89 | memcpy((char*)m->tdefs, (char*)&bts_default_tdefs, sizeof(bts_default_tdefs)); |
| 90 | osmo_tdefs_reset(m->tdefs); |
| 91 | |
| 92 | llist_add_tail(&m->entry, &all_members); |
| 93 | return m; |
| 94 | } |
| 95 | |
| 96 | struct member *member_find(const char *name) |
| 97 | { |
| 98 | struct member *m; |
| 99 | llist_for_each_entry(m, &all_members, entry) { |
| 100 | if (!strcmp(m->name, name)) |
| 101 | return m; |
| 102 | } |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | DEFUN(cfg_member, cfg_member_cmd, |
| 107 | "member NAME", |
| 108 | "Enter member node\n" "Existing or new member node name\n") |
| 109 | { |
| 110 | const char *name = argv[0]; |
| 111 | struct member *m = member_find(name); |
| 112 | if (!m) |
| 113 | m = member_alloc(name); |
| 114 | vty->index = m; |
| 115 | vty->node = MEMBER_NODE; |
| 116 | return CMD_SUCCESS; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /* TDEF SPECIFIC VTY */ |
| 121 | |
| 122 | static bool startswith(const char *str, const char *startswith_str) |
| 123 | { |
| 124 | if (!startswith_str) |
| 125 | return true; |
| 126 | if (!str) |
| 127 | return false; |
| 128 | return strncmp(str, startswith_str, strlen(startswith_str)) == 0; |
| 129 | } |
| 130 | |
| 131 | DEFUN(show_timer, show_member_timer_cmd, |
| 132 | "show member-timer [NAME] " OSMO_TDEF_VTY_ARG_T_OPTIONAL, |
| 133 | SHOW_STR "Show timers for a specific member" "member name\n" |
| 134 | OSMO_TDEF_VTY_DOC_T) |
| 135 | { |
| 136 | const char *name = argc > 0 ? argv[0] : NULL; |
| 137 | struct member *m; |
| 138 | const char *T_arg = argc > 1 ? argv[1] : NULL; |
| 139 | int shown = 0; |
| 140 | |
| 141 | llist_for_each_entry(m, &all_members, entry) { |
| 142 | if (!name || startswith(m->name, name)) { |
| 143 | osmo_tdef_vty_show_cmd(vty, m->tdefs, T_arg, "%11s: ", m->name); |
| 144 | shown ++; |
| 145 | } |
| 146 | } |
| 147 | if (!shown) { |
| 148 | vty_out(vty, "%% No such member: %s%s", name ? : "(none)", VTY_NEWLINE); |
| 149 | return CMD_WARNING; |
| 150 | } |
| 151 | return CMD_SUCCESS; |
| 152 | } |
| 153 | |
| 154 | DEFUN(cfg_member_timer, cfg_member_timer_cmd, |
| 155 | "timer " OSMO_TDEF_VTY_ARG_SET_OPTIONAL, |
| 156 | "Configure or show timers for this member\n" |
| 157 | OSMO_TDEF_VTY_DOC_SET) |
| 158 | { |
| 159 | struct member *m = vty->index; |
| 160 | |
| 161 | if (!m || !m->tdefs) { |
| 162 | vty_out(vty, "%% No timers here%s", VTY_NEWLINE); |
| 163 | return CMD_WARNING; |
| 164 | } |
| 165 | |
| 166 | /* If any arguments are missing, redirect to 'show' */ |
| 167 | if (argc < 2) { |
| 168 | const char *T_arg = argc > 0 ? argv[0] : NULL; |
| 169 | return osmo_tdef_vty_show_cmd(vty, m->tdefs, T_arg, "%11s: ", m->name); |
| 170 | } |
| 171 | |
| 172 | return osmo_tdef_vty_set_cmd(vty, m->tdefs, argv); |
| 173 | } |
| 174 | |
| 175 | static int config_write_member(struct vty *vty) |
| 176 | { |
| 177 | struct member *m; |
| 178 | llist_for_each_entry(m, &all_members, entry) { |
| 179 | vty_out(vty, "member %s%s", m->name, VTY_NEWLINE); |
| 180 | osmo_tdef_vty_write(vty, m->tdefs, " timer "); |
| 181 | } |
| 182 | |
| 183 | return CMD_SUCCESS; |
| 184 | } |
| 185 | |
| 186 | static void member_init_vty() |
| 187 | { |
| 188 | install_node(&member_node, config_write_member); |
| 189 | install_element(CONFIG_NODE, &cfg_member_cmd); |
| 190 | |
| 191 | install_element_ve(&show_member_timer_cmd); |
| 192 | install_element(MEMBER_NODE, &cfg_member_timer_cmd); |
| 193 | } |
| 194 | |
| 195 | /* ------------------- THE REST is just boilerplate osmo main() ------------------- */ |
| 196 | |
| 197 | static void print_help() |
| 198 | { |
| 199 | printf( "options:\n" |
| 200 | " -h --help this text\n" |
| 201 | " -d --debug MASK Enable debugging (e.g. -d DRSL:DOML:DLAPDM)\n" |
| 202 | " -D --daemonize For the process into a background daemon\n" |
| 203 | " -c --config-file Specify the filename of the config file\n" |
| 204 | " -s --disable-color Don't use colors in stderr log output\n" |
| 205 | " -T --timestamp Prefix every log line with a timestamp\n" |
| 206 | " -V --version Print version information and exit\n" |
| 207 | " -e --log-level Set a global log-level\n" |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | static struct { |
| 212 | const char *config_file; |
| 213 | int daemonize; |
| 214 | } cmdline_config = {}; |
| 215 | |
| 216 | static void handle_options(int argc, char **argv) |
| 217 | { |
| 218 | while (1) { |
| 219 | int option_idx = 0, c; |
| 220 | static const struct option long_options[] = { |
| 221 | { "help", 0, 0, 'h' }, |
| 222 | { "debug", 1, 0, 'd' }, |
| 223 | { "daemonize", 0, 0, 'D' }, |
| 224 | { "config-file", 1, 0, 'c' }, |
| 225 | { "disable-color", 0, 0, 's' }, |
| 226 | { "timestamp", 0, 0, 'T' }, |
| 227 | { "version", 0, 0, 'V' }, |
| 228 | { "log-level", 1, 0, 'e' }, |
| 229 | {} |
| 230 | }; |
| 231 | |
| 232 | c = getopt_long(argc, argv, "hc:d:Dc:sTVe:", |
| 233 | long_options, &option_idx); |
| 234 | if (c == -1) |
| 235 | break; |
| 236 | |
| 237 | switch (c) { |
| 238 | case 'h': |
| 239 | print_help(); |
| 240 | exit(0); |
| 241 | case 's': |
| 242 | log_set_use_color(osmo_stderr_target, 0); |
| 243 | break; |
| 244 | case 'd': |
| 245 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 246 | break; |
| 247 | case 'D': |
| 248 | cmdline_config.daemonize = 1; |
| 249 | break; |
| 250 | case 'c': |
| 251 | cmdline_config.config_file = optarg; |
| 252 | break; |
| 253 | case 'T': |
| 254 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 255 | break; |
| 256 | case 'e': |
| 257 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 258 | break; |
| 259 | case 'V': |
| 260 | print_version(1); |
| 261 | exit(0); |
| 262 | break; |
| 263 | default: |
| 264 | /* catch unknown options *as well as* missing arguments. */ |
| 265 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 266 | exit(-1); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static int quit = 0; |
| 272 | |
| 273 | static void signal_handler(int signal) |
| 274 | { |
| 275 | fprintf(stdout, "signal %u received\n", signal); |
| 276 | |
| 277 | switch (signal) { |
| 278 | case SIGINT: |
| 279 | case SIGTERM: |
| 280 | quit++; |
| 281 | break; |
| 282 | case SIGABRT: |
| 283 | osmo_generate_backtrace(); |
| 284 | /* in case of abort, we want to obtain a talloc report |
| 285 | * and then return to the caller, who will abort the process */ |
| 286 | case SIGUSR1: |
| 287 | talloc_report(tall_vty_ctx, stderr); |
| 288 | talloc_report_full(root_ctx, stderr); |
| 289 | break; |
| 290 | case SIGUSR2: |
| 291 | talloc_report_full(tall_vty_ctx, stderr); |
| 292 | break; |
| 293 | default: |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static struct vty_app_info vty_info = { |
| 299 | .name = "tdef_vty_test", |
| 300 | .version = PACKAGE_VERSION, |
| 301 | }; |
| 302 | |
| 303 | static const struct log_info_cat default_categories[] = {}; |
| 304 | |
| 305 | const struct log_info log_info = { |
| 306 | .cat = default_categories, |
| 307 | .num_cat = ARRAY_SIZE(default_categories), |
| 308 | }; |
| 309 | |
| 310 | int main(int argc, char **argv) |
| 311 | { |
| 312 | int rc; |
| 313 | |
| 314 | root_ctx = talloc_named_const(NULL, 0, "tdef_vty_test"); |
| 315 | |
| 316 | osmo_init_logging2(root_ctx, &log_info); |
| 317 | |
| 318 | vty_info.tall_ctx = root_ctx; |
| 319 | vty_init(&vty_info); |
| 320 | osmo_talloc_vty_add_cmds(); |
| 321 | |
| 322 | member_init_vty(); /* <--- relevant init for this example */ |
| 323 | |
| 324 | handle_options(argc, argv); |
| 325 | |
| 326 | if (cmdline_config.config_file) { |
| 327 | rc = vty_read_config_file(cmdline_config.config_file, NULL); |
| 328 | if (rc < 0) { |
| 329 | fprintf(stderr, "Failed to parse the config file: '%s'\n", cmdline_config.config_file); |
| 330 | return 1; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | rc = telnet_init_dynif(root_ctx, NULL, vty_get_bind_addr(), 42042); |
| 335 | if (rc < 0) |
| 336 | return 2; |
| 337 | |
| 338 | signal(SIGINT, &signal_handler); |
| 339 | signal(SIGTERM, &signal_handler); |
| 340 | signal(SIGABRT, &signal_handler); |
| 341 | signal(SIGUSR1, &signal_handler); |
| 342 | signal(SIGUSR2, &signal_handler); |
| 343 | osmo_init_ignore_signals(); |
| 344 | |
| 345 | if (cmdline_config.daemonize) { |
| 346 | rc = osmo_daemonize(); |
| 347 | if (rc < 0) { |
| 348 | perror("Error during daemonize"); |
| 349 | return 6; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | while (!quit) { |
| 354 | log_reset_context(); |
| 355 | osmo_select_main(0); |
| 356 | } |
| 357 | |
| 358 | talloc_free(root_ctx); |
| 359 | talloc_free(tall_vty_ctx); |
| 360 | |
| 361 | return 0; |
| 362 | } |