From 051ee4061b1f605b4b95a868c8c4d84b5dfd09b8 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 17 Dec 2014 23:11:47 -0500 Subject: Have log_log automatically append strerror(errno) Instead of having strerror used everywhere. I did this because strerror is NOT thread safe. This still isn't thread safe, but at least now it's all in one place, easy to fix. --- nslcd/cfg.c | 3 +-- nslcd/common.h | 6 +++--- nslcd/hackers.c | 3 +-- nslcd/invalidator.c | 20 +++++++++----------- nslcd/log.c | 12 +++++++++++- nslcd/nslcd.c | 52 ++++++++++++++++++++++++++-------------------------- nslcd/nsswitch.c | 4 ++-- 7 files changed, 53 insertions(+), 47 deletions(-) diff --git a/nslcd/cfg.c b/nslcd/cfg.c index c9a691f..5b42272 100644 --- a/nslcd/cfg.c +++ b/nslcd/cfg.c @@ -447,8 +447,7 @@ static void cfg_read(const char *filename, struct nslcd_config *cfg) /* open config file */ if ((fp = fopen(filename, "r")) == NULL) { - log_log(LOG_ERR, "cannot open config file (%s): %s", - filename, strerror(errno)); + log_log(LOG_ERR, "cannot open config file (%s)", filename); exit(EXIT_FAILURE); } /* read file and parse lines */ diff --git a/nslcd/common.h b/nslcd/common.h index ca543fb..0c6f3f0 100644 --- a/nslcd/common.h +++ b/nslcd/common.h @@ -48,15 +48,15 @@ #define ERROR_OUT_WRITEERROR(fp) \ do { \ if (errno == EPIPE) \ - log_log(LOG_DEBUG, "error writing to client: %s", strerror(errno)); \ + log_log(LOG_DEBUG, "error writing to client"); \ else \ - log_log(LOG_WARNING, "error writing to client: %s", strerror(errno)); \ + log_log(LOG_WARNING, "error writing to client"); \ return -1; \ } while(0) #define ERROR_OUT_READERROR(fp) \ do { \ - log_log(LOG_WARNING, "error reading from client: %s", strerror(errno)); \ + log_log(LOG_WARNING, "error reading from client"); \ return -1; \ } while(0) diff --git a/nslcd/hackers.c b/nslcd/hackers.c index fe0e614..f471266 100644 --- a/nslcd/hackers.c +++ b/nslcd/hackers.c @@ -25,8 +25,7 @@ session_create(const struct nslcd_config *cfg, pthread_t *thread) { } if (pthread_create(thread, NULL, session_worker, (void*)session)) { - log_log(LOG_ERR, "unable to start hackers worker thread: %s", - strerror(errno)); + log_log(LOG_ERR, "unable to start hackers worker thread"); exit(EXIT_FAILURE); } diff --git a/nslcd/invalidator.c b/nslcd/invalidator.c index 550ca8b..d0474bc 100644 --- a/nslcd/invalidator.c +++ b/nslcd/invalidator.c @@ -116,7 +116,7 @@ static void exec_invalidate(const char *db) _exit(EXIT_FAILURE); break; case -1: /* we are the parent, but have an error */ - log_log(LOG_ERR, "invalidator: fork() failed: %s", strerror(errno)); + log_log(LOG_ERR, "invalidator: fork() failed"); break; default: /* we are the parent */ /* wait for child exit */ @@ -127,7 +127,7 @@ static void exec_invalidate(const char *db) } while ((i < 0) && (errno == EINTR)); if (i < 0) - log_log(LOG_ERR, "invalidator: waitpid(%d) failed: %s", (int)cpid, strerror(errno)); + log_log(LOG_ERR, "invalidator: waitpid(%d) failed", (int)cpid); else if (WIFEXITED(status)) { i = WEXITSTATUS(status); @@ -177,11 +177,10 @@ static void handle_requests(int fd) else if (i < 0) { if (errno == EINTR) - log_log(LOG_DEBUG, "invalidator: read failed (ignored): %s", - strerror(errno)); + log_log(LOG_DEBUG, "invalidator: read failed (ignored)"); else { - log_log(LOG_ERR, "invalidator: read failed: %s", strerror(errno)); + log_log(LOG_ERR, "invalidator: read failed"); _exit(EXIT_SUCCESS); } } @@ -206,21 +205,21 @@ int invalidator_start(void) /* set up a pipe for communication */ if (pipe(pipefds) < 0) { - log_log(LOG_ERR, "pipe() failed: %s", strerror(errno)); + log_log(LOG_ERR, "pipe() failed"); return -1; } /* set O_NONBLOCK on the write end to ensure that a hanging invalidator process does not bring down the rest of the application */ if ((i = fcntl(pipefds[1], F_GETFL, 0)) < 0) { - log_log(LOG_ERR, "fctnl(F_GETFL) failed: %s", strerror(errno)); + log_log(LOG_ERR, "fctnl(F_GETFL) failed"); close(pipefds[0]); close(pipefds[1]); return -1; } if (fcntl(pipefds[1], F_SETFL, i | O_NONBLOCK) < 0) { - log_log(LOG_ERR, "fctnl(F_SETFL,O_NONBLOCK) failed: %s", strerror(errno)); + log_log(LOG_ERR, "fctnl(F_SETFL,O_NONBLOCK) failed"); close(pipefds[0]); close(pipefds[1]); return -1; @@ -229,7 +228,7 @@ int invalidator_start(void) cpid = fork(); if (cpid < 0) { - log_log(LOG_ERR, "fork() failed: %s", strerror(errno)); + log_log(LOG_ERR, "fork() failed"); close(pipefds[0]); close(pipefds[1]); return -1; @@ -269,6 +268,5 @@ void invalidator_do(enum nss_map_selector map) c = (uint8_t)map; rc = write(signalfd, &c, sizeof(uint8_t)); if (rc <= 0) - log_log(LOG_WARNING, "error signalling invalidator: %s", - strerror(errno)); + log_log(LOG_WARNING, "error signalling invalidator"); } diff --git a/nslcd/log.c b/nslcd/log.c index 56b5936..b0e205d 100644 --- a/nslcd/log.c +++ b/nslcd/log.c @@ -155,6 +155,7 @@ void log_setrequest(const char *format, ...) void log_log(int pri, const char *format, ...) { char *msg = NULL; + char *tmp = NULL; va_list ap; #ifndef TLS char *sessionid, *requestid; @@ -165,9 +166,18 @@ void log_log(int pri, const char *format, ...) /* make the message */ va_start(ap, format); if (vasprintf(&msg, format, ap) < 0) { - fprintf(stderr, SD_ERR "vasprintf() in logger failed"); + fprintf(stderr, SD_ERR "vasprintf() failed: %s", strerror(errno)); + return; } va_end(ap); + if (errno != 0) { + if (asprintf(&tmp, "%s: %s", msg, strerror(errno)) < 0) { + fprintf(stderr, SD_ERR "asprintf() failed: %s", strerror(errno)); + } else { + free(msg); + msg = tmp; + errno = 0; + } } /* do the logging */ diff --git a/nslcd/nslcd.c b/nslcd/nslcd.c index c0cf90e..a1d450a 100644 --- a/nslcd/nslcd.c +++ b/nslcd/nslcd.c @@ -161,8 +161,8 @@ static void exithandler(void) if (nslcd_serversocket >= 0) { if (close(nslcd_serversocket)) - log_log(LOG_WARNING, "problem closing server socket (ignored):%d: %s", - nslcd_serversocket, strerror(errno)); + log_log(LOG_WARNING, "problem closing server socket (ignored):%d", + nslcd_serversocket); } /* log exit */ log_log(LOG_INFO, "version %s bailing out", VERSION); @@ -174,8 +174,10 @@ static int get_socket() r = sd_listen_fds(1); if (r != 1) { if (r < 0) - log_log(LOG_ERR, "failed to aquire sockets from systemd: %s", - strerror(-r)); + { + errno = -r; + log_log(LOG_ERR, "failed to aquire sockets from systemd"); + } else log_log(LOG_ERR, "wrong number of sockets from systemd: " "expected %d but got %d", 1, r); @@ -188,8 +190,10 @@ static int get_socket() if (r < 1) { if (r < 0) - log_log(LOG_ERR, "unable to verify socket type:%d: %s", - fd, strerror(-r)); + { + errno = -r; + log_log(LOG_ERR, "unable to verify socket type:%d", fd); + } else log_log(LOG_ERR, "socket not of the right type:%d", fd); exit(EXIT_FAILURE); @@ -226,7 +230,7 @@ static void handleconnection(int sock, struct session *session) pid_t pid = (pid_t)-1; /* log connection */ if (getpeercred(sock, &uid, &gid, &pid)) - log_log(LOG_DEBUG, "connection from unknown client: %s", strerror(errno)); + log_log(LOG_DEBUG, "connection from unknown client"); else log_log(LOG_DEBUG, "connection from pid=%d uid=%d gid=%d", (int)pid, (int)uid, (int)gid); @@ -235,8 +239,7 @@ static void handleconnection(int sock, struct session *session) READBUFFER_MINSIZE, READBUFFER_MAXSIZE, WRITEBUFFER_MINSIZE, WRITEBUFFER_MAXSIZE)) == NULL) { - log_log(LOG_WARNING, "cannot create stream for writing: %s", - strerror(errno)); + log_log(LOG_WARNING, "cannot create stream for writing"); (void)close(sock); return; } @@ -265,8 +268,8 @@ static void install_sighandler(int signum, void (*handler) (int)) act.sa_flags = SA_RESTART | SA_NOCLDSTOP; if (sigaction(signum, &act, NULL) != 0) { - log_log(LOG_ERR, "error installing signal handler for '%s': %s", - signame(signum), strerror(errno)); + log_log(LOG_ERR, "error installing signal handler for '%s'", + signame(signum)); exit(EXIT_FAILURE); } } @@ -303,9 +306,9 @@ static void *worker(void *_sess) if (j < 0) { if (errno == EINTR) - log_log(LOG_DEBUG, "select() failed (ignored): %s", strerror(errno)); + log_log(LOG_DEBUG, "select() failed (ignored)"); else - log_log(LOG_ERR, "select() failed: %s", strerror(errno)); + log_log(LOG_ERR, "select() failed"); continue; } /* see if our file descriptor is actually ready */ @@ -317,24 +320,24 @@ static void *worker(void *_sess) if (csock < 0) { if ((errno == EINTR) || (errno == EAGAIN) || (errno == EWOULDBLOCK)) - log_log(LOG_DEBUG, "accept() failed (ignored): %s", strerror(errno)); + log_log(LOG_DEBUG, "accept() failed (ignored)"); else - log_log(LOG_ERR, "accept() failed: %s", strerror(errno)); + log_log(LOG_ERR, "accept() failed"); continue; } /* make sure O_NONBLOCK is not inherited */ if ((j = fcntl(csock, F_GETFL, 0)) < 0) { - log_log(LOG_ERR, "fctnl(F_GETFL) failed: %s", strerror(errno)); + log_log(LOG_ERR, "fctnl(F_GETFL) failed"); if (close(csock)) - log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno)); + log_log(LOG_WARNING, "problem closing socket"); continue; } if (fcntl(csock, F_SETFL, j & ~O_NONBLOCK) < 0) { - log_log(LOG_ERR, "fctnl(F_SETFL,~O_NONBLOCK) failed: %s", strerror(errno)); + log_log(LOG_ERR, "fctnl(F_SETFL,~O_NONBLOCK) failed"); if (close(csock)) - log_log(LOG_WARNING, "problem closing socket: %s", strerror(errno)); + log_log(LOG_WARNING, "problem closing socket"); continue; } /* indicate new connection to logging module (generates unique id) */ @@ -388,8 +391,7 @@ static void disable_nss_module(void) /* fall back to changing the way host lookup is done */ #ifdef HAVE___NSS_CONFIGURE_LOOKUP if (__nss_configure_lookup("hosts", "files dns")) - log_log(LOG_ERR, "unable to override hosts lookup method: %s", - strerror(errno)); + log_log(LOG_ERR, "unable to override hosts lookup method"); #endif /* HAVE___NSS_CONFIGURE_LOOKUP */ dlclose(handle); return; @@ -422,7 +424,7 @@ int main(int argc, char *argv[]) /* install handler to close stuff off on exit and log notice */ if (atexit(exithandler)) { - log_log(LOG_ERR, "atexit() failed: %s", strerror(errno)); + log_log(LOG_ERR, "atexit() failed"); exit(EXIT_FAILURE); } /* get socket */ @@ -458,8 +460,7 @@ int main(int argc, char *argv[]) { if (pthread_create(&nslcd_threads[i], NULL, worker, (void*)session)) { - log_log(LOG_ERR, "unable to start worker thread %d: %s", - i, strerror(errno)); + log_log(LOG_ERR, "unable to start worker thread %d", i); exit(EXIT_FAILURE); } } @@ -493,8 +494,7 @@ int main(int argc, char *argv[]) /* cancel all running threads */ for (i = 0; i < nslcd_cfg->threads; i++) if (pthread_cancel(nslcd_threads[i])) - log_log(LOG_WARNING, "failed to stop thread %d (ignored): %s", - i, strerror(errno)); + log_log(LOG_WARNING, "failed to stop thread %d (ignored)", i); /* close server socket to trigger failures in threads waiting on accept() */ close(nslcd_serversocket); nslcd_serversocket = -1; diff --git a/nslcd/nsswitch.c b/nslcd/nsswitch.c index 56cb21b..526d773 100644 --- a/nslcd/nsswitch.c +++ b/nslcd/nsswitch.c @@ -56,7 +56,7 @@ void nsswitch_check_reload(void) cached_shadow_lastcheck = t; if (stat(NSSWITCH_FILE, &buf)) { - log_log(LOG_ERR, "stat(%s) failed: %s", NSSWITCH_FILE, strerror(errno)); + log_log(LOG_ERR, "stat(%s) failed", NSSWITCH_FILE); /* trigger a recheck anyway */ cached_shadow_uses_module = CACHED_UNKNOWN; return; @@ -140,7 +140,7 @@ static int shadow_uses_module(const char *module_name) /* open config file */ if ((fp = fopen(NSSWITCH_FILE, "r")) == NULL) { - log_log(LOG_ERR, "cannot open %s: %s", NSSWITCH_FILE, strerror(errno)); + log_log(LOG_ERR, "cannot open %s", NSSWITCH_FILE); return 0; } /* read file and parse lines */ -- cgit v1.2.3