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 int aftercheck_debug(struct conf_option *opt, int argc, char **argv,
53 /* --debug implies --verbose */
55 test_conf.verbose = 1;
60 static int aftercheck_help(struct conf_option *opt, int argc, char **argv,
63 /* exit after printing --help information */
66 test_conf.exit = true;
72 #define TEST_OPTIONS \
73 CONF_OPTION_BOOL('h', "help", aftercheck_help, &test_conf.help, false), \
74 CONF_OPTION_BOOL('v', "verbose", NULL, &test_conf.verbose, false), \
75 CONF_OPTION_BOOL(0, "debug", aftercheck_debug, &test_conf.debug, false), \
76 CONF_OPTION_BOOL(0, "silent", NULL, &test_conf.silent, false)
78 static void sig_generic(struct ev_eloop *p, struct signalfd_siginfo *info,
81 struct ev_eloop *eloop = data;
84 log_info("terminating due to caught signal %d", info->ssi_signo);
87 static int test_prepare(struct conf_option *opts, size_t len,
88 int argc, char **argv, struct ev_eloop **out)
91 struct ev_eloop *eloop;
93 ret = conf_parse_argv(opts, len, argc, argv);
102 if (!test_conf.debug && !test_conf.verbose && test_conf.silent)
103 log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0));
105 log_set_config(&LOG_CONFIG_INFO(test_conf.debug,
108 log_print_init(argv[0]);
110 ret = ev_eloop_new(&eloop, log_llog);
114 ret = ev_eloop_register_signal_cb(eloop, SIGTERM, sig_generic, eloop);
118 ret = ev_eloop_register_signal_cb(eloop, SIGINT, sig_generic, eloop);
120 ev_eloop_unregister_signal_cb(eloop, SIGTERM,
129 ev_eloop_unref(eloop);
131 conf_free(opts, len);
135 static void test_fail(int ret)
138 log_err("init failed, errno %d: %s", ret, strerror(-ret));
141 static void test_exit(struct conf_option *opts, size_t len,
142 struct ev_eloop *eloop)
144 ev_eloop_unregister_signal_cb(eloop, SIGINT, sig_generic, eloop);
145 ev_eloop_unregister_signal_cb(eloop, SIGTERM, sig_generic, eloop);
146 ev_eloop_unref(eloop);
147 conf_free(opts, len);