shithub: riscv

Download patch

ref: 958d698bf8b58bf30357882242b8fc84c93116ae
parent: a0e001a234796825f809e28c48880932b9e2aea9
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Fri Nov 7 03:10:19 EST 2014

libc: improve dial error handling

when dial is called with a generic dialstring, it will try
/net and /net.alt in sequence. error out if the /net dial
gets interrupted and do not continue dialing /net.alt.

reduce stack usage by using the swaping nature of errstr()
instead of keeping two error string buffers on the stack.

--- a/sys/src/libc/9sys/dial.c
+++ b/sys/src/libc/9sys/dial.c
@@ -35,7 +35,7 @@
 {
 	DS ds;
 	int rv;
-	char err[ERRMAX], alterr[ERRMAX];
+	char err[ERRMAX];
 
 	ds.local = local;
 	ds.dir = dir;
@@ -49,23 +49,20 @@
 	rv = csdial(&ds);
 	if(rv >= 0)
 		return rv;
-	err[0] = '\0';
+	*err = 0;
 	errstr(err, sizeof err);
-	if(strstr(err, "refused") != 0){
-		werrstr("%s", err);
+	if(strcmp(err, "interrupted") == 0 || strstr(err, "refused") != nil){
+		errstr(err, sizeof err);
 		return rv;
 	}
+
 	ds.netdir = "/net.alt";
 	rv = csdial(&ds);
 	if(rv >= 0)
 		return rv;
-
-	alterr[0] = 0;
-	errstr(alterr, sizeof alterr);
-	if(strstr(alterr, "translate") || strstr(alterr, "does not exist"))
-		werrstr("%s", err);
-	else
-		werrstr("%s", alterr);
+	errstr(err, sizeof err);
+	if(strstr(err, "translate") == nil && strstr(err, "does not exist") == nil)
+		errstr(err, sizeof err);
 	return rv;
 }
 
@@ -73,7 +70,7 @@
 csdial(DS *ds)
 {
 	int n, fd, rv;
-	char *p, buf[Maxstring], clone[Maxpath], err[ERRMAX], besterr[ERRMAX];
+	char *p, buf[Maxstring], clone[Maxpath], err[ERRMAX];
 
 	/*
 	 *  open connection server
@@ -101,7 +98,6 @@
 	 */
 	rv = -1;
 	*err = 0;
-	*besterr = 0;
 	seek(fd, 0, 0);
 	while((n = read(fd, buf, sizeof(buf) - 1)) > 0){
 		buf[n] = 0;
@@ -112,18 +108,17 @@
 		rv = call(buf, p, ds);
 		if(rv >= 0)
 			break;
-		*err = 0;
 		errstr(err, sizeof err);
 		if(strcmp(err, "interrupted") == 0)
 			break;
-		if(strstr(err, "does not exist") == 0)
-			strcpy(besterr, err);
+		if(strstr(err, "does not exist") != nil)
+			errstr(err, sizeof err);	/* get previous error back */
 	}
 	close(fd);
 
 	/* restore errstr if any */
 	if(rv < 0 && *err)
-		errstr(*besterr ? besterr : err, sizeof err);
+		errstr(err, sizeof err);
 
 	return rv;
 }