summaryrefslogtreecommitdiff
path: root/sys/src/cmd/hjfs/main.c
blob: c497b6166dd0e59c5bff35b1b8e628764eb97fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <u.h>
#include <libc.h>
#include <libsec.h>
#include <thread.h>
#include "dat.h"
#include "fns.h"

char Eio[] = "i/o error";
char Enotadir[] = "not a directory";
char Enoent[] = "not found";
char Einval[] = "invalid operation";
char Eperm[] = "permission denied";
char Eexists[] = "file exists";
char Elocked[] = "file locked";

int mainstacksize = 65536;

void*
emalloc(int c)
{
	void *v;
	
	v = mallocz(c, 1);
	if(v == 0)
		sysfatal("malloc: %r");
	setmalloctag(v, getcallerpc(&c));
	return v;
}

void*
erealloc(void *v, int c)
{
	v = realloc(v, c);
	if(v == 0 && c != 0)
		sysfatal("realloc: %r");
	setrealloctag(v, getcallerpc(&c));
	return v;
}

char*
estrdup(char *s)
{
	s = strdup(s);
	if(s == 0)
		sysfatal("strdup: %r");
	setmalloctag(s, getcallerpc(&s));
	return s;
}

ThrData *
getthrdata(void)
{
	ThrData **v;
	
	v = (ThrData **) threaddata();
	if(*v == nil){
		*v = emalloc(sizeof(**v));
		(*v)->resp = chancreate(sizeof(void *), 0);
	}
	return *v;
}

Fs *fsmain;

void
dprint(char *fmt, ...)
{
	char buf[128];
	va_list va;

	snprint(buf, sizeof(buf), "hjfs: %s", fmt);

	va_start(va, fmt);
	vfprint(2, buf, va);
	va_end(va);
}

static void
syncproc(void *)
{
	for(;;){
		sync(0);
		sleep(SYNCINTERVAL);
	}
}

void
usage(void)
{
	fprint(2, "usage: %s [-rsS] [-m mem] [-n service] [-a announce-string]... -f dev\n", argv0);
	exits("usage");
}

void
threadmain(int argc, char **argv)
{
	Dev *d;
	static char *nets[8];
	char *file, *service;
	int doream, flags, stdio, nbuf, netc;

	netc = 0;
	doream = 0;
	stdio = 0;
	flags = FSNOAUTH;
	service = "hjfs";
	file = nil;
	nbuf = 1000;
	ARGBEGIN {
	case 'A': flags &= ~FSNOAUTH; break;
	case 'r': doream++; break;
	case 'S': flags |= FSNOPERM | FSCHOWN; break;
	case 's': stdio++; break;
	case 'f': file = estrdup(EARGF(usage())); break;
	case 'n': service = estrdup(EARGF(usage())); break;
	case 'm':
		nbuf = muldiv(atoi(EARGF(usage())), 1048576, sizeof(Buf));
		if(nbuf < 10)
			nbuf = 10;
		break;
	case 'a':
		if(netc >= nelem(nets)-1){
			fprint(2, "%s: too many networks to announce\n", argv0);
			exits("too many nets");
		}
		nets[netc++] = estrdup(EARGF(usage()));
		break;
	default: usage();
	} ARGEND;
	rfork(RFNOTEG);
	bufinit(nbuf);
	if(file == nil)
		sysfatal("no default file");
	if(argc != 0)
		usage();
	d = newdev(file);
	if(d == nil)
		sysfatal("newdev: %r");
	fsmain = initfs(d, doream, flags);
	if(fsmain == nil)
		sysfatal("fsinit: %r");
	initcons(service);
	proccreate(syncproc, nil, mainstacksize);
	start9p(service, nets, stdio);
	threadexits(nil);
}

void
shutdown(void)
{
	wlock(fsmain);
	sync(1);
	dprint("ending\n");
	sleep(1000);
	sync(1);
	threadexitsall(nil);
}