2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
11 #include <sys/signal.h>
14 #include "kern_util.h"
15 #include "user_util.h"
20 void (*pre_exec)(void*);
26 /* Debugging aid, changed only from gdb */
29 static void helper_hup(int sig)
33 static int helper_child(void *arg)
35 struct helper_data *data = arg;
36 char **argv = data->argv;
40 signal(SIGHUP, helper_hup);
43 if(data->pre_exec != NULL)
44 (*data->pre_exec)(data->pre_data);
45 execvp(argv[0], argv);
47 printk("execvp of '%s' failed - errno = %d\n", argv[0], errno);
48 os_write_file(data->fd, &errval, sizeof(errval));
49 os_kill_process(os_getpid(), 0);
53 /* Returns either the pid of the child process we run or -E* on failure.
54 * XXX The alloc_stack here breaks if this is called in the tracing thread */
55 int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
56 unsigned long *stack_out)
58 struct helper_data data;
59 unsigned long stack, sp;
60 int pid, fds[2], ret, n;
62 if((stack_out != NULL) && (*stack_out != 0))
64 else stack = alloc_stack(0, um_in_interrupt());
68 ret = os_pipe(fds, 1, 0);
70 printk("run_helper : pipe failed, ret = %d\n", -ret);
74 ret = os_set_exec_close(fds[1], 1);
76 printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
81 sp = stack + page_size() - sizeof(void *);
82 data.pre_exec = pre_exec;
83 data.pre_data = pre_data;
86 pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
89 printk("run_helper : clone failed, errno = %d\n", errno);
93 os_close_file(fds[1]);
96 /*Read the errno value from the child.*/
97 n = os_read_file(fds[0], &ret, sizeof(ret));
99 printk("run_helper : read on pipe failed, ret = %d\n", -n);
101 os_kill_process(pid, 1);
104 CATCH_EINTR(n = waitpid(pid, NULL, 0));
112 os_close_file(fds[1]);
113 os_close_file(fds[0]);
115 if(stack_out == NULL)
116 free_stack(stack, 0);
117 else *stack_out = stack;
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, um_in_interrupt());
128 if(stack == 0) return(-ENOMEM);
130 sp = stack + (page_size() << stack_order) - sizeof(void *);
131 pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
134 printk("run_helper_thread : clone failed, errno = %d\n",
138 if(stack_out == NULL){
139 CATCH_EINTR(pid = waitpid(pid, &status, 0));
142 printk("run_helper_thread - wait failed, errno = %d\n",
146 if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
147 printk("run_helper_thread - thread returned status "
149 free_stack(stack, stack_order);
151 else *stack_out = stack;
155 int helper_wait(int pid)
159 CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
162 printk("helper_wait : waitpid failed, errno = %d\n", errno);