Imported Upstream version 1.8.6p8
[debian/sudo] / compat / sig2str.c
1 /*
2  * Copyright (c) 2012 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20
21 #include <errno.h>
22 #include <stdio.h>
23 #ifdef STDC_HEADERS
24 # include <stdlib.h>
25 # include <stddef.h>
26 #else
27 # ifdef HAVE_STDLIB_H
28 #  include <stdlib.h>
29 # endif
30 #endif /* STDC_HEADERS */
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #endif /* HAVE_STRING_H */
34 #ifdef HAVE_STRINGS_H
35 # include <strings.h>
36 #endif /* HAVE_STRINGS_H */
37 #include <signal.h>
38
39 #include "missing.h"
40
41 #if defined(HAVE_DECL_SYS_SIGNAME) && HAVE_DECL_SYS_SIGNAME == 1
42 #  define sudo_sys_signame      sys_signame
43 #elif defined(HAVE_DECL__SYS_SIGNAME) && HAVE_DECL__SYS_SIGNAME == 1
44 #  define sudo_sys_signame      _sys_signame
45 #elif defined(HAVE_DECL___SYS_SIGNAME) && HAVE_DECL___SYS_SIGNAME == 1
46 #  define sudo_sys_signame      __sys_signame
47 #elif defined(HAVE_DECL_SYS_SIGABBREV) && HAVE_DECL_SYS_SIGABBREV == 1
48 #  define sudo_sys_signame      sys_sigabbrev
49 #else
50 # ifdef HAVE_SYS_SIGABBREV
51    /* sys_sigabbrev is not declared by glibc */
52 #  define sudo_sys_signame      sys_sigabbrev
53 # endif
54 extern const char *const sudo_sys_signame[NSIG];
55 #endif
56
57 /*
58  * Translate signal number to name.
59  */
60 int
61 sig2str(int signo, char *signame)
62 {
63 #if defined(SIGRTMIN) && defined(SIGRTMAX)
64     /* Realtime signal support as per Solaris. */
65     if (signo >= SIGRTMIN && signo <= SIGRTMAX) {
66         snprintf(signame, SIG2STR_MAX, "RTMIN+%d", (signo - SIGRTMIN));
67         return 0;
68     }
69 #endif
70     if (signo > 0 && signo < NSIG && sudo_sys_signame[signo] != NULL) {
71         strlcpy(signame, sudo_sys_signame[signo], SIG2STR_MAX);
72         return 0;
73     }
74     errno = EINVAL;
75     return -1;
76 }