Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <sys/types.h> |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 19 | #include <sys/socket.h> |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 20 | #include <signal.h> |
| 21 | #include <poll.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 22 | #include <sys/wait.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <errno.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <libgen.h> |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 29 | #include <stdbool.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 30 | |
| 31 | #include <logwrap/logwrap.h> |
| 32 | #include "private/android_filesystem_config.h" |
| 33 | #include "cutils/log.h" |
| 34 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 35 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
| 36 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 37 | static int signal_fd_write; |
| 38 | |
| 39 | static void fatal(const char *msg) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 40 | fprintf(stderr, "%s", msg); |
| 41 | ALOG(LOG_ERROR, "logwrapper", "%s", msg); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 44 | static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid, |
| 45 | int *chld_sts) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 46 | int status = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 47 | char buffer[4096]; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 48 | struct pollfd poll_fds[] = { |
| 49 | [0] = { |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 50 | .fd = signal_fd, |
| 51 | .events = POLLIN, |
| 52 | }, |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 53 | [1] = { |
| 54 | .fd = parent_read, |
| 55 | .events = POLLIN, |
| 56 | }, |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 57 | }; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 58 | int rc = 0; |
| 59 | sigset_t chldset; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 60 | |
| 61 | int a = 0; // start index of unprocessed data |
| 62 | int b = 0; // end index of unprocessed data |
| 63 | int sz; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 64 | bool remote_hung = false; |
| 65 | bool found_child = false; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 66 | |
| 67 | char *btag = basename(tag); |
| 68 | if (!btag) btag = (char*) tag; |
| 69 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 70 | sigemptyset(&chldset); |
| 71 | sigaddset(&chldset, SIGCHLD); |
| 72 | sigprocmask(SIG_UNBLOCK, &chldset, NULL); |
| 73 | |
| 74 | while (!found_child) { |
| 75 | if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) { |
| 76 | if (errno == EINTR) |
| 77 | continue; |
| 78 | fatal("poll failed\n"); |
| 79 | rc = -1; |
| 80 | goto err_poll; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 81 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 82 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 83 | if (!remote_hung) { |
| 84 | if (poll_fds[1].revents & POLLIN) { |
| 85 | sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 86 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 87 | sz += b; |
| 88 | // Log one line at a time |
| 89 | for (b = 0; b < sz; b++) { |
| 90 | if (buffer[b] == '\r') { |
| 91 | buffer[b] = '\0'; |
| 92 | } else if (buffer[b] == '\n') { |
| 93 | buffer[b] = '\0'; |
| 94 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
| 95 | a = b + 1; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (a == 0 && b == sizeof(buffer) - 1) { |
| 100 | // buffer is full, flush |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 101 | buffer[b] = '\0'; |
| 102 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 103 | b = 0; |
| 104 | } else if (a != b) { |
| 105 | // Keep left-overs |
| 106 | b -= a; |
| 107 | memmove(buffer, &buffer[a], b); |
| 108 | a = 0; |
| 109 | } else { |
| 110 | a = 0; |
| 111 | b = 0; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 115 | if (poll_fds[1].revents & POLLHUP) { |
| 116 | remote_hung = true; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 120 | if (poll_fds[0].revents & POLLIN) { |
| 121 | char tmp[32]; |
| 122 | int ret; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 123 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 124 | read(signal_fd, tmp, sizeof(tmp)); |
| 125 | while (!found_child) { |
| 126 | do { |
| 127 | ret = waitpid(-1, &status, WNOHANG); |
| 128 | } while (ret < 0 && errno == EINTR); |
| 129 | |
| 130 | if (ret <= 0) |
| 131 | break; |
| 132 | |
| 133 | found_child = (pid == ret); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 134 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 135 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 136 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 137 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 138 | // Flush remaining data |
| 139 | if (a != b) { |
| 140 | buffer[b] = '\0'; |
| 141 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
| 142 | } |
| 143 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 144 | if (WIFEXITED(status)) { |
| 145 | if (WEXITSTATUS(status)) |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 146 | ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag, |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 147 | WEXITSTATUS(status)); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 148 | } else if (WIFSIGNALED(status)) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 149 | ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag, |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 150 | WTERMSIG(status)); |
| 151 | } else if (WIFSTOPPED(status)) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 152 | ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag, |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 153 | WSTOPSIG(status)); |
| 154 | } |
| 155 | if (chld_sts != NULL) |
| 156 | *chld_sts = status; |
| 157 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 158 | err_poll: |
| 159 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 162 | static void child(int argc, char* argv[]) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 163 | // create null terminated argv_child array |
| 164 | char* argv_child[argc + 1]; |
| 165 | memcpy(argv_child, argv, argc * sizeof(char *)); |
| 166 | argv_child[argc] = NULL; |
| 167 | |
| 168 | if (execvp(argv_child[0], argv_child)) { |
| 169 | ALOG(LOG_ERROR, "logwrapper", |
| 170 | "executing %s failed: %s\n", argv_child[0], strerror(errno)); |
| 171 | exit(-1); |
| 172 | } |
| 173 | } |
| 174 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 175 | void sigchld_handler(int sig) { |
| 176 | write(signal_fd_write, &sig, 1); |
| 177 | } |
| 178 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 179 | int logwrap(int argc, char* argv[], int *status) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 180 | pid_t pid; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 181 | int parent_ptty; |
| 182 | int child_ptty; |
| 183 | char *child_devname = NULL; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 184 | struct sigaction chldact; |
| 185 | struct sigaction oldchldact; |
| 186 | sigset_t blockset; |
| 187 | sigset_t oldset; |
| 188 | int sockets[2]; |
| 189 | int rc = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 190 | |
| 191 | /* Use ptty instead of socketpair so that STDOUT is not buffered */ |
| 192 | parent_ptty = open("/dev/ptmx", O_RDWR); |
| 193 | if (parent_ptty < 0) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 194 | fatal("Cannot create parent ptty\n"); |
| 195 | rc = -1; |
| 196 | goto err_open; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (grantpt(parent_ptty) || unlockpt(parent_ptty) || |
| 200 | ((child_devname = (char*)ptsname(parent_ptty)) == 0)) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 201 | fatal("Problem with /dev/ptmx\n"); |
| 202 | rc = -1; |
| 203 | goto err_ptty; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 206 | sigemptyset(&blockset); |
| 207 | sigaddset(&blockset, SIGCHLD); |
| 208 | sigprocmask(SIG_BLOCK, &blockset, &oldset); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 209 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 210 | pid = fork(); |
| 211 | if (pid < 0) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 212 | fatal("Failed to fork\n"); |
| 213 | rc = -1; |
| 214 | goto err_fork; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 215 | } else if (pid == 0) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 216 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 217 | close(parent_ptty); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 218 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 219 | child_ptty = open(child_devname, O_RDWR); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 220 | if (child_ptty < 0) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 221 | fatal("Problem with child ptty\n"); |
| 222 | return -1; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // redirect stdout and stderr |
| 226 | dup2(child_ptty, 1); |
| 227 | dup2(child_ptty, 2); |
| 228 | close(child_ptty); |
| 229 | |
| 230 | child(argc - 1, &argv[1]); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 231 | fatal("This should never happen\n"); |
| 232 | return -1; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 233 | } else { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 234 | memset(&chldact, 0, sizeof(chldact)); |
| 235 | chldact.sa_handler = sigchld_handler; |
| 236 | chldact.sa_flags = SA_NOCLDSTOP; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 237 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 238 | sigaction(SIGCHLD, &chldact, &oldchldact); |
| 239 | if ((!(oldchldact.sa_flags & SA_SIGINFO) && |
| 240 | oldchldact.sa_handler != SIG_DFL && |
| 241 | oldchldact.sa_handler != SIG_IGN) || |
| 242 | ((oldchldact.sa_flags & SA_SIGINFO) && |
| 243 | oldchldact.sa_sigaction != NULL)) { |
| 244 | ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD " |
| 245 | "handler and might cause interaction issues"); |
| 246 | } |
| 247 | |
| 248 | rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets); |
| 249 | if (rc == -1) { |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 250 | char msg[40]; |
| 251 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 252 | snprintf(msg, sizeof(msg), "socketpair failed: %d\n", errno); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 253 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 254 | fatal(msg); |
| 255 | goto err_socketpair; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 258 | fcntl(sockets[0], F_SETFD, FD_CLOEXEC); |
| 259 | fcntl(sockets[0], F_SETFL, O_NONBLOCK); |
| 260 | fcntl(sockets[1], F_SETFD, FD_CLOEXEC); |
| 261 | fcntl(sockets[1], F_SETFL, O_NONBLOCK); |
| 262 | |
| 263 | signal_fd_write = sockets[0]; |
| 264 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 265 | // switch user and group to "log" |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 266 | // this may fail if we are not root, |
| 267 | // but in that case switching user/group is unnecessary |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 268 | setgid(AID_LOG); |
| 269 | setuid(AID_LOG); |
| 270 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 271 | rc = parent(argv[1], parent_ptty, sockets[1], pid, status); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 272 | } |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame^] | 273 | |
| 274 | close(sockets[0]); |
| 275 | close(sockets[1]); |
| 276 | err_socketpair: |
| 277 | sigaction(SIGCHLD, &oldchldact, NULL); |
| 278 | err_fork: |
| 279 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 280 | err_ptty: |
| 281 | close(parent_ptty); |
| 282 | err_open: |
| 283 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 284 | } |