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"
19 void (*pre_exec)(void*);
25 /* Debugging aid, changed only from gdb */
28 static void helper_hup(int sig)
32 static int helper_child(void *arg)
34 struct helper_data *data = arg;
35 char **argv = data->argv;
39 signal(SIGHUP, helper_hup);
42 if(data->pre_exec != NULL)
43 (*data->pre_exec)(data->pre_data);
44 execvp(argv[0], argv);
46 printk("execvp of '%s' failed - errno = %d\n", argv[0], errno);
47 os_write_file(data->fd, &errval, sizeof(errval));
48 os_kill_process(os_getpid(), 0);
52 /* XXX The alloc_stack here breaks if this is called in the tracing thread */
54 int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
55 unsigned long *stack_out)
57 struct helper_data data;
58 unsigned long stack, sp;
59 int pid, fds[2], err, n;
61 if((stack_out != NULL) && (*stack_out != 0))
63 else stack = alloc_stack(0, um_in_interrupt());
67 err = os_pipe(fds, 1, 0);
69 printk("run_helper : pipe failed, err = %d\n", -err);
73 err = os_set_exec_close(fds[1], 1);
75 printk("run_helper : setting FD_CLOEXEC failed, err = %d\n",
80 sp = stack + page_size() - sizeof(void *);
81 data.pre_exec = pre_exec;
82 data.pre_data = pre_data;
85 pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
87 printk("run_helper : clone failed, errno = %d\n", errno);
92 os_close_file(fds[1]);
93 n = os_read_file(fds[0], &err, sizeof(err));
95 printk("run_helper : read on pipe failed, err = %d\n", -n);
100 CATCH_EINTR(n = waitpid(pid, NULL, 0));
104 if(stack_out == NULL) free_stack(stack, 0);
105 else *stack_out = stack;
109 os_kill_process(pid, 1);
111 os_close_file(fds[0]);
112 os_close_file(fds[1]);
114 free_stack(stack, 0);
118 int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
119 unsigned long *stack_out, int stack_order)
121 unsigned long stack, sp;
124 stack = alloc_stack(stack_order, um_in_interrupt());
125 if(stack == 0) return(-ENOMEM);
127 sp = stack + (page_size() << stack_order) - sizeof(void *);
128 pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
130 printk("run_helper_thread : clone failed, errno = %d\n",
134 if(stack_out == NULL){
135 CATCH_EINTR(pid = waitpid(pid, &status, 0));
137 printk("run_helper_thread - wait failed, errno = %d\n",
141 if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
142 printk("run_helper_thread - thread returned status "
144 free_stack(stack, stack_order);
146 else *stack_out = stack;
150 int helper_wait(int pid, int block)
154 CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
156 printk("helper_wait : waitpid failed, errno = %d\n", errno);
163 * Overrides for Emacs so that we follow Linus's tabbing style.
164 * Emacs will notice this stuff at the end of the file and automatically
165 * adjust the settings for this buffer only. This must remain at the end
167 * ---------------------------------------------------------------------------
169 * c-file-style: "linux"