test_input: use new test_include.h infrastructure
[platform/upstream/kmscon.git] / tests / test_include.h
1 /*
2  * kmscon - Common test functions
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
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.
24  */
25
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/signalfd.h>
32 #include "conf.h"
33 #include "eloop.h"
34 #include "log.h"
35
36 static void sig_generic(struct ev_eloop *p, struct signalfd_siginfo *info,
37                         void *data)
38 {
39         struct ev_eloop *eloop = data;
40
41         ev_eloop_exit(eloop);
42         log_info("terminating due to caught signal %d", info->ssi_signo);
43 }
44
45 static int test_prepare(int argc, char **argv, struct ev_eloop **out)
46 {
47         int ret;
48         struct ev_eloop *eloop;
49
50         ret = conf_parse_argv(argc, argv);
51         if (ret)
52                 return -EINVAL;
53
54         if (conf_global.exit)
55                 return -1;
56
57         if (!conf_global.debug && !conf_global.verbose && conf_global.silent)
58                 log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0));
59         else
60                 log_set_config(&LOG_CONFIG_INFO(conf_global.debug,
61                                                 conf_global.verbose));
62
63         log_print_init(argv[0]);
64
65         ret = ev_eloop_new(&eloop);
66         if (ret)
67                 return ret;
68
69         ret = ev_eloop_register_signal_cb(eloop, SIGTERM, sig_generic, eloop);
70         if (ret) {
71                 ev_eloop_unref(eloop);
72                 return ret;
73         }
74
75         ret = ev_eloop_register_signal_cb(eloop, SIGINT, sig_generic, eloop);
76         if (ret) {
77                 ev_eloop_unregister_signal_cb(eloop, SIGTERM,
78                                                 sig_generic, eloop);
79                 ev_eloop_unref(eloop);
80                 return ret;
81         }
82
83         *out = eloop;
84         return 0;
85 }
86
87 static void test_fail(int ret)
88 {
89         if (ret)
90                 log_err("init failed, errno %d: %s", ret, strerror(-ret));
91 }
92
93 static void test_exit(struct ev_eloop *eloop)
94 {
95         ev_eloop_unregister_signal_cb(eloop, SIGINT, sig_generic, eloop);
96         ev_eloop_unregister_signal_cb(eloop, SIGTERM, sig_generic, eloop);
97         ev_eloop_unref(eloop);
98 }