uml: remove user_util.h
[safe/jmp/linux-2.6] / arch / um / os-Linux / helper.c
1 /*
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <sched.h>
11 #include <limits.h>
12 #include <sys/signal.h>
13 #include <sys/wait.h>
14 #include "user.h"
15 #include "kern_util.h"
16 #include "os.h"
17 #include "um_malloc.h"
18
19 struct helper_data {
20         void (*pre_exec)(void*);
21         void *pre_data;
22         char **argv;
23         int fd;
24         char *buf;
25 };
26
27 static int helper_child(void *arg)
28 {
29         struct helper_data *data = arg;
30         char **argv = data->argv;
31         int errval;
32
33         if (data->pre_exec != NULL)
34                 (*data->pre_exec)(data->pre_data);
35         errval = execvp_noalloc(data->buf, argv[0], argv);
36         printk("helper_child - execvp of '%s' failed - errno = %d\n", argv[0], -errval);
37         os_write_file(data->fd, &errval, sizeof(errval));
38         kill(os_getpid(), SIGKILL);
39         return 0;
40 }
41
42 /* Returns either the pid of the child process we run or -E* on failure.
43  * XXX The alloc_stack here breaks if this is called in the tracing thread, so
44  * we need to receive a preallocated stack (a local buffer is ok). */
45 int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
46                unsigned long *stack_out)
47 {
48         struct helper_data data;
49         unsigned long stack, sp;
50         int pid, fds[2], ret, n;
51
52         if ((stack_out != NULL) && (*stack_out != 0))
53                 stack = *stack_out;
54         else
55                 stack = alloc_stack(0, __cant_sleep());
56         if (stack == 0)
57                 return -ENOMEM;
58
59         ret = os_pipe(fds, 1, 0);
60         if (ret < 0) {
61                 printk("run_helper : pipe failed, ret = %d\n", -ret);
62                 goto out_free;
63         }
64
65         ret = os_set_exec_close(fds[1], 1);
66         if (ret < 0) {
67                 printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
68                        -ret);
69                 goto out_close;
70         }
71
72         sp = stack + page_size() - sizeof(void *);
73         data.pre_exec = pre_exec;
74         data.pre_data = pre_data;
75         data.argv = argv;
76         data.fd = fds[1];
77         data.buf = __cant_sleep() ? um_kmalloc_atomic(PATH_MAX) :
78                                         um_kmalloc(PATH_MAX);
79         pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
80         if (pid < 0) {
81                 ret = -errno;
82                 printk("run_helper : clone failed, errno = %d\n", errno);
83                 goto out_free2;
84         }
85
86         close(fds[1]);
87         fds[1] = -1;
88
89         /* Read the errno value from the child, if the exec failed, or get 0 if
90          * the exec succeeded because the pipe fd was set as close-on-exec. */
91         n = os_read_file(fds[0], &ret, sizeof(ret));
92         if (n == 0) {
93                 ret = pid;
94         } else {
95                 if (n < 0) {
96                         printk("run_helper : read on pipe failed, ret = %d\n",
97                                -n);
98                         ret = n;
99                         kill(pid, SIGKILL);
100                 }
101                 CATCH_EINTR(waitpid(pid, NULL, 0));
102         }
103
104 out_free2:
105         kfree(data.buf);
106 out_close:
107         if (fds[1] != -1)
108                 close(fds[1]);
109         close(fds[0]);
110 out_free:
111         if ((stack_out == NULL) || (*stack_out == 0))
112                 free_stack(stack, 0);
113         return ret;
114 }
115
116 int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
117                       unsigned long *stack_out, int stack_order)
118 {
119         unsigned long stack, sp;
120         int pid, status, err;
121
122         stack = alloc_stack(stack_order, __cant_sleep());
123         if (stack == 0)
124                 return -ENOMEM;
125
126         sp = stack + (page_size() << stack_order) - sizeof(void *);
127         pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
128         if (pid < 0) {
129                 err = -errno;
130                 printk("run_helper_thread : clone failed, errno = %d\n",
131                        errno);
132                 return err;
133         }
134         if (stack_out == NULL) {
135                 CATCH_EINTR(pid = waitpid(pid, &status, 0));
136                 if (pid < 0) {
137                         err = -errno;
138                         printk("run_helper_thread - wait failed, errno = %d\n",
139                                errno);
140                         pid = err;
141                 }
142                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
143                         printk("run_helper_thread - thread returned status "
144                                "0x%x\n", status);
145                 free_stack(stack, stack_order);
146         } else
147                 *stack_out = stack;
148         return pid;
149 }
150
151 int helper_wait(int pid)
152 {
153         int ret;
154
155         CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
156         if (ret < 0) {
157                 ret = -errno;
158                 printk("helper_wait : waitpid failed, errno = %d\n", errno);
159         }
160         return ret;
161 }