blob: 8a4c9e47326c62d9bb72f97574b2a21c36570ae5 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "os.h"
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080017#include "um_malloc.h"
Jeff Dike1ffb9162007-05-06 14:51:22 -070018#include "kern_constants.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;
32 int errval;
33
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);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080036 errval = execvp_noalloc(data->buf, argv[0], argv);
Jeff Dikeef0470c2007-05-06 14:51:33 -070037 printk("helper_child - execvp of '%s' failed - errno = %d\n", argv[0],
38 -errval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 os_write_file(data->fd, &errval, sizeof(errval));
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080040 kill(os_getpid(), SIGKILL);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44/* Returns either the pid of the child process we run or -E* on failure.
Paolo 'Blaisorblade' Giarrussod9d645f2006-10-19 23:28:24 -070045 * XXX The alloc_stack here breaks if this is called in the tracing thread, so
46 * we need to receive a preallocated stack (a local buffer is ok). */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
48 unsigned long *stack_out)
49{
50 struct helper_data data;
51 unsigned long stack, sp;
52 int pid, fds[2], ret, n;
53
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070054 if ((stack_out != NULL) && (*stack_out != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 stack = *stack_out;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070056 else
57 stack = alloc_stack(0, __cant_sleep());
58 if (stack == 0)
Jeff Dike6b7aaad2006-09-25 23:33:02 -070059 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 ret = os_pipe(fds, 1, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070062 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 printk("run_helper : pipe failed, ret = %d\n", -ret);
64 goto out_free;
65 }
66
67 ret = os_set_exec_close(fds[1], 1);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070068 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n",
70 -ret);
71 goto out_close;
72 }
73
Jeff Dike1ffb9162007-05-06 14:51:22 -070074 sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 data.pre_exec = pre_exec;
76 data.pre_data = pre_data;
77 data.argv = argv;
78 data.fd = fds[1];
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080079 data.buf = __cant_sleep() ? um_kmalloc_atomic(PATH_MAX) :
80 um_kmalloc(PATH_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070082 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ret = -errno;
Jeff Dikeb4fd3102005-09-16 19:27:49 -070084 printk("run_helper : clone failed, errno = %d\n", errno);
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -080085 goto out_free2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
Jeff Dikeff5c6ff2005-11-07 00:58:51 -080088 close(fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 fds[1] = -1;
90
Jeff Dikeef0470c2007-05-06 14:51:33 -070091 /*
92 * Read the errno value from the child, if the exec failed, or get 0 if
93 * the exec succeeded because the pipe fd was set as close-on-exec.
94 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 n = os_read_file(fds[0], &ret, sizeof(ret));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070096 if (n == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 ret = pid;
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -070098 } else {
99 if (n < 0) {
Jeff Dike6b7aaad2006-09-25 23:33:02 -0700100 printk("run_helper : read on pipe failed, ret = %d\n",
101 -n);
102 ret = n;
103 kill(pid, SIGKILL);
104 }
105 CATCH_EINTR(waitpid(pid, NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
Paolo 'Blaisorblade' Giarrusso5d485452006-11-25 11:09:39 -0800108out_free2:
109 kfree(data.buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110out_close:
111 if (fds[1] != -1)
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800112 close(fds[1]);
113 close(fds[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114out_free:
Paolo 'Blaisorblade' Giarrussod9d645f2006-10-19 23:28:24 -0700115 if ((stack_out == NULL) || (*stack_out == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 free_stack(stack, 0);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800120int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned long *stack_out, int stack_order)
122{
123 unsigned long stack, sp;
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700124 int pid, status, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Paolo 'Blaisorblade' Giarrussob6316292006-01-18 17:42:58 -0800126 stack = alloc_stack(stack_order, __cant_sleep());
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700127 if (stack == 0)
128 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jeff Dike1ffb9162007-05-06 14:51:22 -0700130 sp = stack + (UM_KERN_PAGE_SIZE << stack_order) - sizeof(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700132 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700133 err = -errno;
Jeff Dikeff5c6ff2005-11-07 00:58:51 -0800134 printk("run_helper_thread : clone failed, errno = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700136 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700138 if (stack_out == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 CATCH_EINTR(pid = waitpid(pid, &status, 0));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700140 if (pid < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700141 err = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 printk("run_helper_thread - wait failed, errno = %d\n",
143 errno);
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700144 pid = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700146 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 printk("run_helper_thread - thread returned status "
148 "0x%x\n", status);
149 free_stack(stack, stack_order);
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700150 } else
151 *stack_out = stack;
152 return pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Jeff Dike0d4579e2005-07-27 11:43:33 -0700155int helper_wait(int pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int ret;
158
159 CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700160 if (ret < 0) {
Jeff Dikeb4fd3102005-09-16 19:27:49 -0700161 ret = -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 printk("helper_wait : waitpid failed, errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Paolo 'Blaisorblade' Giarrusso8b028bc2006-10-19 23:28:21 -0700164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}