blob: f4bd349d441222fb7c400ac63d8e7b8c390784a2 [file] [log] [blame]
Jeff Dikeff5c6ff2005-11-07 00:58:51 -08001/*
Jeff Dike1aa351a2008-02-04 22:31:10 -08002 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <sched.h>
Jeff Dike512b6fb2007-10-16 01:27:11 -070010#include <sys/socket.h>
Jeff Dike1aa351a2008-02-04 22:31:10 -080011#include <sys/wait.h>
12#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "kern_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "os.h"
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080015#include "um_malloc.h"
Jeff Dike1aa351a2008-02-04 22:31:10 -080016#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18struct helper_data {
19 void (*pre_exec)(void*);
20 void *pre_data;
21 char **argv;
22 int fd;
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080023 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024};
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int helper_child(void *arg)
27{
28 struct helper_data *data = arg;
29 char **argv = data->argv;
Jeff Dike1aa351a2008-02-04 22:31:10 -080030 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070032 if (data->pre_exec != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 (*data->pre_exec)(data->pre_data);
Jeff Dike1aa351a2008-02-04 22:31:10 -080034 err = execvp_noalloc(data->buf, argv[0], argv);
35
36 /* If the exec succeeds, we don't get here */
37 write(data->fd, &err, sizeof(err));
38
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Jeff Dike1aa351a2008-02-04 22:31:10 -080042/* Returns either the pid of the child process we run or -E* on failure. */
Jeff Dikec4399012007-07-15 23:38:56 -070043int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 struct helper_data data;
46 unsigned long stack, sp;
47 int pid, fds[2], ret, n;
48
Jeff Dikec4399012007-07-15 23:38:56 -070049 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070050 if (stack == 0)
Jeff Dike6b7aaad2006-09-25 23:33:02 -070051 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jeff Dike512b6fb2007-10-16 01:27:11 -070053 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070054 if (ret < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -070055 ret = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080056 printk(UM_KERN_ERR "run_helper : pipe failed, errno = %d\n",
57 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 goto out_free;
59 }
60
Jeff Dike512b6fb2007-10-16 01:27:11 -070061 ret = os_set_exec_close(fds[1]);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070062 if (ret < 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -080063 printk(UM_KERN_ERR "run_helper : setting FD_CLOEXEC failed, "
64 "ret = %d\n", -ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 goto out_close;
66 }
67
Jeff Dike1ffb9162007-05-06 14:51:22 -070068 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 data.pre_exec = pre_exec;
70 data.pre_data = pre_data;
71 data.argv = argv;
72 data.fd = fds[1];
Jeff Dikee4c4bf992007-07-15 23:38:56 -070073 data.buf = __cant_sleep() ? kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
74 kmalloc(PATH_MAX, UM_GFP_KERNEL);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080075 pid = clone(helper_child, (void *) sp, CLONE_VM, &data);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070076 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 ret = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080078 printk(UM_KERN_ERR "run_helper : clone failed, errno = %d\n",
79 errno);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080080 goto out_free2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080083 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 fds[1] = -1;
85
Jeff Dikeef0470c2007-05-06 14:51:33 -070086 /*
87 * Read the errno value from the child, if the exec failed, or get 0 if
88 * the exec succeeded because the pipe fd was set as close-on-exec.
89 */
Jeff Dikea61f3342007-05-06 14:51:35 -070090 n = read(fds[0], &ret, sizeof(ret));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070091 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 ret = pid;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070093 } else {
94 if (n < 0) {
Jeff Dikea61f3342007-05-06 14:51:35 -070095 n = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080096 printk(UM_KERN_ERR "run_helper : read on pipe failed, "
97 "ret = %d\n", -n);
Jeff Dike6b7aaad2006-09-25 23:33:02 -070098 ret = n;
Jeff Dike6b7aaad2006-09-25 23:33:02 -070099 }
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800100 CATCH_EINTR(waitpid(pid, NULL, __WCLONE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -0800103out_free2:
104 kfree(data.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105out_close:
106 if (fds[1] != -1)
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800107 close(fds[1]);
108 close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109out_free:
Jeff Dikec4399012007-07-15 23:38:56 -0700110 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800114int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
Jeff Dikec4399012007-07-15 23:38:56 -0700115 unsigned long *stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 unsigned long stack, sp;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700118 int pid, status, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jeff Dikec4399012007-07-15 23:38:56 -0700120 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700121 if (stack == 0)
122 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jeff Dikec4399012007-07-15 23:38:56 -0700124 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800125 pid = clone(proc, (void *) sp, flags, arg);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700126 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700127 err = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -0800128 printk(UM_KERN_ERR "run_helper_thread : clone failed, "
129 "errno = %d\n", errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700132 if (stack_out == NULL) {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800133 CATCH_EINTR(pid = waitpid(pid, &status, __WCLONE));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700134 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700135 err = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -0800136 printk(UM_KERN_ERR "run_helper_thread - wait failed, "
137 "errno = %d\n", errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700138 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700140 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
Jeff Dike1aa351a2008-02-04 22:31:10 -0800141 printk(UM_KERN_ERR "run_helper_thread - thread "
142 "returned status 0x%x\n", status);
Jeff Dikec4399012007-07-15 23:38:56 -0700143 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700144 } else
145 *stack_out = stack;
146 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Jeff Dike1aa351a2008-02-04 22:31:10 -0800149int helper_wait(int pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800151 int ret, status;
152 int wflags = __WCLONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800154 CATCH_EINTR(ret = waitpid(pid, &status, wflags));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700155 if (ret < 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -0800156 printk(UM_KERN_ERR "helper_wait : waitpid process %d failed, "
157 "errno = %d\n", pid, errno);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800158 return -errno;
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800159 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -0800160 printk(UM_KERN_ERR "helper_wait : process %d exited with "
161 "status 0x%x\n", pid, status);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800162 return -ECHILD;
163 } else
164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}