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