|
From: Latchesar I. <li...@us...> - 2006-01-21 00:35:07
|
Update of /cvsroot/v9fs/npfs/fs In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2347/fs Modified Files: Makefile nullfs.c ufs.c Added Files: cpu.c cpuhelper.c Log Message: various fixes, (not very useful at the moment) implementation of cpu command for Linux --- NEW FILE: cpuhelper.c --- /* * Copyright (C) 2005 by Latchesar Ionkov <lu...@io...> * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sched.h> #include <string.h> #include <sys/mount.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <errno.h> #include <signal.h> #include <linux/unistd.h> #define MS_MOVE 8192 static char *host; static int port; static char *uname; static char *cmd; static char **args; static char mntpt[256]; int procfn(void *a) { int n; char opts[256]; char mdir[256]; snprintf(opts, sizeof(opts), "name=%s,port=%d,debug=1", uname, port); n = mount(host, mntpt, "9P", 0, opts); if (n < 0) { perror("mount"); return -1; } snprintf(mdir, sizeof(mdir), "%s/dev", mntpt); n = mount("/dev", mdir, NULL, MS_BIND, NULL); if (n < 0) { perror("bind dev"); return -1; } snprintf(mdir, sizeof(mdir), "%s/proc", mntpt); n = mount("/proc", mdir, NULL, MS_BIND, NULL); if (n < 0) { perror("bind proc"); return -1; } snprintf(mdir, sizeof(mdir), "%s/sys", mntpt); n = mount("/sys", mdir, NULL, MS_BIND, NULL); if (n < 0) { perror("bind sys"); return -1; } chdir(mntpt); n = mount(mntpt, "/", NULL, MS_MOVE, NULL); if (n < 0) { perror("move mount"); return -1; } chroot("."); execv(cmd, args); snprintf(mdir, sizeof(mdir), "execv %s", cmd); perror(mdir); return -1; } static void usage() { fprintf(stderr, "cpud uname host port\n"); exit(-1); } int main(int argc, char **argv) { int i, nargs; char *s; pid_t pid, p; char stk[10000]; if (argc < 4) usage(); uname = argv[1]; host = argv[2]; port = strtol(argv[3], &s, 10); if (*s != 0) usage(); nargs = argc - 3; if (nargs < 2) nargs = 2; cmd = "/bin/bash"; args = calloc(nargs, sizeof(char *)); if (argc > 4) { cmd = argv[4]; for(i = 4; i < argc; i++) args[i-4] = argv[i]; } else { cmd = "/bin/bash"; args[1] = "--login"; } args[0] = cmd; snprintf(mntpt, sizeof(mntpt), "/tmp/cpu%d", getpid()); mkdir(mntpt, 0700); pid = clone(procfn, stk + 5000, CLONE_NEWNS | CLONE_FILES | CLONE_PTRACE | SIGCHLD, NULL); if ((int) pid == -1) { perror("clone"); exit(-1); } p = waitpid(pid, NULL, 0); if ((int) p == -1) { perror("waitpid"); exit(-1); } if (!rmdir(mntpt)) perror("rmdir"); return 0; } Index: Makefile =================================================================== RCS file: /cvsroot/v9fs/npfs/fs/Makefile,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Makefile 29 Dec 2005 01:42:34 -0000 1.2 --- Makefile 21 Jan 2006 00:34:52 -0000 1.3 *************** *** 18,22 **** mboxfs.o\ ! all: ufs ramfs nullfs ramfs2 gphotofs mboxfs ufs: ufs.o --- 18,22 ---- mboxfs.o\ ! all: ufs ramfs nullfs ramfs2 ufs: ufs.o *************** *** 44,47 **** --- 44,53 ---- $(CC) $(CFLAGS) -I/usr/include/libetpan -c $*.c + cpu: cpu.o ../libnpfs/libnpfs.a + $(CC) -o cpu $(CFLAGS) cpu.o $(LFLAGS) + + cpuhelper: cpuhelper.o + $(CC) -o cpuhelper $(CFLAGS) cpuhelper.o $(LFLAGS) + clean: rm -f *.o ufs ramfs nullfs ramfs2 gphotofs mboxfs --- NEW FILE: cpu.c --- /* * Copyright (C) 2005 by Latchesar Ionkov <lu...@io...> * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, [...991 lines suppressed...] create_rerror(errno); goto out; } free(f->path); f->path = npath; } } if (stat->length != ~0) { if (truncate(f->path, stat->length) < 0) { create_rerror(errno); goto out; } } ret = np_create_rwstat(); out: return ret; } Index: nullfs.c =================================================================== RCS file: /cvsroot/v9fs/npfs/fs/nullfs.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** nullfs.c 29 Dec 2005 01:42:34 -0000 1.2 --- nullfs.c 21 Jan 2006 00:34:52 -0000 1.3 *************** *** 32,242 **** #include "npfs.h" ! #define ROOTPERM 0755 ! #define NELEM(x) (sizeof(x)/sizeof((x)[0])) ! ! typedef struct Fid Fid; ! struct Fid { ! int isnull; ! }; static Npsrv *srv; ! static Npqid rootqid; ! static Npqid nullqid; ! static Npfcall* nullfs_attach(Npfid *fid, Npfid *afid, Npstr *uname, Npstr *aname); ! static int nullfs_clone(Npfid *fid, Npfid *newfid); ! static int nullfs_walk(Npfid *fid, Npstr* wname, Npqid *wqid); ! static Npfcall* nullfs_open(Npfid *fid, u8 mode); ! static Npfcall* nullfs_create(Npfid *fid, Npstr* name, u32 perm, u8 mode); ! static Npfcall* nullfs_read(Npfid *fid, u64 offset, u32 count); ! static Npfcall* nullfs_write(Npfid *fid, u64 offset, u32 count, u8 *data); ! static Npfcall* nullfs_clunk(Npfid *fid); ! static Npfcall* nullfs_remove(Npfid *fid); ! static Npfcall* nullfs_stat(Npfid *fid); ! static Npfcall* nullfs_wstat(Npfid *fid, Npstat *stat); static void - fillwstat(Npwstat *wstat, int isnull) { - wstat->size = 0; - wstat->type = 0; - wstat->dev = 0; - if (!isnull) { - wstat->qid = nullqid; - wstat->mode = Dmdir | 0777; - wstat->name = ""; - wstat->qid.path = 0; - } else { - wstat->qid = rootqid; - wstat->mode = 0666; - wstat->name = "null"; - wstat->qid.path = 1; - } - - wstat->qid.type = 0; - wstat->qid.version = 0; - wstat->length = 0; - wstat->uid = "root"; - wstat->gid = "root"; - wstat->muid = "root"; - wstat->extension = ""; - wstat->n_uid = 0; - wstat->n_gid = 0; - wstat->n_muid = 0; - } - - static Npfcall* - nullfs_attach(Npfid *nfid, Npfid *nafid, Npstr *uname, Npstr *aname) - { - Npfcall* ret; - Fid *fid; - Npuser *user; - - user = NULL; - ret = NULL; - - if (nafid != NULL) { - np_werror(Enoauth, EIO); - return NULL; - } - - fid = malloc(sizeof(*fid)); - fid->isnull = 0; - nfid->aux = fid; - - np_fid_incref(nfid); - ret = np_create_rattach(&rootqid); - return ret; - } - - static int - nullfs_clone(Npfid *fid, Npfid *newfid) - { - Fid *f, *nf; - - f = fid->aux; - nf = malloc(sizeof(*nf)); - nf->isnull = f->isnull; - newfid->aux = nf; - - return 1; - } - - static int - nullfs_walk(Npfid *fid, Npstr* wname, Npqid *wqid) - { - char *name; - Fid *f; - - f = fid->aux; - - if (f->isnull) { - np_werror("can't walk", EPERM); - return 0; - } - - name = np_strdup(wname); - if (strcmp(name, "null") != 0) { - np_werror(Enotfound, ENOENT); - return 0; - } - - f->isnull = 1; - *wqid = nullqid; - return 1; - } - - static Npfcall* - nullfs_open(Npfid *fid, u8 mode) - { - Fid *f; - Npqid *qid; - - f = fid->aux; - if (f->isnull) - qid = &nullqid; - else - qid = &rootqid; - - return np_create_ropen(qid, 0); - } - - static Npfcall* - nullfs_create(Npfid *fid, Npstr* name, u32 perm, u8 mode) - { - np_werror(Eperm, EPERM); - return NULL; - } - - static Npfcall* - nullfs_read(Npfid *fid, u64 offset, u32 count) - { - int n; - Fid *f; - Npfcall *ret; - u8* buf; - Npwstat wstat; - - buf = malloc(count); - f = fid->aux; - if (f->isnull) { - memset(buf, 0, count); - ret = np_create_rread(count, buf); - goto out; - } - - n = 0; - if (offset == 0) { - fillwstat(&wstat, 1); - n = np_serialize_stat(&wstat, buf, count - 1, fid->conn->dotu); - } - - ret = np_create_rread(n, buf); - - out: - free(buf); - return ret; - } - - static Npfcall* - nullfs_write(Npfid *fid, u64 offset, u32 count, u8 *data) - { - return np_create_rwrite(count); - } - - static Npfcall* - nullfs_clunk(Npfid *fid) - { - np_fid_decref(fid); - // fprintf(stderr, "fid %d refcount %d\n", fid->fid, fid->refcount); - return np_create_rclunk(); - } - - static Npfcall* - nullfs_remove(Npfid *fid) - { - np_werror("permission denied", EPERM); - return NULL; - } - - static Npfcall* - nullfs_stat(Npfid *fid) - { - Fid *f; - Npwstat wstat; - - f = fid->aux; - fillwstat(&wstat, f->isnull); - return np_create_rstat(&wstat, fid->conn->dotu); - } - - - static Npfcall* - nullfs_wstat(Npfid *fid, Npstat *stat) - { - return np_create_rwstat(); - // return np_werror("permission denided", EPERM); - } - - void usage() { --- 32,57 ---- #include "npfs.h" ! static Npfile* null_first(Npfile *); ! static Npfile* null_next(Npfile *, Npfile *); ! static u32 null_read(Npfilefid *, u64, u32, u8*); ! static u32 null_write(Npfilefid *, u64, u32, u8*); ! static int null_wstat(Npfile *, Npstat *); ! static void null_connclose(Npconn *); static Npsrv *srv; ! static Npfile *root; ! static Npfile * null; ! static Npdirops rootops = { ! .first = null_first, ! .next = null_next, ! }; ! static Npfileops nullops = { ! .read = null_read, ! .write = null_write, ! .wstat = null_wstat, ! }; static void usage() { *************** *** 273,276 **** --- 88,94 ---- } + if (optind >= argc) + usage(); + if (!user) { fprintf(stderr, "invalid user\n"); *************** *** 278,313 **** } srv = np_pipesrv_create(nwthreads); if (!srv) return -1; - srv->dotu = 1; - srv->attach = nullfs_attach; - srv->clone = nullfs_clone; - srv->walk = nullfs_walk; - srv->open = nullfs_open; - srv->create = nullfs_create; - srv->read = nullfs_read; - srv->write = nullfs_write; - srv->clunk = nullfs_clunk; - srv->remove = nullfs_remove; - srv->stat = nullfs_stat; - srv->wstat = nullfs_wstat; srv->debuglevel = debuglevel; ! ! if (optind >= argc) ! usage(); np_pipesrv_mount(srv, argv[optind], user->uname, 0, opts); ! /* ! pid = fork(); ! if (pid < 0) ! return -1; ! else if (pid != 0) ! return 0; ! setsid(); ! chdir("/"); ! */ while (1) { sleep(100); --- 96,136 ---- } + close(0); + close(1); + close(2); + pid = fork(); + if (pid < 0) + return -1; + else if (pid != 0) + return 0; + + setsid(); + chdir("/"); + + root = npfile_alloc(NULL, strdup(""), 0755|Dmdir, 0, &rootops, NULL); + root->parent = root; + npfile_incref(root); + root->atime = time(NULL); + root->mtime = root->atime; + root->uid = user; + root->gid = user->dfltgroup; + root->muid = user; + + null = npfile_alloc(root, strdup("null"), 0644, 1, &nullops, NULL); + npfile_incref(null); + root->dirfirst = null; + root->dirlast = null; + srv = np_pipesrv_create(nwthreads); if (!srv) return -1; srv->debuglevel = debuglevel; ! srv->connclose = null_connclose; ! npfile_init_srv(srv, root); np_pipesrv_mount(srv, argv[optind], user->uname, 0, opts); ! while (1) { sleep(100); *************** *** 316,317 **** --- 139,183 ---- return 0; } + + static void + null_connclose(Npconn *conn) + { + exit(0); + } + + static Npfile* + null_first(Npfile *dir) + { + if (dir->dirfirst) + npfile_incref(dir->dirfirst); + + return dir->dirfirst; + } + + static Npfile* + null_next(Npfile *dir, Npfile *prevchild) + { + if (prevchild->next) + npfile_incref(prevchild->next); + + return prevchild->next; + } + + static u32 + null_read(Npfilefid* file, u64 offset, u32 count, u8* data) + { + memset(data, 0, count); + return count; + } + + static u32 + null_write(Npfilefid* file, u64 offset, u32 count, u8* data) + { + return count; + } + + static int + null_wstat(Npfile* file, Npstat* stat) + { + return 1; + } Index: ufs.c =================================================================== RCS file: /cvsroot/v9fs/npfs/fs/ufs.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ufs.c 2 Jan 2006 02:12:48 -0000 1.3 --- ufs.c 21 Jan 2006 00:34:52 -0000 1.4 *************** *** 120,124 **** } ! srv = np_socksrv_create_tcp(nwthreads, port); if (!srv) --- 120,124 ---- } ! srv = np_socksrv_create_tcp(nwthreads, &port); if (!srv) *************** *** 151,155 **** fidstat(Fid *fid) { ! if (stat(fid->path, &fid->stat) < 0) return errno; --- 151,155 ---- fidstat(Fid *fid) { ! if (lstat(fid->path, &fid->stat) < 0) return errno; *************** *** 470,474 **** path[n + wname->len + 1] = '\0'; ! if (stat(path, &st) < 0) { free(path); create_rerror(errno); --- 470,474 ---- path[n + wname->len + 1] = '\0'; ! if (lstat(path, &st) < 0) { free(path); create_rerror(errno); *************** *** 537,541 **** npath[n + name->len + 1] = '\0'; ! if (stat(npath, &st)==0 || errno!=ENOENT) { np_werror(Eexist, EEXIST); goto out; --- 537,541 ---- npath[n + name->len + 1] = '\0'; ! if (lstat(npath, &st)==0 || errno!=ENOENT) { np_werror(Eexist, EEXIST); goto out; *************** *** 656,660 **** sprintf(path, "%s/%s", f->path, dname); ! if (stat(path, &st) < 0) { free(path); create_rerror(errno); --- 656,660 ---- sprintf(path, "%s/%s", f->path, dname); ! if (lstat(path, &st) < 0) { free(path); create_rerror(errno); |