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