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