diff --git a/lib/siglist.h b/lib/siglist.h new file mode 100644 index 0000000..6673a15 --- /dev/null +++ b/lib/siglist.h @@ -0,0 +1,131 @@ +/* Canonical list of all signal names. + Copyright (C) 1996,97,98,99,2008 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +/* This file should be usable for any platform, since it just associates + the SIG* macros with text names and descriptions. The actual values + come from (via ). For any signal macros do not + exist on every platform, we can use #ifdef tests here and still use + this single common file for all platforms. */ + +/* This file is included multiple times. */ + +/* Standard signals */ +#ifdef SIGHUP + init_sig (SIGHUP, "HUP", N_("Hangup")) +#endif +#ifdef SIGINT + init_sig (SIGINT, "INT", N_("Interrupt")) +#endif +#ifdef SIGQUIT + init_sig (SIGQUIT, "QUIT", N_("Quit")) +#endif +#ifdef SIGILL + init_sig (SIGILL, "ILL", N_("Illegal instruction")) +#endif +#ifdef SIGTRAP + init_sig (SIGTRAP, "TRAP", N_("Trace/breakpoint trap")) +#endif +#ifdef SIGABRT + init_sig (SIGABRT, "ABRT", N_("Aborted")) +#endif +#ifdef SIGFPE + init_sig (SIGFPE, "FPE", N_("Floating point exception")) +#endif +#ifdef SIGKILL + init_sig (SIGKILL, "KILL", N_("Killed")) +#endif +#ifdef SIGBUS + init_sig (SIGBUS, "BUS", N_("Bus error")) +#endif +#ifdef SIGSEGV + init_sig (SIGSEGV, "SEGV", N_("Segmentation fault")) +#endif +#ifdef SIGPIPE + init_sig (SIGPIPE, "PIPE", N_("Broken pipe")) +#endif +#ifdef SIGALRM + init_sig (SIGALRM, "ALRM", N_("Alarm clock")) +#endif +#ifdef SIGTERM + init_sig (SIGTERM, "TERM", N_("Terminated")) +#endif +#ifdef SIGURG + init_sig (SIGURG, "URG", N_("Urgent I/O condition")) +#endif +#ifdef SIGSTOP + init_sig (SIGSTOP, "STOP", N_("Stopped (signal)")) +#endif +#ifdef SIGTSTP + init_sig (SIGTSTP, "TSTP", N_("Stopped")) +#endif +#ifdef SIGCONT + init_sig (SIGCONT, "CONT", N_("Continued")) +#endif +#ifdef SIGCHLD + init_sig (SIGCHLD, "CHLD", N_("Child exited")) +#endif +#ifdef SIGTTIN + init_sig (SIGTTIN, "TTIN", N_("Stopped (tty input)")) +#endif +#ifdef SIGTTOU + init_sig (SIGTTOU, "TTOU", N_("Stopped (tty output)")) +#endif +#ifdef SIGIO + init_sig (SIGIO, "IO", N_("I/O possible")) +#endif +#ifdef SIGXCPU + init_sig (SIGXCPU, "XCPU", N_("CPU time limit exceeded")) +#endif +#ifdef SIGXFSZ + init_sig (SIGXFSZ, "XFSZ", N_("File size limit exceeded")) +#endif +#ifdef SIGVTALRM + init_sig (SIGVTALRM, "VTALRM", N_("Virtual timer expired")) +#endif +#ifdef SIGPROF + init_sig (SIGPROF, "PROF", N_("Profiling timer expired")) +#endif +#ifdef SIGWINCH + init_sig (SIGWINCH, "WINCH", N_("Window changed")) +#endif +#ifdef SIGUSR1 + init_sig (SIGUSR1, "USR1", N_("User defined signal 1")) +#endif +#ifdef SIGUSR2 + init_sig (SIGUSR2, "USR2", N_("User defined signal 2")) +#endif + +/* Variations */ +#ifdef SIGEMT + init_sig (SIGEMT, "EMT", N_("EMT trap")) +#endif +#ifdef SIGSYS + init_sig (SIGSYS, "SYS", N_("Bad system call")) +#endif +#ifdef SIGSTKFLT + init_sig (SIGSTKFLT, "STKFLT", N_("Stack fault")) +#endif +#ifdef SIGINFO + init_sig (SIGINFO, "INFO", N_("Information request")) +#elif defined(SIGPWR) && (!defined(SIGLOST) || (SIGPWR != SIGLOST)) + init_sig (SIGPWR, "PWR", N_("Power failure")) +#endif +#ifdef SIGLOST + init_sig (SIGLOST, "LOST", N_("Resource lost")) +#endif diff --git a/lib/string.in.h b/lib/string.in.h index 09205e7..e2c17f1 100644 --- a/lib/string.in.h +++ b/lib/string.in.h @@ -1,6 +1,6 @@ /* A GNU-like . - Copyright (C) 1995-1996, 2001-2007 Free Software Foundation, Inc. + Copyright (C) 1995-1996, 2001-2008 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -514,6 +514,18 @@ extern char *strerror (int); strerror (e)) #endif +#if @GNULIB_STRSIGNAL@ +# if ! @HAVE_DECL_STRSIGNAL@ +extern char *strsignal (int __sig); +# endif +#elif defined GNULIB_POSIXCHECK +# undef strsignal +# define strsignal(a) \ + (GL_LINK_WARNING ("strsignal is unportable - " \ + "use gnulib module strsignal for portability"), \ + strsignal (a)) +#endif + #ifdef __cplusplus } diff --git a/lib/strsignal.c b/lib/strsignal.c new file mode 100644 index 0000000..0a4c5dc --- /dev/null +++ b/lib/strsignal.c @@ -0,0 +1,195 @@ +/* Copyright (C) 1991, 1994-2002, 2005, 2008 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#ifndef _LIBC +# include +#endif + +#include +#include +#include +#include + +#ifdef _LIBC +# include +#else /* !_LIBC */ +# include "gettext.h" +# define _(msgid) gettext (msgid) +# define N_(msgid) gettext_noop (msgid) +#endif /* _LIBC */ + +#ifdef _LIBC +# include +#else /* !_LIBC */ +# include "lock.h" +# include "tls.h" +# define __libc_once_define(CLASS, NAME) gl_once_define (CLASS, NAME); +# define __libc_once(NAME, INIT) gl_once ((NAME), (INIT)) +# define __libc_key_t gl_tls_key_t +# define __libc_getspecific(NAME) gl_tls_get ((NAME)) +# define __libc_setspecific(NAME, POINTER) gl_tls_set ((NAME), (POINTER)) +# define __snprintf snprintf +#endif /* _LIBC */ + +#ifdef _LIBC + +/* Defined in siglist.c. */ +extern const char *const _sys_siglist[]; +extern const char *const _sys_siglist_internal[] attribute_hidden; + +#else /* !_LIBC */ + +/* NetBSD declares sys_siglist in unistd.h. */ +# include + +# define INTUSE(x) (x) + +# if HAVE_DECL_SYS_SIGLIST +# undef _sys_siglist +# define _sys_siglist sys_siglist +# else /* !HAVE_DECL_SYS_SIGLIST */ +# ifndef NSIG +# define NSIG 32 +# endif /* NSIG */ +static const char *_sys_siglist[NSIG]; +# endif /* !HAVE_DECL_SYS_SIGLIST */ + +#endif /* _LIBC */ + +static __libc_key_t key; + +/* If nonzero the key allocation failed and we should better use a + static buffer than fail. */ +#define BUFFERSIZ 100 +static char local_buf[BUFFERSIZ]; +static char *static_buf; + +/* Destructor for the thread-specific data. */ +static void init (void); +static void free_key_mem (void *mem); +static char *getbuffer (void); + + +/* Return a string describing the meaning of the signal number SIGNUM. */ +char * +strsignal (int signum) +{ + __libc_once_define (static, once); + const char *desc; + + /* If we have not yet initialized the buffer do it now. */ + __libc_once (once, init); + + if ( +#ifdef SIGRTMIN + (signum >= SIGRTMIN && signum <= SIGRTMAX) || +#endif + signum < 0 || signum >= NSIG + || (desc = INTUSE(_sys_siglist)[signum]) == NULL) + { + char *buffer = getbuffer (); + int len; +#ifdef SIGRTMIN + if (signum >= SIGRTMIN && signum <= SIGRTMAX) + len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"), + signum - SIGRTMIN); + else +#endif + len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"), + signum); + if (len >= BUFFERSIZ) + buffer = NULL; + else + buffer[len] = '\0'; + + return buffer; + } + + return (char *) _(desc); +} + + +/* Initialize buffer. */ +static void +init (void) +{ +#ifdef _LIBC + if (__libc_key_create (&key, free_key_mem)) + /* Creating the key failed. This means something really went + wrong. In any case use a static buffer which is better than + nothing. */ + static_buf = local_buf; +#else /* !_LIBC */ + gl_tls_key_init (key, free_key_mem); + +# if !HAVE_DECL_SYS_SIGLIST + memset (_sys_siglist, 0, NSIG * sizeof *_sys_siglist); + + /* The trailing semicolon is because siglist.h is really designed for + array initializers, but we don't want that here because the signal + numbers might exceed NSIG. */ +# define init_sig(sig, abbrev, desc) do { \ + if (sig >= 0 && sig < NSIG) \ + _sys_siglist[sig] = desc; \ +} while (0); + +# include "siglist.h" + +# undef init_sig + +# endif /* !HAVE_DECL_SYS_SIGLIST */ +#endif /* !_LIBC */ +} + + +/* Free the thread specific data, this is done if a thread terminates. */ +static void +free_key_mem (void *mem) +{ + free (mem); + __libc_setspecific (key, NULL); +} + + +/* Return the buffer to be used. */ +static char * +getbuffer (void) +{ + char *result; + + if (static_buf != NULL) + result = static_buf; + else + { + /* We don't use the static buffer and so we have a key. Use it + to get the thread-specific buffer. */ + result = __libc_getspecific (key); + if (result == NULL) + { + /* No buffer allocated so far. */ + result = malloc (BUFFERSIZ); + if (result == NULL) + /* No more memory available. We use the static buffer. */ + result = local_buf; + else + __libc_setspecific (key, result); + } + } + + return result; +} diff --git a/m4/string_h.m4 b/m4/string_h.m4 index 99a0dab..2d2b1ad 100644 --- a/m4/string_h.m4 +++ b/m4/string_h.m4 @@ -1,6 +1,6 @@ # Configure a GNU-like replacement for . -# Copyright (C) 2007 Free Software Foundation, Inc. +# Copyright (C) 2007, 2008 Free Software Foundation, Inc. # This file is free software; the Free Software Foundation # gives unlimited permission to copy and/or distribute it, # with or without modifications, as long as this notice is preserved. @@ -60,6 +60,7 @@ AC_DEFUN([gl_HEADER_STRING_H_DEFAULTS], GNULIB_MBSSEP=0; AC_SUBST([GNULIB_MBSSEP]) GNULIB_MBSTOK_R=0; AC_SUBST([GNULIB_MBSTOK_R]) GNULIB_STRERROR=0; AC_SUBST([GNULIB_STRERROR]) + GNULIB_STRSIGNAL=0; AC_SUBST([GNULIB_STRSIGNAL]) dnl Assume proper GNU behavior unless another module says otherwise. HAVE_DECL_MEMMEM=1; AC_SUBST([HAVE_DECL_MEMMEM]) HAVE_MEMPCPY=1; AC_SUBST([HAVE_MEMPCPY]) @@ -76,6 +77,7 @@ AC_DEFUN([gl_HEADER_STRING_H_DEFAULTS], HAVE_STRCASESTR=1; AC_SUBST([HAVE_STRCASESTR]) HAVE_DECL_STRTOK_R=1; AC_SUBST([HAVE_DECL_STRTOK_R]) HAVE_DECL_STRERROR=1; AC_SUBST([HAVE_DECL_STRERROR]) + HAVE_DECL_STRSIGNAL=1; AC_SUBST([HAVE_DECL_STRSIGNAL]) REPLACE_STRERROR=0; AC_SUBST([REPLACE_STRERROR]) REPLACE_MEMMEM=0; AC_SUBST([REPLACE_MEMMEM]) ]) diff --git a/m4/strsignal.m4 b/m4/strsignal.m4 new file mode 100644 index 0000000..9bcd374 --- /dev/null +++ b/m4/strsignal.m4 @@ -0,0 +1,24 @@ +# strsignal.m4 serial 1 +dnl Copyright (C) 2008 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_STRSIGNAL], +[ + dnl Persuade glibc to declare strsignal(). + AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) + + AC_REQUIRE([AC_DECL_SYS_SIGLIST]) + + AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS]) + AC_REPLACE_FUNCS([strsignal]) + AC_CHECK_DECLS_ONCE([strsignal]) + if test $ac_cv_have_decl_strsignal = no; then + HAVE_DECL_STRSIGNAL=0 + gl_PREREQ_STRSIGNAL + fi +]) + +# Prerequisites of lib/strsignal.c. +AC_DEFUN([gl_PREREQ_STRSIGNAL], [:]) diff --git a/modules/string b/modules/string index 6f3226d..226b0e0 100644 --- a/modules/string +++ b/modules/string @@ -51,6 +51,7 @@ string.h: string.in.h -e 's|@''GNULIB_STRCASESTR''@|$(GNULIB_STRCASESTR)|g' \ -e 's|@''GNULIB_STRTOK_R''@|$(GNULIB_STRTOK_R)|g' \ -e 's|@''GNULIB_STRERROR''@|$(GNULIB_STRERROR)|g' \ + -e 's|@''GNULIB_STRSIGNAL''@|$(GNULIB_STRSIGNAL)|g' \ -e 's|@''HAVE_DECL_MEMMEM''@|$(HAVE_DECL_MEMMEM)|g' \ -e 's|@''HAVE_MEMPCPY''@|$(HAVE_MEMPCPY)|g' \ -e 's|@''HAVE_DECL_MEMRCHR''@|$(HAVE_DECL_MEMRCHR)|g' \ @@ -66,6 +67,7 @@ string.h: string.in.h -e 's|@''HAVE_STRCASESTR''@|$(HAVE_STRCASESTR)|g' \ -e 's|@''HAVE_DECL_STRTOK_R''@|$(HAVE_DECL_STRTOK_R)|g' \ -e 's|@''HAVE_DECL_STRERROR''@|$(HAVE_DECL_STRERROR)|g' \ + -e 's|@''HAVE_DECL_STRSIGNAL''@|$(HAVE_DECL_STRSIGNAL)|g' \ -e 's|@''REPLACE_MEMMEM''@|$(REPLACE_MEMMEM)|g' \ -e 's|@''REPLACE_STRERROR''@|$(REPLACE_STRERROR)|g' \ -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \ diff --git a/modules/strsignal b/modules/strsignal new file mode 100644 index 0000000..1a6580b --- /dev/null +++ b/modules/strsignal @@ -0,0 +1,30 @@ +Description: +strsignal() function: return string describing signal. + +Files: +lib/strsignal.c +lib/siglist.h +m4/strsignal.m4 + +Depends-on: +string +gettext-h +lock +tls +snprintf-posix +memset + +configure.ac: +gl_FUNC_STRSIGNAL +gl_STRING_MODULE_INDICATOR([strsignal]) + +Makefile.am: + +Include: +"strsignal.h" + +License: +LGPLv2+ + +Maintainer: +Colin Watson, glibc diff --git a/modules/strsignal-tests b/modules/strsignal-tests new file mode 100644 index 0000000..d0e50bf --- /dev/null +++ b/modules/strsignal-tests @@ -0,0 +1,10 @@ +Files: +tests/test-strsignal.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-strsignal +check_PROGRAMS += test-strsignal diff --git a/tests/test-strsignal.c b/tests/test-strsignal.c new file mode 100644 index 0000000..16544f2 --- /dev/null +++ b/tests/test-strsignal.c @@ -0,0 +1,69 @@ +/* Test of strsignal() function. + Copyright (C) 2008 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Colin Watson , 2008. */ + +#include + +#include +#include +#include +#include + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + abort (); \ + } \ + } \ + while (0) + +#if HAVE_DECL_SYS_SIGLIST +# define ASSERT_DESCRIPTION(got, expect) +#else +/* In this case, we can guarantee some signal descriptions. */ +# define ASSERT_DESCRIPTION(got, expect) ASSERT (!strcmp (got, expect)) +#endif + +int +main (int argc, char **argv) +{ + char *str; + + /* We try a couple of signals, since not all signals are supported + everywhere. Notwithstanding the #ifdef for neatness, SIGINT should in + fact be available on all platforms. */ + +#ifdef SIGHUP + str = strsignal (SIGHUP); + ASSERT (str); + ASSERT (*str); + ASSERT_DESCRIPTION (str, "Hangup"); +#endif + +#ifdef SIGINT + str = strsignal (SIGINT); + ASSERT (str); + ASSERT (*str); + ASSERT_DESCRIPTION (str, "Interrupt"); +#endif + + return 0; +}