5 #define NAMELEN 64 /* reasonable upper limit for name elements */
7 typedef struct Service Service;
10 char serv[NAMELEN]; /* name of the service */
11 char remote[3*NAMELEN]; /* address of remote system */
12 char prog[5*NAMELEN+1]; /* program to execute */
15 typedef struct Announce Announce;
24 int readstr(char*, char*, char*, int);
25 void dolisten(char*, char*, int, char*, char*, long*);
26 void newcall(int, char*, char*, Service*);
27 int findserv(char*, char*, Service*, char*);
28 int getserv(char*, char*, Service*);
30 void scandir(char*, char*, char*);
31 void becomenone(void);
32 void listendir(char*, char*, int);
34 char listenlog[] = "listen";
42 Announce *announcements;
50 error("usage: aux/listen [-q] [-n namespace] [-d servdir] [-t trustdir] [-p maxprocs]"
55 * based on libthread's threadsetname, but drags in less library code.
56 * actually just sets the arguments displayed.
59 procsetname(char *fmt, ...)
67 cmdname = vsmprint(fmt, arg);
71 snprint(buf, sizeof buf, "#p/%d/args", getpid());
72 if((fd = open(buf, OWRITE)) >= 0){
73 write(fd, cmdname, strlen(cmdname)+1);
80 main(int argc, char *argv[])
94 cpu = getenv("cputype");
96 error("can't get cputype");
100 servdir = EARGF(usage());
106 trustdir = EARGF(usage());
109 namespace = EARGF(usage());
112 maxprocs = atoi(EARGF(usage()));
116 * fixed configuration, no periodic
117 * rescan of the service directory.
125 if(!servdir && !trustdir)
126 servdir = "/bin/service";
128 if(servdir && strlen(servdir) + NAMELEN >= sizeof(s->prog))
129 error("service directory too long");
130 if(trustdir && strlen(trustdir) + NAMELEN >= sizeof(s->prog))
131 error("trusted service directory too long");
143 syslog(0, listenlog, "started on %s", proto);
146 proto = strrchr(proto, '/');
151 listendir(protodir, servdir, 0);
152 listendir(protodir, trustdir, 1);
154 /* command returns */
159 dingdong(void*, char *msg)
161 if(strstr(msg, "alarm") != nil)
167 listendir(char *protodir, char *srvdir, int trusted)
170 char dir[40], err[128], ds[128];
180 * insulate ourselves from later
181 * changing of console environment variables
182 * erase privileged crypt state
184 switch(rfork(RFNOTEG|RFPROC|RFFDG|RFNOWAIT|RFENVG|RFNAMEG)) {
193 procsetname("%s %s %s", protodir, srvdir, namespace);
200 scandir(proto, protodir, srvdir);
203 * loop through announcements and process trusted services in
204 * invoker's ns and untrusted in none's.
206 for(a = announcements; a; a = a->next){
213 strncpy(ds, a->a, sizeof(ds));
216 /* a process per service */
217 switch(pid = rfork(RFFDG|RFPROC|RFMEM)){
219 syslog(1, listenlog, "couldn't fork for %s", ds);
224 ctl = announce(ds, dir);
226 errstr(err, sizeof err);
229 "giving up on %s: %r",
231 if(strstr(err, "address in use")
233 exits("addr-in-use");
237 dolisten(proto, dir, ctl, srvdir, ds, &childs);
247 * if not running a fixed configuration,
248 * pick up any children that gave up and
249 * sleep for at least 60 seconds.
250 * If a service process dies in a fixed
251 * configuration what should be done -
252 * nothing? restart? restart after a delay?
253 * - currently just wait for something to
254 * die and delay at least 60 seconds
260 while((wm = wait()) != nil) {
261 for(a = announcements; a; a = a->next)
262 if(a->announced == wm->pid) {
264 if (strstr(wm->msg, "addr-in-use") !=
266 /* don't fill log file */
275 scandir(proto, protodir, srvdir);
277 start = 60 - (time(0)-start);
285 * make a list of all services to announce for
288 addannounce(char *str)
292 /* look for duplicate */
294 for(a = announcements; a; a = a->next){
295 if(strcmp(str, a->a) == 0)
301 a = mallocz(sizeof(*a) + strlen(str) + 1, 1);
304 a->a = ((char*)a)+sizeof(*a);
311 * delete a service for announcement list
314 delannounce(char *str)
318 /* look for service */
320 for(a = announcements; a; a = a->next){
321 if(strcmp(str, a->a) == 0)
327 *l = a->next; /* drop from the list */
328 if (a->announced > 0)
329 postnote(PNPROC, a->announced, "die");
335 ignore(char *srvdir, char *name)
338 char *file = smprint("%s/%s", srvdir, name);
339 Dir *d = dirstat(file);
341 rv = !d || d->length <= 0; /* ignore unless it's non-empty */
348 scandir(char *proto, char *protodir, char *dname)
355 fd = open(dname, OREAD);
359 nlen = strlen(proto);
360 while((n=dirread(fd, &db)) > 0){
363 if(!(db[i].qid.type&QTDIR) &&
364 strncmp(nm, proto, nlen) == 0) {
365 snprint(ds, sizeof ds, "%s!*!%s", protodir,
367 if (ignore(dname, nm))
384 fd = open("#c/user", OWRITE);
385 if(fd < 0 || write(fd, "none", strlen("none")) < 0)
386 error("can't become none");
388 if(newns("none", namespace) < 0)
389 error("can't build namespace");
393 dolisten(char *proto, char *dir, int ctl, char *srvdir, char *dialstr, long *pchilds)
396 char ndir[40], wbuf[64];
397 int nctl, data, wfd, nowait;
399 procsetname("%s %s", dir, dialstr);
403 if(pchilds && maxprocs > 0){
404 snprint(wbuf, sizeof(wbuf), "/proc/%d/wait", getpid());
405 if((wfd = open(wbuf, OREAD)) >= 0)
415 * check for exited subprocesses
417 if(procs >= maxprocs || (*pchilds % 8) == 0)
420 if(d == nil || d->length == 0){
425 if(read(wfd, wbuf, sizeof(wbuf)) > 0){
431 if(procs >= maxprocs){
433 syslog(1, listenlog, "%s: process limit of %ld reached",
445 * wait for a call (or an error)
447 nctl = listen(dir, ndir);
450 syslog(1, listenlog, "listen: %r");
457 * start a subprocess for the connection
459 switch(rfork(RFFDG|RFPROC|RFMEM|RFENVG|RFNAMEG|RFNOTEG|RFREND|nowait)){
461 reject(nctl, ndir, "host overloaded");
466 * see if we know the service requested
468 memset(&s, 0, sizeof s);
469 if(!findserv(proto, ndir, &s, srvdir)){
471 syslog(1, listenlog, "%s: unknown service '%s' from '%s': %r",
472 proto, s.serv, s.remote);
473 reject(nctl, ndir, "connection refused");
476 data = accept(nctl, ndir);
478 syslog(1, listenlog, "can't open %s/data: %r", ndir);
481 fprint(nctl, "keepalive");
486 newcall(data, proto, ndir, &s);
500 * look in the service directory for the service.
501 * if the shell script or program is zero-length, ignore it,
502 * thus providing a way to disable a service with a bind.
505 findserv(char *proto, char *dir, Service *s, char *srvdir)
510 if(!getserv(proto, dir, s))
512 snprint(s->prog, sizeof s->prog, "%s/%s", srvdir, s->serv);
513 d = dirstat(s->prog);
514 rv = d && d->length > 0; /* ignore unless it's non-empty */
520 * get the service name out of the local address
523 getserv(char *proto, char *dir, Service *s)
525 char addr[128], *serv, *p;
528 readstr(dir, "remote", s->remote, sizeof(s->remote)-1);
529 if(p = utfrune(s->remote, L'\n'))
532 n = readstr(dir, "local", addr, sizeof(addr)-1);
535 if(p = utfrune(addr, L'\n'))
537 serv = utfrune(addr, L'!');
544 * disallow service names like
545 * ../../usr/user/bin/rc/su
547 if(strlen(serv) +strlen(proto) >= NAMELEN || utfrune(serv, L'/') || *serv == '.')
549 snprint(s->serv, sizeof s->serv, "%s%s", proto, serv);
555 remoteaddr(char *dir)
560 snprint(buf, sizeof buf, "%s/remote", dir);
561 fd = open(buf, OREAD);
564 n = read(fd, buf, sizeof(buf));
568 p = strchr(buf, '!');
577 newcall(int fd, char *proto, char *dir, Service *s)
579 char data[4*NAMELEN];
585 syslog(0, listenlog, "%s call for %s on chan %s (%s)", proto, s->serv, dir, p);
588 syslog(0, listenlog, "%s call for %s on chan %s", proto, s->serv, dir);
591 snprint(data, sizeof data, "%s/data", dir);
592 bind(data, "/dev/cons", MREPL);
595 /* dup(fd, 2); keep stderr */
601 for(fd=3; fd<20; fd++)
603 execl(s->prog, s->prog, s->serv, proto, dir, nil);
610 syslog(1, listenlog, "%s: %s: %r", proto, s);
615 * read a string from a device
618 readstr(char *dir, char *info, char *s, int len)
621 char buf[3*NAMELEN+4];
623 snprint(buf, sizeof buf, "%s/%s", dir, info);
624 fd = open(buf, OREAD);
628 n = read(fd, s, len-1);