diff options
author | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2022-01-28 12:53:52 +0000 |
---|---|---|
committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2022-06-02 15:43:28 -0300 |
commit | 1002f1af1ca954f6bb8fbda67fccdb7a4b0055cd (patch) | |
tree | 1b484790fdbedfad90c14cc7abea2277914f4b62 /sysdeps/unix/sysv/linux/tst-process_mrelease.c | |
parent | d19ee3473d68ca0e794f3a8b7677a0983ae1342e (diff) | |
download | glibc-1002f1af1ca954f6bb8fbda67fccdb7a4b0055cd.tar glibc-1002f1af1ca954f6bb8fbda67fccdb7a4b0055cd.tar.gz glibc-1002f1af1ca954f6bb8fbda67fccdb7a4b0055cd.tar.bz2 glibc-1002f1af1ca954f6bb8fbda67fccdb7a4b0055cd.zip |
linux: Add process_mrelease
Added in Linux 5.15 (884a7e5964e06ed93c7771c0d7cf19c09a8946f1), the new
syscalls allows a caller to free the memory of a dying target process.
Checked on x86_64-linux-gnu.
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'sysdeps/unix/sysv/linux/tst-process_mrelease.c')
-rw-r--r-- | sysdeps/unix/sysv/linux/tst-process_mrelease.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/sysdeps/unix/sysv/linux/tst-process_mrelease.c b/sysdeps/unix/sysv/linux/tst-process_mrelease.c new file mode 100644 index 0000000000..958db3cbb8 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-process_mrelease.c @@ -0,0 +1,86 @@ +/* Basic tests for Linux process_mrelease. + Copyright (C) 2022 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/>. */ + +#include <errno.h> +#include <stdlib.h> +#include <support/check.h> +#include <support/support.h> +#include <support/xsocket.h> +#include <support/xunistd.h> +#include <sys/mman.h> +#include <sys/pidfd.h> +#include <sys/wait.h> + +static void +exit_subprocess (int dummy) +{ + exit (EXIT_FAILURE); +} + +static void +subprocess (void) +{ + /* In case something goes wrong with parent before pidfd_send_signal. */ + support_create_timer (5, 0, false, exit_subprocess); + + pause (); + _exit (0); +} + +static int +do_test (void) +{ + { + int r = process_mrelease (-1, 0); + TEST_COMPARE (r, -1); + if (errno == ENOSYS) + FAIL_UNSUPPORTED ("kernel does not support process_mrelease, " + "skipping test"); + TEST_COMPARE (errno, EBADF); + } + + pid_t pid = xfork (); + if (pid == 0) + subprocess (); + + int pidfd = pidfd_open (pid, 0); + TEST_VERIFY (pidfd != -1); + + /* The syscall only succeedes if the target process is exiting and there + is no guarantee that calling if after pidfd_send_signal will not error + (ince the process might have already been reaped by the OS). So just + check if it does fail when the process is stll running. */ + TEST_COMPARE (process_mrelease (pidfd, 0), -1); + TEST_COMPARE (errno, EINVAL); + + TEST_COMPARE (pidfd_send_signal (pidfd, SIGKILL, NULL, 0), 0); + { + siginfo_t info; + int r = waitid (P_PIDFD, pidfd, &info, WEXITED); + TEST_COMPARE (r, 0); + TEST_COMPARE (info.si_status, SIGKILL); + TEST_COMPARE (info.si_code, CLD_KILLED); + } + + TEST_COMPARE (pidfd_send_signal (pidfd, SIGKILL, NULL, 0), -1); + TEST_COMPARE (errno, ESRCH); + + return 0; +} + +#include <support/test-driver.c> |