| 1 | /* |
| 2 | * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef LTTNG_TESTAPP_SIGNAL_HELPER_H |
| 9 | #define LTTNG_TESTAPP_SIGNAL_HELPER_H |
| 10 | |
| 11 | #include <signal.h> |
| 12 | #include <stdio.h> |
| 13 | |
| 14 | static volatile int should_quit; |
| 15 | |
| 16 | static void sighandler(int sig) |
| 17 | { |
| 18 | if (sig == SIGTERM) { |
| 19 | should_quit = 1; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | static int set_signal_handler() |
| 24 | { |
| 25 | int ret; |
| 26 | struct sigaction sa {}; |
| 27 | sa.sa_flags = 0; |
| 28 | sa.sa_handler = sighandler; |
| 29 | |
| 30 | ret = sigemptyset(&sa.sa_mask); |
| 31 | if (ret) { |
| 32 | perror("sigemptyset"); |
| 33 | goto end; |
| 34 | } |
| 35 | |
| 36 | ret = sigaction(SIGTERM, &sa, nullptr); |
| 37 | if (ret) { |
| 38 | perror("sigaction"); |
| 39 | goto end; |
| 40 | } |
| 41 | end: |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | #endif /* LTTNG_TESTAPP_SIGNAL_HELPER_H */ |