shithub: riscv

Download patch

ref: 0b22e8b45565e9781e829f7148ceecc7cbbdae4c
parent: 623901ed1fce6212619537ad4a5eb01dc73881b5
author: cinap_lenrek <[email protected]>
date: Wed Jun 27 11:54:42 EDT 2012

aan: use netconninfo from dial(2), threadsetname

--- a/sys/src/cmd/aan.c
+++ b/sys/src/cmd/aan.c
@@ -66,11 +66,8 @@
 static int 		sendcommand(ulong, ulong);
 static void		showmsg(int, char *, Buf *);
 static int		writen(int, uchar *, int);
-static int		getport(char *);
 static void		dmessage(int, char *, ...);
 static void		timerproc(void *);
-static Endpoints*	getendpoints(char *);
-static void		freeendpoints(Endpoints *);
 
 static void
 usage(void)
@@ -103,18 +100,6 @@
 	return v;
 }
 
-static char*
-estrdup(char *s)
-{
-	char *v;
-	int n;
-
-	n = strlen(s)+1;
-	v = emalloc(n);
-	memmove(v, s, n);
-	return v;
-}
-
 void
 threadmain(int argc, char **argv)
 {
@@ -259,6 +244,8 @@
 	int n;
 	Buf *b;
 
+	threadsetname("fromclient");
+
 	do {
 		b = recvp(empty);
 		n = read(0, b->buf, Bufsize);
@@ -284,6 +271,8 @@
 	int n, m, len, acked;
 	Buf *b;
 
+	threadsetname("fromnet");
+
 	b = emalloc(sizeof(Buf));
 	while (!done) {
 		while (netfd < 0) {
@@ -366,6 +355,7 @@
 static void
 reconnect(int secs)
 {
+	NetConnInfo *nci;
 	char ldir[40];
 	int lcfd, fd;
 
@@ -372,7 +362,7 @@
 	if (dialstring) {
 		syslog(0, Logname, "dialing %s", dialstring);
 		alarm(secs*1000);
-  		while ((fd = dial(dialstring, nil, nil, nil)) < 0) {
+  		while ((fd = dial(dialstring, nil, ldir, nil)) < 0) {
 			char err[32];
 
 			err[0] = '\0';
@@ -388,8 +378,6 @@
 		syslog(0, Logname, "reconnected to %s", dialstring);
 	} 
 	else {
-		Endpoints *ep;
-
 		syslog(0, Logname, "waiting for connection on %s", devdir);
 		alarm(secs*1000);
  		if ((lcfd = listen(devdir, ldir)) < 0) 
@@ -398,12 +386,14 @@
 			sysfatal("reconnect; cannot accept; %r");
 		alarm(0);
 		close(lcfd);
-		
-		ep = getendpoints(ldir);
-		dmessage(1, "rsys '%s'\n", ep->rsys);
-		syslog(0, Logname, "connected from %s", ep->rsys);
-		freeendpoints(ep);
 	}
+
+	if(nci = getnetconninfo(ldir, fd)){
+		syslog(0, Logname, "connected from %s", nci->rsys);
+		threadsetname(client? "client %s %s" : "server %s %s", ldir, nci->rsys);
+		freenetconninfo(nci);
+	} else
+		syslog(0, Logname, "connected");
 	
 	// Wakes up the netreader.
 	netfd = fd;
@@ -474,6 +464,9 @@
 timerproc(void *x)
 {
 	Channel *timer = x;
+
+	threadsetname("timer");
+
 	while (!done) {
 		sleep((Synctime / MS(1)) >> 1);
 		sendp(timer, "timer");
@@ -491,57 +484,4 @@
 	va_start(arg, fmt);
 	vfprint(2, fmt, arg);
 	va_end(arg);
-}
-
-static void
-getendpoint(char *dir, char *file, char **sysp, char **servp)
-{
-	int fd, n;
-	char buf[128];
-	char *sys, *serv;
-
-	sys = serv = 0;
-
-	snprint(buf, sizeof buf, "%s/%s", dir, file);
-	fd = open(buf, OREAD);
-	if(fd >= 0){
-		n = read(fd, buf, sizeof(buf)-1);
-		if(n>0){
-			buf[n-1] = 0;
-			serv = strchr(buf, '!');
-			if(serv){
-				*serv++ = 0;
-				serv = estrdup(serv);
-			}
-			sys = estrdup(buf);
-		}
-		close(fd);
-	}
-	if(serv == 0)
-		serv = estrdup("unknown");
-	if(sys == 0)
-		sys = estrdup("unknown");
-	*servp = serv;
-	*sysp = sys;
-}
-
-static Endpoints *
-getendpoints(char *dir)
-{
-	Endpoints *ep;
-
-	ep = emalloc(sizeof(*ep));
-	getendpoint(dir, "local", &ep->lsys, &ep->lserv);
-	getendpoint(dir, "remote", &ep->rsys, &ep->rserv);
-	return ep;
-}
-
-static void
-freeendpoints(Endpoints *ep)
-{
-	free(ep->lsys);
-	free(ep->rsys);
-	free(ep->lserv);
-	free(ep->rserv);
-	free(ep);
 }