shithub: scc

Download patch

ref: 23cb5f4c52263967dfa971b8912f6a2fcbef02a2
parent: e0701f28ae8920a135298ea06a2d493732770c65
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat Aug 25 06:35:43 EDT 2018

[lib/c] Add posix support for signal

We need a specific sys.h header that depends of every target.

--- a/lib/c/_fpopen.c
+++ b/lib/c/_fpopen.c
@@ -2,6 +2,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys.h>
 #include "syscall.h"
 #include "libc.h"
 #undef fopen
--- a/lib/c/libc.h
+++ b/lib/c/libc.h
@@ -3,12 +3,3 @@
                      const char * restrict mode,
                      FILE * restrict fp);
 #endif
-
-/* TODO: Move to system headers */
-#define O_RDONLY  0x00000000
-#define O_WRONLY  0x00000001
-#define O_RDWR    0x00000002
-
-#define O_TRUNC   0x00000400
-#define O_APPEND  0x00000008
-#define O_CREAT   0x00000200
--- a/lib/c/target/amd64-sysv-linux/.gitignore
+++ b/lib/c/target/amd64-sysv-linux/.gitignore
@@ -6,5 +6,5 @@
 _lseek.s
 _open.s
 _read.s
-_rt_sigaction.s
+_sigaction.s
 _write.s
--- a/lib/c/target/amd64-sysv-linux/Makefile
+++ b/lib/c/target/amd64-sysv-linux/Makefile
@@ -4,7 +4,7 @@
 include $(PROJECTDIR)/rules.mk
 
 SYSNAME    = amd64-sysv-linux
-SCC_CFLAGS = -nostdinc -I$(INCDIR) -I$(INCDIR)/bits/amd64-sysv/
+SCC_CFLAGS = -nostdinc -I$(INCDIR) -I$(INCDIR)/bits/amd64-sysv/ -I.
 SYSOBJ = raise.o signal.o
 
 include syscall.mk
--- /dev/null
+++ b/lib/c/target/amd64-sysv-linux/sys.h
@@ -1,0 +1,20 @@
+
+#define O_RDONLY  0x00000000
+#define O_WRONLY  0x00000001
+#define O_RDWR    0x00000002
+
+#define O_TRUNC   0x00000400
+#define O_APPEND  0x00000008
+#define O_CREAT   0x00000200
+
+typedef int pid_t;
+
+struct sigaction {
+	void (*sa_handler)(int);
+	int sa_mask;
+	int sa_flags;
+};
+
+extern pid_t _getpid(void);
+extern int _kill(pid_t pid, int signum);
+extern int _sigaction(int sig, struct sigaction *new, struct sigaction *old);
--- a/lib/c/target/amd64-sysv-linux/syscall.lst
+++ b/lib/c/target/amd64-sysv-linux/syscall.lst
@@ -1,10 +1,11 @@
 #number	name
-0	_read
-1	_write
-2	_open
-3	_close
-3	_lseek
-12	_brk
-38	_getpid
-60	_Exit
-32	_kill
+1	_Exit
+3	_read
+4	_write
+5	_open
+6	_close
+17	_brk
+20	_getpid
+37	_kill
+46	_sigaction
+199	_lseek
--- a/lib/c/target/amd64-sysv-linux/syscall.mk
+++ b/lib/c/target/amd64-sysv-linux/syscall.mk
@@ -1,1 +1,1 @@
-SYSCALL = _read.o _write.o _open.o _close.o _lseek.o _brk.o _getpid.o _Exit.o _kill.o 
+SYSCALL = _Exit.o _read.o _write.o _open.o _close.o _brk.o _getpid.o _kill.o _sigaction.o _lseek.o 
--- a/lib/c/target/amd64-sysv-netbsd/.gitignore
+++ b/lib/c/target/amd64-sysv-netbsd/.gitignore
@@ -7,3 +7,4 @@
 _open.s
 _read.s
 _write.s
+_sigaction
--- a/lib/c/target/amd64-sysv-netbsd/Makefile
+++ b/lib/c/target/amd64-sysv-netbsd/Makefile
@@ -4,7 +4,7 @@
 include $(PROJECTDIR)/rules.mk
 
 SYSNAME    = amd64-sysv-netbsd
-SCC_CFLAGS = -nostdinc -I$(INCDIR) -I$(INCDIR)/bits/amd64-sysv/
+SCC_CFLAGS = -nostdinc -I$(INCDIR) -I$(INCDIR)/bits/amd64-sysv/ -I.
 SYSOBJ = raise.o signal.o
 
 include syscall.mk
--- /dev/null
+++ b/lib/c/target/amd64-sysv-netbsd/sys.h
@@ -1,0 +1,20 @@
+
+#define O_RDONLY  0x00000000
+#define O_WRONLY  0x00000001
+#define O_RDWR    0x00000002
+
+#define O_TRUNC   0x00000400
+#define O_APPEND  0x00000008
+#define O_CREAT   0x00000200
+
+typedef int pid_t;
+
+struct sigaction {
+	void (*sa_handler)(int);
+	int sa_mask;
+	int sa_flags;
+};
+
+extern pid_t _getpid(void);
+extern int _kill(pid_t pid, int signum);
+extern int _sigaction(int sig, struct sigaction *new, struct sigaction *old);
--- a/lib/c/target/amd64-sysv-netbsd/syscall.lst
+++ b/lib/c/target/amd64-sysv-netbsd/syscall.lst
@@ -8,3 +8,4 @@
 20	_getpid
 37	_kill
 199	_lseek
+340	_sigaction
--- a/lib/c/target/amd64-sysv-netbsd/syscall.mk
+++ b/lib/c/target/amd64-sysv-netbsd/syscall.mk
@@ -1,1 +1,1 @@
-SYSCALL = _Exit.o _read.o _write.o _open.o _close.o _brk.o _getpid.o _kill.o _lseek.o 
+SYSCALL = _Exit.o _read.o _write.o _open.o _close.o _brk.o _getpid.o _kill.o _sigaction.o _lseek.o 
--- /dev/null
+++ b/lib/c/target/amd64-sysv-openbsd/.gitignore
@@ -1,0 +1,10 @@
+_Exit.s
+_brk.s
+_close.s
+_getpid.s
+_kill.s
+_lseek.s
+_open.s
+_read.s
+_sigaction.s
+_write.s
--- a/lib/c/target/amd64-sysv-openbsd/Makefile
+++ b/lib/c/target/amd64-sysv-openbsd/Makefile
@@ -4,7 +4,7 @@
 include $(PROJECTDIR)/rules.mk
 
 SYSNAME    = amd64-sysv-netbsd
-SCC_CFLAGS = -nostdinc -I$(INCDIR) -I$(INCDIR)/bits/amd64-sysv/
+SCC_CFLAGS = -nostdinc -I$(INCDIR) -I$(INCDIR)/bits/amd64-sysv/ -I.
 SYSOBJ = raise.o
 
 include syscall.mk
--- /dev/null
+++ b/lib/c/target/amd64-sysv-openbsd/sys.h
@@ -1,0 +1,20 @@
+
+#define O_RDONLY  0x00000000
+#define O_WRONLY  0x00000001
+#define O_RDWR    0x00000002
+
+#define O_TRUNC   0x00000400
+#define O_APPEND  0x00000008
+#define O_CREAT   0x00000200
+
+typedef int pid_t;
+
+struct sigaction {
+	void (*sa_handler)(int);
+	int sa_mask;
+	int sa_flags;
+};
+
+extern pid_t _getpid(void);
+extern int _kill(pid_t pid, int signum);
+extern int _sigaction(int sig, struct sigaction *new, struct sigaction *old);
--- a/lib/c/target/amd64-sysv-openbsd/syscall.lst
+++ b/lib/c/target/amd64-sysv-openbsd/syscall.lst
@@ -1,13 +1,11 @@
 #number	name
-0	_read
-1	_write
-2	_open
-3	_close
-3	_lseek
-12	_brk
-13	_rt_sigaction
-38	_getpid
-60	_Exit
-32	_kill
-39	_getpid
-62	_kill
+1	_Exit
+3	_read
+4	_write
+5	_open
+6	_close
+17	_brk
+20	_getpid
+37	_kill
+46	_sigaction
+199	_lseek
--- a/lib/c/target/amd64-sysv-openbsd/syscall.mk
+++ b/lib/c/target/amd64-sysv-openbsd/syscall.mk
@@ -1,1 +1,1 @@
-SYSCALL = _read.o _write.o _open.o _close.o _lseek.o _brk.o _rt_sigaction.o _getpid.o _Exit.o _kill.o _getpid.o _kill.o 
+SYSCALL = _Exit.o _read.o _write.o _open.o _close.o _brk.o _getpid.o _kill.o _sigaction.o _lseek.o 
--- a/lib/c/target/posix/raise.c
+++ b/lib/c/target/posix/raise.c
@@ -1,14 +1,9 @@
 
 #include <stddef.h>
 #include <signal.h>
-#include "../../syscall.h"
+#include <sys.h>
 
 #undef raise
-
-/* TODO: Add sys.h and store these definitions there */
-
-extern int _getpid(void);
-extern int _kill(int pid, int signum);
 
 int
 raise(int signum)
--- a/lib/c/target/posix/signal.c
+++ b/lib/c/target/posix/signal.c
@@ -1,9 +1,22 @@
 
+#include <stddef.h>
 #include <signal.h>
+#include <sys.h>
 
+#include "../../syscall.h"
+
 #undef signal
 
 void
 (*signal(int signum, void (*func)(int)))(int)
 {
+	struct sigaction sa = {
+		.sa_handler = func,
+		.sa_flags = 0,
+	};
+
+	if (_sigaction(signum, &sa, &sa) < 0)
+		return SIG_ERR;
+
+	return sa.sa_handler;
 }
--- a/rootdir/include/scc/bits/amd64-sysv/arch/signal.h
+++ b/rootdir/include/scc/bits/amd64-sysv/arch/signal.h
@@ -1,8 +1,8 @@
 typedef int sig_atomic_t;
 
-#define SIG_ERR    -1
-#define SIG_DFL     0
-#define SIG_IGN     1
+#define SIG_ERR    ((void (*)(int))-1)
+#define SIG_DFL    ((void (*)(int)) 0)
+#define SIG_IGN    ((void (*)(int)) 1)
 
 #define SIGHUP      1
 #define SIGINT      2