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