Skip to content

Commit 55527e3

Browse files
committed
toolchain/newlib: configure to use our own POSIX signal implementation
Also implement BSD's killpg() which is defined but not implemented by newlib.
1 parent 29615ca commit 55527e3

5 files changed

Lines changed: 77 additions & 0 deletions

File tree

toolchain/newlib/port/newlib/configure.host

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ case "${host}" in
614614
;;
615615
*-*-dailyrun*)
616616
syscall_dir=syscalls
617+
newlib_cflags="${newlib_cflags} -DSIGNAL_PROVIDED"
617618
;;
618619
# RTEMS supplies its own versions of some routines:
619620
# malloc() (reentrant version)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
libc_a_SOURCES += %D%/syscalls.c
22
libc_a_SOURCES += %D%/isatty.c
3+
libc_a_SOURCES += %D%/killpg.c

toolchain/newlib/port/newlib/libc/sys/dailyrun/crt0.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1+
#include <errno.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <sys/signal.h>
15
#include <unistd.h>
26

37
extern int main(int argc, char **argv, char **envp);
48

59
extern char _edata;
610
extern char _end;
711

12+
/*
13+
* Signal handler trampoline.
14+
*
15+
* This function calls the actual signal handler and makes sure
16+
* the process exits from the signal correctly.
17+
*
18+
* Must be installed using sigsethandler() first before installing
19+
* custom signal handlers.
20+
*/
21+
static void handle_signal(int signal, siginfo_t *info, void *data)
22+
{
23+
ucontext_t *ucontext = data;
24+
sig_sa_sigaction_t handler = ucontext->sa_handler;
25+
26+
handler(signal, info, ucontext);
27+
sigreturn(ucontext);
28+
}
29+
830
void _start(int argc, char **argv, char **envp)
931
{
1032
char *bss;
@@ -15,6 +37,15 @@ void _start(int argc, char **argv, char **envp)
1537
while (bss < &_end)
1638
*bss++ = 0;
1739

40+
/* Install signal handler trampoline. */
41+
ret = sigsethandler(handle_signal);
42+
if (ret) {
43+
fprintf(stderr, "crt0: sigsethandler failed: %s\n", strerror(errno));
44+
goto out;
45+
}
46+
1847
ret = main(argc, argv, envp);
48+
49+
out:
1950
_exit(ret);
2051
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <sys/signal.h>
2+
#include <sys/types.h>
3+
4+
int killpg(pid_t pid, int signal)
5+
{
6+
return kill(-pid, signal);
7+
}

toolchain/newlib/port/newlib/libc/sys/dailyrun/syscalls.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <sys/time.h>
1818
#include <sys/times.h>
1919
#include <sys/types.h>
20+
#include <sys/signal.h>
2021

2122
char **environ; /* pointer to array of char * strings that define the current
2223
environment variables */
@@ -85,6 +86,24 @@ char **environ; /* pointer to array of char * strings that define the current
8586
return ret; \
8687
}
8788

89+
#define DEFINE_SYSCALL_4_default(_ret_type, _syscall, _nr, _type1, _type2, \
90+
_type3, _type4, ...) \
91+
_ret_type _##_syscall(_type1 arg1, _type2 arg2, _type3 arg3, _type4 arg4, \
92+
##__VA_ARGS__) \
93+
{ \
94+
_ret_type ret; \
95+
__asm__ volatile("int $0x80" \
96+
: "=a"(ret) \
97+
: "a"(_nr), "b"(arg1), "c"(arg2), "d"(arg3), \
98+
"S"(arg4) \
99+
: "memory"); \
100+
if (ret < 0) { \
101+
errno = (int)ret; \
102+
ret = (_ret_type) - 1; \
103+
} \
104+
return ret; \
105+
}
106+
88107
/*
89108
* Noreturn syscalls (exit)
90109
*/
@@ -113,6 +132,24 @@ pid_t _wait(int *status)
113132
return _waitpid(-1, status, 0);
114133
}
115134

135+
/*
136+
* signal() should not be used for anything else than setting the handler
137+
* to SIGDFL or SIGIGN. For other values follow the BSD semantics: the signal
138+
* is masked until delivered and handler (also the same as glibc).
139+
*/
140+
int _signal(int signo, sig_sa_handler_t handler)
141+
{
142+
struct sigaction sigaction = {
143+
.sa_handler = handler,
144+
.sa_flags = 0,
145+
};
146+
147+
sigemptyset(&sigaction.sa_mask);
148+
sigaddset(&sigaction.sa_mask, signo);
149+
150+
return _sigaction(signo, &sigaction, NULL);
151+
}
152+
116153
/*
117154
* Unimplemented syscalls
118155
*/

0 commit comments

Comments
 (0)