]> git.ipfire.org Git - thirdparty/util-linux.git/blame - lib/signames.c
whereis: use xstrncpy()
[thirdparty/util-linux.git] / lib / signames.c
CommitLineData
6855f6e8
NH
1/*
2 * Copyright (c) 1988, 1993, 1994, 2017
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33/*
34 * 2017-10-14 Niklas Hambüchen <mail@nh2.me>
35 * - Extracted signal names mapping from kill.c
36 *
37 * Copyright (C) 2014 Sami Kerola <kerolasa@iki.fi>
38 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
39 * Copyright (C) 2017 Niklas Hambüchen <mail@nh2.me>
40 */
41
42#include <ctype.h> /* for isdigit() */
6855f6e8
NH
43#include <signal.h>
44#include <unistd.h>
45#include <string.h>
46#include <errno.h>
47
48#include "c.h"
49#include "strutils.h"
c61e986f 50#include "signames.h"
6855f6e8 51
7b41ad36 52static const struct ul_signal_name {
c61e986f
KZ
53 const char *name;
54 int val;
7b41ad36 55} ul_signames[] = {
c61e986f
KZ
56 /* POSIX signals */
57 { "HUP", SIGHUP }, /* 1 */
58 { "INT", SIGINT }, /* 2 */
59 { "QUIT", SIGQUIT }, /* 3 */
60 { "ILL", SIGILL }, /* 4 */
61#ifdef SIGTRAP
62 { "TRAP", SIGTRAP }, /* 5 */
63#endif
64 { "ABRT", SIGABRT }, /* 6 */
65#ifdef SIGIOT
66 { "IOT", SIGIOT }, /* 6, same as SIGABRT */
67#endif
68#ifdef SIGEMT
69 { "EMT", SIGEMT }, /* 7 (mips,alpha,sparc*) */
70#endif
71#ifdef SIGBUS
72 { "BUS", SIGBUS }, /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
73#endif
74 { "FPE", SIGFPE }, /* 8 */
75 { "KILL", SIGKILL }, /* 9 */
76 { "USR1", SIGUSR1 }, /* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */
77 { "SEGV", SIGSEGV }, /* 11 */
78 { "USR2", SIGUSR2 }, /* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */
79 { "PIPE", SIGPIPE }, /* 13 */
80 { "ALRM", SIGALRM }, /* 14 */
81 { "TERM", SIGTERM }, /* 15 */
82#ifdef SIGSTKFLT
83 { "STKFLT", SIGSTKFLT }, /* 16 (arm,i386,m68k,ppc) */
84#endif
85 { "CHLD", SIGCHLD }, /* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */
86#ifdef SIGCLD
87 { "CLD", SIGCLD }, /* same as SIGCHLD (mips) */
88#endif
89 { "CONT", SIGCONT }, /* 18 (arm,i386,m68k,ppc), 19 (alpha,sparc*), 25 (mips) */
90 { "STOP", SIGSTOP }, /* 19 (arm,i386,m68k,ppc), 17 (alpha,sparc*), 23 (mips) */
91 { "TSTP", SIGTSTP }, /* 20 (arm,i386,m68k,ppc), 18 (alpha,sparc*), 24 (mips) */
92 { "TTIN", SIGTTIN }, /* 21 (arm,i386,m68k,ppc,alpha,sparc*), 26 (mips) */
93 { "TTOU", SIGTTOU }, /* 22 (arm,i386,m68k,ppc,alpha,sparc*), 27 (mips) */
94#ifdef SIGURG
95 { "URG", SIGURG }, /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
96#endif
97#ifdef SIGXCPU
98 { "XCPU", SIGXCPU }, /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
99#endif
100#ifdef SIGXFSZ
101 { "XFSZ", SIGXFSZ }, /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
102#endif
103#ifdef SIGVTALRM
104 { "VTALRM", SIGVTALRM }, /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
105#endif
106#ifdef SIGPROF
107 { "PROF", SIGPROF }, /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
108#endif
109#ifdef SIGWINCH
110 { "WINCH", SIGWINCH }, /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
111#endif
112#ifdef SIGIO
113 { "IO", SIGIO }, /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
114#endif
115#ifdef SIGPOLL
116 { "POLL", SIGPOLL }, /* same as SIGIO */
117#endif
118#ifdef SIGINFO
119 { "INFO", SIGINFO }, /* 29 (alpha) */
120#endif
121#ifdef SIGLOST
122 { "LOST", SIGLOST }, /* 29 (arm,i386,m68k,ppc,sparc*) */
123#endif
124#ifdef SIGPWR
125 { "PWR", SIGPWR }, /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
126#endif
127#ifdef SIGUNUSED
128 { "UNUSED", SIGUNUSED }, /* 31 (arm,i386,m68k,ppc) */
129#endif
130#ifdef SIGSYS
131 { "SYS", SIGSYS }, /* 31 (mips,alpha,sparc*) */
132#endif
133};
6855f6e8
NH
134
135#ifdef SIGRTMIN
b6e1977d 136static int rtsig_to_signum(const char *sig)
6855f6e8
NH
137{
138 int num, maxi = 0;
139 char *ep = NULL;
140
141 if (strncasecmp(sig, "min+", 4) == 0)
142 sig += 4;
143 else if (strncasecmp(sig, "max-", 4) == 0) {
144 sig += 4;
145 maxi = 1;
146 }
147 if (!isdigit(*sig))
148 return -1;
149 errno = 0;
150 num = strtol(sig, &ep, 10);
151 if (!ep || sig == ep || errno || num < 0)
152 return -1;
153 num = maxi ? SIGRTMAX - num : SIGRTMIN + num;
154 if (num < SIGRTMIN || SIGRTMAX < num)
155 return -1;
156 return num;
157}
158#endif
159
b6e1977d 160int signame_to_signum(const char *sig)
6855f6e8
NH
161{
162 size_t n;
163
164 if (!strncasecmp(sig, "sig", 3))
165 sig += 3;
166#ifdef SIGRTMIN
167 /* RT signals */
168 if (!strncasecmp(sig, "rt", 2))
169 return rtsig_to_signum(sig + 2);
170#endif
171 /* Normal signals */
7b41ad36
KZ
172 for (n = 0; n < ARRAY_SIZE(ul_signames); n++) {
173 if (!strcasecmp(ul_signames[n].name, sig))
174 return ul_signames[n].val;
6855f6e8
NH
175 }
176 return -1;
177}
c61e986f
KZ
178
179const char *signum_to_signame(int signum)
180{
181 size_t n;
182
7b41ad36
KZ
183 for (n = 0; n < ARRAY_SIZE(ul_signames); n++) {
184 if (ul_signames[n].val == signum) {
185 return ul_signames[n].name;
c61e986f
KZ
186 }
187 }
188
189 return NULL;
190}
191
c61e986f
KZ
192int get_signame_by_idx(size_t idx, const char **signame, int *signum)
193{
7b41ad36 194 if (idx >= ARRAY_SIZE(ul_signames))
c61e986f
KZ
195 return -1;
196
197 if (signame)
7b41ad36 198 *signame = ul_signames[idx].name;
c61e986f 199 if (signum)
7b41ad36 200 *signum = ul_signames[idx].val;
c61e986f
KZ
201 return 0;
202
203}
204