blob: 13b1f5c2f7ee3018c407b06ea851da1108af04d1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 <sys/signal.h>
12#include <sys/wait.h>
13#include "user.h"
14#include "kern_util.h"
15#include "user_util.h"
16#include "os.h"
17
18struct helper_data {
19 void (*pre_exec)(void*);
20 void *pre_data;
21 char **argv;
22 int fd;
23};
24
25/* Debugging aid, changed only from gdb */
26int helper_pause = 0;
27
28static void helper_hup(int sig)
29{
30}
31
32static int helper_child(void *arg)
33{
34 struct helper_data *data = arg;
35 char **argv = data->argv;
36 int errval;
37
38 if(helper_pause){
39 signal(SIGHUP, helper_hup);
40 pause();
41 }
42 if(data->pre_exec != NULL)
43 (*data->pre_exec)(data->pre_data);
44 execvp(argv[0], argv);
45 errval = errno;
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);
49 return(0);
50}
51
52/* Returns either the pid of the child process we run or -E* on failure.
53 * XXX The alloc_stack here breaks if this is called in the tracing thread */
54int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
55 unsigned long *stack_out)
56{
57 struct helper_data data;
58 unsigned long stack, sp;
59 int pid, fds[2], ret, n;
60
61 if((stack_out != NULL) && (*stack_out != 0))
62 stack = *stack_out;
63 else stack = alloc_stack(0, um_in_interrupt());
64 if(stack == 0)
65 return(-ENOMEM);
66
67 ret = os_pipe(fds, 1, 0);
68 if(ret < 0){
69 printk("run_helper : pipe failed, ret = %d\n", -ret);
70 goto out_free;
71 }
72
73 ret = os_set_exec_close(fds[1], 1);
74 if(ret < 0){
75 printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
76 -ret);
77 goto out_close;
78 }
79
80 sp = stack + page_size() - sizeof(void *);
81 data.pre_exec = pre_exec;
82 data.pre_data = pre_data;
83 data.argv = argv;
84 data.fd = fds[1];
85 pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
86 if(pid < 0){
87 printk("run_helper : clone failed, errno = %d\n", errno);
88 ret = -errno;
89 goto out_close;
90 }
91
92 os_close_file(fds[1]);
93 fds[1] = -1;
94
95 /*Read the errno value from the child.*/
96 n = os_read_file(fds[0], &ret, sizeof(ret));
97 if(n < 0){
98 printk("run_helper : read on pipe failed, ret = %d\n", -n);
99 ret = n;
100 os_kill_process(pid, 1);
101 }
102 else if(n != 0){
103 CATCH_EINTR(n = waitpid(pid, NULL, 0));
104 ret = -errno;
105 } else {
106 ret = pid;
107 }
108
109out_close:
110 if (fds[1] != -1)
111 os_close_file(fds[1]);
112 os_close_file(fds[0]);
113out_free:
114 if(stack_out == NULL)
115 free_stack(stack, 0);
116 else *stack_out = stack;
117 return(ret);
118}
119
120int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
121 unsigned long *stack_out, int stack_order)
122{
123 unsigned long stack, sp;
124 int pid, status;
125
126 stack = alloc_stack(stack_order, um_in_interrupt());
127 if(stack == 0) return(-ENOMEM);
128
129 sp = stack + (page_size() << stack_order) - sizeof(void *);
130 pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
131 if(pid < 0){
132 printk("run_helper_thread : clone failed, errno = %d\n",
133 errno);
134 return(-errno);
135 }
136 if(stack_out == NULL){
137 CATCH_EINTR(pid = waitpid(pid, &status, 0));
138 if(pid < 0){
139 printk("run_helper_thread - wait failed, errno = %d\n",
140 errno);
141 pid = -errno;
142 }
143 if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
144 printk("run_helper_thread - thread returned status "
145 "0x%x\n", status);
146 free_stack(stack, stack_order);
147 }
148 else *stack_out = stack;
149 return(pid);
150}
151
152int helper_wait(int pid, int block)
153{
154 int ret;
155
156 CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
157 if(ret < 0){
158 printk("helper_wait : waitpid failed, errno = %d\n", errno);
159 return(-errno);
160 }
161 return(ret);
162}
163
164/*
165 * Overrides for Emacs so that we follow Linus's tabbing style.
166 * Emacs will notice this stuff at the end of the file and automatically
167 * adjust the settings for this buffer only. This must remain at the end
168 * of the file.
169 * ---------------------------------------------------------------------------
170 * Local variables:
171 * c-file-style: "linux"
172 * End:
173 */