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 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <libgen.h> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 20 | #include <poll.h> |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 21 | #include <pthread.h> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 22 | #include <stdbool.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/wait.h> |
| 29 | #include <unistd.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 30 | |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 31 | #include <cutils/klog.h> |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 32 | #include <log/log.h> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 33 | #include <logwrap/logwrap.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 34 | |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 35 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 36 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 37 | |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 38 | static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER; |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 39 | // Protected by fd_mutex. These signals must be blocked while modifying as well. |
| 40 | static pid_t child_pid; |
| 41 | static struct sigaction old_int; |
| 42 | static struct sigaction old_quit; |
| 43 | static struct sigaction old_hup; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 44 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 45 | #define ERROR(fmt, args...) \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 46 | do { \ |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 47 | fprintf(stderr, fmt, ## args); \ |
| 48 | ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 49 | } while(0) |
| 50 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 51 | #define FATAL_CHILD(fmt, args...) \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 52 | do { \ |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 53 | ERROR(fmt, ## args); \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 54 | _exit(-1); \ |
| 55 | } while(0) |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 56 | |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 57 | #define MAX_KLOG_TAG 16 |
| 58 | |
| 59 | /* This is a simple buffer that holds up to the first beginning_buf->buf_size |
| 60 | * bytes of output from a command. |
| 61 | */ |
| 62 | #define BEGINNING_BUF_SIZE 0x1000 |
| 63 | struct beginning_buf { |
| 64 | char *buf; |
| 65 | size_t alloc_len; |
| 66 | /* buf_size is the usable space, which is one less than the allocated size */ |
| 67 | size_t buf_size; |
| 68 | size_t used_len; |
| 69 | }; |
| 70 | |
| 71 | /* This is a circular buf that holds up to the last ending_buf->buf_size bytes |
| 72 | * of output from a command after the first beginning_buf->buf_size bytes |
| 73 | * (which are held in beginning_buf above). |
| 74 | */ |
| 75 | #define ENDING_BUF_SIZE 0x1000 |
| 76 | struct ending_buf { |
| 77 | char *buf; |
| 78 | ssize_t alloc_len; |
| 79 | /* buf_size is the usable space, which is one less than the allocated size */ |
| 80 | ssize_t buf_size; |
| 81 | ssize_t used_len; |
| 82 | /* read and write offsets into the circular buffer */ |
| 83 | int read; |
| 84 | int write; |
| 85 | }; |
| 86 | |
| 87 | /* A structure to hold all the abbreviated buf data */ |
| 88 | struct abbr_buf { |
| 89 | struct beginning_buf b_buf; |
| 90 | struct ending_buf e_buf; |
| 91 | int beginning_buf_full; |
| 92 | }; |
| 93 | |
| 94 | /* Collect all the various bits of info needed for logging in one place. */ |
| 95 | struct log_info { |
| 96 | int log_target; |
| 97 | char klog_fmt[MAX_KLOG_TAG * 2]; |
| 98 | char *btag; |
| 99 | bool abbreviated; |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 100 | FILE *fp; |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 101 | struct abbr_buf a_buf; |
| 102 | }; |
| 103 | |
| 104 | /* Forware declaration */ |
| 105 | static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen); |
| 106 | |
| 107 | /* Return 0 on success, and 1 when full */ |
| 108 | static int add_line_to_linear_buf(struct beginning_buf *b_buf, |
| 109 | char *line, ssize_t line_len) |
| 110 | { |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 111 | int full = 0; |
| 112 | |
| 113 | if ((line_len + b_buf->used_len) > b_buf->buf_size) { |
| 114 | full = 1; |
| 115 | } else { |
| 116 | /* Add to the end of the buf */ |
| 117 | memcpy(b_buf->buf + b_buf->used_len, line, line_len); |
| 118 | b_buf->used_len += line_len; |
| 119 | } |
| 120 | |
| 121 | return full; |
| 122 | } |
| 123 | |
| 124 | static void add_line_to_circular_buf(struct ending_buf *e_buf, |
| 125 | char *line, ssize_t line_len) |
| 126 | { |
| 127 | ssize_t free_len; |
| 128 | ssize_t needed_space; |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 129 | int cnt; |
| 130 | |
| 131 | if (e_buf->buf == NULL) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | if (line_len > e_buf->buf_size) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | free_len = e_buf->buf_size - e_buf->used_len; |
| 140 | |
| 141 | if (line_len > free_len) { |
| 142 | /* remove oldest entries at read, and move read to make |
| 143 | * room for the new string */ |
| 144 | needed_space = line_len - free_len; |
| 145 | e_buf->read = (e_buf->read + needed_space) % e_buf->buf_size; |
| 146 | e_buf->used_len -= needed_space; |
| 147 | } |
| 148 | |
| 149 | /* Copy the line into the circular buffer, dealing with possible |
| 150 | * wraparound. |
| 151 | */ |
| 152 | cnt = MIN(line_len, e_buf->buf_size - e_buf->write); |
| 153 | memcpy(e_buf->buf + e_buf->write, line, cnt); |
| 154 | if (cnt < line_len) { |
| 155 | memcpy(e_buf->buf, line + cnt, line_len - cnt); |
| 156 | } |
| 157 | e_buf->used_len += line_len; |
| 158 | e_buf->write = (e_buf->write + line_len) % e_buf->buf_size; |
| 159 | } |
| 160 | |
| 161 | /* Log directly to the specified log */ |
| 162 | static void do_log_line(struct log_info *log_info, char *line) { |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 163 | if (log_info->log_target & LOG_KLOG) { |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 164 | klog_write(6, log_info->klog_fmt, line); |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 165 | } |
| 166 | if (log_info->log_target & LOG_ALOG) { |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 167 | ALOG(LOG_INFO, log_info->btag, "%s", line); |
| 168 | } |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 169 | if (log_info->log_target & LOG_FILE) { |
| 170 | fprintf(log_info->fp, "%s\n", line); |
| 171 | } |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* Log to either the abbreviated buf, or directly to the specified log |
| 175 | * via do_log_line() above. |
| 176 | */ |
| 177 | static void log_line(struct log_info *log_info, char *line, int len) { |
| 178 | if (log_info->abbreviated) { |
| 179 | add_line_to_abbr_buf(&log_info->a_buf, line, len); |
| 180 | } else { |
| 181 | do_log_line(log_info, line); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * The kernel will take a maximum of 1024 bytes in any single write to |
| 187 | * the kernel logging device file, so find and print each line one at |
| 188 | * a time. The allocated size for buf should be at least 1 byte larger |
| 189 | * than buf_size (the usable size of the buffer) to make sure there is |
| 190 | * room to temporarily stuff a null byte to terminate a line for logging. |
| 191 | */ |
| 192 | static void print_buf_lines(struct log_info *log_info, char *buf, int buf_size) |
| 193 | { |
| 194 | char *line_start; |
| 195 | char c; |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 196 | int i; |
| 197 | |
| 198 | line_start = buf; |
| 199 | for (i = 0; i < buf_size; i++) { |
| 200 | if (*(buf + i) == '\n') { |
| 201 | /* Found a line ending, print the line and compute new line_start */ |
| 202 | /* Save the next char and replace with \0 */ |
| 203 | c = *(buf + i + 1); |
| 204 | *(buf + i + 1) = '\0'; |
| 205 | do_log_line(log_info, line_start); |
| 206 | /* Restore the saved char */ |
| 207 | *(buf + i + 1) = c; |
| 208 | line_start = buf + i + 1; |
| 209 | } else if (*(buf + i) == '\0') { |
| 210 | /* The end of the buffer, print the last bit */ |
| 211 | do_log_line(log_info, line_start); |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | /* If the buffer was completely full, and didn't end with a newline, just |
| 216 | * ignore the partial last line. |
| 217 | */ |
| 218 | } |
| 219 | |
| 220 | static void init_abbr_buf(struct abbr_buf *a_buf) { |
| 221 | char *new_buf; |
| 222 | |
| 223 | memset(a_buf, 0, sizeof(struct abbr_buf)); |
| 224 | new_buf = malloc(BEGINNING_BUF_SIZE); |
| 225 | if (new_buf) { |
| 226 | a_buf->b_buf.buf = new_buf; |
| 227 | a_buf->b_buf.alloc_len = BEGINNING_BUF_SIZE; |
| 228 | a_buf->b_buf.buf_size = BEGINNING_BUF_SIZE - 1; |
| 229 | } |
| 230 | new_buf = malloc(ENDING_BUF_SIZE); |
| 231 | if (new_buf) { |
| 232 | a_buf->e_buf.buf = new_buf; |
| 233 | a_buf->e_buf.alloc_len = ENDING_BUF_SIZE; |
| 234 | a_buf->e_buf.buf_size = ENDING_BUF_SIZE - 1; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static void free_abbr_buf(struct abbr_buf *a_buf) { |
| 239 | free(a_buf->b_buf.buf); |
| 240 | free(a_buf->e_buf.buf); |
| 241 | } |
| 242 | |
| 243 | static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen) { |
| 244 | if (!a_buf->beginning_buf_full) { |
| 245 | a_buf->beginning_buf_full = |
| 246 | add_line_to_linear_buf(&a_buf->b_buf, linebuf, linelen); |
| 247 | } |
| 248 | if (a_buf->beginning_buf_full) { |
| 249 | add_line_to_circular_buf(&a_buf->e_buf, linebuf, linelen); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | static void print_abbr_buf(struct log_info *log_info) { |
| 254 | struct abbr_buf *a_buf = &log_info->a_buf; |
| 255 | |
| 256 | /* Add the abbreviated output to the kernel log */ |
| 257 | if (a_buf->b_buf.alloc_len) { |
| 258 | print_buf_lines(log_info, a_buf->b_buf.buf, a_buf->b_buf.used_len); |
| 259 | } |
| 260 | |
| 261 | /* Print an ellipsis to indicate that the buffer has wrapped or |
| 262 | * is full, and some data was not logged. |
| 263 | */ |
| 264 | if (a_buf->e_buf.used_len == a_buf->e_buf.buf_size) { |
| 265 | do_log_line(log_info, "...\n"); |
| 266 | } |
| 267 | |
| 268 | if (a_buf->e_buf.used_len == 0) { |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | /* Simplest way to print the circular buffer is allocate a second buf |
| 273 | * of the same size, and memcpy it so it's a simple linear buffer, |
| 274 | * and then cal print_buf_lines on it */ |
| 275 | if (a_buf->e_buf.read < a_buf->e_buf.write) { |
| 276 | /* no wrap around, just print it */ |
| 277 | print_buf_lines(log_info, a_buf->e_buf.buf + a_buf->e_buf.read, |
| 278 | a_buf->e_buf.used_len); |
| 279 | } else { |
| 280 | /* The circular buffer will always have at least 1 byte unused, |
| 281 | * so by allocating alloc_len here we will have at least |
| 282 | * 1 byte of space available as required by print_buf_lines(). |
| 283 | */ |
| 284 | char * nbuf = malloc(a_buf->e_buf.alloc_len); |
| 285 | if (!nbuf) { |
| 286 | return; |
| 287 | } |
| 288 | int first_chunk_len = a_buf->e_buf.buf_size - a_buf->e_buf.read; |
| 289 | memcpy(nbuf, a_buf->e_buf.buf + a_buf->e_buf.read, first_chunk_len); |
| 290 | /* copy second chunk */ |
| 291 | memcpy(nbuf + first_chunk_len, a_buf->e_buf.buf, a_buf->e_buf.write); |
| 292 | print_buf_lines(log_info, nbuf, first_chunk_len + a_buf->e_buf.write); |
| 293 | free(nbuf); |
| 294 | } |
| 295 | } |
| 296 | |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 297 | static void signal_handler(int signal_num); |
| 298 | |
| 299 | static void block_signals(sigset_t* oldset) { |
| 300 | sigset_t blockset; |
| 301 | |
| 302 | sigemptyset(&blockset); |
| 303 | sigaddset(&blockset, SIGINT); |
| 304 | sigaddset(&blockset, SIGQUIT); |
| 305 | sigaddset(&blockset, SIGHUP); |
| 306 | pthread_sigmask(SIG_BLOCK, &blockset, oldset); |
| 307 | } |
| 308 | |
| 309 | static void unblock_signals(sigset_t* oldset) { |
| 310 | pthread_sigmask(SIG_SETMASK, oldset, NULL); |
| 311 | } |
| 312 | |
| 313 | static void setup_signal_handlers(pid_t pid) { |
| 314 | struct sigaction handler = {.sa_handler = signal_handler}; |
| 315 | |
| 316 | child_pid = pid; |
| 317 | sigaction(SIGINT, &handler, &old_int); |
| 318 | sigaction(SIGQUIT, &handler, &old_quit); |
| 319 | sigaction(SIGHUP, &handler, &old_hup); |
| 320 | } |
| 321 | |
| 322 | static void restore_signal_handlers() { |
| 323 | sigaction(SIGINT, &old_int, NULL); |
| 324 | sigaction(SIGQUIT, &old_quit, NULL); |
| 325 | sigaction(SIGHUP, &old_hup, NULL); |
| 326 | child_pid = 0; |
| 327 | } |
| 328 | |
| 329 | static void signal_handler(int signal_num) { |
| 330 | if (child_pid == 0 || kill(child_pid, signal_num) != 0) { |
| 331 | restore_signal_handlers(); |
| 332 | raise(signal_num); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, int log_target, |
| 337 | bool abbreviated, char* file_path, bool forward_signals) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 338 | int status = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 339 | char buffer[4096]; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 340 | struct pollfd poll_fds[] = { |
| 341 | [0] = { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 342 | .fd = parent_read, |
| 343 | .events = POLLIN, |
| 344 | }, |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 345 | }; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 346 | int rc = 0; |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 347 | int fd; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 348 | |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 349 | struct log_info log_info; |
| 350 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 351 | int a = 0; // start index of unprocessed data |
| 352 | int b = 0; // end index of unprocessed data |
| 353 | int sz; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 354 | bool found_child = false; |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 355 | // There is a very small chance that opening child_ptty in the child will fail, but in this case |
| 356 | // POLLHUP will not be generated below. Therefore, we use a 1 second timeout for poll() until |
| 357 | // we receive a message from child_ptty. If this times out, we call waitpid() with WNOHANG to |
| 358 | // check the status of the child process and exit appropriately if it has terminated. |
| 359 | bool received_messages = false; |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 360 | char tmpbuf[256]; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 361 | |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 362 | log_info.btag = basename(tag); |
| 363 | if (!log_info.btag) { |
| 364 | log_info.btag = (char*) tag; |
| 365 | } |
| 366 | |
| 367 | if (abbreviated && (log_target == LOG_NONE)) { |
| 368 | abbreviated = 0; |
| 369 | } |
| 370 | if (abbreviated) { |
| 371 | init_abbr_buf(&log_info.a_buf); |
| 372 | } |
| 373 | |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 374 | if (log_target & LOG_KLOG) { |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 375 | snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt), |
Greg Hartman | 465f75f | 2015-02-03 09:05:29 -0800 | [diff] [blame] | 376 | "<6>%.*s: %%s\n", MAX_KLOG_TAG, log_info.btag); |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 377 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 378 | |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 379 | if ((log_target & LOG_FILE) && !file_path) { |
| 380 | /* No file_path specified, clear the LOG_FILE bit */ |
| 381 | log_target &= ~LOG_FILE; |
| 382 | } |
| 383 | |
| 384 | if (log_target & LOG_FILE) { |
| 385 | fd = open(file_path, O_WRONLY | O_CREAT, 0664); |
| 386 | if (fd < 0) { |
| 387 | ERROR("Cannot log to file %s\n", file_path); |
| 388 | log_target &= ~LOG_FILE; |
| 389 | } else { |
| 390 | lseek(fd, 0, SEEK_END); |
| 391 | log_info.fp = fdopen(fd, "a"); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | log_info.log_target = log_target; |
| 396 | log_info.abbreviated = abbreviated; |
| 397 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 398 | while (!found_child) { |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 399 | int timeout = received_messages ? -1 : 1000; |
| 400 | if (TEMP_FAILURE_RETRY(poll(poll_fds, ARRAY_SIZE(poll_fds), timeout)) < 0) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 401 | ERROR("poll failed\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 402 | rc = -1; |
| 403 | goto err_poll; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 404 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 405 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame] | 406 | if (poll_fds[0].revents & POLLIN) { |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 407 | received_messages = true; |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 408 | sz = TEMP_FAILURE_RETRY( |
| 409 | read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 410 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame] | 411 | sz += b; |
| 412 | // Log one line at a time |
| 413 | for (b = 0; b < sz; b++) { |
| 414 | if (buffer[b] == '\r') { |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 415 | if (abbreviated) { |
| 416 | /* The abbreviated logging code uses newline as |
| 417 | * the line separator. Lucikly, the pty layer |
| 418 | * helpfully cooks the output of the command |
| 419 | * being run and inserts a CR before NL. So |
| 420 | * I just change it to NL here when doing |
| 421 | * abbreviated logging. |
| 422 | */ |
| 423 | buffer[b] = '\n'; |
| 424 | } else { |
| 425 | buffer[b] = '\0'; |
| 426 | } |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame] | 427 | } else if (buffer[b] == '\n') { |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 428 | buffer[b] = '\0'; |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 429 | log_line(&log_info, &buffer[a], b - a); |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame] | 430 | a = b + 1; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame] | 434 | if (a == 0 && b == sizeof(buffer) - 1) { |
| 435 | // buffer is full, flush |
| 436 | buffer[b] = '\0'; |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 437 | log_line(&log_info, &buffer[a], b - a); |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame] | 438 | b = 0; |
| 439 | } else if (a != b) { |
| 440 | // Keep left-overs |
| 441 | b -= a; |
| 442 | memmove(buffer, &buffer[a], b); |
| 443 | a = 0; |
| 444 | } else { |
| 445 | a = 0; |
| 446 | b = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 450 | if (!received_messages || (poll_fds[0].revents & POLLHUP)) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 451 | int ret; |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 452 | sigset_t oldset; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 453 | |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 454 | if (forward_signals) { |
| 455 | // Our signal handlers forward these signals to 'child_pid', but waitpid() may reap |
| 456 | // the child, so we must block these signals until we either 1) conclude that the |
| 457 | // child is still running or 2) determine the child has been reaped and we have |
| 458 | // reset the signals to their original disposition. |
| 459 | block_signals(&oldset); |
| 460 | } |
| 461 | |
| 462 | int flags = (poll_fds[0].revents & POLLHUP) ? 0 : WNOHANG; |
| 463 | ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, flags)); |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame] | 464 | if (ret < 0) { |
| 465 | rc = errno; |
| 466 | ALOG(LOG_ERROR, "logwrap", "waitpid failed with %s\n", strerror(errno)); |
| 467 | goto err_waitpid; |
| 468 | } |
| 469 | if (ret > 0) { |
| 470 | found_child = true; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 471 | } |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 472 | |
| 473 | if (forward_signals) { |
| 474 | if (found_child) { |
| 475 | restore_signal_handlers(); |
| 476 | } |
| 477 | unblock_signals(&oldset); |
| 478 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 479 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 480 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 481 | |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 482 | if (chld_sts != NULL) { |
| 483 | *chld_sts = status; |
| 484 | } else { |
| 485 | if (WIFEXITED(status)) |
| 486 | rc = WEXITSTATUS(status); |
| 487 | else |
| 488 | rc = -ECHILD; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 491 | // Flush remaining data |
| 492 | if (a != b) { |
| 493 | buffer[b] = '\0'; |
| 494 | log_line(&log_info, &buffer[a], b - a); |
| 495 | } |
| 496 | |
| 497 | /* All the output has been processed, time to dump the abbreviated output */ |
| 498 | if (abbreviated) { |
| 499 | print_abbr_buf(&log_info); |
| 500 | } |
| 501 | |
| 502 | if (WIFEXITED(status)) { |
| 503 | if (WEXITSTATUS(status)) { |
| 504 | snprintf(tmpbuf, sizeof(tmpbuf), |
| 505 | "%s terminated by exit(%d)\n", log_info.btag, WEXITSTATUS(status)); |
| 506 | do_log_line(&log_info, tmpbuf); |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 507 | } |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 508 | } else { |
| 509 | if (WIFSIGNALED(status)) { |
| 510 | snprintf(tmpbuf, sizeof(tmpbuf), |
| 511 | "%s terminated by signal %d\n", log_info.btag, WTERMSIG(status)); |
| 512 | do_log_line(&log_info, tmpbuf); |
| 513 | } else if (WIFSTOPPED(status)) { |
| 514 | snprintf(tmpbuf, sizeof(tmpbuf), |
| 515 | "%s stopped by signal %d\n", log_info.btag, WSTOPSIG(status)); |
| 516 | do_log_line(&log_info, tmpbuf); |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 517 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 518 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 519 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame] | 520 | err_waitpid: |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 521 | err_poll: |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 522 | if (log_target & LOG_FILE) { |
| 523 | fclose(log_info.fp); /* Also closes underlying fd */ |
| 524 | } |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 525 | if (abbreviated) { |
| 526 | free_abbr_buf(&log_info.a_buf); |
| 527 | } |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 528 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 531 | static void child(int argc, char* argv[]) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 532 | // create null terminated argv_child array |
| 533 | char* argv_child[argc + 1]; |
| 534 | memcpy(argv_child, argv, argc * sizeof(char *)); |
| 535 | argv_child[argc] = NULL; |
| 536 | |
| 537 | if (execvp(argv_child[0], argv_child)) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 538 | FATAL_CHILD("executing %s failed: %s\n", argv_child[0], |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 539 | strerror(errno)); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 543 | int android_fork_execvp_ext2(int argc, char* argv[], int* status, bool forward_signals, |
| 544 | int log_target, bool abbreviated, char* file_path) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 545 | pid_t pid; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 546 | int parent_ptty; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 547 | sigset_t blockset; |
| 548 | sigset_t oldset; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 549 | int rc = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 550 | |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 551 | rc = pthread_mutex_lock(&fd_mutex); |
| 552 | if (rc) { |
| 553 | ERROR("failed to lock signal_fd mutex\n"); |
| 554 | goto err_lock; |
| 555 | } |
| 556 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 557 | /* Use ptty instead of socketpair so that STDOUT is not buffered */ |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 558 | parent_ptty = TEMP_FAILURE_RETRY(posix_openpt(O_RDWR | O_CLOEXEC)); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 559 | if (parent_ptty < 0) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 560 | ERROR("Cannot create parent ptty\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 561 | rc = -1; |
| 562 | goto err_open; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 563 | } |
| 564 | |
Elliott Hughes | 84cfd10 | 2014-07-29 11:05:18 -0700 | [diff] [blame] | 565 | char child_devname[64]; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 566 | if (grantpt(parent_ptty) || unlockpt(parent_ptty) || |
Elliott Hughes | 84cfd10 | 2014-07-29 11:05:18 -0700 | [diff] [blame] | 567 | ptsname_r(parent_ptty, child_devname, sizeof(child_devname)) != 0) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 568 | ERROR("Problem with /dev/ptmx\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 569 | rc = -1; |
| 570 | goto err_ptty; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 573 | if (forward_signals) { |
| 574 | // Block these signals until we have the child pid and our signal handlers set up. |
| 575 | block_signals(&oldset); |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 578 | pid = fork(); |
| 579 | if (pid < 0) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 580 | ERROR("Failed to fork\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 581 | rc = -1; |
| 582 | goto err_fork; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 583 | } else if (pid == 0) { |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 584 | pthread_mutex_unlock(&fd_mutex); |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 585 | unblock_signals(&oldset); |
| 586 | |
| 587 | setsid(); |
| 588 | |
| 589 | int child_ptty = TEMP_FAILURE_RETRY(open(child_devname, O_RDWR | O_CLOEXEC)); |
| 590 | if (child_ptty < 0) { |
| 591 | FATAL_CHILD("Cannot open child_ptty: %s\n", strerror(errno)); |
| 592 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 593 | close(parent_ptty); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 594 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 595 | dup2(child_ptty, 1); |
| 596 | dup2(child_ptty, 2); |
| 597 | close(child_ptty); |
| 598 | |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 599 | child(argc, argv); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 600 | } else { |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 601 | if (forward_signals) { |
| 602 | setup_signal_handlers(pid); |
| 603 | unblock_signals(&oldset); |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 606 | rc = parent(argv[0], parent_ptty, pid, status, log_target, abbreviated, file_path, |
| 607 | forward_signals); |
| 608 | |
| 609 | if (forward_signals) { |
| 610 | restore_signal_handlers(); |
| 611 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 612 | } |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 613 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 614 | err_fork: |
Tom Cherry | 0132893 | 2019-09-24 18:58:38 -0700 | [diff] [blame^] | 615 | if (forward_signals) { |
| 616 | unblock_signals(&oldset); |
| 617 | } |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 618 | err_ptty: |
| 619 | close(parent_ptty); |
| 620 | err_open: |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 621 | pthread_mutex_unlock(&fd_mutex); |
| 622 | err_lock: |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 623 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 624 | } |