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;
25 int readstr(char*, char*, char*, int);
26 void dolisten(char*, char*, int, char*, char*, long*);
27 void newcall(int, char*, char*, Service*);
28 int findserv(char*, char*, Service*, char*);
29 int getserv(char*, char*, Service*);
31 void scandir(char*, char*, char*, char*);
32 void becomenone(void);
33 void listendir(char*, char*, char*, int);
35 char listenlog[] = "listen";
44 Announce *announcements;
52 error("usage: aux/listen [-iq] [-d srvdir] [-t trustsrvdir] [-n namespace] [-p maxprocs]"
53 " [-a addr] [proto]");
57 main(int argc, char *argv[])
72 cpu = getenv("cputype");
74 error("can't get cputype");
78 addr = EARGF(usage());
81 servdir = EARGF(usage());
87 trustdir = EARGF(usage());
90 namespace = EARGF(usage());
93 maxprocs = atoi(EARGF(usage()));
97 * fixed configuration, no periodic
98 * rescan of the service directory.
106 if(!servdir && !trustdir)
107 servdir = "/bin/service";
109 if(servdir && strlen(servdir) + NAMELEN >= sizeof(s->prog))
110 error("service directory too long");
111 if(trustdir && strlen(trustdir) + NAMELEN >= sizeof(s->prog))
112 error("trusted service directory too long");
124 syslog(0, listenlog, "started on %s", proto);
127 proto = strrchr(proto, '/');
132 listendir(protodir, addr, servdir, 0);
133 listendir(protodir, addr, trustdir, 1);
135 /* command returns */
140 dingdong(void*, char *msg)
142 if(strstr(msg, "alarm") != nil)
148 listendir(char *protodir, char *addr, char *srvdir, int trusted)
151 char dir[40], err[128], ds[128];
161 * insulate ourselves from later
162 * changing of console environment variables
163 * erase privileged crypt state
165 switch(rfork(RFNOTEG|RFPROC|RFFDG|RFNOWAIT|RFENVG|RFNAMEG)) {
174 procsetname("%s %s %s %s", protodir, addr, srvdir, namespace);
181 scandir(proto, protodir, addr, srvdir);
184 * loop through announcements and process trusted services in
185 * invoker's ns and untrusted in none's.
187 for(a = announcements; a; a = a->next){
194 strncpy(ds, a->a, sizeof(ds));
197 /* a process per service */
198 switch(pid = rfork(RFFDG|RFPROC|RFMEM)){
200 syslog(1, listenlog, "couldn't fork for %s", ds);
205 ctl = announce(ds, dir);
207 errstr(err, sizeof err);
210 "giving up on %s: %r",
212 if(strstr(err, "address in use")
214 exits("addr-in-use");
218 dolisten(proto, dir, ctl, srvdir, ds, &childs);
228 * if not running a fixed configuration,
229 * pick up any children that gave up and
230 * sleep for at least 60 seconds.
231 * If a service process dies in a fixed
232 * configuration what should be done -
233 * nothing? restart? restart after a delay?
234 * - currently just wait for something to
235 * die and delay at least 60 seconds
241 while((wm = wait()) != nil) {
242 for(a = announcements; a; a = a->next)
243 if(a->announced == wm->pid) {
245 if (strstr(wm->msg, "addr-in-use") !=
247 /* don't fill log file */
256 scandir(proto, protodir, addr, srvdir);
258 start = 60 - (time(0)-start);
266 * make a list of all services to announce for
269 addannounce(char *str)
273 /* look for duplicate */
275 for(a = announcements; a; a = a->next){
276 if(strcmp(str, a->a) == 0){
284 a = mallocz(sizeof(*a) + strlen(str) + 1, 1);
292 scandir(char *proto, char *protodir, char *addr, char *dname)
300 for(a = announcements; a != nil; a = a->next)
303 fd = open(dname, OREAD);
307 nlen = strlen(proto);
308 while((n=dirread(fd, &db)) > 0){
311 if(db[i].qid.type&QTDIR)
313 if(db[i].length <= 0)
315 if(strncmp(nm, proto, nlen) != 0)
317 snprint(ds, sizeof ds, "%s!%s!%s", protodir, addr, nm + nlen);
326 while((a = *l) != nil){
329 if (a->announced > 0)
330 postnote(PNPROC, a->announced, "die");
343 fd = open("#c/user", OWRITE);
344 if(fd < 0 || write(fd, "none", strlen("none")) < 0)
345 error("can't become none");
347 if(newns("none", namespace) < 0)
348 error("can't build namespace");
352 dolisten(char *proto, char *dir, int ctl, char *srvdir, char *dialstr, long *pchilds)
355 char ndir[40], wbuf[64];
356 int nctl, data, wfd, nowait;
358 procsetname("%s %s", dir, dialstr);
362 if(pchilds && maxprocs > 0){
363 snprint(wbuf, sizeof(wbuf), "/proc/%d/wait", getpid());
364 if((wfd = open(wbuf, OREAD)) >= 0)
374 * check for exited subprocesses
376 if(procs >= maxprocs || (*pchilds % 8) == 0)
379 if(d == nil || d->length == 0){
384 if(read(wfd, wbuf, sizeof(wbuf)) > 0){
390 if(procs >= maxprocs){
392 syslog(1, listenlog, "%s: process limit of %ld reached",
404 * wait for a call (or an error)
406 nctl = listen(dir, ndir);
409 syslog(1, listenlog, "listen: %r");
416 * start a subprocess for the connection
418 switch(rfork(RFFDG|RFPROC|RFMEM|RFENVG|RFNAMEG|RFNOTEG|RFREND|nowait)){
420 reject(nctl, ndir, "host overloaded");
425 * see if we know the service requested
427 memset(&s, 0, sizeof s);
428 if(!findserv(proto, ndir, &s, srvdir)){
430 syslog(1, listenlog, "%s: unknown service '%s' from '%s': %r",
431 proto, s.serv, s.remote);
432 reject(nctl, ndir, "connection refused");
435 data = accept(nctl, ndir);
437 syslog(1, listenlog, "can't open %s/data: %r", ndir);
440 fprint(nctl, "keepalive");
445 newcall(data, proto, ndir, &s);
459 * look in the service directory for the service.
460 * if the shell script or program is zero-length, ignore it,
461 * thus providing a way to disable a service with a bind.
464 findserv(char *proto, char *dir, Service *s, char *srvdir)
469 if(!getserv(proto, dir, s))
471 snprint(s->prog, sizeof s->prog, "%s/%s", srvdir, s->serv);
472 d = dirstat(s->prog);
473 rv = d && d->length > 0; /* ignore unless it's non-empty */
479 * get the service name out of the local address
482 getserv(char *proto, char *dir, Service *s)
484 char addr[128], *serv, *p;
487 readstr(dir, "remote", s->remote, sizeof(s->remote)-1);
488 if(p = utfrune(s->remote, L'\n'))
491 n = readstr(dir, "local", addr, sizeof(addr)-1);
494 if(p = utfrune(addr, L'\n'))
496 serv = utfrune(addr, L'!');
503 * disallow service names like
504 * ../../usr/user/bin/rc/su
506 if(strlen(serv) +strlen(proto) >= NAMELEN || utfrune(serv, L'/') || *serv == '.')
508 snprint(s->serv, sizeof s->serv, "%s%s", proto, serv);
514 remoteaddr(char *dir)
519 snprint(buf, sizeof buf, "%s/remote", dir);
520 fd = open(buf, OREAD);
523 n = read(fd, buf, sizeof(buf));
527 p = strchr(buf, '!');
536 newcall(int fd, char *proto, char *dir, Service *s)
538 char data[4*NAMELEN];
544 syslog(0, listenlog, "%s call for %s on chan %s (%s)", proto, s->serv, dir, p);
547 syslog(0, listenlog, "%s call for %s on chan %s", proto, s->serv, dir);
550 snprint(data, sizeof data, "%s/data", dir);
551 bind(data, "/dev/cons", MREPL);
554 /* dup(fd, 2); keep stderr */
560 for(fd=3; fd<20; fd++)
562 execl(s->prog, s->prog, s->serv, proto, dir, nil);
569 syslog(1, listenlog, "%s: %s: %r", proto, s);
574 * read a string from a device
577 readstr(char *dir, char *info, char *s, int len)
580 char buf[3*NAMELEN+4];
582 snprint(buf, sizeof buf, "%s/%s", dir, info);
583 fd = open(buf, OREAD);
587 n = read(fd, s, len-1);