uml: update to linux 4.14
[openwrt/staging/blogic.git] / target / linux / uml / patches-4.14 / 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 --- a/arch/um/drivers/mconsole.h
19 +++ b/arch/um/drivers/mconsole.h
20 @@ -85,6 +85,7 @@ extern void mconsole_cad(struct mc_reque
21 extern void mconsole_stop(struct mc_request *req);
22 extern void mconsole_go(struct mc_request *req);
23 extern void mconsole_log(struct mc_request *req);
24 +extern void mconsole_exec(struct mc_request *req);
25 extern void mconsole_proc(struct mc_request *req);
26 extern void mconsole_stack(struct mc_request *req);
27
28 --- a/arch/um/drivers/mconsole_kern.c
29 +++ b/arch/um/drivers/mconsole_kern.c
30 @@ -4,6 +4,7 @@
31 * Licensed under the GPL
32 */
33
34 +#include <linux/kmod.h>
35 #include <linux/console.h>
36 #include <linux/ctype.h>
37 #include <linux/string.h>
38 @@ -26,6 +27,7 @@
39 #include <linux/mount.h>
40 #include <linux/file.h>
41 #include <linux/uaccess.h>
42 +#include <linux/completion.h>
43 #include <asm/switch_to.h>
44
45 #include <init.h>
46 @@ -122,6 +124,59 @@ void mconsole_log(struct mc_request *req
47 mconsole_reply(req, "", 0, 0);
48 }
49
50 +void mconsole_exec(struct mc_request *req)
51 +{
52 + struct subprocess_info *sub_info;
53 + int res, len;
54 + struct file *out;
55 + char buf[MCONSOLE_MAX_DATA];
56 +
57 + char *envp[] = {
58 + "HOME=/", "TERM=linux",
59 + "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
60 + NULL
61 + };
62 + char *argv[] = {
63 + "/bin/sh", "-c",
64 + req->request.data + strlen("exec "),
65 + NULL
66 + };
67 +
68 + sub_info = call_usermodehelper_setup("/bin/sh", argv, envp, GFP_ATOMIC, NULL, NULL, NULL);
69 + if (sub_info == NULL) {
70 + mconsole_reply(req, "call_usermodehelper_setup failed", 1, 0);
71 + return;
72 + }
73 + res = call_usermodehelper_stdoutpipe(sub_info, &out);
74 + if (res < 0) {
75 + kfree(sub_info);
76 + mconsole_reply(req, "call_usermodehelper_stdoutpipe failed", 1, 0);
77 + return;
78 + }
79 +
80 + res = call_usermodehelper_exec(sub_info, UMH_WAIT_PROC);
81 + if (res < 0) {
82 + kfree(sub_info);
83 + mconsole_reply(req, "call_usermodehelper_exec failed", 1, 0);
84 + return;
85 + }
86 +
87 + for (;;) {
88 + len = out->f_op->read(out, buf, sizeof(buf), &out->f_pos);
89 + if (len < 0) {
90 + mconsole_reply(req, "reading output failed", 1, 0);
91 + break;
92 + }
93 + if (len == 0)
94 + break;
95 + mconsole_reply_len(req, buf, len, 0, 1);
96 + }
97 + fput(out);
98 +
99 + mconsole_reply_len(req, NULL, 0, 0, 0);
100 +}
101 +
102 +
103 void mconsole_proc(struct mc_request *req)
104 {
105 struct vfsmount *mnt = task_active_pid_ns(current)->proc_mnt;
106 @@ -183,6 +238,7 @@ void mconsole_proc(struct mc_request *re
107 stop - pause the UML; it will do nothing until it receives a 'go' \n\
108 go - continue the UML after a 'stop' \n\
109 log <string> - make UML enter <string> into the kernel log\n\
110 + exec <string> - pass <string> to /bin/sh -c synchronously\n\
111 proc <file> - returns the contents of the UML's /proc/<file>\n\
112 stack <pid> - returns the stack of the specified pid\n\
113 "
114 --- a/arch/um/drivers/mconsole_user.c
115 +++ b/arch/um/drivers/mconsole_user.c
116 @@ -30,6 +30,7 @@ static struct mconsole_command commands[
117 { "stop", mconsole_stop, MCONSOLE_PROC },
118 { "go", mconsole_go, MCONSOLE_INTR },
119 { "log", mconsole_log, MCONSOLE_INTR },
120 + { "exec", mconsole_exec, MCONSOLE_PROC },
121 { "proc", mconsole_proc, MCONSOLE_PROC },
122 { "stack", mconsole_stack, MCONSOLE_INTR },
123 };
124 --- a/arch/um/os-Linux/file.c
125 +++ b/arch/um/os-Linux/file.c
126 @@ -555,6 +555,8 @@ int os_create_unix_socket(const char *fi
127
128 addr.sun_family = AF_UNIX;
129
130 + if (len > sizeof(addr.sun_path))
131 + len = sizeof(addr.sun_path);
132 snprintf(addr.sun_path, len, "%s", file);
133
134 err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
135 --- a/include/linux/kmod.h
136 +++ b/include/linux/kmod.h
137 @@ -45,4 +45,6 @@ static inline int request_module_nowait(
138 #define try_then_request_module(x, mod...) (x)
139 #endif
140
141 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info, struct file **filp);
142 +
143 #endif /* __LINUX_KMOD_H__ */
144 --- a/include/linux/umh.h
145 +++ b/include/linux/umh.h
146 @@ -22,6 +22,7 @@ struct subprocess_info {
147 const char *path;
148 char **argv;
149 char **envp;
150 + struct file *stdout;
151 int wait;
152 int retval;
153 int (*init)(struct subprocess_info *info, struct cred *new);
154 --- a/kernel/umh.c
155 +++ b/kernel/umh.c
156 @@ -25,6 +25,7 @@
157 #include <linux/ptrace.h>
158 #include <linux/async.h>
159 #include <linux/uaccess.h>
160 +#include <linux/pipe_fs_i.h>
161
162 #include <trace/events/module.h>
163
164 @@ -70,6 +71,28 @@ static int call_usermodehelper_exec_asyn
165 flush_signal_handlers(current, 1);
166 spin_unlock_irq(&current->sighand->siglock);
167
168 + /* Install output when needed */
169 + if (sub_info->stdout) {
170 + struct files_struct *f = current->files;
171 + struct fdtable *fdt;
172 +
173 + sys_close(1);
174 + sys_close(2);
175 + get_file(sub_info->stdout);
176 + fd_install(1, sub_info->stdout);
177 + fd_install(2, sub_info->stdout);
178 + spin_lock(&f->file_lock);
179 + fdt = files_fdtable(f);
180 + __set_bit(1, fdt->open_fds);
181 + __clear_bit(1, fdt->close_on_exec);
182 + __set_bit(2, fdt->open_fds);
183 + __clear_bit(2, fdt->close_on_exec);
184 + spin_unlock(&f->file_lock);
185 +
186 + /* disallow core files */
187 + current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
188 + }
189 +
190 /*
191 * Our parent (unbound workqueue) runs with elevated scheduling
192 * priority. Avoid propagating that into the userspace child.
193 @@ -393,6 +416,20 @@ struct subprocess_info *call_usermodehel
194 }
195 EXPORT_SYMBOL(call_usermodehelper_setup);
196
197 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info,
198 + struct file **filp)
199 +{
200 + struct file *f[2];
201 +
202 + if (create_pipe_files(f, 0) < 0)
203 + return PTR_ERR(f);
204 +
205 + sub_info->stdout = f[1];
206 + *filp = f[0];
207 + return 0;
208 +}
209 +EXPORT_SYMBOL(call_usermodehelper_stdoutpipe);
210 +
211 /**
212 * call_usermodehelper_exec - start a usermode application
213 * @sub_info: information about the subprocessa