aux/listen: remove argument clutter

addr and proto are globals. protodir should be
a global. let's also avoid reading the remote
addr more then once.
This commit is contained in:
Jacob Moody 2022-05-20 04:05:49 +00:00
parent a8bf0b365d
commit 25ad126d8c

View file

@ -23,14 +23,14 @@ struct Announce
}; };
int readstr(char*, char*, char*, int); int readstr(char*, char*, char*, int);
void dolisten(char*, char*, int, char*, char*, long*); void dolisten(char*, int, char*, char*, long*);
void newcall(int, char*, char*, Service*); void newcall(int, char*, Service*);
int findserv(char*, char*, Service*, char*); int findserv(char*, Service*, char*);
int getserv(char*, char*, Service*); int getserv(char*, Service*);
void error(char*); void error(char*);
void scandir(char*, char*, char*, char*); void scandir(char*);
void becomenone(void); void becomenone(void);
void listendir(char*, char*, char*, int); void listendir(char*, int);
char listenlog[] = "listen"; char listenlog[] = "listen";
@ -39,6 +39,7 @@ long maxprocs;
int quiet; int quiet;
int immutable; int immutable;
char *proto; char *proto;
char *protodir;
char *addr; char *addr;
Announce *announcements; Announce *announcements;
#define SEC 1000 #define SEC 1000
@ -56,7 +57,6 @@ void
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
Service *s; Service *s;
char *protodir;
char *trustdir; char *trustdir;
char *servdir; char *servdir;
@ -125,8 +125,8 @@ main(int argc, char *argv[])
proto = protodir; proto = protodir;
else else
proto++; proto++;
listendir(protodir, addr, servdir, 0); listendir(servdir, 0);
listendir(protodir, addr, trustdir, 1); listendir(trustdir, 1);
/* command returns */ /* command returns */
exits(0); exits(0);
@ -141,7 +141,7 @@ dingdong(void*, char *msg)
} }
void void
listendir(char *protodir, char *addr, char *srvdir, int trusted) listendir(char *srvdir, int trusted)
{ {
int ctl, pid, start; int ctl, pid, start;
char dir[40], err[128], ds[128]; char dir[40], err[128], ds[128];
@ -174,7 +174,7 @@ listendir(char *protodir, char *addr, char *srvdir, int trusted)
notify(dingdong); notify(dingdong);
pid = getpid(); pid = getpid();
scandir(proto, protodir, addr, srvdir); scandir(srvdir);
for(;;){ for(;;){
/* /*
* loop through announcements and process trusted services in * loop through announcements and process trusted services in
@ -211,7 +211,7 @@ listendir(char *protodir, char *addr, char *srvdir, int trusted)
else else
exits("ctl"); exits("ctl");
} }
dolisten(proto, dir, ctl, srvdir, ds, &childs); dolisten(dir, ctl, srvdir, ds, &childs);
close(ctl); close(ctl);
} }
default: default:
@ -249,7 +249,7 @@ listendir(char *protodir, char *addr, char *srvdir, int trusted)
} }
if(!immutable){ if(!immutable){
alarm(0); alarm(0);
scandir(proto, protodir, addr, srvdir); scandir(srvdir);
} }
start = 60 - (time(0)-start); start = 60 - (time(0)-start);
if(start > 0) if(start > 0)
@ -285,7 +285,7 @@ addannounce(char *str)
} }
void void
scandir(char *proto, char *protodir, char *addr, char *dname) scandir(char *dname)
{ {
Announce *a, **l; Announce *a, **l;
int fd, i, n, nlen; int fd, i, n, nlen;
@ -341,7 +341,7 @@ becomenone(void)
} }
void void
dolisten(char *proto, char *dir, int ctl, char *srvdir, char *dialstr, long *pchilds) dolisten(char *dir, int ctl, char *srvdir, char *dialstr, long *pchilds)
{ {
Service s; Service s;
char ndir[40], wbuf[64]; char ndir[40], wbuf[64];
@ -417,7 +417,7 @@ dolisten(char *proto, char *dir, int ctl, char *srvdir, char *dialstr, long *pch
* see if we know the service requested * see if we know the service requested
*/ */
memset(&s, 0, sizeof s); memset(&s, 0, sizeof s);
if(!findserv(proto, ndir, &s, srvdir)){ if(!findserv(ndir, &s, srvdir)){
if(!quiet) if(!quiet)
syslog(1, listenlog, "%s: unknown service '%s' from '%s': %r", syslog(1, listenlog, "%s: unknown service '%s' from '%s': %r",
proto, s.serv, s.remote); proto, s.serv, s.remote);
@ -434,7 +434,7 @@ dolisten(char *proto, char *dir, int ctl, char *srvdir, char *dialstr, long *pch
close(nctl); close(nctl);
if(wfd >= 0) if(wfd >= 0)
close(wfd); close(wfd);
newcall(data, proto, ndir, &s); newcall(data, ndir, &s);
exits(0); exits(0);
default: default:
close(nctl); close(nctl);
@ -453,12 +453,12 @@ dolisten(char *proto, char *dir, int ctl, char *srvdir, char *dialstr, long *pch
* thus providing a way to disable a service with a bind. * thus providing a way to disable a service with a bind.
*/ */
int int
findserv(char *proto, char *dir, Service *s, char *srvdir) findserv(char *dir, Service *s, char *srvdir)
{ {
int rv; int rv;
Dir *d; Dir *d;
if(!getserv(proto, dir, s)) if(!getserv(dir, s))
return 0; return 0;
snprint(s->prog, sizeof s->prog, "%s/%s", srvdir, s->serv); snprint(s->prog, sizeof s->prog, "%s/%s", srvdir, s->serv);
d = dirstat(s->prog); d = dirstat(s->prog);
@ -471,7 +471,7 @@ findserv(char *proto, char *dir, Service *s, char *srvdir)
* get the service name out of the local address * get the service name out of the local address
*/ */
int int
getserv(char *proto, char *dir, Service *s) getserv(char *dir, Service *s)
{ {
char addr[128], *serv, *p; char addr[128], *serv, *p;
long n; long n;
@ -479,6 +479,8 @@ getserv(char *proto, char *dir, Service *s)
readstr(dir, "remote", s->remote, sizeof(s->remote)-1); readstr(dir, "remote", s->remote, sizeof(s->remote)-1);
if(p = utfrune(s->remote, '\n')) if(p = utfrune(s->remote, '\n'))
*p = '\0'; *p = '\0';
if(p = utfrune(s->remote, '!'))
*p = '\0';
n = readstr(dir, "local", addr, sizeof(addr)-1); n = readstr(dir, "local", addr, sizeof(addr)-1);
if(n <= 0) if(n <= 0)
@ -493,42 +495,13 @@ getserv(char *proto, char *dir, Service *s)
return 1; return 1;
} }
char *
remoteaddr(char *dir)
{
char buf[128], *p;
int n, fd;
snprint(buf, sizeof buf, "%s/remote", dir);
fd = open(buf, OREAD);
if(fd < 0)
return strdup("");
n = read(fd, buf, sizeof(buf));
close(fd);
if(n > 0){
buf[n] = 0;
p = strchr(buf, '!');
if(p)
*p = 0;
return strdup(buf);
}
return strdup("");
}
void void
newcall(int fd, char *proto, char *dir, Service *s) newcall(int fd, char *dir, Service *s)
{ {
char data[4*NAMELEN]; char data[4*NAMELEN];
char *p;
if(!quiet){ if(!quiet)
if(dir != nil){ syslog(0, listenlog, "%s call for %s on chan %s (%s)", proto, s->serv, dir, s->remote);
p = remoteaddr(dir);
syslog(0, listenlog, "%s call for %s on chan %s (%s)", proto, s->serv, dir, p);
free(p);
} else
syslog(0, listenlog, "%s call for %s on chan %s", proto, s->serv, dir);
}
snprint(data, sizeof data, "%s/data", dir); snprint(data, sizeof data, "%s/data", dir);
bind(data, "/dev/cons", MREPL); bind(data, "/dev/cons", MREPL);