diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2014-11-07 08:42:19 +0100 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2014-11-07 08:42:19 +0100 |
commit | 5364fa720de3b963a88dc4810ed83b4f2ab11d12 (patch) | |
tree | 83cf474fde292d02f60e53dfed9b7ed77da8dda1 | |
parent | 958d698bf8b58bf30357882242b8fc84c93116ae (diff) | |
download | plan9front-5364fa720de3b963a88dc4810ed83b4f2ab11d12.tar.xz |
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.
-rw-r--r-- | sys/src/libc/9sys/syslog.c | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/sys/src/libc/9sys/syslog.c b/sys/src/libc/9sys/syslog.c index 828ad04a7..d4d30a80a 100644 --- a/sys/src/libc/9sys/syslog.c +++ b/sys/src/libc/9sys/syslog.c @@ -25,6 +25,14 @@ _syslogopen(void) 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 @@ syslog(int cons, char *logname, char *fmt, ...) * 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 @@ syslog(int cons, char *logname, char *fmt, ...) } 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); |