1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2025 Ricardo Branco <rbranco@suse.de>. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Translate between signal names and numbers 30 */ 31 #include "namespace.h" 32 #include <ctype.h> 33 #include <limits.h> 34 #include <signal.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <ssp/ssp.h> 39 #include "un-namespace.h" 40 41 static const char rtmin_str[] = "RTMIN"; 42 static const char rtmax_str[] = "RTMAX"; 43 44 int 45 __ssp_real(sig2str)(int signum, char *str) 46 { 47 if (signum <= 0 || signum > SIGRTMAX) 48 return (-1); 49 50 if (signum < sys_nsig) 51 (void)strlcpy(str, sys_signame[signum], SIG2STR_MAX); 52 else if (signum < SIGRTMIN) 53 (void)snprintf(str, SIG2STR_MAX, "%d", signum); 54 else if (signum == SIGRTMIN) 55 (void)strlcpy(str, rtmin_str, SIG2STR_MAX); 56 else if (signum == SIGRTMAX) 57 (void)strlcpy(str, rtmax_str, SIG2STR_MAX); 58 else if (signum <= (SIGRTMIN + SIGRTMAX) / 2) 59 (void)snprintf(str, SIG2STR_MAX, "%s+%d", 60 rtmin_str, signum - SIGRTMIN); 61 else 62 (void)snprintf(str, SIG2STR_MAX, "%s-%d", 63 rtmax_str, SIGRTMAX - signum); 64 65 return (0); 66 } 67 68 int 69 str2sig(const char * restrict str, int * restrict pnum) 70 { 71 const char *errstr; 72 long long n; 73 int sig; 74 int rtend = sizeof(rtmin_str) - 1; 75 76 if (strncasecmp(str, "SIG", 3) == 0) 77 str += 3; 78 79 if (strncasecmp(str, rtmin_str, sizeof(rtmin_str) - 1) == 0 || 80 strncasecmp(str, rtmax_str, sizeof(rtmax_str) - 1) == 0) { 81 sig = (toupper(str[4]) == 'X') ? SIGRTMAX : SIGRTMIN; 82 n = 0; 83 if (str[rtend] == '+' || str[rtend] == '-') { 84 n = strtonum(str + rtend, INT_MIN, INT_MAX, &errstr); 85 if (n == 0 || errstr != NULL) 86 return (-1); 87 } else if (str[rtend] != '\0') { 88 return (-1); 89 } 90 sig += (int)n; 91 if (sig < SIGRTMIN || sig > SIGRTMAX) 92 return (-1); 93 *pnum = sig; 94 return (0); 95 } 96 97 if (isdigit((unsigned char)str[0])) { 98 n = strtonum(str, 1, SIGRTMAX, &errstr); 99 if (errstr == NULL) { 100 *pnum = (int)n; 101 return (0); 102 } 103 } 104 105 for (sig = 1; sig < sys_nsig; sig++) { 106 if (strcasecmp(sys_signame[sig], str) == 0) { 107 *pnum = sig; 108 return (0); 109 } 110 } 111 112 return (-1); 113 } 114