853b28f2a22efcfa68d8b614a7cdf0a208cd0996
[strongswan.git] / src / dumm / patches / mconsole-exec-2.6.26.patch
1 --- a/arch/um/drivers/mconsole_kern.c   2008-07-13 23:51:29.000000000 +0200
2 +++ b/arch/um/drivers/mconsole_kern.c   2008-07-31 14:04:57.000000000 +0200
3 @@ -4,6 +4,7 @@
4   * Licensed under the GPL
5   */
6  
7 +#include "linux/kmod.h"
8  #include <linux/console.h>
9  #include <linux/ctype.h>
10  #include <linux/interrupt.h>
11 @@ -18,6 +19,8 @@
12  #include <linux/utsname.h>
13  #include <linux/workqueue.h>
14  #include <linux/mutex.h>
15 +#include <linux/completion.h>
16 +#include <linux/file.h>
17  #include <asm/uaccess.h>
18  
19  #include "init.h"
20 @@ -199,6 +202,64 @@
21  }
22  #endif
23  
24 +void mconsole_exec(struct mc_request *req)
25 +{
26 +       DECLARE_COMPLETION_ONSTACK(done);
27 +       struct subprocess_info *sub_info;
28 +       int res, len;
29 +       struct file *out;
30 +       char buf[MCONSOLE_MAX_DATA];
31 +
32 +       char *envp[] = {
33 +               "HOME=/", "TERM=linux",
34 +               "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
35 +               NULL
36 +       };
37 +       char *argv[] = {
38 +               "/bin/sh", "-c",
39 +               req->request.data + strlen("exec "),
40 +               NULL
41 +       };
42 +       
43 +       sub_info = call_usermodehelper_setup("/bin/sh", argv, envp);
44 +       if (sub_info == NULL) {
45 +               mconsole_reply(req, "call_usermodehelper_setup failed", 1, 0);
46 +               return;
47 +       }
48 +       res = call_usermodehelper_stdoutpipe(sub_info, &out);
49 +       if (res < 0) {
50 +               call_usermodehelper_freeinfo(sub_info);
51 +               mconsole_reply(req, "call_usermodehelper_stdoutpipe failed", 1, 0);
52 +               return;
53 +       }
54 +
55 +       call_usermodehelper_setcomplete(sub_info, &done);
56 +       
57 +       res = call_usermodehelper_exec(sub_info, UMH_WAIT_EXT); 
58 +       if (res < 0) {
59 +               mconsole_reply(req, "call_usermodehelper_exec failed", 1, 0);
60 +               return;
61 +       }
62 +       
63 +       for (;;) {
64 +               len = out->f_op->read(out, buf, sizeof(buf), 0);
65 +               if (len < 0) {
66 +                       mconsole_reply(req, "reading output failed", 1, 0);
67 +                       break;
68 +               }
69 +               if (len == 0)
70 +                       break;
71 +               mconsole_reply_len(req, buf, len, 0, 1);
72 +       }
73 +       fput(out);
74 +       
75 +       wait_for_completion(&done);
76 +       res = call_usermodehelper_getretval(sub_info) >> 8;
77 +       call_usermodehelper_freeinfo(sub_info);
78 +       
79 +       mconsole_reply_len(req, buf, len, res, 0);
80 +}
81 +
82  void mconsole_proc(struct mc_request *req)
83  {
84         char path[64];
85 @@ -270,6 +331,7 @@
86      stop - pause the UML; it will do nothing until it receives a 'go' \n\
87      go - continue the UML after a 'stop' \n\
88      log <string> - make UML enter <string> into the kernel log\n\
89 +    exec <string> - pass <string> to /bin/sh -c synchronously\n\
90      proc <file> - returns the contents of the UML's /proc/<file>\n\
91      stack <pid> - returns the stack of the specified pid\n\
92  "
93 --- a/arch/um/drivers/mconsole_user.c   2008-07-13 23:51:29.000000000 +0200
94 +++ b/arch/um/drivers/mconsole_user.c   2008-07-15 15:41:54.000000000 +0200
95 @@ -32,6 +32,7 @@
96         { "stop", mconsole_stop, MCONSOLE_PROC },
97         { "go", mconsole_go, MCONSOLE_INTR },
98         { "log", mconsole_log, MCONSOLE_INTR },
99 +       { "exec", mconsole_exec, MCONSOLE_PROC },
100         { "proc", mconsole_proc, MCONSOLE_PROC },
101         { "stack", mconsole_stack, MCONSOLE_INTR },
102  };
103 --- a/arch/um/include/mconsole.h        2008-07-13 23:51:29.000000000 +0200
104 +++ b/arch/um/include/mconsole.h        2008-07-15 15:41:54.000000000 +0200
105 @@ -85,6 +85,7 @@
106  extern void mconsole_stop(struct mc_request *req);
107  extern void mconsole_go(struct mc_request *req);
108  extern void mconsole_log(struct mc_request *req);
109 +extern void mconsole_exec(struct mc_request *req);
110  extern void mconsole_proc(struct mc_request *req);
111  extern void mconsole_stack(struct mc_request *req);
112  
113 --- a/kernel/kmod.c     2008-07-13 23:51:29.000000000 +0200
114 +++ b/kernel/kmod.c     2008-07-31 14:46:31.000000000 +0200
115 @@ -118,6 +118,7 @@
116  struct subprocess_info {
117         struct work_struct work;
118         struct completion *complete;
119 +       struct completion *executed;
120         char *path;
121         char **argv;
122         char **envp;
123 @@ -125,6 +126,7 @@
124         enum umh_wait wait;
125         int retval;
126         struct file *stdin;
127 +       struct file *stdout;
128         void (*cleanup)(char **argv, char **envp);
129  };
130  
131 @@ -160,8 +162,26 @@
132                 FD_SET(0, fdt->open_fds);
133                 FD_CLR(0, fdt->close_on_exec);
134                 spin_unlock(&f->file_lock);
135 -
136 -               /* and disallow core files too */
137 +       }
138 +       if (sub_info->stdout) {
139 +               struct files_struct *f = current->files;
140 +               struct fdtable *fdt;
141 +               
142 +               sys_close(1);
143 +               sys_close(2);
144 +               get_file(sub_info->stdout);
145 +               fd_install(1, sub_info->stdout);
146 +               fd_install(2, sub_info->stdout);
147 +               spin_lock(&f->file_lock);
148 +               fdt = files_fdtable(f);
149 +               FD_SET(1, fdt->open_fds);
150 +               FD_CLR(1, fdt->close_on_exec);
151 +               FD_SET(2, fdt->open_fds);
152 +               FD_CLR(2, fdt->close_on_exec);
153 +               spin_unlock(&f->file_lock);
154 +       }
155 +       if (sub_info->stdin || sub_info->stdout) {
156 +               /* disallow core files */
157                 current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
158         }
159  
160 @@ -243,7 +263,7 @@
161         /* CLONE_VFORK: wait until the usermode helper has execve'd
162          * successfully We need the data structures to stay around
163          * until that is done.  */
164 -       if (wait == UMH_WAIT_PROC || wait == UMH_NO_WAIT)
165 +       if (wait == UMH_WAIT_PROC || wait == UMH_NO_WAIT || wait == UMH_WAIT_EXT)
166                 pid = kernel_thread(wait_for_helper, sub_info,
167                                     CLONE_FS | CLONE_FILES | SIGCHLD);
168         else
169 @@ -254,6 +274,16 @@
170         case UMH_NO_WAIT:
171                 break;
172  
173 +       case UMH_WAIT_EXT:
174 +               if (pid > 0) {
175 +                       complete(sub_info->executed);
176 +                       break;
177 +               }
178 +               sub_info->retval = pid;
179 +               complete(sub_info->executed);
180 +               complete(sub_info->complete);
181 +               break;
182 +
183         case UMH_WAIT_PROC:
184                 if (pid > 0)
185                         break;
186 @@ -404,6 +434,19 @@
187  }
188  EXPORT_SYMBOL(call_usermodehelper_setcleanup);
189  
190 +void call_usermodehelper_setcomplete(struct subprocess_info *info,
191 +                                   struct completion* complete)
192 +{
193 +       info->complete = complete;
194 +}
195 +EXPORT_SYMBOL(call_usermodehelper_setcomplete);
196 +
197 +int call_usermodehelper_getretval(struct subprocess_info *info)
198 +{
199 +       return info->retval;
200 +}
201 +EXPORT_SYMBOL(call_usermodehelper_getretval);
202 +
203  /**
204   * call_usermodehelper_stdinpipe - set up a pipe to be used for stdin
205   * @sub_info: a subprocess_info returned by call_usermodehelper_setup
206 @@ -433,6 +476,29 @@
207  }
208  EXPORT_SYMBOL(call_usermodehelper_stdinpipe);
209  
210 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info,
211 +                                 struct file **filp)
212 +{
213 +       struct file *f;
214 +
215 +       f = create_write_pipe();
216 +       if (IS_ERR(f))
217 +               return PTR_ERR(f);
218 +       sub_info->stdout = f;
219 +
220 +       f = create_read_pipe(f);
221 +       if (IS_ERR(f)) {
222 +               free_write_pipe(sub_info->stdout);
223 +               sub_info->stdout = NULL;
224 +               return PTR_ERR(f);
225 +       }
226 +       *filp = f;
227 +
228 +       return 0;
229 +}
230 +EXPORT_SYMBOL(call_usermodehelper_stdoutpipe);
231 +
232 +
233  /**
234   * call_usermodehelper_exec - start a usermode application
235   * @sub_info: information about the subprocessa
236 @@ -460,15 +526,22 @@
237                 goto out;
238         }
239  
240 -       sub_info->complete = &done;
241 +       if (wait == UMH_WAIT_EXT)
242 +               sub_info->executed = &done;
243 +       else
244 +               sub_info->complete = &done;
245 +
246         sub_info->wait = wait;
247  
248         queue_work(khelper_wq, &sub_info->work);
249         if (wait == UMH_NO_WAIT)        /* task has freed sub_info */
250                 goto unlock;
251 +
252         wait_for_completion(&done);
253 -       retval = sub_info->retval;
254  
255 +       retval = sub_info->retval;
256 +       if (wait == UMH_WAIT_EXT)       /* caller will free sub_info */
257 +               goto unlock;
258  out:
259         call_usermodehelper_freeinfo(sub_info);
260  unlock:
261 --- a/include/linux/kmod.h      2008-07-13 23:51:29.000000000 +0200
262 +++ b/include/linux/kmod.h      2008-07-31 14:43:33.000000000 +0200
263 @@ -38,6 +38,7 @@
264  struct key;
265  struct file;
266  struct subprocess_info;
267 +struct completion;
268  
269  /* Allocate a subprocess_info structure */
270  struct subprocess_info *call_usermodehelper_setup(char *path,
271 @@ -48,13 +49,20 @@
272                                  struct key *session_keyring);
273  int call_usermodehelper_stdinpipe(struct subprocess_info *sub_info,
274                                   struct file **filp);
275 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info,
276 +                                  struct file **filp);
277 +void call_usermodehelper_setcomplete(struct subprocess_info *sub_info,
278 +                                    struct completion *complete);
279  void call_usermodehelper_setcleanup(struct subprocess_info *info,
280                                     void (*cleanup)(char **argv, char **envp));
281 +int call_usermodehelper_getretval(struct subprocess_info *sub_info);
282  
283  enum umh_wait {
284         UMH_NO_WAIT = -1,       /* don't wait at all */
285         UMH_WAIT_EXEC = 0,      /* wait for the exec, but not the process */
286         UMH_WAIT_PROC = 1,      /* wait for the process to complete */
287 +       UMH_WAIT_EXT = 2,       /* wait for the exec then return and signal 
288 +                                  when the process is complete */
289  };
290  
291  /* Actually execute the sub-process */
292 --- a/arch/um/Makefile  2008-07-13 23:51:29.000000000 +0200
293 +++ b/arch/um/Makefile  2008-07-15 16:27:02.000000000 +0200
294 @@ -77,6 +77,7 @@
295  KERNEL_DEFINES = $(strip -Derrno=kernel_errno -Dsigprocmask=kernel_sigprocmask \
296                          -Dmktime=kernel_mktime $(ARCH_KERNEL_DEFINES))
297  KBUILD_CFLAGS += $(KERNEL_DEFINES)
298 +KBUILD_CFLAGS += $(call cc-option,-fno-unit-at-a-time,)
299  
300  PHONY += linux
301  
302 --- a/arch/um/Makefile-i386     2008-07-13 23:51:29.000000000 +0200
303 +++ b/arch/um/Makefile-i386     2008-07-15 16:23:57.000000000 +0200
304 @@ -32,11 +32,4 @@
305  # an unresolved reference.
306  cflags-y += -ffreestanding
307  
308 -# Disable unit-at-a-time mode on pre-gcc-4.0 compilers, it makes gcc use
309 -# a lot more stack due to the lack of sharing of stacklots.  Also, gcc
310 -# 4.3.0 needs -funit-at-a-time for extern inline functions.
311 -KBUILD_CFLAGS += $(shell if [ $(call cc-version) -lt 0400 ] ; then \
312 -                       echo $(call cc-option,-fno-unit-at-a-time); \
313 -                       else echo $(call cc-option,-funit-at-a-time); fi ;)
314 -
315  KBUILD_CFLAGS += $(cflags-y)
316 --- a/arch/um/Makefile-x86_64   2008-07-13 23:51:29.000000000 +0200
317 +++ b/arch/um/Makefile-x86_64   2008-07-15 16:24:20.000000000 +0200
318 @@ -21,6 +21,3 @@
319  
320  LINK-$(CONFIG_LD_SCRIPT_DYN) += -Wl,-rpath,/lib64
321  LINK-y += -m64
322 -
323 -# Do unit-at-a-time unconditionally on x86_64, following the host
324 -KBUILD_CFLAGS += $(call cc-option,-funit-at-a-time)
325 --- a/arch/um/drivers/line.c    2008-07-13 23:51:29.000000000 +0200
326 +++ b/arch/um/drivers/line.c    2008-07-28 15:13:19.000000000 +0200
327 @@ -876,6 +876,6 @@
328                 return base;
329         }
330  
331 -       snprintf(title, len, "%s (%s)", base, umid);
332 +       snprintf(title, len, "%s (%s)", umid, base);
333         return title;
334  }