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