blob: d13299cfa31878e6b725e3ad86d5ac9fdd50922d [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>
11#include <sys/signal.h>
12#include <sys/wait.h>
13#include "user.h"
14#include "kern_util.h"
15#include "user_util.h"
16#include "os.h"
17
18struct helper_data {
19 void (*pre_exec)(void*);
20 void *pre_data;
21 char **argv;
22 int fd;
23};
24
25/* Debugging aid, changed only from gdb */
26int helper_pause = 0;
27
28static void helper_hup(int sig)
29{
30}
31
32static int helper_child(void *arg)
33{
34 struct helper_data *data = arg;
35 char **argv = data->argv;
36 int errval;
37
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070038 if (helper_pause){
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 signal(SIGHUP, helper_hup);
40 pause();
41 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070042 if (data->pre_exec != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 (*data->pre_exec)(data->pre_data);
44 execvp(argv[0], argv);
Jeff Dike6b7aaad2006-09-25 23:33:02 -070045 errval = -errno;
Paolo 'Blaisorblade' Giarrussob1c332c2006-04-10 22:53:37 -070046 printk("helper_child - execve of '%s' failed - errno = %d\n", argv[0], errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 os_write_file(data->fd, &errval, sizeof(errval));
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080048 kill(os_getpid(), SIGKILL);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52/* Returns either the pid of the child process we run or -E* on failure.
Paolo 'Blaisorblade' Giarrussod9d645f2006-10-19 23:28:24 -070053 * XXX The alloc_stack here breaks if this is called in the tracing thread, so
54 * we need to receive a preallocated stack (a local buffer is ok). */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
56 unsigned long *stack_out)
57{
58 struct helper_data data;
59 unsigned long stack, sp;
60 int pid, fds[2], ret, n;
61
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070062 if ((stack_out != NULL) && (*stack_out != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 stack = *stack_out;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070064 else
65 stack = alloc_stack(0, __cant_sleep());
66 if (stack == 0)
Jeff Dike6b7aaad2006-09-25 23:33:02 -070067 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 ret = os_pipe(fds, 1, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070070 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 printk("run_helper : pipe failed, ret = %d\n", -ret);
72 goto out_free;
73 }
74
75 ret = os_set_exec_close(fds[1], 1);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070076 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
78 -ret);
79 goto out_close;
80 }
81
82 sp = stack + page_size() - sizeof(void *);
83 data.pre_exec = pre_exec;
84 data.pre_data = pre_data;
85 data.argv = argv;
86 data.fd = fds[1];
87 pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070088 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 ret = -errno;
Jeff Dikeb4fd3102005-09-16 19:27:49 -070090 printk("run_helper : clone failed, errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 goto out_close;
92 }
93
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080094 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 fds[1] = -1;
96
Paolo 'Blaisorblade' Giarrussob1c332c2006-04-10 22:53:37 -070097 /* Read the errno value from the child, if the exec failed, or get 0 if
98 * the exec succeeded because the pipe fd was set as close-on-exec. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 n = os_read_file(fds[0], &ret, sizeof(ret));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700100 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 ret = pid;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700102 } else {
103 if (n < 0) {
Jeff Dike6b7aaad2006-09-25 23:33:02 -0700104 printk("run_helper : read on pipe failed, ret = %d\n",
105 -n);
106 ret = n;
107 kill(pid, SIGKILL);
108 }
109 CATCH_EINTR(waitpid(pid, NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
112out_close:
113 if (fds[1] != -1)
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800114 close(fds[1]);
115 close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116out_free:
Paolo 'Blaisorblade' Giarrussod9d645f2006-10-19 23:28:24 -0700117 if ((stack_out == NULL) || (*stack_out == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700119 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800122int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned long *stack_out, int stack_order)
124{
125 unsigned long stack, sp;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700126 int pid, status, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800128 stack = alloc_stack(stack_order, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700129 if (stack == 0)
130 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 sp = stack + (page_size() << stack_order) - sizeof(void *);
133 pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700134 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700135 err = -errno;
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800136 printk("run_helper_thread : clone failed, errno = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700138 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700140 if (stack_out == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 CATCH_EINTR(pid = waitpid(pid, &status, 0));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700142 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700143 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 printk("run_helper_thread - wait failed, errno = %d\n",
145 errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700146 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700148 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 printk("run_helper_thread - thread returned status "
150 "0x%x\n", status);
151 free_stack(stack, stack_order);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700152 } else
153 *stack_out = stack;
154 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Jeff Dike0d4579e2005-07-27 11:43:33 -0700157int helper_wait(int pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 int ret;
160
161 CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700162 if (ret < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700163 ret = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 printk("helper_wait : waitpid failed, errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}