summaryrefslogtreecommitdiffstats
path: root/usr/klibc/sigaction.c
blob: 9481750857db906fb6d1a4147af9e7b25b46fac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * sigaction.c
 */

#include <signal.h>
#include <stddef.h>
#include <sys/syscall.h>
#include <klibc/sysconfig.h>

__extern void __sigreturn(void);

#if _KLIBC_NEEDS_SIGACTION_FIXUP
typedef struct sigaction *act_type;
#else
typedef const struct sigaction *act_type;
#endif

__extern int __rt_sigaction(int, act_type, struct sigaction *, size_t);

int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
{
	unsigned int needed_flags = 0
#if _KLIBC_NEEDS_SA_RESTORER
		| SA_RESTORER
#endif
#if _KLIBC_NEEDS_SA_SIGINFO
		| SA_SIGINFO
#endif
		;
	struct sigaction sa;
	int rv;

	if (act &&
	    ((act->sa_flags & needed_flags) != needed_flags ||
	     _KLIBC_NEEDS_SIGACTION_FIXUP)) {
		sa = *act;
		sa.sa_flags |= needed_flags;
#if _KLIBC_NEEDS_SA_RESTORER
		if (!(act->sa_flags & SA_RESTORER))
			sa.sa_restorer = &__sigreturn;
#endif
		act = &sa;
	}

	/* Check that we have the right signal API definitions */
	(void)sizeof(char[_NSIG >= 64 ? 1 : -1]);
	(void)sizeof(char[sizeof(sigset_t) * 8 >= _NSIG ? 1 : -1]);
	(void)sizeof(char[offsetof(struct sigaction, sa_mask)
			  + sizeof(sigset_t) == sizeof(struct sigaction)
			  ? 1 : -1]);

	rv = __rt_sigaction(sig, (act_type)act, oact, sizeof(sigset_t));

#if _KLIBC_NEEDS_SA_RESTORER
	if (oact && (oact->sa_restorer == &__sigreturn)) {
		oact->sa_flags &= ~SA_RESTORER;
	}
#endif

	return rv;
}