ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <fcntl.h>
11 #include <sched.h>
12 #include <sys/signal.h>
13 #include <sys/wait.h>
14 #include "user.h"
15 #include "kern_util.h"
16 #include "os.h"
17
18 struct 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 */
26 int helper_pause = 0;
27
28 static void helper_hup(int sig)
29 {
30 }
31
32 static int helper_child(void *arg)
33 {
34         struct helper_data *data = arg;
35         char **argv = data->argv;
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         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);
47         return(0);
48 }
49
50 /* XXX The alloc_stack here breaks if this is called in the tracing thread */
51
52 int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
53                unsigned long *stack_out)
54 {
55         struct helper_data data;
56         unsigned long stack, sp;
57         int pid, fds[2], err, n;
58
59         if((stack_out != NULL) && (*stack_out != 0))
60                 stack = *stack_out;
61         else stack = alloc_stack(0, um_in_interrupt());
62         if(stack == 0) return(-ENOMEM);
63
64         err = os_pipe(fds, 1, 0);
65         if(err){
66                 printk("run_helper : pipe failed, errno = %d\n", -err);
67                 return(err);
68         }
69         if(fcntl(fds[1], F_SETFD, 1) != 0){
70                 printk("run_helper : setting FD_CLOEXEC failed, errno = %d\n",
71                        errno);
72                 return(-errno);
73         }
74
75         sp = stack + page_size() - sizeof(void *);
76         data.pre_exec = pre_exec;
77         data.pre_data = pre_data;
78         data.argv = argv;
79         data.fd = fds[1];
80         pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
81         if(pid < 0){
82                 printk("run_helper : clone failed, errno = %d\n", errno);
83                 return(-errno);
84         }
85         close(fds[1]);
86         n = read(fds[0], &err, sizeof(err));
87         if(n < 0){
88                 printk("run_helper : read on pipe failed, errno = %d\n", 
89                        errno);
90                 return(-errno);
91         }
92         else if(n != 0){
93                 waitpid(pid, NULL, 0);
94                 pid = -err;
95         }
96
97         if(stack_out == NULL) free_stack(stack, 0);
98         else *stack_out = stack;
99         return(pid);
100 }
101
102 int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags, 
103                       unsigned long *stack_out, int stack_order)
104 {
105         unsigned long stack, sp;
106         int pid, status;
107
108         stack = alloc_stack(stack_order, um_in_interrupt());
109         if(stack == 0) return(-ENOMEM);
110
111         sp = stack + (page_size() << stack_order) - sizeof(void *);
112         pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
113         if(pid < 0){
114                 printk("run_helper_thread : clone failed, errno = %d\n", 
115                        errno);
116                 return(-errno);
117         }
118         if(stack_out == NULL){
119                 pid = waitpid(pid, &status, 0);
120                 if(pid < 0)
121                         printk("run_helper_thread - wait failed, errno = %d\n",
122                                pid);
123                 if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
124                         printk("run_helper_thread - thread returned status "
125                                "0x%x\n", status);
126                 free_stack(stack, stack_order);
127         }
128         else *stack_out = stack;
129         return(pid);
130 }
131
132 int helper_wait(int pid, int block)
133 {
134         int ret;
135
136         ret = waitpid(pid, NULL, WNOHANG);
137         if(ret < 0){
138                 printk("helper_wait : waitpid failed, errno = %d\n", errno);
139                 return(-errno);
140         }
141         return(ret);
142 }
143
144 /*
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
148  * of the file.
149  * ---------------------------------------------------------------------------
150  * Local variables:
151  * c-file-style: "linux"
152  * End:
153  */