Additional system calls, can run bin/sh and use system quite reasonably now
authorNick Downing <downing.nick@gmail.com>
Mon, 30 Jan 2017 13:09:26 +0000 (00:09 +1100)
committerNick Downing <downing.nick@gmail.com>
Mon, 30 Jan 2017 13:09:26 +0000 (00:09 +1100)
18 files changed:
bin/tp/tp3.c
lib/libc/linux/sys/Makefile
lib/libc/linux/sys/chdir.c [new file with mode: 0644]
lib/libc/linux/sys/chroot.c [new file with mode: 0644]
lib/libc/linux/sys/execve.c [new file with mode: 0644]
lib/libc/linux/sys/fork.c [new file with mode: 0644]
lib/libc/linux/sys/getrusage.c [new file with mode: 0644]
lib/libc/linux/sys/lseek.c
lib/libc/linux/sys/readv.c
lib/libc/linux/sys/sigvec.c
lib/libc/linux/sys/utimes.c [new file with mode: 0644]
lib/libc/linux/sys/vopen.c
lib/libc/linux/sys/wait.c [new file with mode: 0644]
lib/libc/linux/sys/writev.c
lib/libc/sys/execl.c
lib/libc/sys/execle.c
lib/libc/sys/execv.c
n.sh

index 4f71a4e..d16a119 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <sys/file.h>
+#include <sys/stat.h>
 #include <time.h>
 #include "tp.h"
 
index 6f1e8eb..9dfb86b 100644 (file)
@@ -7,19 +7,22 @@ LD=ld
 #      @(#)Makefile    5.4 (Berkeley) 9/5/85
 #
 
-SRCS=  chmod.c chown.c close.c dup2.c dup.c _exit.c fchmod.c fchown.c \
-       fstat.c ftruncate.c getdtablesize.c getegid.c geteuid.c getgid.c \
-       getpagesize.c getpgrp.c getpid.c getppid.c gettimeofday.c getuid.c \
-       ioctl.c link.c linux.c lseek.c lstat.c read.c readv.c sbrk.c \
-       setpgrp.c setregid.c setreuid.c settimeofday.c sigvec.c stat.c \
-       symlink.c sync.c truncate.c umask.c unlink.c vopen.c write.c writev.c
-
-OBJS=  chmod.o chown.o close.o dup2.o dup.o _exit.o fchmod.o fchown.o \
-       fstat.o ftruncate.o getdtablesize.o getegid.o geteuid.o getgid.o \
-       getpagesize.o getpgrp.o getpid.o getppid.o gettimeofday.o getuid.o \
-       ioctl.o link.o linux.o lseek.o lstat.o read.o readv.o sbrk.o \
-       setpgrp.o setregid.o setreuid.o settimeofday.o sigvec.o stat.o \
-       symlink.o sync.o truncate.o umask.o unlink.o vopen.o write.o writev.o
+SRCS=  chdir.c chmod.c chown.c chroot.c close.c dup2.c dup.c execve.c \
+       _exit.c fchmod.c fchown.c fork.c fstat.c ftruncate.c getdtablesize.c \
+       getegid.c geteuid.c getgid.c getpagesize.c getpgrp.c getpid.c \
+       getppid.c getrusage.c gettimeofday.c getuid.c ioctl.c link.c linux.c \
+       lseek.c lstat.c read.c readv.c sbrk.c setpgrp.c setregid.c \
+       setreuid.c settimeofday.c sigvec.c stat.c symlink.c sync.c \
+       truncate.c umask.c unlink.c utimes.c vopen.c wait.c write.c writev.c
+
+OBJS=  chdir.o chmod.o chown.o chroot.o close.o dup2.o dup.o execve.o \
+       _exit.o fchmod.o fchown.o fork.o fstat.o ftruncate.o getdtablesize.o \
+       getegid.o geteuid.o getgid.o getpagesize.o getpgrp.o getpid.o \
+       getppid.o getrusage.o gettimeofday.o getuid.o ioctl.o link.o linux.o \
+       lseek.o lstat.o read.o readv.o sbrk.o setpgrp.o setregid.o \
+       setreuid.o settimeofday.o sigvec.o stat.o symlink.o sync.o \
+       truncate.o umask.o unlink.o utimes.o vopen.o wait.o write.o writev.o
+
 TAGSFILE=tags
 
 .c.o:
diff --git a/lib/libc/linux/sys/chdir.c b/lib/libc/linux/sys/chdir.c
new file mode 100644 (file)
index 0000000..082ae6f
--- /dev/null
@@ -0,0 +1,14 @@
+#include <nox_errno.h>
+#include <nox_unistd.h>
+
+#include <errno.h>
+#include <sys/proc.h>
+#include "linux.h"
+
+int chdir(s) char *s; {
+       if (nox_chdir(s)) {
+               errno = htot_errno(nox_errno);
+               return -1;
+       }
+       return 0;
+}
diff --git a/lib/libc/linux/sys/chroot.c b/lib/libc/linux/sys/chroot.c
new file mode 100644 (file)
index 0000000..b20e086
--- /dev/null
@@ -0,0 +1,14 @@
+#include <nox_errno.h>
+#include <nox_unistd.h>
+
+#include <errno.h>
+#include <sys/proc.h>
+#include "linux.h"
+
+int chroot(s) char *s; {
+       if (nox_chroot(s)) {
+               errno = htot_errno(nox_errno);
+               return -1;
+       }
+       return 0;
+}
diff --git a/lib/libc/linux/sys/execve.c b/lib/libc/linux/sys/execve.c
new file mode 100644 (file)
index 0000000..982d290
--- /dev/null
@@ -0,0 +1,7 @@
+#include <nox_unistd.h>
+
+#include <sys/exec.h>
+
+void execve(s, v, e) char *s; char *v[]; char *e[]; {
+       nox_execve(s, v, e);
+}
diff --git a/lib/libc/linux/sys/fork.c b/lib/libc/linux/sys/fork.c
new file mode 100644 (file)
index 0000000..1c2a6f9
--- /dev/null
@@ -0,0 +1,16 @@
+#include <nox_errno.h>
+#include <nox_unistd.h>
+
+#include <sys/errno.h>
+#include <sys/proc.h>
+#include <sys/types.h>
+#include "linux.h"
+
+int fork() {
+       nox_pid_t res;
+       
+       res = nox_fork();
+       if (res == (nox_pid_t)-1)
+               errno = htot_errno(nox_errno);
+       return (int)res;
+}
diff --git a/lib/libc/linux/sys/getrusage.c b/lib/libc/linux/sys/getrusage.c
new file mode 100644 (file)
index 0000000..c0a308b
--- /dev/null
@@ -0,0 +1,48 @@
+#include <nox_errno.h>
+#include <nox_stdlib.h>
+#include <sys/nox_resource.h>
+#include <nox_unistd.h>
+
+#include <sys/errno.h>
+#include <sys/resource.h>
+#include "linux.h"
+
+int getrusage(res, rip) int res; struct rusage *rip; {
+       nox_int who;
+       struct nox_rusage ru;
+
+       switch (res) {
+       case RUSAGE_SELF:
+               who = nox_RUSAGE_SELF;
+               break;
+       case RUSAGE_CHILDREN:
+               who = nox_RUSAGE_CHILDREN;
+               break;
+       default:
+               nox_write((nox_int)2, "getrusage(): invalid who\n", 25);
+               nox_abort();
+       }
+       if (nox_getrusage(who, &ru)) {
+               errno = htot_errno(nox_errno);
+               return -1;
+       }
+       rip->ru_utime.tv_sec = (time_t)ru.nox_ru_utime.nox_tv_sec;
+       rip->ru_utime.tv_usec = (time_t)ru.nox_ru_utime.nox_tv_usec;
+       rip->ru_stime.tv_sec = (time_t)ru.nox_ru_stime.nox_tv_sec;
+       rip->ru_stime.tv_usec = (time_t)ru.nox_ru_stime.nox_tv_usec;
+       rip->ru_maxrss = (long)ru.nox_ru_maxrss;
+       rip->ru_ixrss = (long)ru.nox_ru_ixrss;
+       rip->ru_idrss = (long)ru.nox_ru_idrss;
+       rip->ru_isrss = (long)ru.nox_ru_isrss;
+       rip->ru_minflt = (long)ru.nox_ru_minflt;
+       rip->ru_majflt = (long)ru.nox_ru_majflt;
+       rip->ru_nswap = (long)ru.nox_ru_nswap;
+       rip->ru_inblock = (long)ru.nox_ru_inblock;
+       rip->ru_oublock = (long)ru.nox_ru_oublock;
+       rip->ru_msgsnd = (long)ru.nox_ru_msgsnd;
+       rip->ru_msgrcv = (long)ru.nox_ru_msgrcv;
+       rip->ru_nsignals = (long)ru.nox_ru_nsignals;
+       rip->ru_nvcsw = (long)ru.nox_ru_nvcsw;
+       rip->ru_nivcsw = (long)ru.nox_ru_nivcsw;
+       return 0;
+}
index dee8b21..93ce8ac 100644 (file)
@@ -1,8 +1,8 @@
 #include <nox_errno.h>
+#include <nox_stdlib.h>
 #include <nox_unistd.h>
 #include <sys/nox_types.h>
 
-#include <gen.h>
 #include <sys/errno.h>
 #include <sys/file.h>
 #include <sys/types.h>
@@ -23,8 +23,8 @@ off_t lseek(f, o, d) int f; off_t o; int d; {
                whence = nox_SEEK_END;
                break;
        default:
-               write(2, "lseek(): invalid whence\n", 24);
-               abort();
+               nox_write((nox_int)2, "lseek(): invalid whence\n", 24);
+               nox_abort();
        }
        res = nox_lseek(f, (nox_off_t)o, whence);
        if (res == (nox_off_t)-1)
index 3346c19..a4f369e 100644 (file)
@@ -1,8 +1,8 @@
 #include <nox_errno.h>
+#include <nox_stdlib.h>
 #include <nox_unistd.h>
 #include <sys/nox_uio.h>
 
-#include <gen.h>
 #include <sys/errno.h>
 #include <sys/file.h>
 #include <sys/uio.h>
 
 int readv(f, v, l) int f; struct iovec *v; int l; {
        int i;
-       struct nox_iovec iov[8];
+       struct nox_iovec iov[16];
        nox_int res;
 
-       if (l > 8) {
-               write(2, "readv(): iovec too large\n", 25);
-               abort();
+       if (l > 16) {
+               nox_write((nox_int)2, "readv(): iovec too large\n", 25);
+               nox_abort();
        }
        for (i = 0; i < l; ++i) {
                iov[i].nox_iov_base = v[i].iov_base;
index d50c513..69edf65 100644 (file)
@@ -1,7 +1,8 @@
 #include <nox_errno.h>
 #include <nox_signal.h>
+#include <nox_stdlib.h>
+#include <nox_unistd.h>
 
-#include <gen.h>
 #include <errno.h>
 #include <sys/file.h>
 #include <sys/signal.h>
@@ -17,12 +18,12 @@ int sigvec(c, f, m) int c; struct sigvec *f; struct sigvec *m; {
        if (f->sv_flags & SV_ONSTACK)
                act.nox_sa_flags |= nox_SA_ONSTACK;
        if (f->sv_flags & SV_INTERRUPT) {
-               write(2, "sigvec(): SV_INTERRUPT\n", 23);
-               abort();
+               nox_write((nox_int)2, "sigvec(): SV_INTERRUPT\n", 23);
+               nox_abort();
        }
        if (c < 0 || c >= NSIG) {
-               write(2, "sigvec(): out of range\n", 23);
-               abort();
+               nox_write((nox_int)2, "sigvec(): out of range\n", 23);
+               nox_abort();
        }
        if (nox_sigaction(ttoh_signo[c], &act, &oldact)) {
                errno = htot_errno(nox_errno);
diff --git a/lib/libc/linux/sys/utimes.c b/lib/libc/linux/sys/utimes.c
new file mode 100644 (file)
index 0000000..a90b80c
--- /dev/null
@@ -0,0 +1,21 @@
+#include <nox_errno.h>
+#include <sys/nox_time.h>
+#include <sys/nox_types.h>
+
+#include <errno.h>
+#include <sys/time.h>
+#include "linux.h"
+
+int utimes(f, t) char *f; struct timeval t[2]; {
+       struct nox_timeval tv[2];
+
+       tv[0].nox_tv_sec = (nox_time_t)t[0].tv_sec;
+       tv[0].nox_tv_usec = (nox_suseconds_t)t[0].tv_usec;
+       tv[1].nox_tv_sec = (nox_time_t)t[1].tv_sec;
+       tv[1].nox_tv_usec = (nox_suseconds_t)t[1].tv_usec;
+       if (nox_utimes(f, tv)) {
+               errno = htot_errno(nox_errno);
+               return -1;
+       }
+       return 0;
+}
index 6bbe5e5..1046130 100644 (file)
@@ -1,9 +1,9 @@
 #include <nox_errno.h>
 #include <nox_fcntl.h>
+#include <nox_stdlib.h>
 #include <sys/nox_stat.h>
 #include <nox_unistd.h>
 
-#include <gen.h>
 #include <sys/errno.h>
 #include <sys/file.h>
 #ifdef __STDC__
@@ -31,8 +31,8 @@ int vopen(f, m, argp) char *f; int m; va_list argp; {
                flags = nox_O_RDWR;
                break;
        default:
-               write(2, "vopen(): invalid flags\n", 23);
-               abort();
+               nox_write((nox_int)2, "vopen(): invalid flags\n", 23);
+               nox_abort();
        }
        if (m & O_NDELAY)
                flags |= nox_O_NDELAY;
diff --git a/lib/libc/linux/sys/wait.c b/lib/libc/linux/sys/wait.c
new file mode 100644 (file)
index 0000000..8979e36
--- /dev/null
@@ -0,0 +1,41 @@
+#include <nox_errno.h>
+#include <nox_stdlib.h>
+#include <sys/nox_types.h>
+#include <sys/nox_wait.h>
+#include <nox_unistd.h>
+
+#include <sys/errno.h>
+#include <sys/wait.h>
+#include "linux.h"
+
+int wait(s) int *s; {
+       nox_pid_t res;
+       nox_int stat;
+       union wait w;
+
+       res = nox_wait(&stat);
+       if (res == (nox_pid_t)-1) {
+               errno = htot_errno(nox_errno);
+               return -1;
+       }
+       if (nox_WIFEXITED(stat)) {
+               w.w_termsig = 0;
+               w.w_coredump = 0;
+               w.w_retcode = nox_WEXITSTATUS(stat);
+       }
+       else if (nox_WIFSIGNALED(stat)) {
+               w.w_termsig = htot_signo(nox_WTERMSIG(stat));
+               w.w_coredump = nox_WCOREDUMP(stat);
+               w.w_retcode = 0;
+       }
+       else if (nox_WIFSTOPPED(stat)) {
+               w.w_stopval = WSTOPPED;
+               w.w_stopsig = htot_signo(nox_WSTOPSIG(stat));
+       }
+       else {
+               nox_write((nox_int)2, "wait(): invalid status\n", 23);
+               nox_abort();
+       }
+       *s = w.w_status;
+       return (int)res;
+}
index 96edafa..bb6c521 100644 (file)
@@ -1,8 +1,8 @@
 #include <nox_errno.h>
+#include <nox_stdlib.h>
 #include <nox_unistd.h>
 #include <sys/nox_uio.h>
 
-#include <gen.h>
 #include <sys/errno.h>
 #include <sys/file.h>
 #include <sys/uio.h>
 
 int writev(f, v, l) int f; struct iovec *v; int l; {
        int i;
-       struct nox_iovec iov[8];
+       struct nox_iovec iov[16];
        nox_int res;
 
-       if (l > 8) {
-               write(2, "writev(): iovec too large\n", 26);
-               abort();
+       if (l > 16) {
+               nox_write((nox_int)2, "writev(): iovec too large\n", 26);
+               nox_abort();
        }
        for (i = 0; i < l; ++i) {
                iov[i].nox_iov_base = v[i].iov_base;
index 5e6bbb2..d5321df 100644 (file)
@@ -1,6 +1,4 @@
-#include <gen.h>
-/*#include <sys/exec.h> gen.h*/
-#include <sys/file.h>
+#include <sys/exec.h>
 #ifdef __STDC__
 #include <stdarg.h>
 #define _va_start(argp, arg) va_start(argp, arg)
@@ -9,13 +7,31 @@
 #define _va_start(argp, arg) va_start(argp)
 #endif
 
-/*#include <varargs.h>*/
+#ifdef __x86_64__
+#include <gen.h>
+#include <sys/file.h>
+#endif
+
 #ifdef __STDC__
 void execl(char *f, ...)
 #else
 void execl(f, va_alist) char *f; va_dcl
 #endif
 {
-       write(2, "execl()\n", 8);
-       abort();
+       va_list argp;
+#ifdef __x86_64__
+       int i;
+       char *argv[32];
+
+       _va_start(argp, f);
+       for (i = 0; (argv[i] = va_arg(argp, char *)) != 0; ++i)
+               if (i >= 32) {
+                       write(2, "execl(): too many arguments\n", 28);
+                       abort();
+               }
+       execv(f, argv);
+#else
+       _va_start(argp, f);
+       execv(f, (char **)argp);
+#endif
 }
index b883a6e..41673b8 100644 (file)
@@ -1,6 +1,4 @@
-#include <gen.h>
-/*#include <sys/exec.h> gen.h*/
-#include <sys/file.h>
+#include <sys/exec.h>
 #ifdef __STDC__
 #include <stdarg.h>
 #define _va_start(argp, arg) va_start(argp, arg)
@@ -9,13 +7,35 @@
 #define _va_start(argp, arg) va_start(argp)
 #endif
 
-/*#include <varargs.h>*/
+#ifdef __x86_64__
+#include <gen.h>
+#include <sys/file.h>
+#endif
+
 #ifdef __STDC__
 void execle(char *f, ...)
 #else
 void execle(f, va_alist) char *f; va_dcl
 #endif
 {
-       write(2, "execle()\n", 9);
-       abort();
+       va_list argp;
+#ifdef __x86_64__
+       int i;
+       char *argv[32];
+
+       _va_start(argp, f);
+       for (i = 0; (argv[i] = va_arg(argp, char *)) != 0; ++i)
+               if (i >= 32) {
+                       write(2, "execle(): too many arguments\n", 29);
+                       abort();
+               }
+#else
+       char **argv;
+
+       _va_start(argp, f);
+       argv = (char **)argp;
+       while (va_arg(argp, char *))
+               ;
+#endif
+       execve(f, argv, va_arg(argp, char **));
 }
index 94a4557..f1fd034 100644 (file)
@@ -1,8 +1,7 @@
-#include <gen.h>
-/*#include <sys/exec.h> gen.h*/
-#include <sys/file.h>
+#include <sys/exec.h>
+
+extern char **environ;
 
 void execv(s, v) char *s; char *v[]; {
-       write(2, "execv()\n", 8);
-       abort();
+       execve(s, v, environ);
 }
diff --git a/n.sh b/n.sh
index a3d723e..000db66 100755 (executable)
--- a/n.sh
+++ b/n.sh
@@ -26,6 +26,6 @@ mkdir -p lib/libc/ns/profiled
 mkdir -p lib/libc/stdio/profiled
 mkdir -p lib/libc/sys/profiled
 (cd lib/libc && make.sh clean && make.sh && make.sh install)
-(cd bin && make.sh clean && make.sh SUBDIR="as csh diff sed" NSTD= KMEM= && make.sh SUBDIR="as csh diff sed" NSTD= KMEM= install)
+(cd bin && make.sh clean && make.sh SUBDIR="as csh diff sed sh tp" NSTD= KMEM= && make.sh SUBDIR="as csh diff sed sh tp" NSTD= KMEM= install)
 
 #(cd test && make clean && make)