[PATCH] uml: Fix handling of failed execs of helpers
[deliverable/linux.git] / arch / um / os-Linux / helper.c
CommitLineData
ff5c6ff5 1/*
1da177e4
LT
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 <sys/signal.h>
12#include <sys/wait.h>
13#include "user.h"
14#include "kern_util.h"
15#include "user_util.h"
16#include "os.h"
17
18struct helper_data {
19 void (*pre_exec)(void*);
20 void *pre_data;
21 char **argv;
22 int fd;
23};
24
25/* Debugging aid, changed only from gdb */
26int helper_pause = 0;
27
28static void helper_hup(int sig)
29{
30}
31
32static int helper_child(void *arg)
33{
34 struct helper_data *data = arg;
35 char **argv = data->argv;
36 int errval;
37
38 if(helper_pause){
39 signal(SIGHUP, helper_hup);
40 pause();
41 }
42 if(data->pre_exec != NULL)
43 (*data->pre_exec)(data->pre_data);
44 execvp(argv[0], argv);
6b7aaad9 45 errval = -errno;
b1c332c9 46 printk("helper_child - execve of '%s' failed - errno = %d\n", argv[0], errno);
1da177e4 47 os_write_file(data->fd, &errval, sizeof(errval));
ff5c6ff5 48 kill(os_getpid(), SIGKILL);
1da177e4
LT
49 return(0);
50}
51
52/* Returns either the pid of the child process we run or -E* on failure.
53 * XXX The alloc_stack here breaks if this is called in the tracing thread */
54int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
55 unsigned long *stack_out)
56{
57 struct helper_data data;
58 unsigned long stack, sp;
59 int pid, fds[2], ret, n;
60
61 if((stack_out != NULL) && (*stack_out != 0))
62 stack = *stack_out;
b6316293 63 else stack = alloc_stack(0, __cant_sleep());
1da177e4 64 if(stack == 0)
6b7aaad9 65 return -ENOMEM;
1da177e4
LT
66
67 ret = os_pipe(fds, 1, 0);
68 if(ret < 0){
69 printk("run_helper : pipe failed, ret = %d\n", -ret);
70 goto out_free;
71 }
72
73 ret = os_set_exec_close(fds[1], 1);
74 if(ret < 0){
75 printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
76 -ret);
77 goto out_close;
78 }
79
80 sp = stack + page_size() - sizeof(void *);
81 data.pre_exec = pre_exec;
82 data.pre_data = pre_data;
83 data.argv = argv;
84 data.fd = fds[1];
85 pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
86 if(pid < 0){
1da177e4 87 ret = -errno;
b4fd310e 88 printk("run_helper : clone failed, errno = %d\n", errno);
1da177e4
LT
89 goto out_close;
90 }
91
ff5c6ff5 92 close(fds[1]);
1da177e4
LT
93 fds[1] = -1;
94
b1c332c9
PBG
95 /* Read the errno value from the child, if the exec failed, or get 0 if
96 * the exec succeeded because the pipe fd was set as close-on-exec. */
1da177e4 97 n = os_read_file(fds[0], &ret, sizeof(ret));
6b7aaad9 98 if(n == 0)
1da177e4 99 ret = pid;
6b7aaad9
JD
100 else {
101 if(n < 0){
102 printk("run_helper : read on pipe failed, ret = %d\n",
103 -n);
104 ret = n;
105 kill(pid, SIGKILL);
106 }
107 CATCH_EINTR(waitpid(pid, NULL, 0));
1da177e4
LT
108 }
109
110out_close:
111 if (fds[1] != -1)
ff5c6ff5
JD
112 close(fds[1]);
113 close(fds[0]);
1da177e4
LT
114out_free:
115 if(stack_out == NULL)
116 free_stack(stack, 0);
117 else *stack_out = stack;
118 return(ret);
119}
120
ff5c6ff5 121int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
1da177e4
LT
122 unsigned long *stack_out, int stack_order)
123{
124 unsigned long stack, sp;
b4fd310e 125 int pid, status, err;
1da177e4 126
b6316293 127 stack = alloc_stack(stack_order, __cant_sleep());
1da177e4
LT
128 if(stack == 0) return(-ENOMEM);
129
130 sp = stack + (page_size() << stack_order) - sizeof(void *);
131 pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
132 if(pid < 0){
b4fd310e 133 err = -errno;
ff5c6ff5 134 printk("run_helper_thread : clone failed, errno = %d\n",
1da177e4 135 errno);
b4fd310e 136 return err;
1da177e4
LT
137 }
138 if(stack_out == NULL){
139 CATCH_EINTR(pid = waitpid(pid, &status, 0));
140 if(pid < 0){
b4fd310e 141 err = -errno;
1da177e4
LT
142 printk("run_helper_thread - wait failed, errno = %d\n",
143 errno);
b4fd310e 144 pid = err;
1da177e4
LT
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 }
151 else *stack_out = stack;
152 return(pid);
153}
154
0d4579ed 155int helper_wait(int pid)
1da177e4
LT
156{
157 int ret;
158
159 CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
160 if(ret < 0){
b4fd310e 161 ret = -errno;
1da177e4 162 printk("helper_wait : waitpid failed, errno = %d\n", errno);
1da177e4
LT
163 }
164 return(ret);
165}
This page took 0.156113 seconds and 5 git commands to generate.