Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 1 | /* |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <sched.h> |
Ingo Molnar | b6d8adf | 2008-06-05 22:46:14 -0700 | [diff] [blame] | 10 | #include <linux/limits.h> |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 11 | #include <sys/socket.h> |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 12 | #include <sys/wait.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 13 | #include <kern_util.h> |
| 14 | #include <os.h> |
| 15 | #include <um_malloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | struct helper_data { |
| 18 | void (*pre_exec)(void*); |
| 19 | void *pre_data; |
| 20 | char **argv; |
| 21 | int fd; |
Paolo 'Blaisorblade' Giarrusso | 5d48545 | 2006-11-25 11:09:39 -0800 | [diff] [blame] | 22 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | static int helper_child(void *arg) |
| 26 | { |
| 27 | struct helper_data *data = arg; |
| 28 | char **argv = data->argv; |
Vitaliy Ivanov | 2fdf213 | 2011-07-25 17:12:50 -0700 | [diff] [blame] | 29 | int err, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 31 | if (data->pre_exec != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | (*data->pre_exec)(data->pre_data); |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 33 | err = execvp_noalloc(data->buf, argv[0], argv); |
| 34 | |
| 35 | /* If the exec succeeds, we don't get here */ |
Vitaliy Ivanov | 2fdf213 | 2011-07-25 17:12:50 -0700 | [diff] [blame] | 36 | CATCH_EINTR(ret = write(data->fd, &err, sizeof(err))); |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 37 | |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 38 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 41 | /* Returns either the pid of the child process we run or -E* on failure. */ |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 42 | int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
| 44 | struct helper_data data; |
| 45 | unsigned long stack, sp; |
| 46 | int pid, fds[2], ret, n; |
| 47 | |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 48 | stack = alloc_stack(0, __cant_sleep()); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 49 | if (stack == 0) |
Jeff Dike | 6b7aaad | 2006-09-25 23:33:02 -0700 | [diff] [blame] | 50 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 52 | ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 53 | if (ret < 0) { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 54 | ret = -errno; |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 55 | printk(UM_KERN_ERR "run_helper : pipe failed, errno = %d\n", |
| 56 | errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | goto out_free; |
| 58 | } |
| 59 | |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 60 | ret = os_set_exec_close(fds[1]); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 61 | if (ret < 0) { |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 62 | printk(UM_KERN_ERR "run_helper : setting FD_CLOEXEC failed, " |
| 63 | "ret = %d\n", -ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | goto out_close; |
| 65 | } |
| 66 | |
Jeff Dike | 1ffb916 | 2007-05-06 14:51:22 -0700 | [diff] [blame] | 67 | sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | data.pre_exec = pre_exec; |
| 69 | data.pre_data = pre_data; |
| 70 | data.argv = argv; |
| 71 | data.fd = fds[1]; |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 72 | data.buf = __cant_sleep() ? uml_kmalloc(PATH_MAX, UM_GFP_ATOMIC) : |
| 73 | uml_kmalloc(PATH_MAX, UM_GFP_KERNEL); |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 74 | pid = clone(helper_child, (void *) sp, CLONE_VM, &data); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 75 | if (pid < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | ret = -errno; |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 77 | printk(UM_KERN_ERR "run_helper : clone failed, errno = %d\n", |
| 78 | errno); |
Paolo 'Blaisorblade' Giarrusso | 5d48545 | 2006-11-25 11:09:39 -0800 | [diff] [blame] | 79 | goto out_free2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 82 | close(fds[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | fds[1] = -1; |
| 84 | |
Jeff Dike | ef0470c | 2007-05-06 14:51:33 -0700 | [diff] [blame] | 85 | /* |
| 86 | * Read the errno value from the child, if the exec failed, or get 0 if |
| 87 | * the exec succeeded because the pipe fd was set as close-on-exec. |
| 88 | */ |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 89 | n = read(fds[0], &ret, sizeof(ret)); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 90 | if (n == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | ret = pid; |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 92 | } else { |
| 93 | if (n < 0) { |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 94 | n = -errno; |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 95 | printk(UM_KERN_ERR "run_helper : read on pipe failed, " |
| 96 | "ret = %d\n", -n); |
Jeff Dike | 6b7aaad | 2006-09-25 23:33:02 -0700 | [diff] [blame] | 97 | ret = n; |
Jeff Dike | 6b7aaad | 2006-09-25 23:33:02 -0700 | [diff] [blame] | 98 | } |
Richard Weinberger | 6b18733 | 2015-08-09 21:49:07 +0200 | [diff] [blame] | 99 | CATCH_EINTR(waitpid(pid, NULL, __WALL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Paolo 'Blaisorblade' Giarrusso | 5d48545 | 2006-11-25 11:09:39 -0800 | [diff] [blame] | 102 | out_free2: |
| 103 | kfree(data.buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | out_close: |
| 105 | if (fds[1] != -1) |
Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 106 | close(fds[1]); |
| 107 | close(fds[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | out_free: |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 109 | free_stack(stack, 0); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 110 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Jeff Dike | ff5c6ff | 2005-11-07 00:58:51 -0800 | [diff] [blame] | 113 | int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags, |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 114 | unsigned long *stack_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
| 116 | unsigned long stack, sp; |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 117 | int pid, status, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 119 | stack = alloc_stack(0, __cant_sleep()); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 120 | if (stack == 0) |
| 121 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 123 | sp = stack + UM_KERN_PAGE_SIZE - sizeof(void *); |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 124 | pid = clone(proc, (void *) sp, flags, arg); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 125 | if (pid < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 126 | err = -errno; |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 127 | printk(UM_KERN_ERR "run_helper_thread : clone failed, " |
| 128 | "errno = %d\n", errno); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 129 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 131 | if (stack_out == NULL) { |
Richard Weinberger | 6b18733 | 2015-08-09 21:49:07 +0200 | [diff] [blame] | 132 | CATCH_EINTR(pid = waitpid(pid, &status, __WALL)); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 133 | if (pid < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 134 | err = -errno; |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 135 | printk(UM_KERN_ERR "run_helper_thread - wait failed, " |
| 136 | "errno = %d\n", errno); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 137 | pid = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 139 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 140 | printk(UM_KERN_ERR "run_helper_thread - thread " |
| 141 | "returned status 0x%x\n", status); |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 142 | free_stack(stack, 0); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 143 | } else |
| 144 | *stack_out = stack; |
| 145 | return pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 148 | int helper_wait(int pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 150 | int ret, status; |
Richard Weinberger | 6b18733 | 2015-08-09 21:49:07 +0200 | [diff] [blame] | 151 | int wflags = __WALL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 153 | CATCH_EINTR(ret = waitpid(pid, &status, wflags)); |
Paolo 'Blaisorblade' Giarrusso | 8b028bc | 2006-10-19 23:28:21 -0700 | [diff] [blame] | 154 | if (ret < 0) { |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 155 | printk(UM_KERN_ERR "helper_wait : waitpid process %d failed, " |
| 156 | "errno = %d\n", pid, errno); |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 157 | return -errno; |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 158 | } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Jeff Dike | 1aa351a | 2008-02-04 22:31:10 -0800 | [diff] [blame] | 159 | printk(UM_KERN_ERR "helper_wait : process %d exited with " |
| 160 | "status 0x%x\n", pid, status); |
Stanislaw Gruszka | 4dbed85 | 2007-12-17 16:19:46 -0800 | [diff] [blame] | 161 | return -ECHILD; |
| 162 | } else |
| 163 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |