Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #ifndef CLONE_PIDFD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define CLONE_PIDFD 0x00001000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #ifndef __NR_pidfd_send_signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define __NR_pidfd_send_signal -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int do_child(void *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	printf("%d\n", getpid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	_exit(EXIT_SUCCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static pid_t pidfd_clone(int flags, int *pidfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	size_t stack_size = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	char *stack[1024] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #ifdef __ia64__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return __clone2(do_child, stack, stack_size, flags | SIGCHLD, NULL, pidfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return clone(do_child, stack + stack_size, flags | SIGCHLD, NULL, pidfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 					unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static int pidfd_metadata_fd(pid_t pid, int pidfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int procfd, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	char path[100];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	snprintf(path, sizeof(path), "/proc/%d", pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	procfd = open(path, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (procfd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		warn("Failed to open %s\n", path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 * Verify that the pid has not been recycled and our /proc/<pid> handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 * is still valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	ret = sys_pidfd_send_signal(pidfd, 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		switch (errno) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		case EPERM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			/* Process exists, just not allowed to signal it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			warn("Failed to signal process\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			close(procfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			procfd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return procfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	int pidfd = -1, ret = EXIT_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	char buf[4096] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	int procfd, statusfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	ssize_t bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	pid = pidfd_clone(CLONE_PIDFD, &pidfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (pid < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		err(ret, "CLONE_PIDFD");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (pidfd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		warnx("CLONE_PIDFD is not supported by the kernel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	procfd = pidfd_metadata_fd(pid, pidfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	close(pidfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (procfd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	statusfd = openat(procfd, "status", O_RDONLY | O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	close(procfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (statusfd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	bytes = read(statusfd, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (bytes > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		bytes = write(STDOUT_FILENO, buf, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	close(statusfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ret = EXIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	(void)wait(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	exit(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }