public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Gabriel F. T. Gomes" <gftg@linux.vnet.ibm.com>
To: libc-alpha@sourceware.org
Cc: fweimer@redhat.com
Subject: [PATCH 2/2] Add write_message_to_stderr and use it instead of write
Date: Tue, 01 Nov 2016 00:44:00 -0000	[thread overview]
Message-ID: <1477961028-12150-3-git-send-email-gftg@linux.vnet.ibm.com> (raw)
In-Reply-To: <1477961028-12150-1-git-send-email-gftg@linux.vnet.ibm.com>

Replaces calls to write on file descriptor 2 with calls to the new function
write_message_to_stderr, which writes to STDERR_FILENO (2) and properly
deals with the return of write.

2016-10-30  Gabriel F. T. Gomes  <gftg@linux.vnet.ibm.com>

	* test-skeleton.c (write_message_to_stderr): New function.
	* nptl/tst-basic1.c (tf, do_test): Use write_message_to_stderr.
	* nptl/tst-cleanup1.c (do_test): Likewise.
	* nptl/tst-cleanup3.c (do_test): Likewise.
	* nptl/tst-key1.c (do_test): Likewise.
	* nptl/tst-key2.c (tf, do_test): Likewise.
	* nptl/tst-key3.c (tf, do_test): Likewise.
	* nptl/tst-stdio1.c (do_test): Likewise.
	* nptl/tst-stdio2.c (do_test): Likewise.
---
 nptl/tst-basic1.c   | 15 ++++++++-------
 nptl/tst-cleanup1.c | 11 ++++++-----
 nptl/tst-cleanup3.c | 11 ++++++-----
 nptl/tst-key1.c     | 11 ++++++-----
 nptl/tst-key2.c     | 23 ++++++++++++-----------
 nptl/tst-key3.c     | 31 ++++++++++++++++---------------
 nptl/tst-stdio1.c   |  2 +-
 nptl/tst-stdio2.c   | 11 ++++++-----
 test-skeleton.c     |  9 +++++++++
 9 files changed, 70 insertions(+), 54 deletions(-)

diff --git a/nptl/tst-basic1.c b/nptl/tst-basic1.c
index 8ea4523..144aaf6 100644
--- a/nptl/tst-basic1.c
+++ b/nptl/tst-basic1.c
@@ -22,6 +22,11 @@
 #include <sys/types.h>
 
 
+static int do_test (void);
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
+
 static pid_t pid;
 
 static void *
@@ -29,7 +34,7 @@ tf (void *a)
 {
   if (getpid () != pid)
     {
-      write (2, "pid mismatch\n", 13);
+      write_message_to_stderr ("pid mismatch\n");
       _exit (1);
     }
 
@@ -49,7 +54,7 @@ do_test (void)
   for (i = 0; i < N; ++i)
     if (pthread_create (&t[i], NULL, tf, (void *) (long int) (i + 1)) != 0)
       {
-	write (2, "create failed\n", 14);
+	write_message_to_stderr ("create failed\n");
 	_exit (1);
       }
     else
@@ -66,7 +71,7 @@ do_test (void)
 	}
       else if (r != (void *) (long int) (i + 1))
 	{
-	  write (2, "result wrong\n", 13);
+	  write_message_to_stderr ("result wrong\n");
 	  _exit (1);
 	}
       else
@@ -75,7 +80,3 @@ do_test (void)
 
   return 0;
 }
-
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
diff --git a/nptl/tst-cleanup1.c b/nptl/tst-cleanup1.c
index 82a568b..bca057a 100644
--- a/nptl/tst-cleanup1.c
+++ b/nptl/tst-cleanup1.c
@@ -22,6 +22,11 @@
 #include <unistd.h>
 
 
+static int do_test (void);
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
+
 static int global;
 
 
@@ -67,7 +72,7 @@ do_test (void)
 
   if (pthread_create (&th, NULL, tf, NULL) != 0)
     {
-      write (2, "create failed\n", 14);
+      write_message_to_stderr ("create failed\n");
       _exit (1);
     }
 
@@ -93,7 +98,3 @@ do_test (void)
 
   return 0;
 }
-
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
diff --git a/nptl/tst-cleanup3.c b/nptl/tst-cleanup3.c
index 3c34a1e..da20f28 100644
--- a/nptl/tst-cleanup3.c
+++ b/nptl/tst-cleanup3.c
@@ -22,6 +22,11 @@
 #include <unistd.h>
 
 
+static int do_test (void);
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
+
 static int global;
 
 
@@ -65,7 +70,7 @@ do_test (void)
 
   if (pthread_create (&th, NULL, tf, NULL) != 0)
     {
-      write (2, "create failed\n", 14);
+      write_message_to_stderr ("create failed\n");
       _exit (1);
     }
 
@@ -91,7 +96,3 @@ do_test (void)
 
   return 0;
 }
-
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
diff --git a/nptl/tst-key1.c b/nptl/tst-key1.c
index 0ed1787..a3ab5bf 100644
--- a/nptl/tst-key1.c
+++ b/nptl/tst-key1.c
@@ -23,6 +23,11 @@
 #include <unistd.h>
 
 
+static int do_test (void);
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
+
 int
 do_test (void)
 {
@@ -38,7 +43,7 @@ do_test (void)
   for (i = 0; i < max; ++i)
     if (pthread_key_create (&keys[i], NULL) != 0)
       {
-	write (2, "key_create failed\n", 18);
+	write_message_to_stderr ("key_create failed\n");
 	_exit (1);
       }
     else
@@ -82,7 +87,3 @@ do_test (void)
 
   return 0;
 }
-
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
diff --git a/nptl/tst-key2.c b/nptl/tst-key2.c
index 3142c86..bdf17fe 100644
--- a/nptl/tst-key2.c
+++ b/nptl/tst-key2.c
@@ -23,6 +23,11 @@
 #define N 2
 
 
+static int do_test (void);
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
+
 static int cnt0;
 static void
 f0 (void *p)
@@ -53,7 +58,7 @@ tf (void *arg)
 
   if (pthread_setspecific (*key, (void *) -1l) != 0)
     {
-      write (2, "setspecific failed\n", 19);
+      write_message_to_stderr ("setspecific failed\n");
       _exit (1);
     }
 
@@ -70,45 +75,41 @@ do_test (void)
   for (i = 0; i < N; ++i)
     if (pthread_key_create (&keys[i], fcts[i]) != 0)
       {
-	write (2, "key_create failed\n", 18);
+	write_message_to_stderr ("key_create failed\n");
 	_exit (1);
       }
 
   pthread_t th;
   if (pthread_create (&th, NULL, tf, &keys[1]) != 0)
     {
-      write (2, "create failed\n", 14);
+      write_message_to_stderr ("create failed\n");
       _exit (1);
     }
 
   if (pthread_join (th, NULL) != 0)
     {
-      write (2, "join failed\n", 12);
+      write_message_to_stderr ("join failed\n");
       _exit (1);
     }
 
   if (cnt0 != 0)
     {
-      write (2, "cnt0 != 0\n", 10);
+      write_message_to_stderr ("cnt0 != 0\n");
       _exit (1);
     }
 
   if (cnt1 != 1)
     {
-      write (2, "cnt1 != 1\n", 10);
+      write_message_to_stderr ("cnt1 != 1\n");
       _exit (1);
     }
 
   for (i = 0; i < N; ++i)
     if (pthread_key_delete (keys[i]) != 0)
       {
-	write (2, "key_delete failed\n", 18);
+	write_message_to_stderr ("key_delete failed\n");
 	_exit (1);
       }
 
   return 0;
 }
-
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
diff --git a/nptl/tst-key3.c b/nptl/tst-key3.c
index a298569..24156a4 100644
--- a/nptl/tst-key3.c
+++ b/nptl/tst-key3.c
@@ -23,6 +23,11 @@
 #define N 2
 
 
+static int do_test (void);
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
+
 static int cnt0;
 static void
 f0 (void *p)
@@ -56,7 +61,7 @@ tf (void *arg)
 
   if (pthread_setspecific (*key, (void *) -1l) != 0)
     {
-      write (2, "setspecific failed\n", 19);
+      write_message_to_stderr ("setspecific failed\n");
       _exit (1);
     }
 
@@ -80,20 +85,20 @@ do_test (void)
   for (i = 0; i < N; ++i)
     if (pthread_key_create (&keys[i], fcts[i]) != 0)
       {
-	write (2, "key_create failed\n", 18);
+	write_message_to_stderr ("key_create failed\n");
 	_exit (1);
       }
 
   if (pthread_barrier_init (&b, NULL, 2) != 0)
     {
-      write (2, "barrier_init failed\n", 20);
+      write_message_to_stderr ("barrier_init failed\n");
       _exit (1);
     }
 
   pthread_t th;
   if (pthread_create (&th, NULL, tf, &keys[1]) != 0)
     {
-      write (2, "create failed\n", 14);
+      write_message_to_stderr ("create failed\n");
       _exit (1);
     }
 
@@ -101,20 +106,20 @@ do_test (void)
 
   if (pthread_cancel (th) != 0)
     {
-      write (2, "cancel failed\n", 14);
+      write_message_to_stderr ("cancel failed\n");
       _exit (1);
     }
 
   void *status;
   if (pthread_join (th, &status) != 0)
     {
-      write (2, "join failed\n", 12);
+      write_message_to_stderr ("join failed\n");
       _exit (1);
     }
 
   if (status != PTHREAD_CANCELED)
     {
-      write (2, "thread not canceled\n", 20);
+      write_message_to_stderr ("thread not canceled\n");
       _exit (1);
     }
 
@@ -124,32 +129,28 @@ do_test (void)
      have run and therefore these tests succeed.  */
   if (cnt0 != 0)
     {
-      write (2, "cnt0 != 0\n", 10);
+      write_message_to_stderr ("cnt0 != 0\n");
       _exit (1);
     }
 
   if (cnt1 != 1)
     {
-      write (2, "cnt1 != 1\n", 10);
+      write_message_to_stderr ("cnt1 != 1\n");
       _exit (1);
     }
 
   for (i = 0; i < N; ++i)
     if (pthread_key_delete (keys[i]) != 0)
       {
-	write (2, "key_delete failed\n", 18);
+	write_message_to_stderr ("key_delete failed\n");
 	_exit (1);
       }
 
   if (pthread_barrier_destroy (&b) != 0)
     {
-      write (2, "barrier_destroy failed\n", 23);
+      write_message_to_stderr ("barrier_destroy failed\n");
       _exit (1);
     }
 
   return 0;
 }
-
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
diff --git a/nptl/tst-stdio1.c b/nptl/tst-stdio1.c
index 4250e53..e9b9f18 100644
--- a/nptl/tst-stdio1.c
+++ b/nptl/tst-stdio1.c
@@ -43,7 +43,7 @@ do_test (void)
 
   if (pthread_create (&th, NULL, tf, NULL) != 0)
     {
-      write (2, "create failed\n", 14);
+      write_message_to_stderr ("create failed\n");
       _exit (1);
     }
 
diff --git a/nptl/tst-stdio2.c b/nptl/tst-stdio2.c
index 6168d27..910c13c 100644
--- a/nptl/tst-stdio2.c
+++ b/nptl/tst-stdio2.c
@@ -23,6 +23,11 @@
 #include <unistd.h>
 
 
+static int do_test (void);
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
+
 static void *tf (void *a)
 {
   puts ("start tf");
@@ -55,7 +60,7 @@ do_test (void)
 
   if (pthread_create (&th, NULL, tf, NULL) != 0)
     {
-      write (2, "create failed\n", 14);
+      write_message_to_stderr ("create failed\n");
       _exit (1);
     }
 
@@ -75,7 +80,3 @@ do_test (void)
 
   return 0;
 }
-
-
-#define TEST_FUNCTION do_test ()
-#include "../test-skeleton.c"
diff --git a/test-skeleton.c b/test-skeleton.c
index 55841fb..803da2a 100644
--- a/test-skeleton.c
+++ b/test-skeleton.c
@@ -148,6 +148,15 @@ xasprintf (const char *format, ...)
   return result;
 }
 
+/* Write a message to standard error.  Can be used in signal handlers.  */
+static void
+__attribute__ ((unused))
+write_message_to_stderr (const char *message)
+{
+  ssize_t unused __attribute__ ((unused));
+  unused = write (STDERR_FILENO, message, strlen (message));
+}
+
 /* Write a message to standard output.  Can be used in signal
    handlers.  */
 static void
-- 
2.4.11

  parent reply	other threads:[~2016-11-01  0:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-01  0:44 [PATCH 0/2] Cleanup some calls to write with unused result Gabriel F. T. Gomes
2016-11-01  0:44 ` [PATCH 1/2] Use write_message instead of write Gabriel F. T. Gomes
2016-11-07 17:33   ` Florian Weimer
2016-11-08  0:40     ` Gabriel F. T. Gomes
2016-11-01  0:44 ` Gabriel F. T. Gomes [this message]
2016-11-07 17:32   ` [PATCH 2/2] Add write_message_to_stderr and use it " Florian Weimer
2016-11-08  0:41     ` Gabriel F. T. Gomes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1477961028-12150-3-git-send-email-gftg@linux.vnet.ibm.com \
    --to=gftg@linux.vnet.ibm.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).