diff options
author | Ben Pfaff <blp@nicira.com> | 2013-04-16 15:25:10 -0700 |
---|---|---|
committer | Ben Pfaff <blp@nicira.com> | 2013-06-05 12:46:59 -0700 |
commit | eee8089cbeffa39eef3858be57628be663b229b7 (patch) | |
tree | 1bd789c9954f7d8d6dd77f87276c3f97902a218f /lib/signals.c | |
parent | 22c4e104f68f25d833c52a269117430c410dc59f (diff) | |
download | openvswitch-eee8089cbeffa39eef3858be57628be663b229b7.tar.gz |
signals: Make signal_name() thread-safe.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Diffstat (limited to 'lib/signals.c')
-rw-r--r-- | lib/signals.c | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/lib/signals.c b/lib/signals.c index c47fefa4a..99a36a427 100644 --- a/lib/signals.c +++ b/lib/signals.c @@ -137,37 +137,38 @@ signal_handler(int signr) } } -/* Returns the name of signal 'signum' as a string. The string may be in a - * static buffer that is reused from one call to the next. +/* Returns the name of signal 'signum' as a string. The return value is either + * a statically allocated constant string or the 'bufsize'-byte buffer + * 'namebuf'. 'bufsize' should be at least SIGNAL_NAME_BUFSIZE. * * The string is probably a (possibly multi-word) description of the signal * (e.g. "Hangup") instead of just the stringified version of the macro * (e.g. "SIGHUP"). */ const char * -signal_name(int signum) +signal_name(int signum, char *namebuf, size_t bufsize) { - const char *name = NULL; - #if HAVE_DECL_SYS_SIGLIST if (signum >= 0 && signum < ARRAY_SIZE(sys_siglist)) { - name = sys_siglist[signum]; + const char *name = sys_siglist[signum]; + if (name) { + return name; + } } #endif - if (!name) { - static char buffer[7 + INT_STRLEN(int) + 1]; - sprintf(buffer, "signal %d", signum); - name = buffer; - } - return name; + snprintf(namebuf, bufsize, "signal %d", signum); + return namebuf; } void xsigaction(int signum, const struct sigaction *new, struct sigaction *old) { if (sigaction(signum, new, old)) { + char namebuf[SIGNAL_NAME_BUFSIZE]; + VLOG_FATAL("sigaction(%s) failed (%s)", - signal_name(signum), strerror(errno)); + signal_name(signum, namebuf, sizeof namebuf), + strerror(errno)); } } |