- filled in group field, previously missing.
[npfs.git] / fs / nullfs.c
blob6d75c6c44e55b05831240a3db7617484b4cc3e82
1 /*
2 * Copyright (C) 2005 by Latchesar Ionkov <lucho@ionkov.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * LATCHESAR IONKOV AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 #define _XOPEN_SOURCE 600
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <pthread.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <assert.h>
32 #include "npfs.h"
34 static Npfile* null_first(Npfile *);
35 static Npfile* null_next(Npfile *, Npfile *);
36 static int null_read(Npfilefid *, u64, u32, u8*, Npreq *);
37 static int null_write(Npfilefid *, u64, u32, u8*, Npreq *);
38 static int null_wstat(Npfile *, Npstat *);
39 static void null_connclose(Npconn *);
41 static Npsrv *srv;
42 static Npfile *root;
43 static Npfile * null;
44 static Npdirops rootops = {
45 .first = null_first,
46 .next = null_next,
49 static Npfileops nullops = {
50 .read = null_read,
51 .write = null_write,
52 .wstat = null_wstat,
55 static void
56 usage()
58 fprintf(stderr, "nullfs: -d -w nthreads mount-point\n");
59 exit(-1);
62 int
63 main(int argc, char **argv)
65 int c, debuglevel, nwthreads;
66 pid_t pid;
67 Npuser *user;
68 char *opts, *s;
70 debuglevel = 0;
71 nwthreads = 16;
72 user = np_default_users->uid2user(np_default_users, getuid());
73 while ((c = getopt(argc, argv, "dw:")) != -1) {
74 switch (c) {
75 case 'd':
76 debuglevel = 1;
77 break;
79 case 'w':
80 nwthreads = strtol(optarg, &s, 10);
81 if (*s != '\0')
82 usage();
83 break;
85 default:
86 fprintf(stderr, "invalid option\n");
90 if (optind >= argc)
91 usage();
93 if (!user) {
94 fprintf(stderr, "invalid user\n");
95 return -1;
98 close(0);
99 close(1);
100 close(2);
101 pid = fork();
102 if (pid < 0)
103 return -1;
104 else if (pid != 0)
105 return 0;
107 setsid();
108 chdir("/");
110 root = npfile_alloc(NULL, strdup(""), 0755|Dmdir, 0, &rootops, NULL);
111 root->parent = root;
112 npfile_incref(root);
113 root->atime = time(NULL);
114 root->mtime = root->atime;
115 root->uid = user;
116 root->gid = user->dfltgroup;
117 root->muid = user;
119 null = npfile_alloc(root, strdup("null"), 0644, 1, &nullops, NULL);
120 npfile_incref(null);
121 root->dirfirst = null;
122 root->dirlast = null;
124 srv = np_pipesrv_create(nwthreads);
125 if (!srv)
126 return -1;
128 srv->debuglevel = debuglevel;
129 srv->connclose = null_connclose;
130 npfile_init_srv(srv, root);
132 np_pipesrv_mount(srv, argv[optind], user->uname, 0, opts);
135 while (1) {
136 sleep(100);
139 return 0;
142 static void
143 null_connclose(Npconn *conn)
145 exit(0);
148 static Npfile*
149 null_first(Npfile *dir)
151 if (dir->dirfirst)
152 npfile_incref(dir->dirfirst);
154 return dir->dirfirst;
157 static Npfile*
158 null_next(Npfile *dir, Npfile *prevchild)
160 if (prevchild->next)
161 npfile_incref(prevchild->next);
163 return prevchild->next;
166 static int
167 null_read(Npfilefid* file, u64 offset, u32 count, u8* data, Npreq *req)
169 memset(data, 0, count);
170 return count;
173 static int
174 null_write(Npfilefid* file, u64 offset, u32 count, u8* data, Npreq *req)
176 return count;
179 static int
180 null_wstat(Npfile* file, Npstat* stat)
182 return 1;