|
| 1 | +#include <sched.h> |
| 2 | +#include <sys/stat.h> |
| 3 | +#include <sys/statfs.h> |
| 4 | +#include <fcntl.h> |
| 5 | + |
| 6 | +#include "zdtmtst.h" |
| 7 | +#include "lock.h" |
| 8 | + |
| 9 | +const char *test_doc = "Check C/R of pidfds that point to threads\n"; |
| 10 | +const char *test_author = "Bhavik Sachdev <[email protected]>"; |
| 11 | + |
| 12 | +/* see also: https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tst-clone3.c.html */ |
| 13 | + |
| 14 | +#ifndef PIDFD_THREAD |
| 15 | +#define PIDFD_THREAD O_EXCL |
| 16 | +#endif |
| 17 | + |
| 18 | +#ifndef PIDFD_SIGNAL_THREAD |
| 19 | +#define PIDFD_SIGNAL_THREAD (1UL << 0) |
| 20 | +#endif |
| 21 | + |
| 22 | +#ifndef PID_FS_MAGIC |
| 23 | +#define PID_FS_MAGIC 0x50494446 |
| 24 | +#endif |
| 25 | + |
| 26 | +static long get_fs_type(int lfd) |
| 27 | +{ |
| 28 | + struct statfs fst; |
| 29 | + |
| 30 | + if (fstatfs(lfd, &fst)) { |
| 31 | + return -1; |
| 32 | + } |
| 33 | + return fst.f_type; |
| 34 | +} |
| 35 | + |
| 36 | +static int pidfd_open(pid_t pid, unsigned int flags) |
| 37 | +{ |
| 38 | + return syscall(__NR_pidfd_open, pid, flags); |
| 39 | +} |
| 40 | + |
| 41 | +static int pidfd_send_signal(int pidfd, int sig, siginfo_t* info, unsigned int flags) |
| 42 | +{ |
| 43 | + return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags); |
| 44 | +} |
| 45 | + |
| 46 | +static int thread_func(void *a) |
| 47 | +{ |
| 48 | + test_waitsig(); |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +#define CTID_INIT_VAL 1 |
| 53 | + |
| 54 | +int main(int argc, char* argv[]) |
| 55 | +{ |
| 56 | + char st[64 * 1024] __attribute__ ((aligned)); |
| 57 | + pid_t tid; |
| 58 | + int pidfd, test_pidfd; |
| 59 | + futex_t exited; |
| 60 | + |
| 61 | + int clone_flags = CLONE_THREAD; |
| 62 | + clone_flags |= CLONE_VM | CLONE_SIGHAND; |
| 63 | + clone_flags |= CLONE_CHILD_CLEARTID; |
| 64 | + |
| 65 | + test_init(argc, argv); |
| 66 | + |
| 67 | + test_pidfd = pidfd_open(getpid(), 0); |
| 68 | + if (test_pidfd < 0) { |
| 69 | + pr_perror("pidfd_open() failed"); |
| 70 | + return 1; |
| 71 | + } |
| 72 | + |
| 73 | + /* PIDFD_THREAD, PIDFD_SIGNAL_THREAD are supported only with pidfs */ |
| 74 | + if (get_fs_type(test_pidfd) != PID_FS_MAGIC) { |
| 75 | + test_daemon(); |
| 76 | + test_waitsig(); |
| 77 | + skip("pidfs not supported."); |
| 78 | + close(test_pidfd); |
| 79 | + return 0; |
| 80 | + } |
| 81 | + close(test_pidfd); |
| 82 | + |
| 83 | + futex_set(&exited, CTID_INIT_VAL); |
| 84 | + |
| 85 | + tid = clone(thread_func, st + sizeof(st), clone_flags, NULL, NULL, NULL, &(exited.raw)); |
| 86 | + if (tid == -1) { |
| 87 | + pr_perror("clone() failed"); |
| 88 | + return 1; |
| 89 | + } |
| 90 | + |
| 91 | + test_msg("Successfully created a thread with tid: %d\n", tid); |
| 92 | + pidfd = pidfd_open(tid, PIDFD_THREAD); |
| 93 | + if (pidfd < 0) { |
| 94 | + pr_perror("pidfd_open() failed"); |
| 95 | + return 1; |
| 96 | + } |
| 97 | + |
| 98 | + test_daemon(); |
| 99 | + test_waitsig(); |
| 100 | + |
| 101 | + if (pidfd_send_signal(pidfd, SIGTERM, NULL, PIDFD_SIGNAL_THREAD)) { |
| 102 | + pr_perror("pidfd_send_signal() failed"); |
| 103 | + fail(); |
| 104 | + close(pidfd); |
| 105 | + return 1; |
| 106 | + } |
| 107 | + |
| 108 | + test_msg("Waiting for thread to exit\n"); |
| 109 | + futex_wait_until(&exited, 0); |
| 110 | + |
| 111 | + pass(); |
| 112 | + close(pidfd); |
| 113 | + return 0; |
| 114 | +} |
0 commit comments