Update UML target to 3.10 and various improvements
[15.05/openwrt.git] / target / linux / uml / patches-3.10 / 101-mconsole-exec.patch
1 #
2 # Minimalist mconsole exec patch 
3 #
4 # 3.10 version (with bit more synchronous behavior) by fingon at iki dot fi
5 # Adaptation to kernel 3.3.8 made by David Fernández (david at dit.upm.es) for 
6 # Starting point: mconsole-exec-2.6.30.patch for kernel 2.6.30
7 # Author of original patch: Paolo Giarrusso, aka Blaisorblade 
8 #                           (http://www.user-mode-linux.org/~blaisorblade)
9 #
10 # Known misfeatures:
11 #
12 # - If output is too long, blocks (and breaks horribly) 
13 # (this misfeature from 3.10 patches, when minimalizing the patch;
14 #  workaround: redirect to a shared filesystem if long output is expected)
15 #
16 # - Nothing useful is done with stdin
17 #
18 diff --git a/arch/um/drivers/mconsole.h b/arch/um/drivers/mconsole.h
19 index 8b22535..77cc5f7 100644
20 --- a/arch/um/drivers/mconsole.h
21 +++ b/arch/um/drivers/mconsole.h
22 @@ -85,6 +85,7 @@ extern void mconsole_cad(struct mc_request *req);
23  extern void mconsole_stop(struct mc_request *req);
24  extern void mconsole_go(struct mc_request *req);
25  extern void mconsole_log(struct mc_request *req);
26 +extern void mconsole_exec(struct mc_request *req);
27  extern void mconsole_proc(struct mc_request *req);
28  extern void mconsole_stack(struct mc_request *req);
29  
30 diff --git a/arch/um/drivers/mconsole_kern.c b/arch/um/drivers/mconsole_kern.c
31 index 3df3bd5..307bf75 100644
32 --- a/arch/um/drivers/mconsole_kern.c
33 +++ b/arch/um/drivers/mconsole_kern.c
34 @@ -4,6 +4,7 @@
35   * Licensed under the GPL
36   */
37  
38 +#include "linux/kmod.h"
39  #include <linux/console.h>
40  #include <linux/ctype.h>
41  #include <linux/string.h>
42 @@ -24,6 +25,7 @@
43  #include <linux/fs.h>
44  #include <linux/mount.h>
45  #include <linux/file.h>
46 +#include <linux/completion.h>
47  #include <asm/uaccess.h>
48  #include <asm/switch_to.h>
49  
50 @@ -121,6 +123,59 @@ void mconsole_log(struct mc_request *req)
51         mconsole_reply(req, "", 0, 0);
52  }
53  
54 +void mconsole_exec(struct mc_request *req)
55 +{
56 +  struct subprocess_info *sub_info;
57 +  int res, len;
58 +  struct file *out;
59 +  char buf[MCONSOLE_MAX_DATA];
60 +
61 +  char *envp[] = {
62 +    "HOME=/", "TERM=linux",
63 +    "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
64 +    NULL
65 +  };
66 +  char *argv[] = {
67 +    "/bin/sh", "-c",
68 +    req->request.data + strlen("exec "),
69 +    NULL
70 +  };
71 +
72 +  sub_info = call_usermodehelper_setup("/bin/sh", argv, envp, GFP_ATOMIC, NULL, NULL, NULL);
73 +  if (sub_info == NULL) {
74 +    mconsole_reply(req, "call_usermodehelper_setup failed", 1, 0);
75 +    return;
76 +  }
77 +  res = call_usermodehelper_stdoutpipe(sub_info, &out);
78 +  if (res < 0) {
79 +    kfree(sub_info);
80 +    mconsole_reply(req, "call_usermodehelper_stdoutpipe failed", 1, 0);
81 +    return;
82 +  }
83 +
84 +  res = call_usermodehelper_exec(sub_info, UMH_WAIT_PROC);
85 +  if (res < 0) {
86 +    kfree(sub_info);
87 +    mconsole_reply(req, "call_usermodehelper_exec failed", 1, 0);
88 +    return;
89 +  }
90 +
91 +  for (;;) {
92 +    len = out->f_op->read(out, buf, sizeof(buf), &out->f_pos);
93 +    if (len < 0) {
94 +      mconsole_reply(req, "reading output failed", 1, 0);
95 +      break;
96 +    }
97 +    if (len == 0)
98 +      break;
99 +    mconsole_reply_len(req, buf, len, 0, 1);
100 +  }
101 +  fput(out);
102 +
103 +  mconsole_reply_len(req, NULL, 0, 0, 0);
104 +}
105 +
106 +
107  void mconsole_proc(struct mc_request *req)
108  {
109         struct vfsmount *mnt = task_active_pid_ns(current)->proc_mnt;
110 @@ -187,6 +242,7 @@ void mconsole_proc(struct mc_request *req)
111      stop - pause the UML; it will do nothing until it receives a 'go' \n\
112      go - continue the UML after a 'stop' \n\
113      log <string> - make UML enter <string> into the kernel log\n\
114 +    exec <string> - pass <string> to /bin/sh -c synchronously\n\
115      proc <file> - returns the contents of the UML's /proc/<file>\n\
116      stack <pid> - returns the stack of the specified pid\n\
117  "
118 diff --git a/arch/um/drivers/mconsole_user.c b/arch/um/drivers/mconsole_user.c
119 index 9920982..3ed0d32 100644
120 --- a/arch/um/drivers/mconsole_user.c
121 +++ b/arch/um/drivers/mconsole_user.c
122 @@ -30,6 +30,7 @@ static struct mconsole_command commands[] = {
123         { "stop", mconsole_stop, MCONSOLE_PROC },
124         { "go", mconsole_go, MCONSOLE_INTR },
125         { "log", mconsole_log, MCONSOLE_INTR },
126 +       { "exec", mconsole_exec, MCONSOLE_PROC },
127         { "proc", mconsole_proc, MCONSOLE_PROC },
128         { "stack", mconsole_stack, MCONSOLE_INTR },
129  };
130 diff --git a/arch/um/os-Linux/file.c b/arch/um/os-Linux/file.c
131 index c17bd6f..1c55fa8 100644
132 --- a/arch/um/os-Linux/file.c
133 +++ b/arch/um/os-Linux/file.c
134 @@ -519,6 +519,8 @@ int os_create_unix_socket(const char *file, int len, int close_on_exec)
135  
136         addr.sun_family = AF_UNIX;
137  
138 +       if (len > sizeof(addr.sun_path))
139 +               len = sizeof(addr.sun_path);
140         snprintf(addr.sun_path, len, "%s", file);
141  
142         err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
143 diff --git a/include/linux/kmod.h b/include/linux/kmod.h
144 index 0555cc6..476084d 100644
145 --- a/include/linux/kmod.h
146 +++ b/include/linux/kmod.h
147 @@ -62,6 +62,7 @@ struct subprocess_info {
148         int wait;
149         int retval;
150         int (*init)(struct subprocess_info *info, struct cred *new);
151 +       struct file *stdout;
152         void (*cleanup)(struct subprocess_info *info);
153         void *data;
154  };
155 @@ -104,4 +105,6 @@ extern int usermodehelper_read_trylock(void);
156  extern long usermodehelper_read_lock_wait(long timeout);
157  extern void usermodehelper_read_unlock(void);
158  
159 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info, struct file **filp);
160 +
161  #endif /* __LINUX_KMOD_H__ */
162 diff --git a/kernel/kmod.c b/kernel/kmod.c
163 index 8241906..2d7f718 100644
164 --- a/kernel/kmod.c
165 +++ b/kernel/kmod.c
166 @@ -39,6 +39,7 @@
167  #include <linux/rwsem.h>
168  #include <linux/ptrace.h>
169  #include <linux/async.h>
170 +#include <linux/pipe_fs_i.h>
171  #include <asm/uaccess.h>
172  
173  #include <trace/events/module.h>
174 @@ -206,6 +207,28 @@ static int ____call_usermodehelper(void *data)
175         flush_signal_handlers(current, 1);
176         spin_unlock_irq(&current->sighand->siglock);
177  
178 +        /* Install output when needed */
179 +        if (sub_info->stdout) {
180 +                struct files_struct *f = current->files;
181 +                struct fdtable *fdt;
182 +
183 +                sys_close(1);
184 +                sys_close(2);
185 +                get_file(sub_info->stdout);
186 +                fd_install(1, sub_info->stdout);
187 +                fd_install(2, sub_info->stdout);
188 +                spin_lock(&f->file_lock);
189 +                fdt = files_fdtable(f);
190 +                __set_bit(1, fdt->open_fds);
191 +                __clear_bit(1, fdt->close_on_exec);
192 +                __set_bit(2, fdt->open_fds);
193 +                __clear_bit(2, fdt->close_on_exec);
194 +                spin_unlock(&f->file_lock);
195 +
196 +                /* disallow core files */
197 +                current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
198 +        }
199 +
200         /* We can run anywhere, unlike our parent keventd(). */
201         set_cpus_allowed_ptr(current, cpu_all_mask);
202  
203 @@ -551,6 +574,20 @@ struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
204  }
205  EXPORT_SYMBOL(call_usermodehelper_setup);
206  
207 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info,
208 +                                 struct file **filp)
209 +{
210 +  struct file *f[2];
211 +
212 +  if (create_pipe_files(f, 0)<0)
213 +    return PTR_ERR(f);
214 +  sub_info->stdout = f[1];
215 +  *filp = f[0];
216 +  return 0;
217 +}
218 +EXPORT_SYMBOL(call_usermodehelper_stdoutpipe);
219 +
220 +
221  /**
222   * call_usermodehelper_exec - start a usermode application
223   * @sub_info: information about the subprocessa