blob: b6b1096152aac3f2e98b2da44aa3ea46a02cf4ba [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19struct helper_data {
20 void (*pre_exec)(void*);
21 void *pre_data;
22 char **argv;
23 int fd;
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080024 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int helper_child(void *arg)
28{
29 struct helper_data *data = arg;
30 char **argv = data->argv;
Jeff Dike1aa351a2008-02-04 22:31:10 -080031 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070033 if (data->pre_exec != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 (*data->pre_exec)(data->pre_data);
Jeff Dike1aa351a2008-02-04 22:31:10 -080035 err = execvp_noalloc(data->buf, argv[0], argv);
36
37 /* If the exec succeeds, we don't get here */
38 write(data->fd, &err, sizeof(err));
39
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Jeff Dike1aa351a2008-02-04 22:31:10 -080043/* Returns either the pid of the child process we run or -E* on failure. */
Jeff Dikec4399012007-07-15 23:38:56 -070044int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct helper_data data;
47 unsigned long stack, sp;
48 int pid, fds[2], ret, n;
49
Jeff Dikec4399012007-07-15 23:38:56 -070050 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070051 if (stack == 0)
Jeff Dike6b7aaad2006-09-25 23:33:02 -070052 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jeff Dike512b6fb2007-10-16 01:27:11 -070054 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070055 if (ret < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -070056 ret = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080057 printk(UM_KERN_ERR "run_helper : pipe failed, errno = %d\n",
58 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 goto out_free;
60 }
61
Jeff Dike512b6fb2007-10-16 01:27:11 -070062 ret = os_set_exec_close(fds[1]);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070063 if (ret < 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -080064 printk(UM_KERN_ERR "run_helper : setting FD_CLOEXEC failed, "
65 "ret = %d\n", -ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 goto out_close;
67 }
68
Jeff Dike1ffb9162007-05-06 14:51:22 -070069 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 data.pre_exec = pre_exec;
71 data.pre_data = pre_data;
72 data.argv = argv;
73 data.fd = fds[1];
Jeff Dike43f5b302008-05-12 14:01:52 -070074 data.buf = __cant_sleep() ? uml_kmalloc(PATH_MAX, UM_GFP_ATOMIC) :
75 uml_kmalloc(PATH_MAX, UM_GFP_KERNEL);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -080076 pid = clone(helper_child, (void *) sp, CLONE_VM, &data);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070077 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 ret = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080079 printk(UM_KERN_ERR "run_helper : clone failed, errno = %d\n",
80 errno);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080081 goto out_free2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080084 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 fds[1] = -1;
86
Jeff Dikeef0470c2007-05-06 14:51:33 -070087 /*
88 * Read the errno value from the child, if the exec failed, or get 0 if
89 * the exec succeeded because the pipe fd was set as close-on-exec.
90 */
Jeff Dikea61f3342007-05-06 14:51:35 -070091 n = read(fds[0], &ret, sizeof(ret));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070092 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 ret = pid;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070094 } else {
95 if (n < 0) {
Jeff Dikea61f3342007-05-06 14:51:35 -070096 n = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -080097 printk(UM_KERN_ERR "run_helper : read on pipe failed, "
98 "ret = %d\n", -n);
Jeff Dike6b7aaad2006-09-25 23:33:02 -070099 ret = n;
Jeff Dike6b7aaad2006-09-25 23:33:02 -0700100 }
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800101 CATCH_EINTR(waitpid(pid, NULL, __WCLONE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -0800104out_free2:
105 kfree(data.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106out_close:
107 if (fds[1] != -1)
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800108 close(fds[1]);
109 close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110out_free:
Jeff Dikec4399012007-07-15 23:38:56 -0700111 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800115int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
Jeff Dikec4399012007-07-15 23:38:56 -0700116 unsigned long *stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 unsigned long stack, sp;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700119 int pid, status, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Jeff Dikec4399012007-07-15 23:38:56 -0700121 stack = alloc_stack(0, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700122 if (stack == 0)
123 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Jeff Dikec4399012007-07-15 23:38:56 -0700125 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800126 pid = clone(proc, (void *) sp, flags, arg);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700127 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700128 err = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -0800129 printk(UM_KERN_ERR "run_helper_thread : clone failed, "
130 "errno = %d\n", errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700131 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700133 if (stack_out == NULL) {
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800134 CATCH_EINTR(pid = waitpid(pid, &status, __WCLONE));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700135 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700136 err = -errno;
Jeff Dike1aa351a2008-02-04 22:31:10 -0800137 printk(UM_KERN_ERR "run_helper_thread - wait failed, "
138 "errno = %d\n", errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700139 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700141 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
Jeff Dike1aa351a2008-02-04 22:31:10 -0800142 printk(UM_KERN_ERR "run_helper_thread - thread "
143 "returned status 0x%x\n", status);
Jeff Dikec4399012007-07-15 23:38:56 -0700144 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700145 } else
146 *stack_out = stack;
147 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Jeff Dike1aa351a2008-02-04 22:31:10 -0800150int helper_wait(int pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800152 int ret, status;
153 int wflags = __WCLONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800155 CATCH_EINTR(ret = waitpid(pid, &status, wflags));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700156 if (ret < 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -0800157 printk(UM_KERN_ERR "helper_wait : waitpid process %d failed, "
158 "errno = %d\n", pid, errno);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800159 return -errno;
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800160 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Jeff Dike1aa351a2008-02-04 22:31:10 -0800161 printk(UM_KERN_ERR "helper_wait : process %d exited with "
162 "status 0x%x\n", pid, status);
Stanislaw Gruszka4dbed852007-12-17 16:19:46 -0800163 return -ECHILD;
164 } else
165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}