Skip to content

Commit 7f08619

Browse files
committed
Add stdlib support for sigaction().
1 parent 1828c7e commit 7f08619

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

examples/stdlib.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
#define mprotect __hide__mprotect
109109
#define msync __hide__msync
110110
#define munmap __hide__munmap
111+
#define sigaction(a, b, c) __hide__sigaction(a, b, c)
111112
#define ioctl __hide__ioctl
112113
#define pipe __hide__pipe
113114
#define select __hide__select
@@ -275,6 +276,7 @@
275276
#undef mprotect
276277
#undef msync
277278
#undef munmap
279+
#undef sigaction
278280
#undef ioctl
279281
#undef pipe
280282
#undef select
@@ -1403,6 +1405,49 @@ static void *realloc_unlocked(void *ptr, size_t size)
14031405
return malloc_reallocate(ptr, size, /*lock=*/false);
14041406
}
14051407

1408+
/****************************************************************************/
1409+
/* SIGNAL */
1410+
/****************************************************************************/
1411+
1412+
struct ksigaction
1413+
{
1414+
void *sa_handler_2;
1415+
unsigned long sa_flags;
1416+
void (*sa_restorer)(void);
1417+
sigset_t sa_mask;
1418+
};
1419+
#define SA_RESTORER 0x04000000
1420+
1421+
static void signal_restorer(void)
1422+
{
1423+
(void)syscall(SYS_rt_sigreturn);
1424+
}
1425+
1426+
static int sigaction(int signum, const struct sigaction *act,
1427+
struct sigaction *oldact)
1428+
{
1429+
struct ksigaction kact, koldact;
1430+
if (act != NULL)
1431+
{
1432+
kact.sa_handler_2 = act->sa_handler;
1433+
memcpy(&kact.sa_mask, &act->sa_mask, sizeof(kact.sa_mask));
1434+
kact.sa_flags = act->sa_flags | SA_RESTORER;
1435+
kact.sa_restorer = signal_restorer;
1436+
}
1437+
int result = (int)syscall(SYS_rt_sigaction, signum, &kact, &koldact,
1438+
_NSIG / 8);
1439+
if (result < 0)
1440+
return result;
1441+
if (oldact != NULL)
1442+
{
1443+
oldact->sa_handler = koldact.sa_handler_2;
1444+
memcpy(&oldact->sa_mask, &koldact.sa_mask, sizeof(oldact->sa_mask));
1445+
oldact->sa_flags = (koldact.sa_flags & ~SA_RESTORER);
1446+
oldact->sa_restorer = NULL;
1447+
}
1448+
return result;
1449+
}
1450+
14061451
/****************************************************************************/
14071452
/* CTYPE */
14081453
/****************************************************************************/

0 commit comments

Comments
 (0)