2 * kmscon - Common test functions
4 * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <sys/signalfd.h>
37 "\t-h, --help [off] Print this help and exit\n" \
38 "\t-v, --verbose [off] Print verbose messages\n" \
39 "\t --debug [off] Enable debug mode\n" \
40 "\t --silent [off] Suppress notices and warnings\n"
50 static struct conf_ctx *test_ctx;
52 static int aftercheck_debug(struct conf_option *opt, int argc, char **argv,
55 /* --debug implies --verbose */
57 test_conf.verbose = 1;
62 static int aftercheck_help(struct conf_option *opt, int argc, char **argv,
65 /* exit after printing --help information */
68 test_conf.exit = true;
74 #define TEST_OPTIONS \
75 CONF_OPTION_BOOL_FULL('h', "help", aftercheck_help, NULL, NULL, &test_conf.help, false), \
76 CONF_OPTION_BOOL('v', "verbose", &test_conf.verbose, false), \
77 CONF_OPTION_BOOL_FULL(0, "debug", aftercheck_debug, NULL, NULL, &test_conf.debug, false), \
78 CONF_OPTION_BOOL(0, "silent", &test_conf.silent, false)
80 static void sig_generic(struct ev_eloop *p, struct signalfd_siginfo *info,
83 struct ev_eloop *eloop = data;
86 log_info("terminating due to caught signal %d", info->ssi_signo);
89 static int test_prepare(struct conf_option *opts, size_t len,
90 int argc, char **argv, struct ev_eloop **out)
93 struct ev_eloop *eloop;
95 ret = conf_ctx_new(&test_ctx, opts, len, &test_conf);
99 ret = conf_ctx_parse_argv(test_ctx, argc, argv);
103 if (test_conf.exit) {
108 if (!test_conf.debug && !test_conf.verbose && test_conf.silent)
109 log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0));
111 log_set_config(&LOG_CONFIG_INFO(test_conf.debug,
114 log_print_init(argv[0]);
116 ret = ev_eloop_new(&eloop, log_llog, NULL);
120 ret = ev_eloop_register_signal_cb(eloop, SIGTERM, sig_generic, eloop);
124 ret = ev_eloop_register_signal_cb(eloop, SIGINT, sig_generic, eloop);
126 ev_eloop_unregister_signal_cb(eloop, SIGTERM,
135 ev_eloop_unref(eloop);
137 conf_ctx_free(test_ctx);
141 static void test_fail(int ret)
144 log_err("init failed, errno %d: %s", ret, strerror(-ret));
147 static void test_exit(struct conf_option *opts, size_t len,
148 struct ev_eloop *eloop)
150 ev_eloop_unregister_signal_cb(eloop, SIGINT, sig_generic, eloop);
151 ev_eloop_unregister_signal_cb(eloop, SIGTERM, sig_generic, eloop);
152 ev_eloop_unref(eloop);
153 conf_ctx_free(test_ctx);