^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) * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <errno.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 <linux/limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <sys/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <kern_util.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <um_malloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct helper_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) void (*pre_exec)(void*);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) void *pre_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) char **argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static int helper_child(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct helper_data *data = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) char **argv = data->argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int err, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (data->pre_exec != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) (*data->pre_exec)(data->pre_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) err = execvp_noalloc(data->buf, argv[0], argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* If the exec succeeds, we don't get here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) CATCH_EINTR(ret = write(data->fd, &err, sizeof(err)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Returns either the pid of the child process we run or -E* on failure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct helper_data data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned long stack, sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int pid, fds[2], ret, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) stack = alloc_stack(0, __cant_sleep());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (stack == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) printk(UM_KERN_ERR "run_helper : pipe failed, errno = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ret = os_set_exec_close(fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) printk(UM_KERN_ERR "run_helper : setting FD_CLOEXEC failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) "ret = %d\n", -ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) data.pre_exec = pre_exec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) data.pre_data = pre_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) data.argv = argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) data.fd = fds[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) data.buf = __cant_sleep() ? uml_kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) uml_kmalloc(PATH_MAX, UM_GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) pid = clone(helper_child, (void *) sp, CLONE_VM, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (pid < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ret = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) printk(UM_KERN_ERR "run_helper : clone failed, errno = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto out_free2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) close(fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) fds[1] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * Read the errno value from the child, if the exec failed, or get 0 if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * the exec succeeded because the pipe fd was set as close-on-exec.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) n = read(fds[0], &ret, sizeof(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (n == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ret = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) n = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) printk(UM_KERN_ERR "run_helper : read on pipe failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) "ret = %d\n", -n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) CATCH_EINTR(waitpid(pid, NULL, __WALL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) out_free2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) kfree(data.buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) out_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (fds[1] != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) close(fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) close(fds[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) free_stack(stack, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned long *stack_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned long stack, sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int pid, status, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) stack = alloc_stack(0, __cant_sleep());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (stack == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) pid = clone(proc, (void *) sp, flags, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (pid < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) printk(UM_KERN_ERR "run_helper_thread : clone failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (stack_out == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) CATCH_EINTR(pid = waitpid(pid, &status, __WALL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (pid < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) printk(UM_KERN_ERR "run_helper_thread - wait failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) pid = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) printk(UM_KERN_ERR "run_helper_thread - thread "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) "returned status 0x%x\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) free_stack(stack, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *stack_out = stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int helper_wait(int pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int ret, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int wflags = __WALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) CATCH_EINTR(ret = waitpid(pid, &status, wflags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) printk(UM_KERN_ERR "helper_wait : waitpid process %d failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) "errno = %d\n", pid, errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) printk(UM_KERN_ERR "helper_wait : process %d exited with "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) "status 0x%x\n", pid, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }