From ecde9bbba99d4c15512abca4bcea287f38eda5ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Bac=C5=82awski?= Date: Mon, 31 Jul 2023 09:36:29 +0200 Subject: [PATCH] Limit use of sched_yield for sync in tests (#1407) --- bin/utest/fork.c | 10 +++++++++- bin/utest/mmap.c | 20 ++++++-------------- bin/utest/pgrp.c | 35 ++++++++++++++++++----------------- bin/utest/signal.c | 5 ++--- bin/utest/util.c | 2 +- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/bin/utest/fork.c b/bin/utest/fork.c index be2f1b2b5..c821f2f3d 100644 --- a/bin/utest/fork.c +++ b/bin/utest/fork.c @@ -32,13 +32,21 @@ static void sigchld_handler(int signo) { TEST_ADD(fork_signal, 0) { xsignal(SIGCHLD, sigchld_handler); + + sigset_t mask, saved; + sigemptyset(&mask); + sigaddset(&mask, SIGCHLD); + xsigprocmask(SIG_BLOCK, &mask, &saved); + pid_t n = xfork(); if (n == 0) exit(0); /* Wait for the child to get reaped by signal handler. */ while (!done) - sched_yield(); + sigsuspend(&saved); + + xsigprocmask(SIG_UNBLOCK, &mask, NULL); xsignal(SIGCHLD, SIG_DFL); return 0; } diff --git a/bin/utest/mmap.c b/bin/utest/mmap.c index fbc315469..87480c3aa 100644 --- a/bin/utest/mmap.c +++ b/bin/utest/mmap.c @@ -125,16 +125,10 @@ TEST_ADD(munmap, 0) { return 0; } -static volatile int sigcont_handled = 0; - -static void sigcont_handler(int signo) { - debug("sigcont handled!"); - sigcont_handled = 1; -} - TEST_ADD(mmap_private, 0) { size_t pgsz = getpagesize(); - xsignal(SIGCONT, sigcont_handler); + + signal_setup(SIGUSR1); /* mmap & munmap one page */ char *addr = mmap_anon_prw(NULL, pgsz); @@ -153,9 +147,8 @@ TEST_ADD(mmap_private, 0) { debug("Child written: '%s'", addr); /* Wait for parent to check and modify its memory. */ - xkill(getppid(), SIGCONT); - while (!sigcont_handled) - sched_yield(); + xkill(getppid(), SIGUSR1); + wait_for_signal(SIGUSR1); debug("Child read again: '%s'", addr); string_eq(addr, "child"); @@ -163,8 +156,7 @@ TEST_ADD(mmap_private, 0) { } /* Wait for child to check and modify its memory. */ - while (!sigcont_handled) - sched_yield(); + wait_for_signal(SIGUSR1); debug("Parent read: '%s'", addr); /* Check and modify. */ @@ -172,7 +164,7 @@ TEST_ADD(mmap_private, 0) { strcpy(addr, "parent again"); /* Resume child. */ - xkill(pid, SIGCONT); + xkill(pid, SIGUSR1); wait_child_finished(pid); return 0; diff --git a/bin/utest/pgrp.c b/bin/utest/pgrp.c index 2a4fdf194..64cb24529 100644 --- a/bin/utest/pgrp.c +++ b/bin/utest/pgrp.c @@ -51,13 +51,14 @@ TEST_ADD(setpgid_leader, 0) { /* Can't change pgrp of session leader. */ assert(setpgid(0, 0)); + xkill(getppid(), SIGUSR1); wait_for_signal(SIGUSR1); return 0; } /* Wait until child becomes session leader. */ - while (getsid(cpid) != cpid) - sched_yield(); + wait_for_signal(SIGUSR1); + assert(getsid(cpid) == cpid); /* Can't change pgrp of session leader. */ assert(setpgid(cpid, getpgid(0))); @@ -69,13 +70,17 @@ TEST_ADD(setpgid_leader, 0) { } TEST_ADD(setpgid_child, 0) { - signal_setup(SIGUSR1); + signal_setup(SIGUSR1); /* child 1 */ + signal_setup(SIGUSR2); /* child 2 */ pid_t cpid1 = xfork(); if (cpid1 == 0) { /* Become session leader. */ assert(setsid() == getpid()); + /* Signal readiness to parent. */ + xkill(getppid(), SIGUSR1); + /* Wait until our parent gives a signal to exit. */ wait_for_signal(SIGUSR1); return 0; @@ -84,23 +89,23 @@ TEST_ADD(setpgid_child, 0) { pid_t cpid2 = xfork(); if (cpid2 == 0) { /* Signal readiness to parent. */ - xkill(getppid(), SIGUSR1); + xkill(getppid(), SIGUSR2); /* A child should not be able to change its parent's * process group. */ assert(setpgid(getppid(), 0)); /* Wait until our parent gives a signal to exit. */ - wait_for_signal(SIGUSR1); + wait_for_signal(SIGUSR2); return 0; } /* Wait for child 1 to become session leader. */ - while (getsid(cpid1) != cpid1) - sched_yield(); + wait_for_signal(SIGUSR1); + assert(getsid(cpid1) == cpid1); /* Wait until child 2 is ready. */ - wait_for_signal(SIGUSR1); + wait_for_signal(SIGUSR2); /* Move child 2 into its own process group. */ assert(!setpgid(cpid2, cpid2)); @@ -115,7 +120,7 @@ TEST_ADD(setpgid_child, 0) { assert(getpgid(cpid2) == getpgid(0)); xkill(cpid1, SIGUSR1); - xkill(cpid2, SIGUSR1); + xkill(cpid2, SIGUSR2); wait_child_finished(cpid1); wait_child_finished(cpid2); @@ -277,11 +282,9 @@ TEST_ADD(pgrp_orphan, 0) { return 0; } -static volatile pid_t parent_sid; - TEST_ADD(session_basic, 0) { signal_setup(SIGUSR1); - parent_sid = getsid(getpid()); + pid_t parent_sid = getsid(getpid()); assert(parent_sid != -1); pid_t cpid = xfork(); if (cpid == 0) { @@ -292,6 +295,7 @@ TEST_ADD(session_basic, 0) { assert(getsid(ppid) == parent_sid); /* Create a session. This should always succeed. */ assert(setsid() == cpid); + xkill(ppid, SIGUSR1); assert(getsid(0) == cpid); assert(getsid(ppid) == parent_sid); /* Creating a session when we're already a leader should fail. */ @@ -303,11 +307,8 @@ TEST_ADD(session_basic, 0) { return 0; } - pid_t child_sid; - while ((child_sid = getsid(cpid)) != cpid) { - assert(child_sid == parent_sid); - sched_yield(); - } + wait_for_signal(SIGUSR1); + assert(getsid(cpid) == cpid); xkill(cpid, SIGUSR1); wait_child_finished(cpid); diff --git a/bin/utest/signal.c b/bin/utest/signal.c index e512830b1..d2ec0e1d8 100644 --- a/bin/utest/signal.c +++ b/bin/utest/signal.c @@ -47,9 +47,8 @@ TEST_ADD(signal_send, 0) { pid_t pid = xfork(); if (pid == 0) { debug("This is child (mypid = %d)", getpid()); - /* Wait for signal. */ - while (1) - sched_yield(); + pause(); /* Wait for signal. */ + exit(0); } debug("This is parent (childpid = %d, mypid = %d)", pid, getpid()); diff --git a/bin/utest/util.c b/bin/utest/util.c index 384f658a3..7fc8ce65a 100644 --- a/bin/utest/util.c +++ b/bin/utest/util.c @@ -45,7 +45,7 @@ void signal_setup(int signo) { sigset_t mask; sigemptyset(&mask); sigaddset(&mask, signo); - assert(sigprocmask(SIG_BLOCK, &mask, NULL) == 0); + xsigprocmask(SIG_BLOCK, &mask, NULL); } void wait_for_signal(int signo) {