aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/fossil/9lstn.c
blob: 4f9607294afc53756669fd8a13a2940fb9c393ac (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "stdinc.h"

#include "9.h"

typedef struct Lstn Lstn;
struct Lstn {
	int	afd;
	int	flags;
	char*	address;
	char	dir[NETPATHLEN];

	Lstn*	next;
	Lstn*	prev;
};

static struct {
	RWLock	lock;

	Lstn*	head;
	Lstn*	tail;
} lbox;

static void
lstnFree(Lstn* lstn)
{
	wlock(&lbox.lock);
	if(lstn->prev != nil)
		lstn->prev->next = lstn->next;
	else
		lbox.head = lstn->next;
	if(lstn->next != nil)
		lstn->next->prev = lstn->prev;
	else
		lbox.tail = lstn->prev;
	wunlock(&lbox.lock);

	if(lstn->afd != -1)
		close(lstn->afd);
	vtfree(lstn->address);
	vtfree(lstn);
}

static void
lstnListen(void* a)
{
	Lstn *lstn;
	int dfd, lfd;
	char newdir[NETPATHLEN];

 	threadsetname("listen");

	lstn = a;
	for(;;){
		if((lfd = listen(lstn->dir, newdir)) < 0){
			fprint(2, "listen: listen '%s': %r", lstn->dir);
			break;
		}
		if((dfd = accept(lfd, newdir)) >= 0)
			conAlloc(dfd, newdir, lstn->flags);
		else
			fprint(2, "listen: accept %s: %r\n", newdir);
		close(lfd);
	}
	lstnFree(lstn);
}

static Lstn*
lstnAlloc(char* address, int flags)
{
	int afd;
	Lstn *lstn;
	char dir[NETPATHLEN];

	wlock(&lbox.lock);
	for(lstn = lbox.head; lstn != nil; lstn = lstn->next){
		if(strcmp(lstn->address, address) != 0)
			continue;
		werrstr("listen: already serving '%s'", address);
		wunlock(&lbox.lock);
		return nil;
	}

	if((afd = announce(address, dir)) < 0){
		werrstr("listen: announce '%s': %r", address);
		wunlock(&lbox.lock);
		return nil;
	}

	lstn = vtmallocz(sizeof(Lstn));
	lstn->afd = afd;
	lstn->address = vtstrdup(address);
	lstn->flags = flags;
	memmove(lstn->dir, dir, NETPATHLEN);

	if(lbox.tail != nil){
		lstn->prev = lbox.tail;
		lbox.tail->next = lstn;
	}
	else{
		lbox.head = lstn;
		lstn->prev = nil;
	}
	lbox.tail = lstn;
	wunlock(&lbox.lock);

	if(proccreate(lstnListen, lstn, STACK) < 0){
		werrstr("listen: thread '%s': %r", lstn->address);
		lstnFree(lstn);
		return nil;
	}

	return lstn;
}

static int
cmdLstn(int argc, char* argv[])
{
	int dflag, flags;
	Lstn *lstn;
	char *usage = "usage: listen [-dIN] [address]";

	dflag = 0;
	flags = 0;
	ARGBEGIN{
	default:
		return cliError(usage);
	case 'd':
		dflag = 1;
		break;
	case 'I':
		flags |= ConIPCheck;
		break;
	case 'N':
		flags |= ConNoneAllow;
		break;
	}ARGEND

	switch(argc){
	default:
		return cliError(usage);
	case 0:
		rlock(&lbox.lock);
		for(lstn = lbox.head; lstn != nil; lstn = lstn->next)
			consPrint("\t%s\t%s\n", lstn->address, lstn->dir);
		runlock(&lbox.lock);
		break;
	case 1:
		if(!dflag){
			if(lstnAlloc(argv[0], flags) == nil)
				return 0;
			break;
		}

		wlock(&lbox.lock);
		for(lstn = lbox.head; lstn != nil; lstn = lstn->next){
			if(strcmp(lstn->address, argv[0]) != 0)
				continue;
			if(lstn->afd != -1){
				close(lstn->afd);
				lstn->afd = -1;
			}
			break;
		}
		wunlock(&lbox.lock);

		if(lstn == nil){
			werrstr("listen: '%s' not found", argv[0]);
			return 0;
		}
		break;
	}

	return 1;
}

int
lstnInit(void)
{
	cliAddCmd("listen", cmdLstn);

	return 1;
}