tst: Provide test for ppoll

Message ID 20210114120216.12389-1-lukma@denx.de
State Superseded
Headers
Series tst: Provide test for ppoll |

Commit Message

Lukasz Majewski Jan. 14, 2021, 12:02 p.m. UTC
  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
  

Comments

Florian Weimer Jan. 14, 2021, 12:09 p.m. UTC | #1
* 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
  

Patch

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>