From 2508de40ea873aa2c8be6e5fae1e05d65cebcb0c Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Thu, 5 Jan 2017 03:02:56 +0100 Subject: [PATCH] kernel: move sysdup to libc --- arch/amd64/include/u.h | 6 ++++ sys/include/libc.h | 1 + sys/src/kern/port/chan.c | 4 +-- sys/src/kern/port/devdup.c | 19 +++++++++++- sys/src/kern/port/devself.c | 19 +++++------- sys/src/kern/port/proc.c | 3 +- sys/src/lib/c/9sys/dup.c | 35 ++++++++++++++++++++++ sys/src/lib/c/9sys/pipe.c | 12 ++------ sys/src/lib/c/build.json | 1 + sys/src/sysconf.json | 59 +++++++++++++++---------------------- 10 files changed, 100 insertions(+), 59 deletions(-) create mode 100644 sys/src/lib/c/9sys/dup.c diff --git a/arch/amd64/include/u.h b/arch/amd64/include/u.h index ef24b98..96427ec 100644 --- a/arch/amd64/include/u.h +++ b/arch/amd64/include/u.h @@ -69,6 +69,12 @@ union FPdbleword }; }; +typedef union FdPair +{ + int fd[2]; + long aslong; +} FdPair; + typedef __builtin_va_list va_list; #define va_start(v,l) __builtin_va_start(v,l) diff --git a/sys/include/libc.h b/sys/include/libc.h index 5690561..0e6e596 100644 --- a/sys/include/libc.h +++ b/sys/include/libc.h @@ -396,6 +396,7 @@ extern void longjmp(jmp_buf, int); extern char* mktemp(char*); extern double modf(double, double*); extern void notejmp(void*, jmp_buf, int); +extern int dup(int oldfd, int newfd); extern int ocreate(const char* path, unsigned int omode, unsigned int perm); extern void perror(const char*); extern int pipe(int pipes[2]); diff --git a/sys/src/kern/port/chan.c b/sys/src/kern/port/chan.c index fec6cf2..306b64e 100644 --- a/sys/src/kern/port/chan.c +++ b/sys/src/kern/port/chan.c @@ -1358,7 +1358,7 @@ namec(char *aname, int amode, long omode, long perm) */ if(amode == Acreate){ /* perm must have DMDIR if last element is / or /. */ - if(e.mustbedir && !(perm&DMDIR)){ + if(e.mustbedir && !(perm&DMDIR) && omode >= 0){ e.nerror = e.nelems; error("create without DMDIR"); } @@ -1479,8 +1479,8 @@ namec(char *aname, int amode, long omode, long perm) */ e.nelems++; e.nerror++; - if(omode >=0) /* a negative omode means this is a message for the server */ if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) == 0) + if(omode >=0) /* a negative omode means this is a message for the server */ error(Eexist); mh = nil; diff --git a/sys/src/kern/port/devdup.c b/sys/src/kern/port/devdup.c index 566ed0c..8c40343 100644 --- a/sys/src/kern/port/devdup.c +++ b/sys/src/kern/port/devdup.c @@ -5,6 +5,9 @@ #include "fns.h" #include "../port/error.h" +#define INT_MAX ((1<<31)-1) +extern int sysdup(int ofd, int nfd); + /* Qid is (2*fd + (file is ctl))+1 */ static int @@ -57,6 +60,20 @@ dupstat(Chan *c, uint8_t *db, long n) return devstat(c, db, n, (Dirtab *)0, 0L, dupgen); } +static Chan* +dupcreate(Chan* c, char* name, unsigned long omode, unsigned long perm) +{ + FdPair in, out; + + if((omode|OCEXEC) == ~0 && c->qid.path == 0){ + in.aslong = perm; + out.aslong = 0; + out.fd[1] = sysdup(in.fd[0], in.fd[1]); + errorl(nil, ~out.aslong); + } + error(Eperm); +} + static Chan* dupopen(Chan *c, unsigned long omode) { @@ -134,7 +151,7 @@ Dev dupdevtab = { dupwalk, dupstat, dupopen, - devcreate, + dupcreate, dupclose, dupread, devbread, diff --git a/sys/src/kern/port/devself.c b/sys/src/kern/port/devself.c index d2abee9..6839958 100644 --- a/sys/src/kern/port/devself.c +++ b/sys/src/kern/port/devself.c @@ -61,12 +61,6 @@ Cmdtab proccmd[] = { CMsegfree, "free", 3, }; -typedef union PipeSet -{ - long merged; - int fd[2]; -} PipeSet; - static Dirtab selfdir[]={ ".", {Qdir, 0, QTDIR}, 0, DMDIR|0777, "brk", {Qbrk}, 0, 0, @@ -198,11 +192,14 @@ static Chan* selfcreate(Chan* c, char* name, unsigned long omode, unsigned long perm) { long e; - if(strcmp(name, "brk") == 0){ + + switch(QID(c->qid)){ + default: + error(Eperm); + case Qbrk: e = (long)grow_bss(perm); - errorl("brk set", ~e); + errorl(nil, ~e); } - error(Eperm); } static Walkqid* @@ -517,7 +514,7 @@ newfd2(int fd[2], Chan *c[2]) static long newpipe(void) { - PipeSet pipe; + FdPair pipe; Chan *c[2]; static char *datastr[] = {"data", "data1"}; @@ -543,7 +540,7 @@ newpipe(void) error(Enofd); poperror(); - return pipe.merged; + return pipe.aslong; } static void diff --git a/sys/src/kern/port/proc.c b/sys/src/kern/port/proc.c index 579df01..44ccb19 100644 --- a/sys/src/kern/port/proc.c +++ b/sys/src/kern/port/proc.c @@ -1473,7 +1473,8 @@ errorl(char *err, long syscallerr) if(up->nerrlab >= NERR) panic("error stack too deep"); up->syscallerr = syscallerr; - kstrcpy(up->errstr, err, ERRMAX); + if(err != nil) + kstrcpy(up->errstr, err, ERRMAX); setlabel(&up->errlab[NERR-1]); nexterror(); } diff --git a/sys/src/lib/c/9sys/dup.c b/sys/src/lib/c/9sys/dup.c new file mode 100644 index 0000000..6b69c56 --- /dev/null +++ b/sys/src/lib/c/9sys/dup.c @@ -0,0 +1,35 @@ +/* + * This file is part of Jehanne. + * + * Copyright (C) 2017 Giacomo Tesio + * + * Jehanne 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, version 2 of the License. + * + * Jehanne 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 Jehanne. If not, see . + */ +#include +#include + +int +dup(int oldfd, int newfd) +{ + FdPair in, out; + long f; + + in.fd[0] = oldfd; + in.fd[1] = newfd; + + f = create("#d/new", -1, in.aslong); + if(f == -1) + return -1; + out.aslong = ~f; + return out.fd[1]; +} diff --git a/sys/src/lib/c/9sys/pipe.c b/sys/src/lib/c/9sys/pipe.c index 4e4d89e..c3fd6b4 100644 --- a/sys/src/lib/c/9sys/pipe.c +++ b/sys/src/lib/c/9sys/pipe.c @@ -18,18 +18,12 @@ #include #include -typedef union PipeSet -{ - long merged; - int fd[2]; -} PipeSet; - int pipe(int pipes[2]) { - PipeSet pset; - pset.merged = remove("#0/pipes"); - if(pset.merged == -1) + FdPair pset; + pset.aslong = remove("#0/pipes"); + if(pset.aslong == -1) return -1; pipes[0] = pset.fd[0]; pipes[1] = pset.fd[1]; diff --git a/sys/src/lib/c/build.json b/sys/src/lib/c/build.json index aa320ac..4674df8 100644 --- a/sys/src/lib/c/build.json +++ b/sys/src/lib/c/build.json @@ -50,6 +50,7 @@ "9sys/dirread.c", "9sys/dirstat.c", "9sys/dirwstat.c", + "9sys/dup.c", "9sys/fork.c", "9sys/getnetconninfo.c", "9sys/getenv.c", diff --git a/sys/src/sysconf.json b/sys/src/sysconf.json index a3922ce..a8c207f 100644 --- a/sys/src/sysconf.json +++ b/sys/src/sysconf.json @@ -75,23 +75,12 @@ "long" ] }, - { - "Args": [ - "int32_t", - "int32_t" - ], - "Id": 5, - "Name": "dup", - "Ret": [ - "int32_t" - ] - }, { "Args": [ "char*", "int" ], - "Id": 6, + "Id": 5, "Name": "errstr", "Ret": [ "int" @@ -102,7 +91,7 @@ "const char*", "const char**" ], - "Id": 7, + "Id": 6, "Name": "exec", "Ret": [ "uintptr_t" @@ -112,7 +101,7 @@ "Args": [ "const char*" ], - "Id": 8, + "Id": 7, "Name": "_exits", "Ret": [ "int" @@ -123,7 +112,7 @@ "int", "const char*" ], - "Id": 9, + "Id": 8, "Name": "fauth", "Ret": [ "int" @@ -135,7 +124,7 @@ "const char*", "uint32_t" ], - "Id": 10, + "Id": 9, "Name": "fd2path", "Ret": [ "int32_t" @@ -147,7 +136,7 @@ "uint8_t*", "int" ], - "Id": 11, + "Id": 10, "Name": "fstat", "Ret": [ "long" @@ -160,7 +149,7 @@ "const char*", "int" ], - "Id": 12, + "Id": 11, "Name": "fversion", "Ret": [ "int" @@ -172,7 +161,7 @@ "const uint8_t*", "uint32_t" ], - "Id": 13, + "Id": 12, "Name": "fwstat", "Ret": [ "long" @@ -187,7 +176,7 @@ "const char*", "int" ], - "Id": 14, + "Id": 13, "Name": "mount", "Ret": [ "int" @@ -197,7 +186,7 @@ "Args": [ "int" ], - "Id": 15, + "Id": 14, "Name": "noted", "Ret": [ "int" @@ -207,14 +196,14 @@ "Args": [ "const void*" ], - "Id": 16, + "Id": 15, "Name": "notify", "Ret": [ "int" ] }, { - "Id": 17, + "Id": 16, "Name": "nsec", "Ret": [ "long" @@ -225,7 +214,7 @@ "const char*", "uint32_t" ], - "Id": 18, + "Id": 17, "Name": "open", "Ret": [ "long" @@ -238,7 +227,7 @@ "long", "long" ], - "Id": 19, + "Id": 18, "Name": "pread", "Ret": [ "long" @@ -251,7 +240,7 @@ "long", "long" ], - "Id": 20, + "Id": 19, "Name": "pwrite", "Ret": [ "long" @@ -261,7 +250,7 @@ "Args": [ "const char*" ], - "Id": 21, + "Id": 20, "Name": "remove", "Ret": [ "long" @@ -272,7 +261,7 @@ "const void*", "void*" ], - "Id": 22, + "Id": 21, "Name": "rendezvous", "Ret": [ "void*" @@ -282,7 +271,7 @@ "Args": [ "uint32_t" ], - "Id": 23, + "Id": 22, "Name": "rfork", "Ret": [ "int" @@ -294,7 +283,7 @@ "long", "int" ], - "Id": 24, + "Id": 23, "Name": "seek", "Ret": [ "long" @@ -305,7 +294,7 @@ "int*", "int" ], - "Id": 25, + "Id": 24, "Name": "semacquire", "Ret": [ "int" @@ -316,7 +305,7 @@ "int*", "int" ], - "Id": 26, + "Id": 25, "Name": "semrelease", "Ret": [ "int" @@ -327,7 +316,7 @@ "int*", "uint64_t" ], - "Id": 27, + "Id": 26, "Name": "tsemacquire", "Ret": [ "int" @@ -338,7 +327,7 @@ "const char*", "const char*" ], - "Id": 28, + "Id": 27, "Name": "unmount", "Ret": [ "int" @@ -348,7 +337,7 @@ "Args": [ "unsigned long" ], - "Id": 29, + "Id": 28, "Name": "alarm", "Ret": [ "long"