public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] tst: Provide test for ppoll
@ 2021-01-14 12:02 Lukasz Majewski
  2021-01-14 12:09 ` Florian Weimer
  0 siblings, 1 reply; 2+ messages in thread
From: Lukasz Majewski @ 2021-01-14 12:02 UTC (permalink / raw)
  To: Joseph Myers, Adhemerval Zanella
  Cc: Paul Eggert, Alistair Francis, Arnd Bergmann, Alistair Francis,
	GNU C Library, Florian Weimer, Carlos O'Donell,
	Florian Weimer, Zack Weinberg, Lukasz Majewski

This change adds new test to assess ppoll()'s timeout related
functionality (the struct pollfd doesn't provide valid fd to wait
for - just wait for timeout).

To be more specific - two use cases are checked:
- if ppoll() times out immediately when passed struct timespec has zero
values of tv_nsec and tv_sec.
- if ppoll() times out adfter timeout specified in passed argument
---
 sysdeps/unix/sysv/linux/Makefile    |  2 +-
 sysdeps/unix/sysv/linux/tst-ppoll.c | 80 +++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 1 deletion(-)
 create mode 100644 sysdeps/unix/sysv/linux/tst-ppoll.c

diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 7503b356c8..f4029a74ca 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -108,7 +108,7 @@ tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
 	 test-errno-linux tst-memfd_create tst-mlock2 tst-pkey \
 	 tst-rlimit-infinity tst-ofdlocks tst-gettid tst-gettid-kill \
 	 tst-tgkill tst-sysvsem-linux tst-sysvmsg-linux tst-sysvshm-linux \
-	 tst-timerfd
+	 tst-timerfd tst-ppoll
 tests-internal += tst-ofdlocks-compat tst-sigcontext-get_pc
 
 CFLAGS-tst-sigcontext-get_pc.c = -fasynchronous-unwind-tables
diff --git a/sysdeps/unix/sysv/linux/tst-ppoll.c b/sysdeps/unix/sysv/linux/tst-ppoll.c
new file mode 100644
index 0000000000..158e3ef122
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-ppoll.c
@@ -0,0 +1,80 @@
+/* Test for ppoll timeout
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* The ppoll() is only provided when _GNU_SOURCE is defined */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <time.h>
+#include <errno.h>
+#include <poll.h>
+#include <stdio.h>
+
+/* Timeout in seconds for PPOLL.  */
+#define PPOLL_TIMEOUT 2
+/* Timeout for test program - must be larger than PPOLL_TIMEOUT.  */
+#define TIMEOUT 3
+
+static int test_ppoll_timeout (int timeout)
+{
+  struct timespec tv0 = { 0, 0 }, tv1 = { 0, 0 };
+  struct timespec tv = { 0, 0 };
+  struct pollfd fds = { -1, 0, 0 };   /* Ignore fds - just wait for timeout */
+  int ret;
+
+  tv.tv_sec = timeout;
+
+  ret = clock_gettime (CLOCK_REALTIME, &tv0);
+  if (ret != 0) {
+    printf ("*** failed to read current time: %m\n");
+    return ret;
+  }
+
+  ret = ppoll (&fds, 1, &tv, 0);
+  if (ret == 0) {
+    ret = clock_gettime (CLOCK_REALTIME, &tv1);
+    if (ret == 0) {
+      if (tv0.tv_sec + timeout != tv1.tv_sec) {
+        printf ("*** ppoll failed to timeout after %d [s]\n", timeout);
+        ret = -1;
+      }
+    } else {
+      printf ("*** failed to read current time: %m\n");
+    }
+  }
+
+  return ret;
+}
+
+static int
+do_test (void)
+{
+  int ret;
+
+  /* Check if ppoll exits immediately */
+  ret = test_ppoll_timeout (0);
+  if (ret != 0)
+    return ret;
+
+  /* Check if ppoll exits immediately */
+  ret = test_ppoll_timeout (PPOLL_TIMEOUT);
+
+  return ret;
+}
+
+#include <support/test-driver.c>
-- 
2.20.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] tst: Provide test for ppoll
  2021-01-14 12:02 [PATCH] tst: Provide test for ppoll Lukasz Majewski
@ 2021-01-14 12:09 ` Florian Weimer
  0 siblings, 0 replies; 2+ messages in thread
From: Florian Weimer @ 2021-01-14 12:09 UTC (permalink / raw)
  To: Lukasz Majewski
  Cc: Joseph Myers, Adhemerval Zanella, GNU C Library, Florian Weimer,
	Alistair Francis

* Lukasz Majewski:

> +/* The ppoll() is only provided when _GNU_SOURCE is defined */
> +#ifndef _GNU_SOURCE
> +#define _GNU_SOURCE
> +#endif

This is not necessary in the glibc test suite.

> +#include <time.h>
> +#include <errno.h>
> +#include <poll.h>
> +#include <stdio.h>
> +
> +/* Timeout in seconds for PPOLL.  */
> +#define PPOLL_TIMEOUT 2
> +/* Timeout for test program - must be larger than PPOLL_TIMEOUT.  */
> +#define TIMEOUT 3
> +
> +static int test_ppoll_timeout (int timeout)
> +{
> +  struct timespec tv0 = { 0, 0 }, tv1 = { 0, 0 };
> +  struct timespec tv = { 0, 0 };
> +  struct pollfd fds = { -1, 0, 0 };   /* Ignore fds - just wait for timeout */

Missing . at the end of comment.

> +  tv.tv_sec = timeout;
> +
> +  ret = clock_gettime (CLOCK_REALTIME, &tv0);
> +  if (ret != 0) {
> +    printf ("*** failed to read current time: %m\n");
> +    return ret;
> +  }

You can use xclock_gettime here.

> +
> +  ret = ppoll (&fds, 1, &tv, 0);
> +  if (ret == 0) {
> +    ret = clock_gettime (CLOCK_REALTIME, &tv1);
> +    if (ret == 0) {
> +      if (tv0.tv_sec + timeout != tv1.tv_sec) {
> +        printf ("*** ppoll failed to timeout after %d [s]\n", timeout);
> +        ret = -1;
> +      }

Please use FAIL_EXIT1.  do_test does not run the second test on error
anyway.

> +static int
> +do_test (void)
> +{
> +  int ret;
> +
> +  /* Check if ppoll exits immediately */
> +  ret = test_ppoll_timeout (0);
> +  if (ret != 0)
> +    return ret;
> +
> +  /* Check if ppoll exits immediately */
> +  ret = test_ppoll_timeout (PPOLL_TIMEOUT);

Missing . at end of comment.  Second comment is incorrect.

Thanks,
Florian
-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-01-14 12:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-14 12:02 [PATCH] tst: Provide test for ppoll Lukasz Majewski
2021-01-14 12:09 ` Florian Weimer

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).