Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef __PIDFD_H
0004 #define __PIDFD_H
0005 
0006 #define _GNU_SOURCE
0007 #include <errno.h>
0008 #include <fcntl.h>
0009 #include <sched.h>
0010 #include <signal.h>
0011 #include <stdio.h>
0012 #include <stdlib.h>
0013 #include <string.h>
0014 #include <syscall.h>
0015 #include <sys/mount.h>
0016 #include <sys/types.h>
0017 #include <sys/wait.h>
0018 
0019 #include "../kselftest.h"
0020 
0021 #ifndef P_PIDFD
0022 #define P_PIDFD 3
0023 #endif
0024 
0025 #ifndef CLONE_NEWTIME
0026 #define CLONE_NEWTIME 0x00000080
0027 #endif
0028 
0029 #ifndef CLONE_PIDFD
0030 #define CLONE_PIDFD 0x00001000
0031 #endif
0032 
0033 #ifndef __NR_pidfd_open
0034 #define __NR_pidfd_open -1
0035 #endif
0036 
0037 #ifndef __NR_pidfd_send_signal
0038 #define __NR_pidfd_send_signal -1
0039 #endif
0040 
0041 #ifndef __NR_clone3
0042 #define __NR_clone3 -1
0043 #endif
0044 
0045 #ifndef __NR_pidfd_getfd
0046 #define __NR_pidfd_getfd -1
0047 #endif
0048 
0049 #ifndef PIDFD_NONBLOCK
0050 #define PIDFD_NONBLOCK O_NONBLOCK
0051 #endif
0052 
0053 /*
0054  * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
0055  * That means, when it wraps around any pid < 300 will be skipped.
0056  * So we need to use a pid > 300 in order to test recycling.
0057  */
0058 #define PID_RECYCLE 1000
0059 
0060 /*
0061  * Define a few custom error codes for the child process to clearly indicate
0062  * what is happening. This way we can tell the difference between a system
0063  * error, a test error, etc.
0064  */
0065 #define PIDFD_PASS 0
0066 #define PIDFD_FAIL 1
0067 #define PIDFD_ERROR 2
0068 #define PIDFD_SKIP 3
0069 #define PIDFD_XFAIL 4
0070 
0071 static inline int wait_for_pid(pid_t pid)
0072 {
0073     int status, ret;
0074 
0075 again:
0076     ret = waitpid(pid, &status, 0);
0077     if (ret == -1) {
0078         if (errno == EINTR)
0079             goto again;
0080 
0081         ksft_print_msg("waitpid returned -1, errno=%d\n", errno);
0082         return -1;
0083     }
0084 
0085     if (!WIFEXITED(status)) {
0086         ksft_print_msg(
0087                "waitpid !WIFEXITED, WIFSIGNALED=%d, WTERMSIG=%d\n",
0088                WIFSIGNALED(status), WTERMSIG(status));
0089         return -1;
0090     }
0091 
0092     ret = WEXITSTATUS(status);
0093     ksft_print_msg("waitpid WEXITSTATUS=%d\n", ret);
0094     return ret;
0095 }
0096 
0097 static inline int sys_pidfd_open(pid_t pid, unsigned int flags)
0098 {
0099     return syscall(__NR_pidfd_open, pid, flags);
0100 }
0101 
0102 static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
0103                     unsigned int flags)
0104 {
0105     return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
0106 }
0107 
0108 static inline int sys_pidfd_getfd(int pidfd, int fd, int flags)
0109 {
0110     return syscall(__NR_pidfd_getfd, pidfd, fd, flags);
0111 }
0112 
0113 static inline int sys_memfd_create(const char *name, unsigned int flags)
0114 {
0115     return syscall(__NR_memfd_create, name, flags);
0116 }
0117 
0118 #endif /* __PIDFD_H */