-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
78 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L | ||
#undef _POSIX_C_SOURCE | ||
#define _POSIX_C_SOURCE 200809L | ||
#endif | ||
#include <signal.h> | ||
|
||
#include "xsignal.h" | ||
#include <stdlib.h> | ||
|
||
struct xsignal | ||
{ | ||
sigset_t old_mask; | ||
sigset_t new_sigmask; | ||
}; | ||
|
||
|
||
struct xsignal *xsignal_new(void) | ||
{ | ||
struct xsignal *x = malloc(sizeof(*x)); | ||
sigemptyset(&x->new_sigmask); | ||
sigaddset(&x->new_sigmask, SIGINT); | ||
sigaddset(&x->new_sigmask, SIGTERM); | ||
sigprocmask(SIG_BLOCK, &x->new_sigmask, &x->old_mask); | ||
return x; | ||
} | ||
|
||
bool xsignal_interrupted(struct xsignal *x) | ||
{ | ||
sigset_t sigpend; | ||
int signal = 0; | ||
|
||
sigemptyset(&sigpend); | ||
sigpending(&sigpend); | ||
|
||
if (sigismember(&sigpend, SIGINT) || sigismember(&sigpend, SIGTERM)) | ||
{ | ||
if (sigwait(&x->new_sigmask, &signal) == 0) return true; | ||
} | ||
return false; | ||
} | ||
|
||
void xsignal_del(struct xsignal *x) | ||
{ | ||
sigprocmask(SIG_SETMASK, &x->old_mask, NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef XSIGNAL_H | ||
#define XSIGNAL_H | ||
|
||
#include <stdbool.h> | ||
|
||
struct xsignal; | ||
|
||
struct xsignal *xsignal_new(void); | ||
bool xsignal_interrupted(struct xsignal *); | ||
void xsignal_del(struct xsignal *); | ||
|
||
#endif |