blob: c7ad6306e22f08e88f3867d6a522fbcdc47cfff0 [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>
14#include "user.h"
15#include "kern_util.h"
16#include "user_util.h"
17#include "os.h"
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080018#include "um_malloc.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
28/* Debugging aid, changed only from gdb */
29int helper_pause = 0;
30
31static void helper_hup(int sig)
32{
33}
34
35static int helper_child(void *arg)
36{
37 struct helper_data *data = arg;
38 char **argv = data->argv;
39 int errval;
40
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070041 if (helper_pause){
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 signal(SIGHUP, helper_hup);
43 pause();
44 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070045 if (data->pre_exec != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 (*data->pre_exec)(data->pre_data);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080047 errval = execvp_noalloc(data->buf, argv[0], argv);
48 printk("helper_child - execvp of '%s' failed - errno = %d\n", argv[0], -errval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 os_write_file(data->fd, &errval, sizeof(errval));
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080050 kill(os_getpid(), SIGKILL);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54/* Returns either the pid of the child process we run or -E* on failure.
Paolo 'Blaisorblade' Giarrussod9d645f2006-10-19 23:28:24 -070055 * XXX The alloc_stack here breaks if this is called in the tracing thread, so
56 * we need to receive a preallocated stack (a local buffer is ok). */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
58 unsigned long *stack_out)
59{
60 struct helper_data data;
61 unsigned long stack, sp;
62 int pid, fds[2], ret, n;
63
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070064 if ((stack_out != NULL) && (*stack_out != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 stack = *stack_out;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070066 else
67 stack = alloc_stack(0, __cant_sleep());
68 if (stack == 0)
Jeff Dike6b7aaad2006-09-25 23:33:02 -070069 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 ret = os_pipe(fds, 1, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070072 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 printk("run_helper : pipe failed, ret = %d\n", -ret);
74 goto out_free;
75 }
76
77 ret = os_set_exec_close(fds[1], 1);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070078 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
80 -ret);
81 goto out_close;
82 }
83
84 sp = stack + page_size() - sizeof(void *);
85 data.pre_exec = pre_exec;
86 data.pre_data = pre_data;
87 data.argv = argv;
88 data.fd = fds[1];
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080089 data.buf = __cant_sleep() ? um_kmalloc_atomic(PATH_MAX) :
90 um_kmalloc(PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070092 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 ret = -errno;
Jeff Dikeb4fd3102005-09-16 19:27:49 -070094 printk("run_helper : clone failed, errno = %d\n", errno);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080095 goto out_free2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080098 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 fds[1] = -1;
100
Paolo 'Blaisorblade' Giarrussob1c332c2006-04-10 22:53:37 -0700101 /* Read the errno value from the child, if the exec failed, or get 0 if
102 * the exec succeeded because the pipe fd was set as close-on-exec. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 n = os_read_file(fds[0], &ret, sizeof(ret));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700104 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 ret = pid;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700106 } else {
107 if (n < 0) {
Jeff Dike6b7aaad2006-09-25 23:33:02 -0700108 printk("run_helper : read on pipe failed, ret = %d\n",
109 -n);
110 ret = n;
111 kill(pid, SIGKILL);
112 }
113 CATCH_EINTR(waitpid(pid, NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -0800116out_free2:
117 kfree(data.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118out_close:
119 if (fds[1] != -1)
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800120 close(fds[1]);
121 close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122out_free:
Paolo 'Blaisorblade' Giarrussod9d645f2006-10-19 23:28:24 -0700123 if ((stack_out == NULL) || (*stack_out == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800128int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned long *stack_out, int stack_order)
130{
131 unsigned long stack, sp;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700132 int pid, status, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800134 stack = alloc_stack(stack_order, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700135 if (stack == 0)
136 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 sp = stack + (page_size() << stack_order) - sizeof(void *);
139 pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700140 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700141 err = -errno;
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800142 printk("run_helper_thread : clone failed, errno = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700144 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700146 if (stack_out == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 CATCH_EINTR(pid = waitpid(pid, &status, 0));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700148 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700149 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 printk("run_helper_thread - wait failed, errno = %d\n",
151 errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700152 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700154 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 printk("run_helper_thread - thread returned status "
156 "0x%x\n", status);
157 free_stack(stack, stack_order);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700158 } else
159 *stack_out = stack;
160 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Jeff Dike0d4579e2005-07-27 11:43:33 -0700163int helper_wait(int pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int ret;
166
167 CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700168 if (ret < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700169 ret = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 printk("helper_wait : waitpid failed, errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700172 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}