Module Name:    src
Committed By:   riastradh
Date:           Tue Aug 25 01:37:39 UTC 2020

Modified Files:
        src/tests/lib/libc/sys: t_getrandom.c

Log Message:
Fix getrandom() tests.

Use sigaction() without SA_RESTART -- signal() implies SA_RESTART so
we never got the EINTR.

While here, reduce the timeout to something more reasonable so we
don't waste 20min of testbed time if anything goes wrong and the
one-second alarm doesn't fire.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/tests/lib/libc/sys/t_getrandom.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libc/sys/t_getrandom.c
diff -u src/tests/lib/libc/sys/t_getrandom.c:1.2 src/tests/lib/libc/sys/t_getrandom.c:1.3
--- src/tests/lib/libc/sys/t_getrandom.c:1.2	Sun Aug 23 17:50:19 2020
+++ src/tests/lib/libc/sys/t_getrandom.c	Tue Aug 25 01:37:38 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_getrandom.c,v 1.2 2020/08/23 17:50:19 riastradh Exp $	*/
+/*	$NetBSD: t_getrandom.c,v 1.3 2020/08/25 01:37:38 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -30,13 +30,14 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_getrandom.c,v 1.2 2020/08/23 17:50:19 riastradh Exp $");
+__RCSID("$NetBSD: t_getrandom.c,v 1.3 2020/08/25 01:37:38 riastradh Exp $");
 
 #include <sys/random.h>
 
 #include <atf-c.h>
 #include <errno.h>
 #include <signal.h>
+#include <stdio.h>
 #include <unistd.h>
 
 static uint8_t buf[65536];
@@ -45,6 +46,22 @@ static uint8_t zero24[24];
 static void
 alarm_handler(int signo)
 {
+
+	fprintf(stderr, "timeout\n");
+}
+
+static void
+install_alarm_handler(void)
+{
+	struct sigaction sa;
+
+	memset(&sa, 0, sizeof sa);
+	sa.sa_handler = alarm_handler;
+	sigfillset(&sa.sa_mask);
+	sa.sa_flags = 0;	/* no SA_RESTART */
+
+	ATF_CHECK_MSG(sigaction(SIGALRM, &sa, NULL) != -1,
+	    "sigaction(SIGALRM): %s", strerror(errno));
 }
 
 /*
@@ -58,14 +75,14 @@ ATF_TC(getrandom_default);
 ATF_TC_HEAD(getrandom_default, tc)
 {
 	atf_tc_set_md_var(tc, "descr", "getrandom(..., 0)");
+	atf_tc_set_md_var(tc, "timeout", "2");
 }
 ATF_TC_BODY(getrandom_default, tc)
 {
 	ssize_t n;
 
-	ATF_REQUIRE(signal(SIGALRM, &alarm_handler) != SIG_ERR);
-
 	/* default */
+	install_alarm_handler();
 	alarm(1);
 	memset(buf, 0, sizeof buf);
 	n = getrandom(buf, sizeof buf, 0);
@@ -141,14 +158,14 @@ ATF_TC(getrandom_random);
 ATF_TC_HEAD(getrandom_random, tc)
 {
 	atf_tc_set_md_var(tc, "descr", "getrandom(..., GRND_RANDOM)");
+	atf_tc_set_md_var(tc, "timeout", "2");
 }
 ATF_TC_BODY(getrandom_random, tc)
 {
 	ssize_t n;
 
-	ATF_REQUIRE(signal(SIGALRM, &alarm_handler) != SIG_ERR);
-
 	/* `random' (hokey) */
+	install_alarm_handler();
 	alarm(1);
 	memset(buf, 0, sizeof buf);
 	n = getrandom(buf, sizeof buf, GRND_RANDOM);

Reply via email to