2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
12 #include <sys/signal.h>
15 #include "kern_util.h"
17 #include "um_malloc.h"
18 #include "kern_constants.h"
21 void (*pre_exec)(void*);
28 static int helper_child(void *arg)
30 struct helper_data *data = arg;
31 char **argv = data->argv;
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],
39 write(data->fd, &errval, sizeof(errval));
40 kill(os_getpid(), SIGKILL);
44 /* Returns either the pid of the child process we run or -E* on failure.
45 * XXX The alloc_stack here breaks if this is called in the tracing thread, so
46 * we need to receive a preallocated stack (a local buffer is ok). */
47 int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
48 unsigned long *stack_out)
50 struct helper_data data;
51 unsigned long stack, sp;
52 int pid, fds[2], ret, n;
54 if ((stack_out != NULL) && (*stack_out != 0))
57 stack = alloc_stack(0, __cant_sleep());
61 ret = os_pipe(fds, 1, 0);
63 printk("run_helper : pipe failed, ret = %d\n", -ret);
67 ret = os_set_exec_close(fds[1], 1);
69 printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
74 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
75 data.pre_exec = pre_exec;
76 data.pre_data = pre_data;
79 data.buf = __cant_sleep() ? um_kmalloc_atomic(PATH_MAX) :
81 pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
84 printk("run_helper : clone failed, errno = %d\n", errno);
92 * Read the errno value from the child, if the exec failed, or get 0 if
93 * the exec succeeded because the pipe fd was set as close-on-exec.
95 n = read(fds[0], &ret, sizeof(ret));
101 printk("run_helper : read on pipe failed, ret = %d\n",
106 CATCH_EINTR(waitpid(pid, NULL, 0));
116 if ((stack_out == NULL) || (*stack_out == 0))
117 free_stack(stack, 0);
121 int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
122 unsigned long *stack_out, int stack_order)
124 unsigned long stack, sp;
125 int pid, status, err;
127 stack = alloc_stack(stack_order, __cant_sleep());
131 sp = stack + (UM_KERN_PAGE_SIZE << stack_order) - sizeof(void *);
132 pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
135 printk("run_helper_thread : clone failed, errno = %d\n",
139 if (stack_out == NULL) {
140 CATCH_EINTR(pid = waitpid(pid, &status, 0));
143 printk("run_helper_thread - wait failed, errno = %d\n",
147 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
148 printk("run_helper_thread - thread returned status "
150 free_stack(stack, stack_order);
156 int helper_wait(int pid)
160 CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
163 printk("helper_wait : waitpid failed, errno = %d\n", errno);