Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * 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 | |
| 18 | struct 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 */ |
| 26 | int helper_pause = 0; |
| 27 | |
| 28 | static void helper_hup(int sig) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | static int helper_child(void *arg) |
| 33 | { |
| 34 | struct helper_data *data = arg; |
| 35 | char **argv = data->argv; |
| 36 | int errval; |
| 37 | |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 38 | if (helper_pause){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | signal(SIGHUP, helper_hup); |
| 40 | pause(); |
| 41 | } |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 42 | if (data->pre_exec != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | (*data->pre_exec)(data->pre_data); |
| 44 | execvp(argv[0], argv); |
Jeff Dike | 6b7aaad | 2006-09-25 23:33:02 -0700 | [diff] [blame] | 45 | errval = -errno; |
Paolo 'Blaisorblade' Giarrusso | b1c332c | 2006-04-10 22:53:37 -0700 | [diff] [blame] | 46 | printk("helper_child - execve of '%s' failed - errno = %d\n", argv[0], errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | os_write_file(data->fd, &errval, sizeof(errval)); |
Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 48 | kill(os_getpid(), SIGKILL); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 49 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* Returns either the pid of the child process we run or -E* on failure. |
| 53 | * XXX The alloc_stack here breaks if this is called in the tracing thread */ |
| 54 | int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv, |
| 55 | unsigned long *stack_out) |
| 56 | { |
| 57 | struct helper_data data; |
| 58 | unsigned long stack, sp; |
| 59 | int pid, fds[2], ret, n; |
| 60 | |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 61 | if ((stack_out != NULL) && (*stack_out != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | stack = *stack_out; |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 63 | else |
| 64 | stack = alloc_stack(0, __cant_sleep()); |
| 65 | if (stack == 0) |
Jeff Dike | 6b7aaad | 2006-09-25 23:33:02 -0700 | [diff] [blame] | 66 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | ret = os_pipe(fds, 1, 0); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 69 | if (ret < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | printk("run_helper : pipe failed, ret = %d\n", -ret); |
| 71 | goto out_free; |
| 72 | } |
| 73 | |
| 74 | ret = os_set_exec_close(fds[1], 1); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 75 | if (ret < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | printk("run_helper : setting FD_CLOEXEC failed, ret = %d\n", |
| 77 | -ret); |
| 78 | goto out_close; |
| 79 | } |
| 80 | |
| 81 | sp = stack + page_size() - sizeof(void *); |
| 82 | data.pre_exec = pre_exec; |
| 83 | data.pre_data = pre_data; |
| 84 | data.argv = argv; |
| 85 | data.fd = fds[1]; |
| 86 | pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 87 | if (pid < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | ret = -errno; |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 89 | printk("run_helper : clone failed, errno = %d\n", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | goto out_close; |
| 91 | } |
| 92 | |
Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 93 | close(fds[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | fds[1] = -1; |
| 95 | |
Paolo 'Blaisorblade' Giarrusso | b1c332c | 2006-04-10 22:53:37 -0700 | [diff] [blame] | 96 | /* Read the errno value from the child, if the exec failed, or get 0 if |
| 97 | * the exec succeeded because the pipe fd was set as close-on-exec. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | n = os_read_file(fds[0], &ret, sizeof(ret)); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 99 | if (n == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | ret = pid; |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 101 | } else { |
| 102 | if (n < 0) { |
Jeff Dike | 6b7aaad | 2006-09-25 23:33:02 -0700 | [diff] [blame] | 103 | printk("run_helper : read on pipe failed, ret = %d\n", |
| 104 | -n); |
| 105 | ret = n; |
| 106 | kill(pid, SIGKILL); |
| 107 | } |
| 108 | CATCH_EINTR(waitpid(pid, NULL, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | out_close: |
| 112 | if (fds[1] != -1) |
Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 113 | close(fds[1]); |
| 114 | close(fds[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | out_free: |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 116 | if (stack_out == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | free_stack(stack, 0); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 118 | else |
| 119 | *stack_out = stack; |
| 120 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 123 | int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | unsigned long *stack_out, int stack_order) |
| 125 | { |
| 126 | unsigned long stack, sp; |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 127 | int pid, status, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Paolo 'Blaisorblade' Giarrusso | b631629 | 2006-01-18 17:42:58 -0800 | [diff] [blame] | 129 | stack = alloc_stack(stack_order, __cant_sleep()); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 130 | if (stack == 0) |
| 131 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | sp = stack + (page_size() << stack_order) - sizeof(void *); |
| 134 | pid = clone(proc, (void *) sp, flags | SIGCHLD, arg); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 135 | if (pid < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 136 | err = -errno; |
Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 137 | printk("run_helper_thread : clone failed, errno = %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | errno); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 139 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 141 | if (stack_out == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | CATCH_EINTR(pid = waitpid(pid, &status, 0)); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 143 | if (pid < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 144 | err = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | printk("run_helper_thread - wait failed, errno = %d\n", |
| 146 | errno); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 147 | pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 149 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | printk("run_helper_thread - thread returned status " |
| 151 | "0x%x\n", status); |
| 152 | free_stack(stack, stack_order); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 153 | } else |
| 154 | *stack_out = stack; |
| 155 | return pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Jeff Dike | 0d4579e | 2005-07-27 11:43:33 -0700 | [diff] [blame] | 158 | int helper_wait(int pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | int ret; |
| 161 | |
| 162 | CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG)); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 163 | if (ret < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 164 | ret = -errno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | printk("helper_wait : waitpid failed, errno = %d\n", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame^] | 167 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |