diff --git a/build-aux/check-mescc.sh b/build-aux/check-mescc.sh index 83d67362..a78f13e7 100755 --- a/build-aux/check-mescc.sh +++ b/build-aux/check-mescc.sh @@ -139,6 +139,7 @@ t 92-stat 93-fread-fwrite 94-unsetenv +95-signal " # 90: needs GNU, fails for mescc, passes for tcc diff --git a/include/signal.h b/include/signal.h index 634e202e..90cb4acd 100644 --- a/include/signal.h +++ b/include/signal.h @@ -51,57 +51,57 @@ typedef int sigval_t; #endif #define NSIG 30 -#define SIGHUP 1 -#define SIGINT 2 -#define SIGQUIT 3 -#define SIGILL 4 -#define SIGTRAP 5 -#define SIGABRT 6 -#define SIGIOT 6 -#define SIGBUS 7 -#define SIGFPE 8 -#define SIGKILL 9 -#define SIGUSR1 10 -#define SIGSEGV 11 -#define SIGUSR2 12 -#define SIGPIPE 13 -#define SIGALRM 14 -#define SIGTERM 15 -#define SIGSTKFLT 16 -#define SIGCHLD 17 -#define SIGCONT 18 -#define SIGSTOP 19 -#define SIGTSTP 20 -#define SIGTTIN 21 -#define SIGTTOU 22 -#define SIGURG 23 -#define SIGXCPU 24 -#define SIGXFSZ 25 -#define SIGVTALRM 26 -#define SIGPROF 27 -#define SIGWINCH 28 -#define SIGIO 29 -#define SIGPOLL SIGIO +#define SIGHUP 1 +#define SIGINT 2 +#define SIGQUIT 3 +#define SIGILL 4 +#define SIGTRAP 5 +#define SIGABRT 6 +#define SIGIOT 6 +#define SIGBUS 7 +#define SIGFPE 8 +#define SIGKILL 9 +#define SIGUSR1 10 +#define SIGSEGV 11 +#define SIGUSR2 12 +#define SIGPIPE 13 +#define SIGALRM 14 +#define SIGTERM 15 +#define SIGSTKFLT 16 +#define SIGCHLD 17 +#define SIGCONT 18 +#define SIGSTOP 19 +#define SIGTSTP 20 +#define SIGTTIN 21 +#define SIGTTOU 22 +#define SIGURG 23 +#define SIGXCPU 24 +#define SIGXFSZ 25 +#define SIGVTALRM 26 +#define SIGPROF 27 +#define SIGWINCH 28 +#define SIGIO 29 +#define SIGPOLL SIGIO -#define FPE_INTDIV 1 -#define FPE_INTOVF 2 -#define FPE_FLTDIV 3 -#define FPE_FLTOVF 4 -#define FPE_FLTUND 5 -#define FPE_FLTRES 6 -#define FPE_FLTINV 7 -#define FPE_FLTSUB 8 +#define FPE_INTDIV 1 +#define FPE_INTOVF 2 +#define FPE_FLTDIV 3 +#define FPE_FLTOVF 4 +#define FPE_FLTUND 5 +#define FPE_FLTRES 6 +#define FPE_FLTINV 7 +#define FPE_FLTSUB 8 -#define SA_NOCLDSTOP 0x00000001 -#define SA_NOCLDWAIT 0x00000002 -#define SA_SIGINFO 0x00000004 -#define SA_ONSTACK 0x08000000 -#define SA_RESTART 0x10000000 -#define SA_NODEFER 0x40000000 -#define SA_RESETHAND 0x80000000 +#define SA_NOCLDSTOP 0x00000001 +#define SA_NOCLDWAIT 0x00000002 +#define SA_SIGINFO 0x00000004 +#define SA_ONSTACK 0x08000000 +#define SA_RESTART 0x10000000 +#define SA_NODEFER 0x40000000 +#define SA_RESETHAND 0x80000000 -#define SA_NOMASK SA_NODEFER -#define SA_ONESHOT SA_RESETHAND +#define SA_NOMASK SA_NODEFER +#define SA_ONESHOT SA_RESETHAND typedef struct siginfo_t { @@ -132,22 +132,25 @@ typedef struct siginfo_t { } siginfo_t; -// typedef void __signalfn_t(int); -// typedef __signalfn_t *__sighandler_t; -// typedef __signalfn_t *__sighandler_t; -typedef void __sighandler_t(int); +typedef void (*sighandler_t)(int); struct sigaction { - void (*sa_sigaction) (int, siginfo_t *, void *); - //__sighandler_t sa_handler; - void (*sa_handler) (int); + union { + void (*sa_sigaction) (int signum, siginfo_t *, void *); +#if __MESC__ + void (*sa_handler) (int); +#else + sighandler_t sa_handler; +#endif + }; unsigned long sa_flags; sigset_t sa_mask; }; -#define SIG_DFL ((__sighandler_t)0) /* default signal handling */ -#define SIG_IGN ((__sighandler_t)1) /* ignore signal */ -#define SIG_ERR ((__sighandler_t)-1) /* error return from signal */ + +#define SIG_DFL ((sighandler_t)0) +#define SIG_IGN ((sighandler_t)1) +#define SIG_ERR ((sighandler_t)-1) #ifdef __i386__ @@ -174,7 +177,7 @@ struct sigaction { typedef int greg_t; /* Number of general registers. */ -#define NGREG 19 +#define NGREG 19 /* Container for all general registers. */ typedef greg_t gregset_t[NGREG]; @@ -224,7 +227,13 @@ typedef struct ucontext } ucontext_t; #endif // !__i386__ +int kill (pid_t pid, int signum); int sigaction (int signum, struct sigaction const *act, struct sigaction *oldact); +#if __MESC__ +void* signal (int signum, void * action); +#else +sighandler_t signal (int signum, sighandler_t action); +#endif int sigemptyset (sigset_t *set); #endif //! WITH_GLIBC diff --git a/include/sys/stat.h b/include/sys/stat.h index e934c95f..38e13d5f 100644 --- a/include/sys/stat.h +++ b/include/sys/stat.h @@ -59,6 +59,7 @@ int chmod (char const *file_name, mode_t mode); int mkdir (char const *file_name, mode_t mode); int chown (char const *file_name, uid_t owner, gid_t group); int rmdir (char const *file_name); +int stat (char const *file_name, struct stat *buf); #define S_IFCHR 0020000 #define S_IFDIR 0040000 diff --git a/include/unistd.h b/include/unistd.h index a555146c..391b19a4 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -39,6 +39,12 @@ #define STDERR_FILENO 2 #endif // STDIN_FILENO +#ifndef STDIN_FILE_NO +#define STDIN_FILE_NO 0 +#define STDOUT_FILE_NO 1 +#define STDERR_FILE_NO 2 +#endif // STDIN_FILE_NO + #ifndef __MES_OFF_T #define __MES_OFF_T #undef off_t @@ -69,6 +75,12 @@ typedef long intptr_t; typedef long ptrdiff_t; #endif +#ifndef __MES_PID_T +#define __MES_PID_T +#undef pid_t +typedef int pid_t; +#endif + #ifndef R_OK #define F_OK 0 #define X_OK 1 @@ -98,6 +110,8 @@ void * sbrk (intptr_t delta); #endif int unlink (char const *file_name); ssize_t write (int filedes, void const *buffer, size_t size); +pid_t getpid (void); + #endif // ! WITH_GLIBC #endif // __MES_UNISTD_H diff --git a/lib/binutils.c b/lib/binutils.c index f8addd18..3f525c2c 100644 --- a/lib/binutils.c +++ b/lib/binutils.c @@ -98,13 +98,6 @@ getgid (int x) return 0; } -int -getpid (int x) -{ - eputs ("getpid stub\n"); - return 0; -} - int getuid (int x) { diff --git a/lib/linux+gnu.c b/lib/linux+gnu.c index 3c54feef..601efa17 100644 --- a/lib/linux+gnu.c +++ b/lib/linux+gnu.c @@ -19,11 +19,13 @@ */ #define SYS_link 0x09 +#define SYS_getpid 0x14 #define SYS_kill 0x25 #define SYS_rename 0x26 #define SYS_mkdir 0x27 #define SYS_dup 0x29 #define SYS_pipe 0x2a +#define SYS_signal 0x30 #define SYS_lstat 0x6b #define SYS_fstat 0x6c #define SYS_nanosleep 0xa2 @@ -34,6 +36,12 @@ link (char const *old_name, char const *new_name) return _sys_call2 (SYS_link, (int)old_name, (int)new_name); } +pid_t +getpid () +{ + return _sys_call (SYS_getpid); +} + int kill (pid_t pid, int signum) { @@ -58,6 +66,17 @@ dup (int old) return _sys_call1 (SYS_dup, (int)old); } +#if __MESC__ +void * +signal (int signum, void * action) +#else +sighandler_t +signal (int signum, sighandler_t action) +#endif +{ + return _sys_call2 (SYS_signal, signum, action); +} + int pipe (int filedes[2]) { diff --git a/lib/m4.c b/lib/m4.c index b4ff9c2e..1e13fe8e 100644 --- a/lib/m4.c +++ b/lib/m4.c @@ -160,13 +160,6 @@ setbuf (int x) return 0; } -int -signal (int x) -{ - eputs ("signal stub\n"); - return 0; -} - int system (int x) { diff --git a/scaffold/tests/95-signal.c b/scaffold/tests/95-signal.c new file mode 100644 index 00000000..7fe41d55 --- /dev/null +++ b/scaffold/tests/95-signal.c @@ -0,0 +1,46 @@ +/* -*-comment-start: "//";comment-end:""-*- + * Mes --- Maxwell Equations of Software + * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * + * This file is part of Mes. + * + * Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mes. If not, see . + */ + +#include +#include +#include +#include +#include + +int g_alarm_handled_p = 0; + +void +handler (int signum) +{ + eputs ("handle:"); eputs (itoa (signum)); eputs ("\n"); + if (signum != SIGALRM) + exit (1); + g_alarm_handled_p = 1; +} + +int +main (void) +{ + signal (SIGALRM, handler); + kill (getpid (), SIGALRM); + if (!g_alarm_handled_p) + return 2; + return 0; +}