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