OrionLibC
Barry Updating libc for pipes and signals ad9b6af (2 years, 2 months ago)diff --git a/include/errno.h b/include/errno.h index b47e18c..614f13d 100644 --- a/include/errno.h +++ b/include/errno.h @@ -21,6 +21,9 @@ enum { ENOMEM, ECHILD, ENOTTY, + ELOOP, + ENAMETOOLONG, + EINTR, }; extern int errno; diff --git a/include/signal.h b/include/signal.h index 3999125..84535e8 100644 --- a/include/signal.h +++ b/include/signal.h @@ -11,6 +11,15 @@ typedef __sigset_t sigset_t; typedef int sig_atomic_t; #endif +typedef void (*sighandler_t)(int); + +#define SIG_ERR (sighandler_t) -1 +#define SIG_IGNORE 0 +#define SIG_DFL 1 +#define SIG_BLOCK 2 +#define SIG_UNBLOCK 3 +#define SIG_SETMASK 4 + #ifdef __cplusplus extern "C" { #endif @@ -25,12 +34,12 @@ enum Signals { SIGKILL, SIGSEGV, SIGPIPE, - }; -void (*signal(int sig, void (*func)(int)))(int); int tgkill(pid_t tgid, pid_t tid, int sig); int kill(pid_t pid, int sig); +sighandler_t signal(int signum, sighandler_t handler); +int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); #ifdef __cplusplus } diff --git a/include/sys/sched.h b/include/sys/sched.h index fe17753..2e3013f 100644 --- a/include/sys/sched.h +++ b/include/sys/sched.h @@ -5,13 +5,14 @@ /* Flags for clone syscall */ enum CloneFlag { - CLONE_NONE = (1 << 0), - CLONE_PARENT = (1 << 1), - CLONE_THREAD = (1 << 2), - CLONE_FILES = (1 << 3), - CLONE_FS = (1 << 4), - CLONE_VM = (1 << 5), - CLONE_IPC = (1 << 6), // TODO: Add IPC namespace + CLONE_NONE = (1 << 0), + CLONE_PARENT = (1 << 1), + CLONE_THREAD = (1 << 2), + CLONE_FILES = (1 << 3), + CLONE_FS = (1 << 4), + CLONE_VM = (1 << 5), + CLONE_SIGHAND = (1 << 6), + CLONE_IPC = (1 << 7), // TODO: Add IPC namespace }; pid_t clone(int flags); diff --git a/include/sys/syscall.h b/include/sys/syscall.h index d732c59..d34101a 100644 --- a/include/sys/syscall.h +++ b/include/sys/syscall.h @@ -19,6 +19,8 @@ enum SystemCall { SYSCALL_WAITPID, SYSCALL_TGKILL, SYSCALL_KILL, + SYSCALL_SIGNAL, + SYSCALL_SIGPROCMASK, SYSCALL_TIME, SYSCALL_TIMES, SYSCALL_SLEEP, @@ -40,6 +42,7 @@ enum SystemCall { SYSCALL_DUP, SYSCALL_DUP2, SYSCALL_ISATTY, + SYSCALL_PIPE, /* File System */ SYSCALL_MOUNT, diff --git a/include/sys/time.h b/include/sys/time.h new file mode 100644 index 0000000..e528978 --- /dev/null +++ b/include/sys/time.h @@ -0,0 +1,11 @@ +#ifndef _SYS_TIME_H +#define _SYS_TIME_H + +#include <sys/types.h> + +struct timeval { + time_t tv_sec; + suseconds_t tv_usec; +}; + +#endif diff --git a/include/sys/types.h b/include/sys/types.h index f8d727a..42ad07c 100644 --- a/include/sys/types.h +++ b/include/sys/types.h @@ -10,5 +10,7 @@ typedef int off_t; typedef int mode_t; typedef int nlink_t; typedef int refcount_t; +typedef unsigned int time_t; +typedef unsigned int suseconds_t; #endif diff --git a/include/time.h b/include/time.h index 24dee55..c325424 100644 --- a/include/time.h +++ b/include/time.h @@ -2,8 +2,7 @@ #define _TIME_H #include <stdint.h> - -typedef uint32_t time_t; +#include <sys/types.h> int sleep(uint32_t ms); time_t time(time_t *tloc); diff --git a/include/unistd.h b/include/unistd.h index c250e84..452cc36 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -31,9 +31,10 @@ int isatty(int fd); int close(int fd); int dup(int oldfd); int dup2(int oldfd, int newfd); -size_t read(int fd, void *buf, size_t count); -size_t write(int fd, void *buf, size_t count); +int read(int fd, void *buf, size_t count); +int write(int fd, void *buf, size_t count); off_t lseek(int fd, off_t offset, int whence); +int pipe(int pipefd[2]); int rmdir(const char *pathname); int chroot(const char *path); int chdir(const char *path); diff --git a/signal/signal.c b/signal/signal.c new file mode 100644 index 0000000..f2495e8 --- /dev/null +++ b/signal/signal.c @@ -0,0 +1,16 @@ +#include <sys/syscall.h> +#include <signal.h> +#include <errno.h> + +/* Send a signal to a process */ +sighandler_t +signal(int signum, sighandler_t handler) +{ + sighandler_t ret; + asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_SIGNAL), + "c" (2), "S" (&signum)); + if ((int) ret >= 0) + return ret; + errno = -((int) ret); + return SIG_ERR; +} diff --git a/signal/sigprocmask.c b/signal/sigprocmask.c new file mode 100644 index 0000000..0522a36 --- /dev/null +++ b/signal/sigprocmask.c @@ -0,0 +1,16 @@ +#include <sys/syscall.h> +#include <signal.h> +#include <errno.h> + +/* Send a signal to a process */ +int +sigprocmask(int how, const sigset_t *set, sigset_t *oldset) +{ + int ret; + asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_SIGPROCMASK), + "c" (3), "S" (&how)); + if (ret >= 0) + return ret; + errno = -ret; + return -1; +} diff --git a/stdio/perror.c b/stdio/perror.c index c852582..85b20ab 100644 --- a/stdio/perror.c +++ b/stdio/perror.c @@ -22,6 +22,9 @@ const char *errorList[] = { [ENOMEM] = "Cannot allocate memory", [ECHILD] = "No child processes", [ENOTTY] = "Inappropriate ioctl for device", + [ELOOP] = "Too many levels of symbolic links", + [ENAMETOOLONG] = "File name too long", + [EINTR] = "Interrupted system call", }; /* Display an error message for errno */ diff --git a/unistd/pipe.c b/unistd/pipe.c new file mode 100644 index 0000000..1143262 --- /dev/null +++ b/unistd/pipe.c @@ -0,0 +1,18 @@ +#include <sys/syscall.h> +#include <errno.h> + +/* Close a file */ +int +pipe(int pipefd[2]) +{ + int *pfd = pipefd; + + int ret; + asm volatile("int $0x80" : "=a" (ret) : "0" (SYSCALL_PIPE), + "c" (1), "S" (&pfd)); + + if (ret >= 0) + return ret; + errno = -ret; + return -1; +}