]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/hjfs/main.c
hjfs: disable hjfs check until more functionality is complete
[plan9front.git] / sys / src / cmd / hjfs / main.c
1 #include <u.h>
2 #include <libc.h>
3 #include <libsec.h>
4 #include <thread.h>
5 #include "dat.h"
6 #include "fns.h"
7
8 char Eio[] = "i/o error";
9 char Enotadir[] = "not a directory";
10 char Enoent[] = "not found";
11 char Einval[] = "invalid operation";
12 char Eperm[] = "permission denied";
13 char Eexists[] = "file exists";
14 char Elocked[] = "file locked";
15
16 int mainstacksize = 65536;
17
18 void*
19 emalloc(int c)
20 {
21         void *v;
22         
23         v = mallocz(c, 1);
24         if(v == 0)
25                 sysfatal("malloc: %r");
26         setmalloctag(v, getcallerpc(&c));
27         return v;
28 }
29
30 void*
31 erealloc(void *v, int c)
32 {
33         v = realloc(v, c);
34         if(v == 0 && c != 0)
35                 sysfatal("realloc: %r");
36         setrealloctag(v, getcallerpc(&c));
37         return v;
38 }
39
40 char*
41 estrdup(char *s)
42 {
43         s = strdup(s);
44         if(s == 0)
45                 sysfatal("strdup: %r");
46         setmalloctag(s, getcallerpc(&s));
47         return s;
48 }
49
50 ThrData *
51 getthrdata(void)
52 {
53         ThrData **v;
54         
55         v = (ThrData **) threaddata();
56         if(*v == nil){
57                 *v = emalloc(sizeof(**v));
58                 (*v)->resp = chancreate(sizeof(void *), 0);
59         }
60         return *v;
61 }
62
63 Fs *fsmain;
64
65 int
66 dprint(char *fmt, ...)
67 {
68         static char buf[2048];
69         static QLock lk;
70         va_list va;
71         int rc;
72
73         qlock(&lk);
74         va_start(va, fmt);
75         snprint(buf, 2048, "hjfs: %s", fmt);
76         rc = vfprint(2, buf, va);
77         va_end(va);
78         qunlock(&lk);
79         return rc;
80 }
81
82 static void
83 syncproc(void *)
84 {
85         for(;;){
86                 sync(0);
87                 sleep(SYNCINTERVAL);
88         }
89 }
90
91 void
92 usage(void)
93 {
94         fprint(2, "usage: %s [-rsS] [-m mem] [-n service] [-a announce-string]... -f dev\n", argv0);
95         exits("usage");
96 }
97
98 void
99 threadmain(int argc, char **argv)
100 {
101         Dev *d;
102         static char *nets[8];
103         char *file, *service;
104         int doream, flags, stdio, nbuf, netc;
105
106         netc = 0;
107         doream = 0;
108         stdio = 0;
109         flags = FSNOAUTH;
110         service = "hjfs";
111         file = nil;
112         nbuf = 1000;
113         ARGBEGIN {
114         case 'A': flags &= ~FSNOAUTH; break;
115         case 'r': doream++; break;
116         case 'S': flags |= FSNOPERM | FSCHOWN; break;
117         case 's': stdio++; break;
118         case 'f': file = estrdup(EARGF(usage())); break;
119         case 'n': service = estrdup(EARGF(usage())); break;
120         case 'm':
121                 nbuf = muldiv(atoi(EARGF(usage())), 1048576, sizeof(Buf));
122                 if(nbuf < 10)
123                         nbuf = 10;
124                 break;
125         case 'a':
126                 if(netc >= nelem(nets)-1){
127                         fprint(2, "%s: too many networks to announce\n", argv0);
128                         exits("too many nets");
129                 }
130                 nets[netc++] = estrdup(EARGF(usage()));
131                 break;
132         default: usage();
133         } ARGEND;
134         rfork(RFNOTEG);
135         bufinit(nbuf);
136         if(file == nil)
137                 sysfatal("no default file");
138         if(argc != 0)
139                 usage();
140         d = newdev(file);
141         if(d == nil)
142                 sysfatal("newdev: %r");
143         fsmain = initfs(d, doream, flags);
144         if(fsmain == nil)
145                 sysfatal("fsinit: %r");
146         initcons(service);
147         proccreate(syncproc, nil, mainstacksize);
148         start9p(service, nets, stdio);
149         threadexits(nil);
150 }
151
152 void
153 shutdown(void)
154 {
155         wlock(fsmain);
156         sync(1);
157         dprint("ending\n");
158         sleep(1000);
159         sync(1);
160         threadexitsall(nil);
161 }