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 | |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame^] | 39 | #define ERROR(fmt, args...) \ |
| 40 | do { \ |
| 41 | fprintf(stderr, fmt, ## args); \ |
| 42 | ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \ |
| 43 | } while(0) |
| 44 | |
| 45 | #define FATAL_CHILD(fmt, args...) \ |
| 46 | do { \ |
| 47 | ERROR(fmt, ## args); \ |
| 48 | _exit(-1); \ |
| 49 | } while(0) |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 50 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 51 | static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid, |
| 52 | int *chld_sts) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 53 | int status = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 54 | char buffer[4096]; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 55 | struct pollfd poll_fds[] = { |
| 56 | [0] = { |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 57 | .fd = signal_fd, |
| 58 | .events = POLLIN, |
| 59 | }, |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 60 | [1] = { |
| 61 | .fd = parent_read, |
| 62 | .events = POLLIN, |
| 63 | }, |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 64 | }; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 65 | int rc = 0; |
| 66 | sigset_t chldset; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 67 | |
| 68 | int a = 0; // start index of unprocessed data |
| 69 | int b = 0; // end index of unprocessed data |
| 70 | int sz; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 71 | bool remote_hung = false; |
| 72 | bool found_child = false; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 73 | |
| 74 | char *btag = basename(tag); |
| 75 | if (!btag) btag = (char*) tag; |
| 76 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 77 | sigemptyset(&chldset); |
| 78 | sigaddset(&chldset, SIGCHLD); |
| 79 | sigprocmask(SIG_UNBLOCK, &chldset, NULL); |
| 80 | |
| 81 | while (!found_child) { |
| 82 | if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) { |
| 83 | if (errno == EINTR) |
| 84 | continue; |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame^] | 85 | ERROR("poll failed\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 86 | rc = -1; |
| 87 | goto err_poll; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 88 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 89 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 90 | if (!remote_hung) { |
| 91 | if (poll_fds[1].revents & POLLIN) { |
| 92 | sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 93 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 94 | sz += b; |
| 95 | // Log one line at a time |
| 96 | for (b = 0; b < sz; b++) { |
| 97 | if (buffer[b] == '\r') { |
| 98 | buffer[b] = '\0'; |
| 99 | } else if (buffer[b] == '\n') { |
| 100 | buffer[b] = '\0'; |
| 101 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
| 102 | a = b + 1; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (a == 0 && b == sizeof(buffer) - 1) { |
| 107 | // buffer is full, flush |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 108 | buffer[b] = '\0'; |
| 109 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 110 | b = 0; |
| 111 | } else if (a != b) { |
| 112 | // Keep left-overs |
| 113 | b -= a; |
| 114 | memmove(buffer, &buffer[a], b); |
| 115 | a = 0; |
| 116 | } else { |
| 117 | a = 0; |
| 118 | b = 0; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 122 | if (poll_fds[1].revents & POLLHUP) { |
| 123 | remote_hung = true; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 127 | if (poll_fds[0].revents & POLLIN) { |
| 128 | char tmp[32]; |
| 129 | int ret; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 130 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 131 | read(signal_fd, tmp, sizeof(tmp)); |
| 132 | while (!found_child) { |
| 133 | do { |
| 134 | ret = waitpid(-1, &status, WNOHANG); |
| 135 | } while (ret < 0 && errno == EINTR); |
| 136 | |
| 137 | if (ret <= 0) |
| 138 | break; |
| 139 | |
| 140 | found_child = (pid == ret); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 141 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 142 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 143 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 144 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 145 | // Flush remaining data |
| 146 | if (a != b) { |
| 147 | buffer[b] = '\0'; |
| 148 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
| 149 | } |
| 150 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 151 | if (WIFEXITED(status)) { |
| 152 | if (WEXITSTATUS(status)) |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 153 | ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag, |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 154 | WEXITSTATUS(status)); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 155 | } else if (WIFSIGNALED(status)) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 156 | ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag, |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 157 | WTERMSIG(status)); |
| 158 | } else if (WIFSTOPPED(status)) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 159 | ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag, |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 160 | WSTOPSIG(status)); |
| 161 | } |
| 162 | if (chld_sts != NULL) |
| 163 | *chld_sts = status; |
| 164 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 165 | err_poll: |
| 166 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 169 | static void child(int argc, char* argv[]) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 170 | // create null terminated argv_child array |
| 171 | char* argv_child[argc + 1]; |
| 172 | memcpy(argv_child, argv, argc * sizeof(char *)); |
| 173 | argv_child[argc] = NULL; |
| 174 | |
| 175 | if (execvp(argv_child[0], argv_child)) { |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame^] | 176 | FATAL_CHILD("executing %s failed: %s\n", argv_child[0], |
| 177 | strerror(errno)); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 181 | void sigchld_handler(int sig) { |
| 182 | write(signal_fd_write, &sig, 1); |
| 183 | } |
| 184 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 185 | int logwrap(int argc, char* argv[], int *status) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 186 | pid_t pid; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 187 | int parent_ptty; |
| 188 | int child_ptty; |
| 189 | char *child_devname = NULL; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 190 | struct sigaction chldact; |
| 191 | struct sigaction oldchldact; |
| 192 | sigset_t blockset; |
| 193 | sigset_t oldset; |
| 194 | int sockets[2]; |
| 195 | int rc = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 196 | |
| 197 | /* Use ptty instead of socketpair so that STDOUT is not buffered */ |
| 198 | parent_ptty = open("/dev/ptmx", O_RDWR); |
| 199 | if (parent_ptty < 0) { |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame^] | 200 | ERROR("Cannot create parent ptty\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 201 | rc = -1; |
| 202 | goto err_open; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if (grantpt(parent_ptty) || unlockpt(parent_ptty) || |
| 206 | ((child_devname = (char*)ptsname(parent_ptty)) == 0)) { |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame^] | 207 | ERROR("Problem with /dev/ptmx\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 208 | rc = -1; |
| 209 | goto err_ptty; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 212 | sigemptyset(&blockset); |
| 213 | sigaddset(&blockset, SIGCHLD); |
| 214 | sigprocmask(SIG_BLOCK, &blockset, &oldset); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 215 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 216 | pid = fork(); |
| 217 | if (pid < 0) { |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame^] | 218 | ERROR("Failed to fork\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 219 | rc = -1; |
| 220 | goto err_fork; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 221 | } else if (pid == 0) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 222 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 223 | close(parent_ptty); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 224 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 225 | child_ptty = open(child_devname, O_RDWR); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 226 | if (child_ptty < 0) { |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame^] | 227 | FATAL_CHILD("Problem with child ptty\n"); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // redirect stdout and stderr |
| 231 | dup2(child_ptty, 1); |
| 232 | dup2(child_ptty, 2); |
| 233 | close(child_ptty); |
| 234 | |
Rom Lemarchand | 611f5b4 | 2013-01-14 14:11:01 -0800 | [diff] [blame] | 235 | child(argc, argv); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 236 | } else { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 237 | memset(&chldact, 0, sizeof(chldact)); |
| 238 | chldact.sa_handler = sigchld_handler; |
| 239 | chldact.sa_flags = SA_NOCLDSTOP; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 240 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 241 | sigaction(SIGCHLD, &chldact, &oldchldact); |
| 242 | if ((!(oldchldact.sa_flags & SA_SIGINFO) && |
| 243 | oldchldact.sa_handler != SIG_DFL && |
| 244 | oldchldact.sa_handler != SIG_IGN) || |
| 245 | ((oldchldact.sa_flags & SA_SIGINFO) && |
| 246 | oldchldact.sa_sigaction != NULL)) { |
| 247 | ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD " |
| 248 | "handler and might cause interaction issues"); |
| 249 | } |
| 250 | |
| 251 | rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets); |
| 252 | if (rc == -1) { |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame^] | 253 | ERROR("socketpair failed: %s\n", strerror(errno)); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 254 | goto err_socketpair; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 257 | fcntl(sockets[0], F_SETFD, FD_CLOEXEC); |
| 258 | fcntl(sockets[0], F_SETFL, O_NONBLOCK); |
| 259 | fcntl(sockets[1], F_SETFD, FD_CLOEXEC); |
| 260 | fcntl(sockets[1], F_SETFL, O_NONBLOCK); |
| 261 | |
| 262 | signal_fd_write = sockets[0]; |
| 263 | |
Rom Lemarchand | 611f5b4 | 2013-01-14 14:11:01 -0800 | [diff] [blame] | 264 | rc = parent(argv[0], parent_ptty, sockets[1], pid, status); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 265 | } |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 266 | |
| 267 | close(sockets[0]); |
| 268 | close(sockets[1]); |
| 269 | err_socketpair: |
| 270 | sigaction(SIGCHLD, &oldchldact, NULL); |
| 271 | err_fork: |
| 272 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 273 | err_ptty: |
| 274 | close(parent_ptty); |
| 275 | err_open: |
| 276 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 277 | } |