Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #define _GNU_SOURCE
0004 #include <err.h>
0005 #include <errno.h>
0006 #include <fcntl.h>
0007 #include <inttypes.h>
0008 #include <limits.h>
0009 #include <sched.h>
0010 #include <signal.h>
0011 #include <stdio.h>
0012 #include <stdlib.h>
0013 #include <string.h>
0014 #include <sys/stat.h>
0015 #include <sys/syscall.h>
0016 #include <sys/types.h>
0017 #include <sys/wait.h>
0018 #include <unistd.h>
0019 
0020 #ifndef CLONE_PIDFD
0021 #define CLONE_PIDFD 0x00001000
0022 #endif
0023 
0024 #ifndef __NR_pidfd_send_signal
0025 #define __NR_pidfd_send_signal -1
0026 #endif
0027 
0028 static int do_child(void *args)
0029 {
0030     printf("%d\n", getpid());
0031     _exit(EXIT_SUCCESS);
0032 }
0033 
0034 static pid_t pidfd_clone(int flags, int *pidfd)
0035 {
0036     size_t stack_size = 1024;
0037     char *stack[1024] = { 0 };
0038 
0039 #ifdef __ia64__
0040     return __clone2(do_child, stack, stack_size, flags | SIGCHLD, NULL, pidfd);
0041 #else
0042     return clone(do_child, stack + stack_size, flags | SIGCHLD, NULL, pidfd);
0043 #endif
0044 }
0045 
0046 static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
0047                     unsigned int flags)
0048 {
0049     return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
0050 }
0051 
0052 static int pidfd_metadata_fd(pid_t pid, int pidfd)
0053 {
0054     int procfd, ret;
0055     char path[100];
0056 
0057     snprintf(path, sizeof(path), "/proc/%d", pid);
0058     procfd = open(path, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
0059     if (procfd < 0) {
0060         warn("Failed to open %s\n", path);
0061         return -1;
0062     }
0063 
0064     /*
0065      * Verify that the pid has not been recycled and our /proc/<pid> handle
0066      * is still valid.
0067      */
0068     ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
0069     if (ret < 0) {
0070         switch (errno) {
0071         case EPERM:
0072             /* Process exists, just not allowed to signal it. */
0073             break;
0074         default:
0075             warn("Failed to signal process\n");
0076             close(procfd);
0077             procfd = -1;
0078         }
0079     }
0080 
0081     return procfd;
0082 }
0083 
0084 int main(int argc, char *argv[])
0085 {
0086     int pidfd = -1, ret = EXIT_FAILURE;
0087     char buf[4096] = { 0 };
0088     pid_t pid;
0089     int procfd, statusfd;
0090     ssize_t bytes;
0091 
0092     pid = pidfd_clone(CLONE_PIDFD, &pidfd);
0093     if (pid < 0)
0094         err(ret, "CLONE_PIDFD");
0095     if (pidfd == -1) {
0096         warnx("CLONE_PIDFD is not supported by the kernel");
0097         goto out;
0098     }
0099 
0100     procfd = pidfd_metadata_fd(pid, pidfd);
0101     close(pidfd);
0102     if (procfd < 0)
0103         goto out;
0104 
0105     statusfd = openat(procfd, "status", O_RDONLY | O_CLOEXEC);
0106     close(procfd);
0107     if (statusfd < 0)
0108         goto out;
0109 
0110     bytes = read(statusfd, buf, sizeof(buf));
0111     if (bytes > 0)
0112         bytes = write(STDOUT_FILENO, buf, bytes);
0113     close(statusfd);
0114     ret = EXIT_SUCCESS;
0115 
0116 out:
0117     (void)wait(NULL);
0118 
0119     exit(ret);
0120 }