blob: cf26c4a9a43a7c38549a2bcd76564eb554c32409 [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>
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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17struct helper_data {
18 void (*pre_exec)(void*);
19 void *pre_data;
20 char **argv;
21 int fd;
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080022 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023};
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static int helper_child(void *arg)
26{
27 struct helper_data *data = arg;
28 char **argv = data->argv;
Vitaliy Ivanov2fdf2132011-07-25 17:12:50 -070029 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070031 if (data->pre_exec != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 (*data->pre_exec)(data->pre_data);
Jeff Dike1aa351a2008-02-04 22:31:10 -080033 err = execvp_noalloc(data->buf, argv[0], argv);
34
35 /* If the exec succeeds, we don't get here */
Vitaliy Ivanov2fdf2132011-07-25 17:12:50 -070036 CATCH_EINTR(ret = write(data->fd, &err, sizeof(err)));
Jeff Dike1aa351a2008-02-04 22:31:10 -080037
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Jeff Dike1aa351a2008-02-04 22:31:10 -080041/* Returns either the pid of the child process we run or -E* on failure. */
Jeff Dikec4399012007-07-15 23:38:56 -070042int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 struct helper_data data;
45 unsigned long stack, sp;
46 int pid, fds[2], ret, n;
47
Jeff Dikec4399012007-07-15 23:38:56 -070048 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070049 if (stack == 0)
Jeff Dike6b7aaad2006-09-25 23:33:02 -070050 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Jeff Dike512b6fb2007-10-16 01:27:11 -070052 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070053 if (ret < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -070054 ret = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080055 printk(UM_KERN_ERR "run_helper : pipe failed, errno = %d\n",
56 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 goto out_free;
58 }
59
Jeff Dike512b6fb2007-10-16 01:27:11 -070060 ret = os_set_exec_close(fds[1]);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070061 if (ret < 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -080062 printk(UM_KERN_ERR "run_helper : setting FD_CLOEXEC failed, "
63 "ret = %d\n", -ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 goto out_close;
65 }
66
Jeff Dike1ffb9162007-05-06 14:51:22 -070067 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 data.pre_exec = pre_exec;
69 data.pre_data = pre_data;
70 data.argv = argv;
71 data.fd = fds[1];
Jeff Dike43f5b302008-05-12 14:01:52 -070072 data.buf = __cant_sleep() ? uml_kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
73 uml_kmalloc(PATH_MAX, UM_GFP_KERNEL);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080074 pid = clone(helper_child, (void *) sp, CLONE_VM, &data);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070075 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 ret = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080077 printk(UM_KERN_ERR "run_helper : clone failed, errno = %d\n",
78 errno);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080079 goto out_free2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080082 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 fds[1] = -1;
84
Jeff Dikeef0470c2007-05-06 14:51:33 -070085 /*
86 * Read the errno value from the child, if the exec failed, or get 0 if
87 * the exec succeeded because the pipe fd was set as close-on-exec.
88 */
Jeff Dikea61f3342007-05-06 14:51:35 -070089 n = read(fds[0], &ret, sizeof(ret));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070090 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 ret = pid;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070092 } else {
93 if (n < 0) {
Jeff Dikea61f3342007-05-06 14:51:35 -070094 n = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080095 printk(UM_KERN_ERR "run_helper : read on pipe failed, "
96 "ret = %d\n", -n);
Jeff Dike6b7aaad2006-09-25 23:33:02 -070097 ret = n;
Jeff Dike6b7aaad2006-09-25 23:33:02 -070098 }
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080099 CATCH_EINTR(waitpid(pid, NULL, __WCLONE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -0800102out_free2:
103 kfree(data.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104out_close:
105 if (fds[1] != -1)
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800106 close(fds[1]);
107 close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108out_free:
Jeff Dikec4399012007-07-15 23:38:56 -0700109 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800113int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
Jeff Dikec4399012007-07-15 23:38:56 -0700114 unsigned long *stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 unsigned long stack, sp;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700117 int pid, status, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jeff Dikec4399012007-07-15 23:38:56 -0700119 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700120 if (stack == 0)
121 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jeff Dikec4399012007-07-15 23:38:56 -0700123 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800124 pid = clone(proc, (void *) sp, flags, arg);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700125 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700126 err = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -0800127 printk(UM_KERN_ERR "run_helper_thread : clone failed, "
128 "errno = %d\n", errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700131 if (stack_out == NULL) {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800132 CATCH_EINTR(pid = waitpid(pid, &status, __WCLONE));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700133 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700134 err = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -0800135 printk(UM_KERN_ERR "run_helper_thread - wait failed, "
136 "errno = %d\n", errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700137 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700139 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
Jeff Dike1aa351a2008-02-04 22:31:10 -0800140 printk(UM_KERN_ERR "run_helper_thread - thread "
141 "returned status 0x%x\n", status);
Jeff Dikec4399012007-07-15 23:38:56 -0700142 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700143 } else
144 *stack_out = stack;
145 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Jeff Dike1aa351a2008-02-04 22:31:10 -0800148int helper_wait(int pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800150 int ret, status;
151 int wflags = __WCLONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800153 CATCH_EINTR(ret = waitpid(pid, &status, wflags));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700154 if (ret < 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -0800155 printk(UM_KERN_ERR "helper_wait : waitpid process %d failed, "
156 "errno = %d\n", pid, errno);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800157 return -errno;
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800158 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -0800159 printk(UM_KERN_ERR "helper_wait : process %d exited with "
160 "status 0x%x\n", pid, status);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800161 return -ECHILD;
162 } else
163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}