blob: fba3f0fefeef7ca78d3edd685f24e8d5d4c254bc [file] [log] [blame]
Jeff Dikeff5c6ff2005-11-07 00:58:51 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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>
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080011#include <limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <sys/signal.h>
13#include <sys/wait.h>
Jeff Dike512b6fb2007-10-16 01:27:11 -070014#include <sys/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "user.h"
16#include "kern_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "os.h"
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080018#include "um_malloc.h"
Jeff Dike1ffb9162007-05-06 14:51:22 -070019#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21struct helper_data {
22 void (*pre_exec)(void*);
23 void *pre_data;
24 char **argv;
25 int fd;
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080026 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027};
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int helper_child(void *arg)
30{
31 struct helper_data *data = arg;
32 char **argv = data->argv;
33 int errval;
34
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070035 if (data->pre_exec != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 (*data->pre_exec)(data->pre_data);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080037 errval = execvp_noalloc(data->buf, argv[0], argv);
Jeff Dikeef0470c2007-05-06 14:51:33 -070038 printk("helper_child - execvp of '%s' failed - errno = %d\n", argv[0],
39 -errval);
Jeff Dikea61f3342007-05-06 14:51:35 -070040 write(data->fd, &errval, sizeof(errval));
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080041 kill(os_getpid(), SIGKILL);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45/* Returns either the pid of the child process we run or -E* on failure.
Paolo 'Blaisorblade' Giarrussod9d645f2006-10-19 23:28:24 -070046 * XXX The alloc_stack here breaks if this is called in the tracing thread, so
47 * we need to receive a preallocated stack (a local buffer is ok). */
Jeff Dikec4399012007-07-15 23:38:56 -070048int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct helper_data data;
51 unsigned long stack, sp;
52 int pid, fds[2], ret, n;
53
Jeff Dikec4399012007-07-15 23:38:56 -070054 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070055 if (stack == 0)
Jeff Dike6b7aaad2006-09-25 23:33:02 -070056 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jeff Dike512b6fb2007-10-16 01:27:11 -070058 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070059 if (ret < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -070060 ret = -errno;
61 printk("run_helper : pipe failed, errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 goto out_free;
63 }
64
Jeff Dike512b6fb2007-10-16 01:27:11 -070065 ret = os_set_exec_close(fds[1]);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070066 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
68 -ret);
69 goto out_close;
70 }
71
Jeff Dike1ffb9162007-05-06 14:51:22 -070072 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 data.pre_exec = pre_exec;
74 data.pre_data = pre_data;
75 data.argv = argv;
76 data.fd = fds[1];
Jeff Dikee4c4bf92007-07-15 23:38:56 -070077 data.buf = __cant_sleep() ? kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
78 kmalloc(PATH_MAX, UM_GFP_KERNEL);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080079 pid = clone(helper_child, (void *) sp, CLONE_VM, &data);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070080 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 ret = -errno;
Jeff Dikeb4fd3102005-09-16 19:27:49 -070082 printk("run_helper : clone failed, errno = %d\n", errno);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080083 goto out_free2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080086 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 fds[1] = -1;
88
Jeff Dikeef0470c2007-05-06 14:51:33 -070089 /*
90 * Read the errno value from the child, if the exec failed, or get 0 if
91 * the exec succeeded because the pipe fd was set as close-on-exec.
92 */
Jeff Dikea61f3342007-05-06 14:51:35 -070093 n = read(fds[0], &ret, sizeof(ret));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070094 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ret = pid;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070096 } else {
97 if (n < 0) {
Jeff Dikea61f3342007-05-06 14:51:35 -070098 n = -errno;
Jeff Dike6b7aaad2006-09-25 23:33:02 -070099 printk("run_helper : read on pipe failed, ret = %d\n",
100 -n);
101 ret = n;
102 kill(pid, SIGKILL);
103 }
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800104 CATCH_EINTR(waitpid(pid, NULL, __WCLONE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -0800107out_free2:
108 kfree(data.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109out_close:
110 if (fds[1] != -1)
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800111 close(fds[1]);
112 close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113out_free:
Jeff Dikec4399012007-07-15 23:38:56 -0700114 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700115 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800118int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
Jeff Dikec4399012007-07-15 23:38:56 -0700119 unsigned long *stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 unsigned long stack, sp;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700122 int pid, status, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jeff Dikec4399012007-07-15 23:38:56 -0700124 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700125 if (stack == 0)
126 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Jeff Dikec4399012007-07-15 23:38:56 -0700128 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800129 pid = clone(proc, (void *) sp, flags, arg);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700130 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700131 err = -errno;
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800132 printk("run_helper_thread : clone failed, errno = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700134 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700136 if (stack_out == NULL) {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800137 CATCH_EINTR(pid = waitpid(pid, &status, __WCLONE));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700138 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700139 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 printk("run_helper_thread - wait failed, errno = %d\n",
141 errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700142 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700144 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 printk("run_helper_thread - thread returned status "
146 "0x%x\n", status);
Jeff Dikec4399012007-07-15 23:38:56 -0700147 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700148 } else
149 *stack_out = stack;
150 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800153int helper_wait(int pid, int nohang, char *pname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800155 int ret, status;
156 int wflags = __WCLONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800158 if (nohang)
159 wflags |= WNOHANG;
160
161 if (!pname)
162 pname = "helper_wait";
163
164 CATCH_EINTR(ret = waitpid(pid, &status, wflags));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700165 if (ret < 0) {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800166 printk(UM_KERN_ERR "%s : waitpid process %d failed, "
167 "errno = %d\n", pname, pid, errno);
168 return -errno;
169 } else if (nohang && ret == 0) {
170 printk(UM_KERN_ERR "%s : process %d has not exited\n",
171 pname, pid);
172 return -ECHILD;
173 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
174 printk(UM_KERN_ERR "%s : process %d didn't exit with "
175 "status 0\n", pname, pid);
176 return -ECHILD;
177 } else
178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}