Skip to content

Commit

Permalink
Limit use of sched_yield for sync in tests (#1407)
Browse files Browse the repository at this point in the history
  • Loading branch information
cahirwpz authored Jul 31, 2023
1 parent f693fc2 commit ecde9bb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
10 changes: 9 additions & 1 deletion bin/utest/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 6 additions & 14 deletions bin/utest/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -153,26 +147,24 @@ 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");
exit(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. */
string_eq(addr, "parent");
strcpy(addr, "parent again");

/* Resume child. */
xkill(pid, SIGCONT);
xkill(pid, SIGUSR1);

wait_child_finished(pid);
return 0;
Expand Down
35 changes: 18 additions & 17 deletions bin/utest/pgrp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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;
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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. */
Expand All @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions bin/utest/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion bin/utest/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ecde9bb

Please sign in to comment.