shithub: riscv

Download patch

ref: 5364fa720de3b963a88dc4810ed83b4f2ab11d12
parent: 958d698bf8b58bf30357882242b8fc84c93116ae
author: cinap_lenrek <[email protected]>
date: Fri Nov 7 03:42:19 EST 2014

libc: import cleaned up syslog() function from sources

this fixes a potential format string problem where the
error string is passed to werrstr() as fmt. also, the
directory comparsion is simplified in this version using
a helper function.

--- a/sys/src/libc/9sys/syslog.c
+++ b/sys/src/libc/9sys/syslog.c
@@ -25,6 +25,14 @@
 	sl.fd = open(buf, OWRITE|OCEXEC);
 }
 
+static int
+eqdirdev(Dir *a, Dir *b)
+{
+	return a != nil && b != nil &&
+		a->dev == b->dev && a->type == b->type &&
+		a->qid.path == b->qid.path;
+}
+
 /*
  * Print
  *  sysname: time: mesg
@@ -50,40 +58,31 @@
 	 *  hasn't broken our fd's
 	 */
 	d = dirfstat(sl.fd);
-	if(sl.fd < 0
-	   || sl.name == nil
-	   || strcmp(sl.name, logname)!=0
-	   || sl.d == nil
-	   || d == nil
-	   || d->dev != sl.d->dev
-	   || d->type != sl.d->type
-	   || d->qid.path != sl.d->qid.path){
+	if(sl.fd < 0 || sl.name == nil || strcmp(sl.name, logname) != 0 ||
+	   !eqdirdev(d, sl.d)){
 		free(sl.name);
 		sl.name = strdup(logname);
 		if(sl.name == nil)
 			cons = 1;
 		else{
+			free(sl.d);
+			sl.d = nil;
 			_syslogopen();
 			if(sl.fd < 0)
 				cons = 1;
-			free(sl.d);
-			sl.d = d;
-			d = nil;	/* don't free it */
+			else
+				sl.d = dirfstat(sl.fd);
 		}
 	}
 	free(d);
 	if(cons){
 		d = dirfstat(sl.consfd);
-		if(sl.consfd < 0
-		   || d == nil
-		   || sl.consd == nil
-		   || d->dev != sl.consd->dev
-		   || d->type != sl.consd->type
-		   || d->qid.path != sl.consd->qid.path){
-			sl.consfd = open("#c/cons", OWRITE|OCEXEC);
+		if(sl.consfd < 0 || !eqdirdev(d, sl.consd)){
 			free(sl.consd);
-			sl.consd = d;
-			d = nil;	/* don't free it */
+			sl.consd = nil;
+			sl.consfd = open("#c/cons", OWRITE|OCEXEC);
+			if(sl.consfd >= 0)
+				sl.consd = dirfstat(sl.consfd);
 		}
 		free(d);
 	}
@@ -94,11 +93,11 @@
 	}
 
 	ctim = ctime(time(0));
-	werrstr(err);
 	p = buf + snprint(buf, sizeof(buf)-1, "%s ", sysname());
 	strncpy(p, ctim+4, 15);
 	p += 15;
 	*p++ = ' ';
+	errstr(err, sizeof err);
 	va_start(arg, fmt);
 	p = vseprint(p, buf+sizeof(buf)-1, fmt, arg);
 	va_end(arg);