2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
12 #include <sys/signal.h>
15 #include "kern_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;
38 signal(SIGHUP, helper_hup);
41 if(data->pre_exec != NULL)
42 (*data->pre_exec)(data->pre_data);
43 execvp(argv[0], argv);
44 printk("execvp of '%s' failed - errno = %d\n", argv[0], errno);
45 write(data->fd, &errno, sizeof(errno));
46 os_kill_process(os_getpid(), 0);
50 /* XXX The alloc_stack here breaks if this is called in the tracing thread */
52 int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
53 unsigned long *stack_out)
55 struct helper_data data;
56 unsigned long stack, sp;
57 int pid, fds[2], err, n;
59 if((stack_out != NULL) && (*stack_out != 0))
61 else stack = alloc_stack(0, um_in_interrupt());
62 if(stack == 0) return(-ENOMEM);
64 err = os_pipe(fds, 1, 0);
66 printk("run_helper : pipe failed, errno = %d\n", -err);
69 if(fcntl(fds[1], F_SETFD, 1) != 0){
70 printk("run_helper : setting FD_CLOEXEC failed, errno = %d\n",
75 sp = stack + page_size() - sizeof(void *);
76 data.pre_exec = pre_exec;
77 data.pre_data = pre_data;
80 pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
82 printk("run_helper : clone failed, errno = %d\n", errno);
86 n = read(fds[0], &err, sizeof(err));
88 printk("run_helper : read on pipe failed, errno = %d\n",
93 waitpid(pid, NULL, 0);
97 if(stack_out == NULL) free_stack(stack, 0);
98 else *stack_out = stack;
102 int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
103 unsigned long *stack_out, int stack_order)
105 unsigned long stack, sp;
108 stack = alloc_stack(stack_order, um_in_interrupt());
109 if(stack == 0) return(-ENOMEM);
111 sp = stack + (page_size() << stack_order) - sizeof(void *);
112 pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
114 printk("run_helper_thread : clone failed, errno = %d\n",
118 if(stack_out == NULL){
119 pid = waitpid(pid, &status, 0);
121 printk("run_helper_thread - wait failed, errno = %d\n",
123 if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
124 printk("run_helper_thread - thread returned status "
126 free_stack(stack, stack_order);
128 else *stack_out = stack;
132 int helper_wait(int pid, int block)
136 ret = waitpid(pid, NULL, WNOHANG);
138 printk("helper_wait : waitpid failed, errno = %d\n", errno);
145 * Overrides for Emacs so that we follow Linus's tabbing style.
146 * Emacs will notice this stuff at the end of the file and automatically
147 * adjust the settings for this buffer only. This must remain at the end
149 * ---------------------------------------------------------------------------
151 * c-file-style: "linux"