|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Copyright (C) 2026 Red Hat, Inc. |
| 4 | + */ |
| 5 | + |
| 6 | +/*\ |
| 7 | + * Check that futex(FUTEX_WAIT) returns EINTR when interrupted by a signal. |
| 8 | + * A child process blocks on futex_wait() with a long timeout. The parent |
| 9 | + * waits for the child to enter sleep state, then sends SIGUSR1 to it. |
| 10 | + * The child verifies it received EINTR and exits accordingly. |
| 11 | + */ |
| 12 | + |
| 13 | +#include <errno.h> |
| 14 | +#include <signal.h> |
| 15 | +#include <sys/wait.h> |
| 16 | + |
| 17 | +#include "futextest.h" |
| 18 | + |
| 19 | +static futex_t *futex; |
| 20 | + |
| 21 | +static struct futex_test_variants variants[] = { |
| 22 | +#if (__NR_futex != __LTP__NR_INVALID_SYSCALL) |
| 23 | + { .fntype = FUTEX_FN_FUTEX, .tstype = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"}, |
| 24 | +#endif |
| 25 | + |
| 26 | +#if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL) |
| 27 | + { .fntype = FUTEX_FN_FUTEX64, .tstype = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"}, |
| 28 | +#endif |
| 29 | +}; |
| 30 | + |
| 31 | +/* We need a handler so SIGUSR1 is caught instead of killing the process. |
| 32 | + * The empty body is needed, just receiving the signal is enough to |
| 33 | + * interrupt futex_wait() and make it return into EINTR -1 status. |
| 34 | + */ |
| 35 | +static void sigusr1_handler(int sig LTP_ATTRIBUTE_UNUSED) |
| 36 | +{ |
| 37 | +} |
| 38 | + |
| 39 | +static void do_child(void) |
| 40 | +{ |
| 41 | + struct futex_test_variants *tv = &variants[tst_variant]; |
| 42 | + struct sigaction sa; |
| 43 | + struct tst_ts timeout; |
| 44 | + int res; |
| 45 | + |
| 46 | + /* Set up the signal handler for SIGUSR1 */ |
| 47 | + sa.sa_handler = sigusr1_handler; |
| 48 | + sa.sa_flags = 0; |
| 49 | + SAFE_SIGEMPTYSET(&sa.sa_mask); |
| 50 | + SAFE_SIGACTION(SIGUSR1, &sa, NULL); |
| 51 | + |
| 52 | + /* Create a timeout for 5 sec for this variant. |
| 53 | + * if no one wakes the child before 5 sec, futex_wait() will return |
| 54 | + * on its own with ETIMEDOUT and will not wait any longer |
| 55 | + */ |
| 56 | + timeout = tst_ts_from_ms(tv->tstype, 5000); |
| 57 | + res = futex_wait(tv->fntype, futex, *futex, &timeout, 0); |
| 58 | + |
| 59 | + if (res != -1) { |
| 60 | + tst_res(TFAIL, "futex_wait() should have failed with EINTR but returned success instead"); |
| 61 | + exit(1); |
| 62 | + } |
| 63 | + if (errno != EINTR) { |
| 64 | + tst_res(TFAIL | TERRNO, "futex_wait() expected EINTR but got something else, errno"); |
| 65 | + exit(1); |
| 66 | + } |
| 67 | + tst_res(TPASS | TERRNO, "futex_wait() returned EINTR as expected"); |
| 68 | + exit(0); |
| 69 | +} |
| 70 | + |
| 71 | +static void run(void) |
| 72 | +{ |
| 73 | + pid_t child; |
| 74 | + int status; |
| 75 | + |
| 76 | + child = SAFE_FORK(); |
| 77 | + |
| 78 | + if (child == 0) { |
| 79 | + do_child(); |
| 80 | + } |
| 81 | + /* Wait until child is sleeping before sending signal */ |
| 82 | + TST_PROCESS_STATE_WAIT(child, 'S', 0); |
| 83 | + /* Wake up the child so it will return EINTR -1 status */ |
| 84 | + SAFE_KILL(child, SIGUSR1); |
| 85 | + SAFE_WAITPID(child, &status, 0); |
| 86 | + /* Check if the child finished everything as it should */ |
| 87 | + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 88 | + tst_res(TFAIL, "child exited abnormally"); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +static void setup(void) |
| 93 | +{ |
| 94 | + struct futex_test_variants *tv = &variants[tst_variant]; |
| 95 | + |
| 96 | + tst_res(TINFO, "Testing variant: %s", tv->desc); |
| 97 | + futex_supported_by_kernel(tv->fntype); |
| 98 | + |
| 99 | + /* Futex needs to be in a shared memory so the parent and the child can access into it */ |
| 100 | + futex = SAFE_MMAP(NULL, sizeof(*futex), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0); |
| 101 | + *futex = FUTEX_INITIALIZER; |
| 102 | +} |
| 103 | + |
| 104 | +static void cleanup(void) |
| 105 | +{ |
| 106 | + if (futex) { |
| 107 | + SAFE_MUNMAP((void *)futex, sizeof(*futex)); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +static struct tst_test test = { |
| 112 | + .setup = setup, |
| 113 | + .cleanup = cleanup, |
| 114 | + .test_all = run, |
| 115 | + .test_variants = ARRAY_SIZE(variants), |
| 116 | + .forks_child = 1, |
| 117 | +}; |
0 commit comments