kernel: move sysdup to libc

This commit is contained in:
2017-01-05 03:02:56 +01:00
parent 882e33b091
commit 2508de40ea
10 changed files with 100 additions and 59 deletions

View File

@ -69,6 +69,12 @@ union FPdbleword
}; };
}; };
typedef union FdPair
{
int fd[2];
long aslong;
} FdPair;
typedef __builtin_va_list va_list; typedef __builtin_va_list va_list;
#define va_start(v,l) __builtin_va_start(v,l) #define va_start(v,l) __builtin_va_start(v,l)

View File

@ -396,6 +396,7 @@ extern void longjmp(jmp_buf, int);
extern char* mktemp(char*); extern char* mktemp(char*);
extern double modf(double, double*); extern double modf(double, double*);
extern void notejmp(void*, jmp_buf, int); 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 int ocreate(const char* path, unsigned int omode, unsigned int perm);
extern void perror(const char*); extern void perror(const char*);
extern int pipe(int pipes[2]); extern int pipe(int pipes[2]);

View File

@ -1358,7 +1358,7 @@ namec(char *aname, int amode, long omode, long perm)
*/ */
if(amode == Acreate){ if(amode == Acreate){
/* perm must have DMDIR if last element is / or /. */ /* 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; e.nerror = e.nelems;
error("create without DMDIR"); error("create without DMDIR");
} }
@ -1479,8 +1479,8 @@ namec(char *aname, int amode, long omode, long perm)
*/ */
e.nelems++; e.nelems++;
e.nerror++; 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(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); error(Eexist);
mh = nil; mh = nil;

View File

@ -5,6 +5,9 @@
#include "fns.h" #include "fns.h"
#include "../port/error.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 */ /* Qid is (2*fd + (file is ctl))+1 */
static int static int
@ -57,6 +60,20 @@ dupstat(Chan *c, uint8_t *db, long n)
return devstat(c, db, n, (Dirtab *)0, 0L, dupgen); 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* static Chan*
dupopen(Chan *c, unsigned long omode) dupopen(Chan *c, unsigned long omode)
{ {
@ -134,7 +151,7 @@ Dev dupdevtab = {
dupwalk, dupwalk,
dupstat, dupstat,
dupopen, dupopen,
devcreate, dupcreate,
dupclose, dupclose,
dupread, dupread,
devbread, devbread,

View File

@ -61,12 +61,6 @@ Cmdtab proccmd[] = {
CMsegfree, "free", 3, CMsegfree, "free", 3,
}; };
typedef union PipeSet
{
long merged;
int fd[2];
} PipeSet;
static Dirtab selfdir[]={ static Dirtab selfdir[]={
".", {Qdir, 0, QTDIR}, 0, DMDIR|0777, ".", {Qdir, 0, QTDIR}, 0, DMDIR|0777,
"brk", {Qbrk}, 0, 0, "brk", {Qbrk}, 0, 0,
@ -198,11 +192,14 @@ static Chan*
selfcreate(Chan* c, char* name, unsigned long omode, unsigned long perm) selfcreate(Chan* c, char* name, unsigned long omode, unsigned long perm)
{ {
long e; long e;
if(strcmp(name, "brk") == 0){
switch(QID(c->qid)){
default:
error(Eperm);
case Qbrk:
e = (long)grow_bss(perm); e = (long)grow_bss(perm);
errorl("brk set", ~e); errorl(nil, ~e);
} }
error(Eperm);
} }
static Walkqid* static Walkqid*
@ -517,7 +514,7 @@ newfd2(int fd[2], Chan *c[2])
static long static long
newpipe(void) newpipe(void)
{ {
PipeSet pipe; FdPair pipe;
Chan *c[2]; Chan *c[2];
static char *datastr[] = {"data", "data1"}; static char *datastr[] = {"data", "data1"};
@ -543,7 +540,7 @@ newpipe(void)
error(Enofd); error(Enofd);
poperror(); poperror();
return pipe.merged; return pipe.aslong;
} }
static void static void

View File

@ -1473,7 +1473,8 @@ errorl(char *err, long syscallerr)
if(up->nerrlab >= NERR) if(up->nerrlab >= NERR)
panic("error stack too deep"); panic("error stack too deep");
up->syscallerr = syscallerr; up->syscallerr = syscallerr;
kstrcpy(up->errstr, err, ERRMAX); if(err != nil)
kstrcpy(up->errstr, err, ERRMAX);
setlabel(&up->errlab[NERR-1]); setlabel(&up->errlab[NERR-1]);
nexterror(); nexterror();
} }

35
sys/src/lib/c/9sys/dup.c Normal file
View File

@ -0,0 +1,35 @@
/*
* This file is part of Jehanne.
*
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <libc.h>
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];
}

View File

@ -18,18 +18,12 @@
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
typedef union PipeSet
{
long merged;
int fd[2];
} PipeSet;
int int
pipe(int pipes[2]) pipe(int pipes[2])
{ {
PipeSet pset; FdPair pset;
pset.merged = remove("#0/pipes"); pset.aslong = remove("#0/pipes");
if(pset.merged == -1) if(pset.aslong == -1)
return -1; return -1;
pipes[0] = pset.fd[0]; pipes[0] = pset.fd[0];
pipes[1] = pset.fd[1]; pipes[1] = pset.fd[1];

View File

@ -50,6 +50,7 @@
"9sys/dirread.c", "9sys/dirread.c",
"9sys/dirstat.c", "9sys/dirstat.c",
"9sys/dirwstat.c", "9sys/dirwstat.c",
"9sys/dup.c",
"9sys/fork.c", "9sys/fork.c",
"9sys/getnetconninfo.c", "9sys/getnetconninfo.c",
"9sys/getenv.c", "9sys/getenv.c",

View File

@ -75,23 +75,12 @@
"long" "long"
] ]
}, },
{
"Args": [
"int32_t",
"int32_t"
],
"Id": 5,
"Name": "dup",
"Ret": [
"int32_t"
]
},
{ {
"Args": [ "Args": [
"char*", "char*",
"int" "int"
], ],
"Id": 6, "Id": 5,
"Name": "errstr", "Name": "errstr",
"Ret": [ "Ret": [
"int" "int"
@ -102,7 +91,7 @@
"const char*", "const char*",
"const char**" "const char**"
], ],
"Id": 7, "Id": 6,
"Name": "exec", "Name": "exec",
"Ret": [ "Ret": [
"uintptr_t" "uintptr_t"
@ -112,7 +101,7 @@
"Args": [ "Args": [
"const char*" "const char*"
], ],
"Id": 8, "Id": 7,
"Name": "_exits", "Name": "_exits",
"Ret": [ "Ret": [
"int" "int"
@ -123,7 +112,7 @@
"int", "int",
"const char*" "const char*"
], ],
"Id": 9, "Id": 8,
"Name": "fauth", "Name": "fauth",
"Ret": [ "Ret": [
"int" "int"
@ -135,7 +124,7 @@
"const char*", "const char*",
"uint32_t" "uint32_t"
], ],
"Id": 10, "Id": 9,
"Name": "fd2path", "Name": "fd2path",
"Ret": [ "Ret": [
"int32_t" "int32_t"
@ -147,7 +136,7 @@
"uint8_t*", "uint8_t*",
"int" "int"
], ],
"Id": 11, "Id": 10,
"Name": "fstat", "Name": "fstat",
"Ret": [ "Ret": [
"long" "long"
@ -160,7 +149,7 @@
"const char*", "const char*",
"int" "int"
], ],
"Id": 12, "Id": 11,
"Name": "fversion", "Name": "fversion",
"Ret": [ "Ret": [
"int" "int"
@ -172,7 +161,7 @@
"const uint8_t*", "const uint8_t*",
"uint32_t" "uint32_t"
], ],
"Id": 13, "Id": 12,
"Name": "fwstat", "Name": "fwstat",
"Ret": [ "Ret": [
"long" "long"
@ -187,7 +176,7 @@
"const char*", "const char*",
"int" "int"
], ],
"Id": 14, "Id": 13,
"Name": "mount", "Name": "mount",
"Ret": [ "Ret": [
"int" "int"
@ -197,7 +186,7 @@
"Args": [ "Args": [
"int" "int"
], ],
"Id": 15, "Id": 14,
"Name": "noted", "Name": "noted",
"Ret": [ "Ret": [
"int" "int"
@ -207,14 +196,14 @@
"Args": [ "Args": [
"const void*" "const void*"
], ],
"Id": 16, "Id": 15,
"Name": "notify", "Name": "notify",
"Ret": [ "Ret": [
"int" "int"
] ]
}, },
{ {
"Id": 17, "Id": 16,
"Name": "nsec", "Name": "nsec",
"Ret": [ "Ret": [
"long" "long"
@ -225,7 +214,7 @@
"const char*", "const char*",
"uint32_t" "uint32_t"
], ],
"Id": 18, "Id": 17,
"Name": "open", "Name": "open",
"Ret": [ "Ret": [
"long" "long"
@ -238,7 +227,7 @@
"long", "long",
"long" "long"
], ],
"Id": 19, "Id": 18,
"Name": "pread", "Name": "pread",
"Ret": [ "Ret": [
"long" "long"
@ -251,7 +240,7 @@
"long", "long",
"long" "long"
], ],
"Id": 20, "Id": 19,
"Name": "pwrite", "Name": "pwrite",
"Ret": [ "Ret": [
"long" "long"
@ -261,7 +250,7 @@
"Args": [ "Args": [
"const char*" "const char*"
], ],
"Id": 21, "Id": 20,
"Name": "remove", "Name": "remove",
"Ret": [ "Ret": [
"long" "long"
@ -272,7 +261,7 @@
"const void*", "const void*",
"void*" "void*"
], ],
"Id": 22, "Id": 21,
"Name": "rendezvous", "Name": "rendezvous",
"Ret": [ "Ret": [
"void*" "void*"
@ -282,7 +271,7 @@
"Args": [ "Args": [
"uint32_t" "uint32_t"
], ],
"Id": 23, "Id": 22,
"Name": "rfork", "Name": "rfork",
"Ret": [ "Ret": [
"int" "int"
@ -294,7 +283,7 @@
"long", "long",
"int" "int"
], ],
"Id": 24, "Id": 23,
"Name": "seek", "Name": "seek",
"Ret": [ "Ret": [
"long" "long"
@ -305,7 +294,7 @@
"int*", "int*",
"int" "int"
], ],
"Id": 25, "Id": 24,
"Name": "semacquire", "Name": "semacquire",
"Ret": [ "Ret": [
"int" "int"
@ -316,7 +305,7 @@
"int*", "int*",
"int" "int"
], ],
"Id": 26, "Id": 25,
"Name": "semrelease", "Name": "semrelease",
"Ret": [ "Ret": [
"int" "int"
@ -327,7 +316,7 @@
"int*", "int*",
"uint64_t" "uint64_t"
], ],
"Id": 27, "Id": 26,
"Name": "tsemacquire", "Name": "tsemacquire",
"Ret": [ "Ret": [
"int" "int"
@ -338,7 +327,7 @@
"const char*", "const char*",
"const char*" "const char*"
], ],
"Id": 28, "Id": 27,
"Name": "unmount", "Name": "unmount",
"Ret": [ "Ret": [
"int" "int"
@ -348,7 +337,7 @@
"Args": [ "Args": [
"unsigned long" "unsigned long"
], ],
"Id": 29, "Id": 28,
"Name": "alarm", "Name": "alarm",
"Ret": [ "Ret": [
"long" "long"