From: Waldemar Kozaczuk <[email protected]>
Committer: Nadav Har'El <[email protected]>
Branch: master

libc: replace string/strsignal.c with musl copy

This patch replaces libc/string/strsignal.c with a musl
copy under musl/src/string/strsignal.c. The musl copy
is newer and only slightly different.

This patch also adds relevant tests to tst-string.cc
to test strsignal() logic. Please note we disable
real-time signal test until we upgrade to newer version
of musl.

I have also noticed that on Linux strsignal()
returns messages ending with signal number like so:
'Unknown signal -1' whereas musl version simply
returns 'Unknown signal' without the signal number.
This might be fixed in future version of musl.

Signed-off-by: Waldemar Kozaczuk <[email protected]>
Message-Id: <[email protected]>

---
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -1600,7 +1600,7 @@ musl += string/strpbrk.o
 musl += string/strrchr.o
 musl += string/strsep.o
 libc += string/stresep.o
-libc += string/strsignal.o
+musl += string/strsignal.o
 musl += string/strspn.o
 musl += string/strstr.o
 musl += string/strtok.o
diff --git a/libc/string/strsignal.c b/libc/string/strsignal.c
--- a/libc/string/strsignal.c
+++ b/libc/string/strsignal.c
@@ -1,99 +0,0 @@
-#include <signal.h>
-#include <string.h>
-
-#if (SIGHUP == 1) && (SIGINT == 2) && (SIGQUIT == 3) && (SIGILL == 4) \
- && (SIGTRAP == 5) && (SIGABRT == 6) && (SIGBUS == 7) && (SIGFPE == 8) \
- && (SIGKILL == 9) && (SIGUSR1 == 10) && (SIGSEGV == 11) && (SIGUSR2 == 12) \
- && (SIGPIPE == 13) && (SIGALRM == 14) && (SIGTERM == 15) && (SIGSTKFLT == 16) 
\
- && (SIGCHLD == 17) && (SIGCONT == 18) && (SIGSTOP == 19) && (SIGTSTP == 20) \
- && (SIGTTIN == 21) && (SIGTTOU == 22) && (SIGURG == 23) && (SIGXCPU == 24) \
- && (SIGXFSZ == 25) && (SIGVTALRM == 26) && (SIGPROF == 27) && (SIGWINCH == 
28) \
- && (SIGPOLL == 29) && (SIGPWR == 30) && (SIGSYS == 31)
-
-#define sigmap(x) x
-
-#else
-
-static const char map[] = {
-       [SIGHUP]    = 1,
-       [SIGINT]    = 2,
-       [SIGQUIT]   = 3,
-       [SIGILL]    = 4,
-       [SIGTRAP]   = 5,
-       [SIGABRT]   = 6,
-       [SIGBUS]    = 7,
-       [SIGFPE]    = 8,
-       [SIGKILL]   = 9,
-       [SIGUSR1]   = 10,
-       [SIGSEGV]   = 11,
-       [SIGUSR2]   = 12,
-       [SIGPIPE]   = 13,
-       [SIGALRM]   = 14,
-       [SIGTERM]   = 15,
-       [SIGSTKFLT] = 16,
-       [SIGCHLD]   = 17,
-       [SIGCONT]   = 18,
-       [SIGSTOP]   = 19,
-       [SIGTSTP]   = 20,
-       [SIGTTIN]   = 21,
-       [SIGTTOU]   = 22,
-       [SIGURG]    = 23,
-       [SIGXCPU]   = 24,
-       [SIGXFSZ]   = 25,
-       [SIGVTALRM] = 26,
-       [SIGPROF]   = 27,
-       [SIGWINCH]  = 28,
-       [SIGPOLL]   = 29,
-       [SIGPWR]    = 30,
-       [SIGSYS]    = 31
-};
-
-#define sigmap(x) ((unsigned)(x) > sizeof map ? 0 : map[(unsigned)(x)])
-
-#endif
-
-static const char strings[] =
-       "Unknown signal\0"
-       "Hangup\0"
-       "Interrupt\0"
-       "Quit\0"
-       "Illegal instruction\0"
-       "Trace/breakpoint trap\0"
-       "Aborted\0"
-       "Bus error\0"
-       "Floating point exception\0"
-       "Killed\0"
-       "User defined signal 1\0"
-       "Segmentation fault\0"
-       "User defined signal 2\0"
-       "Broken pipe\0"
-       "Alarm clock\0"
-       "Terminated\0"
-       "Stack fault\0"
-       "Child exited\0"
-       "Continued\0"
-       "Stopped (signal)\0"
-       "Stopped\0"
-       "Stopped (tty input)\0"
-       "Stopped (tty output)\0"
-       "Urgent I/O condition\0"
-       "CPU time limit exceeded\0"
-       "File size limit exceeded\0"
-       "Virtual timer expired\0"
-       "Profiling timer expired\0"
-       "Window changed\0"
-       "I/O possible\0"
-       "Power failure\0"
-       "Bad system call";
-
-char *strsignal(int signum)
-{
-       char *s = (char *)strings;
-
-       signum = sigmap(signum);
-       if ((unsigned)signum - 1 > 31) signum = 0;
-
-       for (; signum--; s++) for (; *s; s++);
-
-       return s;
-}
diff --git a/tests/tst-string.cc b/tests/tst-string.cc
--- a/tests/tst-string.cc
+++ b/tests/tst-string.cc
@@ -26,6 +26,8 @@
 // PLUS some minor tweaks (mostly macros) that adapt it to run with boost unit 
framework
 // instead of Google's test framework
 
+// gcc tests/tst-string.cc -lstdc++  -lboost_unit_test_framework 
-lboost_filesystem -o /tmp/a
+//#define BOOST_TEST_DYN_LINK //ONLY FOR LINUX
 #define BOOST_TEST_MODULE tst-string
 
 #include <boost/test/unit_test.hpp>
@@ -34,6 +36,10 @@ namespace utf = boost::unit_test;
 #define TEST(MODULE_NAME,TEST_NAME) 
BOOST_AUTO_TEST_CASE(MODULE_NAME##TEST_NAME)
 #define ASSERT_TRUE(EXP) BOOST_REQUIRE(EXP)
 #define ASSERT_GT(EXP1,EXP2) BOOST_REQUIRE((EXP1)>(EXP2))
+#define ASSERT_EQ(EXP1,EXP2) BOOST_CHECK_EQUAL(EXP1,EXP2)
+#define ASSERT_STREQ(EXP1,EXP2) BOOST_CHECK_EQUAL(EXP1,EXP2)
+
+#include <signal.h>
 
 TEST(STRING_TEST, strxfrm_smoke) {
   locale_t l(newlocale(LC_ALL, "C.UTF-8", nullptr));
@@ -59,3 +65,35 @@ TEST(STRING_TEST, strcoll_smoke) {
   ASSERT_TRUE(strcoll_l("aac", "aab", l) > 0);
   freelocale(l);
 }
+
+TEST(STRING_TEST, strsignal) {
+  // A regular signal.
+  ASSERT_STREQ("Hangup", strsignal(1));
+
+  // A real-time signal.
+  // TODO: Disable unti we upgrade musl
+  //ASSERT_STREQ("Real-time signal 14", strsignal(SIGRTMIN + 14));
+
+  // Errors.
+  ASSERT_STREQ("Unknown signal", strsignal(-1)); // Too small.
+  ASSERT_STREQ("Unknown signal", strsignal(0)); // Still too small.
+  ASSERT_STREQ("Unknown signal", strsignal(1234)); // Too large.
+}
+
+static void* ConcurrentStrSignalFn(void*) {
+  bool equal = (strcmp("Unknown signal", strsignal(2002)) == 0);
+  return reinterpret_cast<void*>(equal);
+}
+
+TEST(STRING_TEST, strsignal_concurrent) {
+  const char* strsignal1001 = strsignal(1001);
+  ASSERT_STREQ("Unknown signal", strsignal1001);
+
+  pthread_t t;
+  ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentStrSignalFn, nullptr));
+  void* result;
+  ASSERT_EQ(0, pthread_join(t, &result));
+  ASSERT_TRUE(static_cast<bool>(result));
+
+  ASSERT_STREQ("Unknown signal", strsignal1001);
+}

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/0000000000006406d005adc350f3%40google.com.

Reply via email to