2 # Minimalist mconsole exec patch
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)
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)
16 # - Nothing useful is done with stdin
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);
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
35 * Licensed under the GPL
38 +#include "linux/kmod.h"
39 #include <linux/console.h>
40 #include <linux/ctype.h>
41 #include <linux/string.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>
50 @@ -121,6 +123,59 @@ void mconsole_log(struct mc_request *req)
51 mconsole_reply(req, "", 0, 0);
54 +void mconsole_exec(struct mc_request *req)
56 + struct subprocess_info *sub_info;
59 + char buf[MCONSOLE_MAX_DATA];
62 + "HOME=/", "TERM=linux",
63 + "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
68 + req->request.data + strlen("exec "),
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);
77 + res = call_usermodehelper_stdoutpipe(sub_info, &out);
80 + mconsole_reply(req, "call_usermodehelper_stdoutpipe failed", 1, 0);
84 + res = call_usermodehelper_exec(sub_info, UMH_WAIT_PROC);
87 + mconsole_reply(req, "call_usermodehelper_exec failed", 1, 0);
92 + len = out->f_op->read(out, buf, sizeof(buf), &out->f_pos);
94 + mconsole_reply(req, "reading output failed", 1, 0);
99 + mconsole_reply_len(req, buf, len, 0, 1);
103 + mconsole_reply_len(req, NULL, 0, 0, 0);
107 void mconsole_proc(struct mc_request *req)
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\
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 },
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)
136 addr.sun_family = AF_UNIX;
138 + if (len > sizeof(addr.sun_path))
139 + len = sizeof(addr.sun_path);
140 snprintf(addr.sun_path, len, "%s", file);
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 {
150 int (*init)(struct subprocess_info *info, struct cred *new);
151 + struct file *stdout;
152 void (*cleanup)(struct subprocess_info *info);
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);
159 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info, struct file **filp);
161 #endif /* __LINUX_KMOD_H__ */
162 diff --git a/kernel/kmod.c b/kernel/kmod.c
163 index 8241906..2d7f718 100644
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>
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(¤t->sighand->siglock);
178 + /* Install output when needed */
179 + if (sub_info->stdout) {
180 + struct files_struct *f = current->files;
181 + struct fdtable *fdt;
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);
196 + /* disallow core files */
197 + current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
200 /* We can run anywhere, unlike our parent keventd(). */
201 set_cpus_allowed_ptr(current, cpu_all_mask);
203 @@ -551,6 +574,20 @@ struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
205 EXPORT_SYMBOL(call_usermodehelper_setup);
207 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info,
208 + struct file **filp)
212 + if (create_pipe_files(f, 0)<0)
214 + sub_info->stdout = f[1];
218 +EXPORT_SYMBOL(call_usermodehelper_stdoutpipe);
222 * call_usermodehelper_exec - start a usermode application
223 * @sub_info: information about the subprocessa