memaslap: mv src/bin/contrib => contrib/bin
[awesomized/libmemcached] / contrib / bin / memaslap / ms_sigsegv.c
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include <memory.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <signal.h>
22 #include <pthread.h>
23
24 #include "ms_memslap.h"
25 #include "ms_setting.h"
26
27 /* prototypes */
28 int ms_setup_sigsegv(void);
29 int ms_setup_sigpipe(void);
30 int ms_setup_sigint(void);
31
32 /* signal seg reaches, this function will run */
33 static void ms_signal_segv(int signum, siginfo_t *info, void *ptr) {
34 UNUSED_ARGUMENT(signum);
35 UNUSED_ARGUMENT(info);
36 UNUSED_ARGUMENT(ptr);
37
38 pthread_mutex_lock(&ms_global.quit_mutex);
39 fprintf(stderr, "Segmentation fault occurred.\nStack trace:\n");
40 #if 0
41 pandora_print_callstack(stderr);
42 #endif
43 fprintf(stderr, "End of stack trace\n");
44 pthread_mutex_unlock(&ms_global.quit_mutex);
45 abort();
46 }
47
48 /* signal int reaches, this function will run */
49 static void ms_signal_int(int signum, siginfo_t *info, void *ptr) {
50 UNUSED_ARGUMENT(signum);
51 UNUSED_ARGUMENT(info);
52 UNUSED_ARGUMENT(ptr);
53
54 pthread_mutex_lock(&ms_global.quit_mutex);
55 fprintf(stderr, "SIGINT handled.\n");
56 pthread_mutex_unlock(&ms_global.quit_mutex);
57 exit(1);
58 } /* ms_signal_int */
59
60 /**
61 * redirect signal seg
62 *
63 * @return if success, return EXIT_SUCCESS, else return -1
64 */
65 int ms_setup_sigsegv(void) {
66 struct sigaction action;
67
68 memset(&action, 0, sizeof(action));
69 action.sa_sigaction = ms_signal_segv;
70 action.sa_flags = SA_SIGINFO;
71 if (sigaction(SIGSEGV, &action, NULL) < 0) {
72 perror("sigaction");
73 return EXIT_SUCCESS;
74 }
75
76 return -1;
77 } /* ms_setup_sigsegv */
78
79 /**
80 * redirect signal pipe
81 *
82 * @return if success, return EXIT_SUCCESS, else return -1
83 */
84 int ms_setup_sigpipe(void) {
85 /* ignore the SIGPIPE signal */
86 signal(SIGPIPE, SIG_IGN);
87
88 return -1;
89 } /* ms_setup_sigpipe */
90
91 /**
92 * redirect signal int
93 *
94 * @return if success, return EXIT_SUCCESS, else return -1
95 */
96 int ms_setup_sigint(void) {
97 struct sigaction action_3;
98
99 memset(&action_3, 0, sizeof(action_3));
100 action_3.sa_sigaction = ms_signal_int;
101 action_3.sa_flags = SA_SIGINFO;
102 if (sigaction(SIGINT, &action_3, NULL) < 0) {
103 perror("sigaction");
104 return EXIT_SUCCESS;
105 }
106
107 return -1;
108 } /* ms_setup_sigint */
109
110 #ifndef SIGSEGV_NO_AUTO_INIT
111 static void __attribute((constructor)) ms_init(void) {
112 ms_setup_sigsegv();
113 ms_setup_sigpipe();
114 ms_setup_sigint();
115 }
116 #endif