testing: parser
[awesomized/libmemcached] / test / setup.cpp
1 #include "mem_config.h"
2 #include "test/lib/env.hpp"
3 #include <cstdlib>
4 #include <cstdio>
5 #include <cstring>
6 #include <csignal>
7 #include <iostream>
8 #include <string>
9
10 static void sigchld(int, siginfo_t *si, void *) {
11 switch (si->si_code) {
12 case CLD_EXITED:
13 // expected
14 break;
15 case CLD_KILLED:
16 if (si->si_status == SIGKILL) {
17 // expected
18 break;
19 }
20 [[fallthrough]];
21 case CLD_DUMPED:
22 std::cerr << "Server{pid="
23 << std::to_string(si->si_pid)
24 << "} died: "
25 << strsignal(si->si_status)
26 << std::endl;
27 break;
28 }
29 }
30
31 static inline void setup_signals() {
32 struct sigaction sa;
33
34 memset(&sa, 0, sizeof(sa));
35 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART | SA_SIGINFO | SA_NODEFER;
36
37 sa.sa_sigaction = sigchld;
38 if (0 > sigaction(SIGCHLD, &sa, nullptr)) {
39 perror("sigaction(CHLD)");
40 }
41 }
42
43 #if HAVE_ASAN
44 // this has to be a macro for putenv() to work
45 # define ASAN_OPTIONS \
46 "check_initialization_order=1," \
47 "replace_str=1," \
48 "replace_intrin=1," \
49 "detect_stack_use_after_return=1," \
50 "alloc_dealloc_mismatch=1," \
51 "new_delete_type_mismatch=1," \
52 "detect_odr_violation=2," \
53 "halt_on_error=0," \
54 "verify_asan_link_order=1," \
55 "abort_on_error=0," \
56 ""
57 static inline void setup_asan(char **argv) {
58 const auto set = getenv("ASAN_OPTIONS");
59
60 if (!set || !*set) {
61 SET_ENV_EX(asan, "ASAN_OPTIONS", ASAN_OPTIONS, 0);
62 execvp(argv[0], argv);
63 perror("exec()");
64 }
65 }
66 #else
67 # define setup_asan(a) (void) a
68 #endif
69
70 #if LIBMEMCACHED_WITH_SASL_SUPPORT
71 static inline void setup_sasl() {
72 SET_ENV_EX(sasl_pwdb, "MEMCACHED_SASL_PWDB", LIBMEMCACHED_WITH_SASL_PWDB, 0);
73 SET_ENV_EX(sasl_conf, "SASL_CONF_PATH", LIBMEMCACHED_WITH_SASL_CONF, 0);
74 }
75 #else
76 # define setup_sasl()
77 #endif
78
79 int setup(int &, char ***argv) {
80 setup_signals();
81 setup_asan(*argv);
82 setup_sasl();
83
84 return 0;
85 }