summaryrefslogtreecommitdiff
path: root/svc/src/fs.c
blob: 63150fbbdf92688adca3de749907b7cabda31619 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * svcfs - the control filesystem init serves.
 *
 * This runs in a proc of its own, sharing memory with the supervisor,
 * so everything it touches is under statelock.  It never starts or
 * stops anything itself: a service has to be a child of the supervisor
 * for the supervisor to wait for it, so a write here is validated now
 * and queued for the supervisor to carry out on its next tick.
 */
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include "dat.h"

enum
{
	Qctl,
	Qstatus,
	Qargs,
	Qpid,
	Qlog,
};

typedef struct Fref Fref;
struct Fref
{
	int	type;
	Svc	*svc;		/* nil for the root ctl and status */
};

static char *rootctltext =
	"start <name>     start a service\n"
	"stop <name>      stop a service\n"
	"restart <name>   stop it, then start it\n"
	"reset            stop every service, then start them again\n";

static char *svcctltext =
	"start\n"
	"stop\n"
	"restart\n";

static Fref*
fref(int type, Svc *s)
{
	Fref *f;

	f = mallocz(sizeof *f, 1);
	if(f == nil)
		sysfatal("out of memory");
	f->type = type;
	f->svc = s;
	return f;
}

static void
fsread(Req *r)
{
	Fref *f;
	char *s;
	char buf[64];
	int fd, n;
	char path[512];

	f = r->fid->file->aux;
	if(f == nil){
		respond(r, "not a readable file");
		return;
	}

	switch(f->type){
	case Qctl:
		readstr(r, f->svc == nil ? rootctltext : svcctltext);
		break;

	case Qstatus:
		qlock(&statelock);
		s = f->svc == nil ? allstatus() : statusline(f->svc);
		qunlock(&statelock);
		if(s == nil){
			respond(r, "out of memory");
			return;
		}
		readstr(r, s);
		free(s);
		break;

	case Qargs:
		qlock(&statelock);
		s = smprint("%s", f->svc->exec);
		qunlock(&statelock);
		if(s == nil){
			respond(r, "out of memory");
			return;
		}
		readstr(r, s);
		free(s);
		break;

	case Qpid:
		qlock(&statelock);
		if(f->svc->pid == 0)
			buf[0] = '\0';
		else
			snprint(buf, sizeof buf, "%d\n", f->svc->pid);
		qunlock(&statelock);
		readstr(r, buf);
		break;

	case Qlog:
		snprint(path, sizeof path, "%s/%s", logdir, f->svc->name);
		fd = open(path, OREAD);
		if(fd < 0){
			/* no output yet is not an error */
			r->ofcall.count = 0;
			break;
		}
		seek(fd, r->ifcall.offset, 0);
		n = read(fd, r->ofcall.data, r->ifcall.count);
		close(fd);
		if(n < 0){
			responderror(r);
			return;
		}
		r->ofcall.count = n;
		break;

	default:
		respond(r, "not a readable file");
		return;
	}
	respond(r, nil);
}

static void
fswrite(Req *r)
{
	Fref *f;
	char *buf, *err;
	char *fld[3];
	int nf;

	f = r->fid->file->aux;
	if(f == nil || f->type != Qctl){
		respond(r, "not a writable file");
		return;
	}

	buf = mallocz(r->ifcall.count+1, 1);
	if(buf == nil){
		respond(r, "out of memory");
		return;
	}
	memmove(buf, r->ifcall.data, r->ifcall.count);
	buf[r->ifcall.count] = '\0';

	nf = tokenize(buf, fld, nelem(fld));
	if(nf == 0){
		free(buf);
		respond(r, "no command");
		return;
	}

	if(f->svc != nil){
		/*
		 * A per-service ctl takes no name: which file you wrote to
		 * already said which service you meant.
		 */
		if(nf != 1){
			free(buf);
			respond(r, "this ctl takes no arguments; "
				"try start, stop or restart");
			return;
		}
		err = queuecmd(fld[0], f->svc->name);
	}else if(strcmp(fld[0], "reset") == 0)
		err = queuecmd("reset", nil);
	else if(nf != 2){
		free(buf);
		respond(r, "usage: start, stop or restart <service>, or reset");
		return;
	}else
		err = queuecmd(fld[0], fld[1]);

	free(buf);
	if(err != nil){
		respond(r, err);
		return;
	}
	r->ofcall.count = r->ifcall.count;
	respond(r, nil);
}

static Srv fs =
{
.read=	fsread,
.write=	fswrite,
};

void
startfs(char *srvname, char *mtpt)
{
	Svc *s;
	File *d;
	Tree *t;
	char *me;

	/*
	 * Files belong to whoever is running us, so that the host owner
	 * can write the ctl files.  They are world readable: reading a
	 * ctl file is how you find out what it accepts, which is no use
	 * if only one user may do it.  Writing is what wants restricting.
	 */
	me = getuser();
	if(me == nil)
		me = "none";

	t = alloctree(me, me, DMDIR|0555, nil);
	if(t == nil)
		sysfatal("alloctree: %r");
	fs.tree = t;

	createfile(t->root, "ctl", me, 0644, fref(Qctl, nil));
	createfile(t->root, "status", me, 0444, fref(Qstatus, nil));

	for(s = svcs; s != nil; s = s->next){
		d = createfile(t->root, s->name, me, DMDIR|0555, nil);
		if(d == nil){
			fprint(2, "init: svcfs: create %s: %r\n", s->name);
			continue;
		}
		createfile(d, "ctl", me, 0644, fref(Qctl, s));
		createfile(d, "status", me, 0444, fref(Qstatus, s));
		createfile(d, "args", me, 0444, fref(Qargs, s));
		createfile(d, "pid", me, 0444, fref(Qpid, s));
		createfile(d, "log", me, 0444, fref(Qlog, s));
	}

	postmountsrv(&fs, srvname, mtpt, MREPL);
}