blob: 138ac00a51d84174a5c097ca4f827399f30ec343 [file] [log] [blame]
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +01001/* 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/* ------------------- HERE IS THE INTERESTING TDEF RELEVANT PART ------------------- */
46
47/* This example keeps several separate timer groups and offers 'timer' VTY commands at the root of the config node. See
48 * the tdef_vty_test_config_root.vty transcript test.
49 */
50
51static struct osmo_tdef tdefs_test[] = {
52 { .T=1, .default_val=100, .desc="Testing a hundred seconds" }, // default is .unit=OSMO_TDEF_S == 0
53 { .T=2, .default_val=100, .unit=OSMO_TDEF_MS, .desc="Testing a hundred milliseconds" },
54 { .T=3, .default_val=100, .unit=OSMO_TDEF_M, .desc="Testing a hundred minutes" },
55 { .T=4, .default_val=100, .unit=OSMO_TDEF_CUSTOM, .desc="Testing a hundred potatoes" },
56 { .T=INT_MAX, .default_val=ULONG_MAX, .unit=OSMO_TDEF_M, .desc="Very large" },
57 { .T=-23, .default_val=-15, .desc="Negative T number" },
58 {} // <-- important! last entry shall be zero
59};
60
61static struct osmo_tdef tdefs_tea[] = {
62 { .T=1, .default_val=50, .desc="Water Boiling Timeout" },
63 { .T=2, .default_val=300, .desc="Tea brewing" },
64 { .T=3, .default_val=5, .unit=OSMO_TDEF_M, .desc="Let tea cool down before drinking" },
65 { .T=4, .default_val=20, .unit=OSMO_TDEF_M, .desc="Forgot to drink tea while it's warm" },
66 {}
67};
68
69static struct osmo_tdef tdefs_software[] = {
70 { .T=1, .default_val=30, .unit=OSMO_TDEF_M, .desc="Write code" },
71 { .T=2, .default_val=20, .unit=OSMO_TDEF_MS, .desc="Hit segfault" },
72 { .T=3, .default_val=480, .unit=OSMO_TDEF_M, .desc="Fix bugs" },
73 {}
74};
75
76static struct osmo_tdef_group tdef_groups[] = {
77 {
78 .name = "tea",
79 .desc = "Tea time",
80 .tdefs = tdefs_tea,
81 },
82 {
83 .name = "test",
84 .desc = "Test timers",
85 .tdefs = tdefs_test,
86 },
87 {
88 .name = "software",
89 .desc = "Typical software development cycle",
90 .tdefs = tdefs_software,
91 },
92 {}
93};
94
95enum tdef_vty_test_nodes {
96 TIMER_NODE = _LAST_OSMOVTY_NODE + 1,
97};
98
99/* This example puts 'timer' configuration commands directly at the root of the CONFIG_NODE.
100 * This TIMER_NODE is merely needed as a hook for the vty_write() command, but becomes an empty node in the VTY docs.
101 * It is possible to cheat around needing this if you choose to config_write_timer() in another root nodes' write cb.
102 * Another example using a 'network' subnode is \ref tdef_vty_test_config_subnode.c */
103static struct cmd_node timer_node = {
104 TIMER_NODE,
105 "%s(config-timer)# ",
106 1,
107};
108
109static int config_write_timer(struct vty *vty)
110{
111 osmo_tdef_vty_groups_write(vty, "");
112 return CMD_SUCCESS;
113}
114
115static void timer_init_vty()
116{
117 /* Again, this is merely to get a vty write hook, see above. */
118 install_node(&timer_node, config_write_timer);
119
120 osmo_tdef_vty_groups_init(CONFIG_NODE, tdef_groups);
121}
122
123/* ------------------- THE REST is just boilerplate osmo main() ------------------- */
124
125void *root_ctx = NULL;
126
127static void print_help()
128{
129 printf( "options:\n"
130 " -h --help this text\n"
131 " -d --debug MASK Enable debugging (e.g. -d DRSL:DOML:DLAPDM)\n"
132 " -D --daemonize For the process into a background daemon\n"
133 " -c --config-file Specify the filename of the config file\n"
134 " -s --disable-color Don't use colors in stderr log output\n"
135 " -T --timestamp Prefix every log line with a timestamp\n"
136 " -V --version Print version information and exit\n"
137 " -e --log-level Set a global log-level\n"
138 );
139}
140
141static struct {
142 const char *config_file;
143 int daemonize;
144} cmdline_config = {};
145
146static void handle_options(int argc, char **argv)
147{
148 while (1) {
149 int option_idx = 0, c;
150 static const struct option long_options[] = {
151 { "help", 0, 0, 'h' },
152 { "debug", 1, 0, 'd' },
153 { "daemonize", 0, 0, 'D' },
154 { "config-file", 1, 0, 'c' },
155 { "disable-color", 0, 0, 's' },
156 { "timestamp", 0, 0, 'T' },
157 { "version", 0, 0, 'V' },
158 { "log-level", 1, 0, 'e' },
159 {}
160 };
161
162 c = getopt_long(argc, argv, "hc:d:Dc:sTVe:",
163 long_options, &option_idx);
164 if (c == -1)
165 break;
166
167 switch (c) {
168 case 'h':
169 print_help();
170 exit(0);
171 case 's':
172 log_set_use_color(osmo_stderr_target, 0);
173 break;
174 case 'd':
175 log_parse_category_mask(osmo_stderr_target, optarg);
176 break;
177 case 'D':
178 cmdline_config.daemonize = 1;
179 break;
180 case 'c':
181 cmdline_config.config_file = optarg;
182 break;
183 case 'T':
184 log_set_print_timestamp(osmo_stderr_target, 1);
185 break;
186 case 'e':
187 log_set_log_level(osmo_stderr_target, atoi(optarg));
188 break;
189 case 'V':
190 print_version(1);
191 exit(0);
192 break;
193 default:
194 /* catch unknown options *as well as* missing arguments. */
195 fprintf(stderr, "Error in command line options. Exiting.\n");
196 exit(-1);
197 }
198 }
199}
200
201static int quit = 0;
202
203static void signal_handler(int signal)
204{
205 fprintf(stdout, "signal %u received\n", signal);
206
207 switch (signal) {
208 case SIGINT:
209 case SIGTERM:
210 quit++;
211 break;
212 case SIGABRT:
213 osmo_generate_backtrace();
214 /* in case of abort, we want to obtain a talloc report
215 * and then return to the caller, who will abort the process */
216 case SIGUSR1:
217 talloc_report(tall_vty_ctx, stderr);
218 talloc_report_full(root_ctx, stderr);
219 break;
220 case SIGUSR2:
221 talloc_report_full(tall_vty_ctx, stderr);
222 break;
223 default:
224 break;
225 }
226}
227
228static struct vty_app_info vty_info = {
229 .name = "tdef_vty_test",
230 .version = PACKAGE_VERSION,
231};
232
233static const struct log_info_cat default_categories[] = {};
234
235const struct log_info log_info = {
236 .cat = default_categories,
237 .num_cat = ARRAY_SIZE(default_categories),
238};
239
240int main(int argc, char **argv)
241{
242 int rc;
243
244 root_ctx = talloc_named_const(NULL, 0, "tdef_vty_test");
245
246 osmo_init_logging2(root_ctx, &log_info);
247
248 vty_info.tall_ctx = root_ctx;
249 vty_init(&vty_info);
250 osmo_talloc_vty_add_cmds();
251
252 timer_init_vty(); /* <---- the only tdef relevant init */
253
254 handle_options(argc, argv);
255
256 if (cmdline_config.config_file) {
257 rc = vty_read_config_file(cmdline_config.config_file, NULL);
258 if (rc < 0) {
259 fprintf(stderr, "Failed to parse the config file: '%s'\n", cmdline_config.config_file);
260 return 1;
261 }
262 }
263
264 rc = telnet_init_dynif(root_ctx, NULL, vty_get_bind_addr(), 42042);
265 if (rc < 0)
266 return 2;
267
268 signal(SIGINT, &signal_handler);
269 signal(SIGTERM, &signal_handler);
270 signal(SIGABRT, &signal_handler);
271 signal(SIGUSR1, &signal_handler);
272 signal(SIGUSR2, &signal_handler);
273 osmo_init_ignore_signals();
274
275 if (cmdline_config.daemonize) {
276 rc = osmo_daemonize();
277 if (rc < 0) {
278 perror("Error during daemonize");
279 return 6;
280 }
281 }
282
283 while (!quit) {
284 log_reset_context();
285 osmo_select_main(0);
286 }
287
288 talloc_free(root_ctx);
289 talloc_free(tall_vty_ctx);
290
291 return 0;
292}