blob: 30860b89ec582b5b7daa0ebe04fa4002aa4ccb70 [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>
Ingo Molnarb6d8adf2008-06-05 22:46:14 -070010#include <linux/limits.h>
Jeff Dike512b6fb2007-10-16 01:27:11 -070011#include <sys/socket.h>
Jeff Dike1aa351a2008-02-04 22:31:10 -080012#include <sys/wait.h>
13#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "kern_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "os.h"
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080016#include "um_malloc.h"
Jeff Dike1aa351a2008-02-04 22:31:10 -080017#include "user.h"
Ingo Molnar297e1b22008-04-26 18:59:42 +020018#include <linux/limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20struct helper_data {
21 void (*pre_exec)(void*);
22 void *pre_data;
23 char **argv;
24 int fd;
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080025 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int helper_child(void *arg)
29{
30 struct helper_data *data = arg;
31 char **argv = data->argv;
Jeff Dike1aa351a2008-02-04 22:31:10 -080032 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070034 if (data->pre_exec != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 (*data->pre_exec)(data->pre_data);
Jeff Dike1aa351a2008-02-04 22:31:10 -080036 err = execvp_noalloc(data->buf, argv[0], argv);
37
38 /* If the exec succeeds, we don't get here */
39 write(data->fd, &err, sizeof(err));
40
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Jeff Dike1aa351a2008-02-04 22:31:10 -080044/* Returns either the pid of the child process we run or -E* on failure. */
Jeff Dikec4399012007-07-15 23:38:56 -070045int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct helper_data data;
48 unsigned long stack, sp;
49 int pid, fds[2], ret, n;
50
Jeff Dikec4399012007-07-15 23:38:56 -070051 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070052 if (stack == 0)
Jeff Dike6b7aaad2006-09-25 23:33:02 -070053 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jeff Dike512b6fb2007-10-16 01:27:11 -070055 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070056 if (ret < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -070057 ret = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080058 printk(UM_KERN_ERR "run_helper : pipe failed, errno = %d\n",
59 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 goto out_free;
61 }
62
Jeff Dike512b6fb2007-10-16 01:27:11 -070063 ret = os_set_exec_close(fds[1]);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070064 if (ret < 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -080065 printk(UM_KERN_ERR "run_helper : setting FD_CLOEXEC failed, "
66 "ret = %d\n", -ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 goto out_close;
68 }
69
Jeff Dike1ffb9162007-05-06 14:51:22 -070070 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 data.pre_exec = pre_exec;
72 data.pre_data = pre_data;
73 data.argv = argv;
74 data.fd = fds[1];
Jeff Dike43f5b302008-05-12 14:01:52 -070075 data.buf = __cant_sleep() ? uml_kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
76 uml_kmalloc(PATH_MAX, UM_GFP_KERNEL);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080077 pid = clone(helper_child, (void *) sp, CLONE_VM, &data);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070078 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 ret = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080080 printk(UM_KERN_ERR "run_helper : clone failed, errno = %d\n",
81 errno);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080082 goto out_free2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080085 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 fds[1] = -1;
87
Jeff Dikeef0470c2007-05-06 14:51:33 -070088 /*
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.
91 */
Jeff Dikea61f3342007-05-06 14:51:35 -070092 n = read(fds[0], &ret, sizeof(ret));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070093 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 ret = pid;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070095 } else {
96 if (n < 0) {
Jeff Dikea61f3342007-05-06 14:51:35 -070097 n = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080098 printk(UM_KERN_ERR "run_helper : read on pipe failed, "
99 "ret = %d\n", -n);
Jeff Dike6b7aaad2006-09-25 23:33:02 -0700100 ret = n;
Jeff Dike6b7aaad2006-09-25 23:33:02 -0700101 }
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800102 CATCH_EINTR(waitpid(pid, NULL, __WCLONE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -0800105out_free2:
106 kfree(data.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107out_close:
108 if (fds[1] != -1)
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800109 close(fds[1]);
110 close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111out_free:
Jeff Dikec4399012007-07-15 23:38:56 -0700112 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800116int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
Jeff Dikec4399012007-07-15 23:38:56 -0700117 unsigned long *stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 unsigned long stack, sp;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700120 int pid, status, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jeff Dikec4399012007-07-15 23:38:56 -0700122 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700123 if (stack == 0)
124 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jeff Dikec4399012007-07-15 23:38:56 -0700126 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800127 pid = clone(proc, (void *) sp, flags, arg);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700128 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700129 err = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -0800130 printk(UM_KERN_ERR "run_helper_thread : clone failed, "
131 "errno = %d\n", errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700132 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700134 if (stack_out == NULL) {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800135 CATCH_EINTR(pid = waitpid(pid, &status, __WCLONE));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700136 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700137 err = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -0800138 printk(UM_KERN_ERR "run_helper_thread - wait failed, "
139 "errno = %d\n", errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700140 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700142 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
Jeff Dike1aa351a2008-02-04 22:31:10 -0800143 printk(UM_KERN_ERR "run_helper_thread - thread "
144 "returned status 0x%x\n", status);
Jeff Dikec4399012007-07-15 23:38:56 -0700145 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700146 } else
147 *stack_out = stack;
148 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Jeff Dike1aa351a2008-02-04 22:31:10 -0800151int helper_wait(int pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800153 int ret, status;
154 int wflags = __WCLONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800156 CATCH_EINTR(ret = waitpid(pid, &status, wflags));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700157 if (ret < 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -0800158 printk(UM_KERN_ERR "helper_wait : waitpid process %d failed, "
159 "errno = %d\n", pid, errno);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800160 return -errno;
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800161 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -0800162 printk(UM_KERN_ERR "helper_wait : process %d exited with "
163 "status 0x%x\n", pid, status);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800164 return -ECHILD;
165 } else
166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}