98f04f4a6216da48122304324886831ef374c24f
[openwrt.git] / target / linux / uml / patches-4.4 / 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 @@
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 @@ -24,6 +25,7 @@
39  #include <linux/fs.h>
40  #include <linux/mount.h>
41  #include <linux/file.h>
42 +#include <linux/completion.h>
43  #include <asm/uaccess.h>
44  #include <asm/switch_to.h>
45  
46 @@ -121,6 +123,59 @@
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 @@ -187,6 +242,7 @@
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 @@
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 @@ -535,6 +535,8 @@
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 @@ -62,6 +62,7 @@
138         int wait;
139         int retval;
140         int (*init)(struct subprocess_info *info, struct cred *new);
141 +       struct file *stdout;
142         void (*cleanup)(struct subprocess_info *info);
143         void *data;
144  };
145 @@ -102,4 +103,6 @@
146  extern long usermodehelper_read_lock_wait(long timeout);
147  extern void usermodehelper_read_unlock(void);
148  
149 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info, struct file **filp);
150 +
151  #endif /* __LINUX_KMOD_H__ */
152 --- a/kernel/kmod.c
153 +++ b/kernel/kmod.c
154 @@ -39,6 +39,7 @@
155  #include <linux/rwsem.h>
156  #include <linux/ptrace.h>
157  #include <linux/async.h>
158 +#include <linux/pipe_fs_i.h>
159  #include <asm/uaccess.h>
160  
161  #include <trace/events/module.h>
162 @@ -222,6 +223,28 @@
163         flush_signal_handlers(current, 1);
164         spin_unlock_irq(&current->sighand->siglock);
165  
166 +       /* Install output when needed */
167 +       if (sub_info->stdout) {
168 +               struct files_struct *f = current->files;
169 +               struct fdtable *fdt;
170 +
171 +               sys_close(1);
172 +               sys_close(2);
173 +               get_file(sub_info->stdout);
174 +               fd_install(1, sub_info->stdout);
175 +               fd_install(2, sub_info->stdout);
176 +               spin_lock(&f->file_lock);
177 +               fdt = files_fdtable(f);
178 +               __set_bit(1, fdt->open_fds);
179 +               __clear_bit(1, fdt->close_on_exec);
180 +               __set_bit(2, fdt->open_fds);
181 +               __clear_bit(2, fdt->close_on_exec);
182 +               spin_unlock(&f->file_lock);
183 +
184 +               /* disallow core files */
185 +               current->signal->rlim[RLIMIT_CORE] = (struct rlimit){0, 0};
186 +       }
187 +
188         /*
189          * Our parent (unbound workqueue) runs with elevated scheduling
190          * priority. Avoid propagating that into the userspace child.
191 @@ -540,6 +563,20 @@
192  }
193  EXPORT_SYMBOL(call_usermodehelper_setup);
194  
195 +int call_usermodehelper_stdoutpipe(struct subprocess_info *sub_info,
196 +                                 struct file **filp)
197 +{
198 +  struct file *f[2];
199 +
200 +  if (create_pipe_files(f, 0)<0)
201 +    return PTR_ERR(f);
202 +  sub_info->stdout = f[1];
203 +  *filp = f[0];
204 +  return 0;
205 +}
206 +EXPORT_SYMBOL(call_usermodehelper_stdoutpipe);
207 +
208 +
209  /**
210   * call_usermodehelper_exec - start a usermode application
211   * @sub_info: information about the subprocessa