Skip to content

Commit 2a521a9

Browse files
committed
util: add SIGINT handler
Add a helper for catching Ctrl-C from the user. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 2b2cbf7 commit 2a521a9

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

util/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ sources += [
66
'util/crc32.c',
77
'util/logging.c',
88
'util/mem.c',
9+
'util/sighdl.c',
910
'util/suffix.c',
1011
'util/types.c',
1112
'util/utils.c'

util/sighdl.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <signal.h>
3+
#include <errno.h>
4+
5+
#include "sighdl.h"
6+
7+
bool nvme_sigint_received;
8+
9+
static void nvme_sigint_handler(int signum)
10+
{
11+
nvme_sigint_received = true;
12+
}
13+
14+
int nvme_install_sigint_handler(void)
15+
{
16+
struct sigaction act;
17+
18+
sigemptyset(&act.sa_mask);
19+
act.sa_handler = nvme_sigint_handler;
20+
act.sa_flags = 0;
21+
22+
nvme_sigint_received = false;
23+
if (sigaction(SIGINT, &act, NULL) == -1)
24+
return -errno;
25+
26+
return 0;
27+
}

util/sighdl.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
#ifndef __NVME_SIGHDL
3+
#define __NVME_SIGHDL
4+
5+
#include <stdbool.h>
6+
7+
extern bool nvme_sigint_received;
8+
9+
int nvme_install_sigint_handler(void);
10+
11+
#endif // __NVME_SIGHDL

0 commit comments

Comments
 (0)