summaryrefslogtreecommitdiff
path: root/sys/src/cmd/vnc/exporter.c
blob: bf5198bc3d31d8225ce5dfb8b757850278f1f16a (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
#include <u.h>
#include <libc.h>
#include "compat.h"

typedef struct Exporter	Exporter;
struct Exporter
{
	int	fd;
	Chan	**roots;
	int	nroots;
};

int
mounter(char *mntpt, int how, int fd, int n)
{
	char buf[32];
	int i, ok, mfd;

	ok = 1;
	for(i = 0; i < n; i++){
		snprint(buf, sizeof buf, "%d", i);
		mfd = dup(fd, -1);
		if(mount(mfd, -1, mntpt, how, buf) == -1){
			close(mfd);
			fprint(2, "can't mount on %s: %r\n", mntpt);
			ok = 0;
			break;
		}
		close(mfd);
		if(how == MREPL)
			how = MAFTER;
	}

	close(fd);

	return ok;
}

static void
extramp(void *v)
{
	Exporter *ex;

	rfork(RFNAMEG);
	ex = v;
	sysexport(ex->fd, ex->roots, ex->nroots);
	shutdown();
	exits(nil);
}

int
exporter(Dev **dt, int *fd, int *sfd)
{
	Chan **roots;
	Exporter ex;
	int p[2], i, n, ed;

	for(n = 0; dt[n] != nil; n++)
		;
	if(!n){
		werrstr("no devices specified");
		return 0;
	}

	ed = errdepth(-1);
	if(waserror()){
		werrstr(up->error);
		return 0;
	}

	roots = smalloc(n * sizeof *roots);
	for(i = 0; i < n; i++){
		(*dt[i]->reset)();
		(*dt[i]->init)();
		roots[i] = (*dt[i]->attach)("");
	}
	poperror();
	errdepth(ed);

	if(pipe(p) < 0){
		werrstr("can't make pipe: %r");
		return 0;
	}

	*sfd = p[0];
	*fd = p[1];

	ex.fd = *sfd;
	ex.roots = roots;
	ex.nroots = n;
	kproc("exporter", extramp, &ex);

	return n;
}