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