From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id A07BF385AC32 for ; Sat, 24 Jul 2021 08:37:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A07BF385AC32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-x430.google.com with SMTP id n12so1240887wrr.2 for ; Sat, 24 Jul 2021 01:37:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=erBDrgeWQUHE11kx55OPHChSEGrd15WPiAOwa0em+VQ=; b=ecbVCcYEk56UXTEWAt5zpzmozA7bJOEaa3TSJugBdrfix45QNbhfMhsycK8G2ok1JJ R1A39uF4sXdqSSfsmNUbzKSnQM37zSC8FTGW3e4eGjP/QSr+Lj8MF1/ImV02ofO068e7 kLJRBN0XpV1qqwh7wC09kDEX1A9ORVhuN9px3MLgk12hsv4WJ76v06CnvTbf3vXE2zeB B7sYR3+1PwIRdHy42kJSjf38pyGznXIkmHNPio44lHAuNzOm9yteVwQJpLqMlI5+8HOm fo5B/liIgGsx9vmgsRcJn7CQFGrhDgrOLnZtdG+/0oCVVliP36IOoNWiGVI2KYqx2KqQ wh0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=erBDrgeWQUHE11kx55OPHChSEGrd15WPiAOwa0em+VQ=; b=UpJJzTl571D4MBqCjA26h/gFAz8ymJ17JLuFGz7co7ZAS1YpmRlUpQY+FLrR61S+xm JST+X4pu3D+Td+QwZQQvZEzbHqr+KZ1jHIpHjqfyK62wWEgxJZl05gg+zaPIr8uvvimj 4wJ2yTo/h5S7H6QjMcuXr6tBvaFJ8FuJaYXONvTLMmGbwbIoRojTR58IuWP7eQI0Fz8x Oi68ZVLYCXU3VTggg/UnVbXkHJsqxJFmF+6rhvjFsnTbcT87d7+ZlGWGVfBwXSlCkUuY oQCkjsDLc/czBKD2TTJFHHRJ+kqbzpo1rKp5v2kZQL0t0d0kwHR9sSiKBRCiAniotaPl r+OQ== X-Gm-Message-State: AOAM533URu9yvI0QxXFiWMmqLLjE4+OW3pYK4S+CLcOa8iCLKc/UIZ67 zS/7xTAF+IwefryXqRplCe5KNmVBtJMq X-Google-Smtp-Source: ABdhPJzkf3O7bFO+EvjbNPemQGghdXqWnMoM2qVEeMHieRzWgPKwgF7L9cd3LaiLW9nVb5ejSFC1Rg== X-Received: by 2002:adf:eb43:: with SMTP id u3mr9154932wrn.83.1627115870335; Sat, 24 Jul 2021 01:37:50 -0700 (PDT) Received: from localhost.localdomain (p579c318f.dip0.t-ipconnect.de. [87.156.49.143]) by smtp.gmail.com with ESMTPSA id q17sm171741wre.3.2021.07.24.01.37.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Jul 2021 01:37:49 -0700 (PDT) From: Matt Joyce To: newlib@sourceware.org Cc: Matt Joyce Subject: [PATCH 1/1] libc: Added implementations and prototypes for Date: Sat, 24 Jul 2021 10:37:30 +0200 Message-Id: <20210724083730.16959-2-mfjoyce2004@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210724083730.16959-1-mfjoyce2004@gmail.com> References: <20210724083730.16959-1-mfjoyce2004@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 24 Jul 2021 08:37:53 -0000 Added implementations for sig2str() and str2sig() in libc/signal in order to improve POSIX compliance. Added function prototypes to sys/signal.h. Added Makefile.am entries to build the new file. --- newlib/libc/include/sys/signal.h | 16 ++ newlib/libc/signal/Makefile.am | 4 +- newlib/libc/signal/sig2str.c | 257 +++++++++++++++++++++++++++++++ 3 files changed, 275 insertions(+), 2 deletions(-) create mode 100644 newlib/libc/signal/sig2str.c diff --git a/newlib/libc/include/sys/signal.h b/newlib/libc/include/sys/signal.h index 45cc0366c..e249e7040 100644 --- a/newlib/libc/include/sys/signal.h +++ b/newlib/libc/include/sys/signal.h @@ -12,6 +12,7 @@ extern "C" { #include #include #include +#include #if !defined(_SIGSET_T_DECLARED) #define _SIGSET_T_DECLARED @@ -238,6 +239,21 @@ int sigqueue (pid_t, int, const union sigval); #endif /* __POSIX_VISIBLE >= 199309 */ +#if __GNU_VISIBLE + +/* POSIX Issue 8 adds sig2str() and str2sig(). */ + +#if __STDINT_EXP(INT_MAX) > 0x7fff +#define SIG2STR_MAX (sizeof("RTMAX+") + sizeof("4294967295") - 1) +#else +#define SIG2STR_MAX (sizeof("RTMAX") + sizeof("65535") - 1) +#endif + +int sig2str(int, char *); +int str2sig(const char *__restrict, int *__restrict); + +#endif /* __GNU_VISIBLE */ + #if defined(___AM29K__) /* These all need to be defined for ANSI C, but I don't think they are meaningful. */ diff --git a/newlib/libc/signal/Makefile.am b/newlib/libc/signal/Makefile.am index a93dba7a9..89db26113 100644 --- a/newlib/libc/signal/Makefile.am +++ b/newlib/libc/signal/Makefile.am @@ -4,7 +4,7 @@ AUTOMAKE_OPTIONS = cygnus INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS) -LIB_SOURCES = psignal.c raise.c signal.c +LIB_SOURCES = psignal.c raise.c signal.c sig2str.c libsignal_la_LDFLAGS = -Xcompiler -nostdlib @@ -21,6 +21,6 @@ endif # USE_LIBTOOL include $(srcdir)/../../Makefile.shared -CHEWOUT_FILES = psignal.def raise.def signal.def +CHEWOUT_FILES = psignal.def raise.def signal.def sig2str.def CHAPTERS = signal.tex diff --git a/newlib/libc/signal/sig2str.c b/newlib/libc/signal/sig2str.c new file mode 100644 index 000000000..2ce0049b7 --- /dev/null +++ b/newlib/libc/signal/sig2str.c @@ -0,0 +1,257 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (C) 2021 Matthew Joyce + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* Defining _GNU_SOURCE to have access to SIG2STR_MAX in signal.h. */ +#define _GNU_SOURCE +#include +#include +#include +#include + +#define SPACES_TO_N 6 /* Allows indexing to RT Signal number in str2sig */ +#define NUM_OF_SIGS (sizeof(sig_array) / sizeof(sig_name_and_num)) + +typedef struct sig_name_and_num { + const char *sig_name; + const int sig_num; +} sig_name_and_num; + +static const sig_name_and_num sig_array[] = { + #ifdef SIGHUP + { "HUP", SIGHUP}, + #endif + #ifdef SIGINT + { "INT", SIGINT }, + #endif + #ifdef SIGQUIT + { "QUIT", SIGQUIT }, + #endif + #ifdef SIGILL + { "ILL", SIGILL }, + #endif + #ifdef SIGTRAP + { "TRAP", SIGTRAP }, + #endif + #ifdef SIGABRT + { "ABRT", SIGABRT }, + #endif + #ifdef SIGIOT + { "IOT", SIGIOT}, + #endif + #ifdef SIGEMT + { "EMT", SIGEMT }, + #endif + #ifdef SIGFPE + { "FPE", SIGFPE }, + #endif + #ifdef SIGKILL + { "KILL", SIGKILL }, + #endif + #ifdef SIGBUS + { "BUS", SIGBUS }, + #endif + #ifdef SIGSEGV + { "SEGV", SIGSEGV }, + #endif + #ifdef SIGSYS + { "SYS", SIGSYS }, + #endif + #ifdef SIGPIPE + { "PIPE", SIGPIPE }, + #endif + #ifdef SIGALRM + { "ALRM", SIGALRM }, + #endif + #ifdef SIGTERM + { "TERM", SIGTERM }, + #endif + #ifdef SIGURG + { "URG", SIGURG }, + #endif + #ifdef SIGSTOP + { "STOP", SIGSTOP }, + #endif + #ifdef SIGTSTP + { "TSTP", SIGTSTP }, + #endif + #ifdef SIGCONT + { "CONT", SIGCONT }, + #endif + #ifdef SIGCHLD + { "CHLD", SIGCHLD }, + #endif + #ifdef SIGCLD + { "CLD", SIGCLD }, + #endif + #ifdef SIGTTIN + { "TTIN", SIGTTIN }, + #endif + #ifdef SIGTTOU + { "TTOU", SIGTTOU }, + #endif + #ifdef SIGIO + { "IO", SIGIO }, + #endif + #ifdef SIGPOLL + { "POLL", SIGPOLL }, + #endif + #ifdef SIGWINCH + { "WINCH", SIGWINCH }, + #endif + #ifdef SIGUSR1 + { "USR1", SIGUSR1 }, + #endif + #ifdef SIGUSR2 + { "USR2", SIGUSR2 }, + #endif + #ifdef SIGPWR + { "PWR", SIGPWR }, + #endif + #ifdef SIGXCPU + { "XCPU", SIGXCPU }, + #endif + #ifdef SIGXFSZ + { "XFSZ", SIGXFSZ }, + #endif + #ifdef SIGVTALRM + { "VTALRM", SIGVTALRM }, + #endif + #ifdef SIGPROF + { "PROF", SIGPROF }, + #endif + #ifdef SIGLOST + { "LOST", SIGLOST }, + #endif + /* The Issue 8 standard requires that SIGRTMIN and SIGRTMAX be included + * as valid results to be saved from calls to sig2str/str2sig. */ + #ifdef SIGRTMIN + { "RTMIN", SIGRTMIN }, + #endif + #ifdef SIGRTMAX + { "RTMAX", SIGRTMAX } + #endif +}; + +int +sig2str(int signum, char *str) +{ + const sig_name_and_num *sptr; + + /* If signum falls in lower half of the real time signals range, define + * the saved str value as "RTMIN+n" according to the Issue 8 standard */ + if ((SIGRTMIN + 1) <= signum && + signum <= (SIGRTMIN + SIGRTMAX) / 2) { + sprintf(str, "RTMIN+%d", (signum-SIGRTMIN)); + return 0; + } + + /* If signum falls in upper half of the real time signals range, define + * the saved str value as "RTMAX-m" according to the Issue 8 standard */ + if ((((SIGRTMIN + SIGRTMAX) / 2) + 1) <= signum && + signum <= (SIGRTMAX - 1)) { + sprintf(str, "RTMAX-%d", (SIGRTMAX - signum)); + return 0; + } + + /* Otherwise, search for signal matching signum in sig_array. If found, + * save its string value in str. */ + for (sptr = sig_array; sptr < &sig_array[NUM_OF_SIGS]; sptr++) { + if (sptr->sig_num == signum) { + strcpy(str, sptr->sig_name); + return 0; + } + } + + /* If signum is not a recognized signal number, store this message in str. */ + sprintf(str, "Unknown signal"); + return -1; +} + +int +str2sig(const char *restrict str, int *restrict pnum) +{ + unsigned long j = 0; + char *endp; + const sig_name_and_num *sptr; + int is_valid_decimal; + is_valid_decimal = atoi(str); + + /* If str is a representation of a decimal value, save its integer value + * in pnum. */ + if (1 <= is_valid_decimal && + is_valid_decimal <= SIGRTMAX) { + *pnum = is_valid_decimal; + return 0; + } + + /* i686 Cygwin only supports one RT signal. For this case, skip checks + * for "RTMIN+n" and "RTMAX-m". */ + if (SIGRTMIN != SIGRTMAX) { + + /* If str is in RT signal range, get number of of RT signal, save it as an + * integer. */ + if (strncmp(str, "RTMIN+", SPACES_TO_N) == 0) { + j = strtoul(&str[SPACES_TO_N], &endp, 10); + + /* If number is valid, save it in pnum. */ + if (*endp == '\0') { + if (1 <= j && + j <= ((SIGRTMAX - SIGRTMIN)-1)) { + *pnum = (SIGRTMIN + j); + return 0; + } + return -1; + } + return -1; + } + + /* If str is in RT signal range, get number of of RT signal, save it as an + * integer. */ + if (strncmp(str, "RTMAX-", SPACES_TO_N) == 0) { + j = strtoul(&str[SPACES_TO_N], &endp, 10); // and endptr null check + + /* If number is valid, save it in pnum. */ + if (*endp == '\0') { + if (1 <= j && + j <= ((SIGRTMAX - SIGRTMIN)-1)) { + *pnum = (SIGRTMAX - j); + return 0; + } + return -1; + } + return -1; + } + } + + /*If str is a valid signal name, save its corresponding number in pnum. */ + for (sptr = sig_array; sptr < &sig_array[NUM_OF_SIGS]; sptr++) { + if (strcmp(sptr->sig_name, str) == 0) { + *pnum = sptr->sig_num; + return 0; + } + } + return -1; +} -- 2.31.1