2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
10 #include <linux/limits.h>
11 #include <sys/socket.h>
13 #include "kern_constants.h"
14 #include "kern_util.h"
16 #include "um_malloc.h"
18 #include <linux/limits.h>
21 void (*pre_exec
)(void*);
28 static int helper_child(void *arg
)
30 struct helper_data
*data
= arg
;
31 char **argv
= data
->argv
;
34 if (data
->pre_exec
!= NULL
)
35 (*data
->pre_exec
)(data
->pre_data
);
36 err
= execvp_noalloc(data
->buf
, argv
[0], argv
);
38 /* If the exec succeeds, we don't get here */
39 write(data
->fd
, &err
, sizeof(err
));
44 /* Returns either the pid of the child process we run or -E* on failure. */
45 int run_helper(void (*pre_exec
)(void *), void *pre_data
, char **argv
)
47 struct helper_data data
;
48 unsigned long stack
, sp
;
49 int pid
, fds
[2], ret
, n
;
51 stack
= alloc_stack(0, __cant_sleep());
55 ret
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
);
58 printk(UM_KERN_ERR
"run_helper : pipe failed, errno = %d\n",
63 ret
= os_set_exec_close(fds
[1]);
65 printk(UM_KERN_ERR
"run_helper : setting FD_CLOEXEC failed, "
70 sp
= stack
+ UM_KERN_PAGE_SIZE
- sizeof(void *);
71 data
.pre_exec
= pre_exec
;
72 data
.pre_data
= pre_data
;
75 data
.buf
= __cant_sleep() ? uml_kmalloc(PATH_MAX
, UM_GFP_ATOMIC
) :
76 uml_kmalloc(PATH_MAX
, UM_GFP_KERNEL
);
77 pid
= clone(helper_child
, (void *) sp
, CLONE_VM
, &data
);
80 printk(UM_KERN_ERR
"run_helper : clone failed, errno = %d\n",
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.
92 n
= read(fds
[0], &ret
, sizeof(ret
));
98 printk(UM_KERN_ERR
"run_helper : read on pipe failed, "
102 CATCH_EINTR(waitpid(pid
, NULL
, __WCLONE
));
112 free_stack(stack
, 0);
116 int run_helper_thread(int (*proc
)(void *), void *arg
, unsigned int flags
,
117 unsigned long *stack_out
)
119 unsigned long stack
, sp
;
120 int pid
, status
, err
;
122 stack
= alloc_stack(0, __cant_sleep());
126 sp
= stack
+ UM_KERN_PAGE_SIZE
- sizeof(void *);
127 pid
= clone(proc
, (void *) sp
, flags
, arg
);
130 printk(UM_KERN_ERR
"run_helper_thread : clone failed, "
131 "errno = %d\n", errno
);
134 if (stack_out
== NULL
) {
135 CATCH_EINTR(pid
= waitpid(pid
, &status
, __WCLONE
));
138 printk(UM_KERN_ERR
"run_helper_thread - wait failed, "
139 "errno = %d\n", errno
);
142 if (!WIFEXITED(status
) || (WEXITSTATUS(status
) != 0))
143 printk(UM_KERN_ERR
"run_helper_thread - thread "
144 "returned status 0x%x\n", status
);
145 free_stack(stack
, 0);
151 int helper_wait(int pid
)
154 int wflags
= __WCLONE
;
156 CATCH_EINTR(ret
= waitpid(pid
, &status
, wflags
));
158 printk(UM_KERN_ERR
"helper_wait : waitpid process %d failed, "
159 "errno = %d\n", pid
, errno
);
161 } else if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
162 printk(UM_KERN_ERR
"helper_wait : process %d exited with "
163 "status 0x%x\n", pid
, status
);