Imported Upstream version 1.8.7
[debian/sudo] / compat / sig2str.c
1 /*
2  * Copyright (c) 2012-2013 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 #ifndef HAVE_SIG2STR
20
21 #include <sys/types.h>
22
23 #include <errno.h>
24 #include <stdio.h>
25 #ifdef STDC_HEADERS
26 # include <stdlib.h>
27 # include <stddef.h>
28 #else
29 # ifdef HAVE_STDLIB_H
30 #  include <stdlib.h>
31 # endif
32 #endif /* STDC_HEADERS */
33 #ifdef HAVE_STRING_H
34 # include <string.h>
35 #endif /* HAVE_STRING_H */
36 #ifdef HAVE_STRINGS_H
37 # include <strings.h>
38 #endif /* HAVE_STRINGS_H */
39 #include <signal.h>
40
41 #include "missing.h"
42
43 #if 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_SIGNAME) && HAVE_DECL___SYS_SIGNAME == 1
48 #  define sudo_sys_signame      __sys_signame
49 #elif defined(HAVE_DECL_SYS_SIGABBREV) && HAVE_DECL_SYS_SIGABBREV == 1
50 #  define sudo_sys_signame      sys_sigabbrev
51 #else
52 # ifdef HAVE_SYS_SIGABBREV
53    /* sys_sigabbrev is not declared by glibc */
54 #  define sudo_sys_signame      sys_sigabbrev
55 # endif
56 extern const char *const sudo_sys_signame[NSIG];
57 #endif
58
59 /*
60  * Translate signal number to name.
61  */
62 int
63 sig2str(int signo, char *signame)
64 {
65 #if defined(SIGRTMIN) && defined(SIGRTMAX)
66     /* Realtime signal support as per Solaris. */
67     if (signo >= SIGRTMIN && signo <= SIGRTMAX) {
68         snprintf(signame, SIG2STR_MAX, "RTMIN+%d", (signo - SIGRTMIN));
69         return 0;
70     }
71 #endif
72     if (signo > 0 && signo < NSIG && sudo_sys_signame[signo] != NULL) {
73         strlcpy(signame, sudo_sys_signame[signo], SIG2STR_MAX);
74         return 0;
75     }
76     errno = EINVAL;
77     return -1;
78 }
79 #endif /* HAVE_SIG2STR */