blob: 0314f374ab3156435b15834c0c2f251ac4c13759 [file] [log] [blame]
Rom Lemarchand113bd472013-01-10 15:21:18 -08001/*
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 Lemarchand113bd472013-01-10 15:21:18 -080017#include <errno.h>
18#include <fcntl.h>
19#include <libgen.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070020#include <poll.h>
Rom Lemarchand74a7b912013-03-11 14:00:58 -070021#include <pthread.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070022#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 Lemarchand113bd472013-01-10 15:21:18 -080030
Ken Sumrall96e11b52013-04-03 13:45:10 -070031#include <cutils/klog.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080032#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070033#include <logwrap/logwrap.h>
Rom Lemarchand113bd472013-01-10 15:21:18 -080034
JP Abgralla689d132013-02-13 16:31:58 -080035#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
Ken Sumrall96e11b52013-04-03 13:45:10 -070036#define MIN(a,b) (((a)<(b))?(a):(b))
Rom Lemarchandb58a8222013-01-09 21:31:25 -080037
Rom Lemarchand74a7b912013-03-11 14:00:58 -070038static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
Tom Cherry01328932019-09-24 18:58:38 -070039// Protected by fd_mutex. These signals must be blocked while modifying as well.
40static pid_t child_pid;
41static struct sigaction old_int;
42static struct sigaction old_quit;
43static struct sigaction old_hup;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080044
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080045#define ERROR(fmt, args...) \
Rom Lemarchand99e19662013-01-07 15:50:02 -080046do { \
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080047 fprintf(stderr, fmt, ## args); \
48 ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
Rom Lemarchand99e19662013-01-07 15:50:02 -080049} while(0)
50
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080051#define FATAL_CHILD(fmt, args...) \
Rom Lemarchand99e19662013-01-07 15:50:02 -080052do { \
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080053 ERROR(fmt, ## args); \
Rom Lemarchand99e19662013-01-07 15:50:02 -080054 _exit(-1); \
55} while(0)
Rom Lemarchand113bd472013-01-10 15:21:18 -080056
Ken Sumrall96e11b52013-04-03 13:45:10 -070057#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
63struct 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
76struct 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 */
88struct 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. */
95struct log_info {
96 int log_target;
97 char klog_fmt[MAX_KLOG_TAG * 2];
98 char *btag;
99 bool abbreviated;
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700100 FILE *fp;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700101 struct abbr_buf a_buf;
102};
103
104/* Forware declaration */
105static 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 */
108static int add_line_to_linear_buf(struct beginning_buf *b_buf,
109 char *line, ssize_t line_len)
110{
Ken Sumrall96e11b52013-04-03 13:45:10 -0700111 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
124static 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 Sumrall96e11b52013-04-03 13:45:10 -0700129 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 */
162static void do_log_line(struct log_info *log_info, char *line) {
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700163 if (log_info->log_target & LOG_KLOG) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700164 klog_write(6, log_info->klog_fmt, line);
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700165 }
166 if (log_info->log_target & LOG_ALOG) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700167 ALOG(LOG_INFO, log_info->btag, "%s", line);
168 }
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700169 if (log_info->log_target & LOG_FILE) {
170 fprintf(log_info->fp, "%s\n", line);
171 }
Ken Sumrall96e11b52013-04-03 13:45:10 -0700172}
173
174/* Log to either the abbreviated buf, or directly to the specified log
175 * via do_log_line() above.
176 */
177static 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 */
192static void print_buf_lines(struct log_info *log_info, char *buf, int buf_size)
193{
194 char *line_start;
195 char c;
Ken Sumrall96e11b52013-04-03 13:45:10 -0700196 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
220static 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
238static 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
243static 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
253static 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 Cherry01328932019-09-24 18:58:38 -0700297static void signal_handler(int signal_num);
298
299static 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
309static void unblock_signals(sigset_t* oldset) {
310 pthread_sigmask(SIG_SETMASK, oldset, NULL);
311}
312
313static 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
322static 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
329static 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
336static 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 Lemarchand0cc2cab2013-01-16 12:08:04 -0800338 int status = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800339 char buffer[4096];
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800340 struct pollfd poll_fds[] = {
341 [0] = {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800342 .fd = parent_read,
343 .events = POLLIN,
344 },
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800345 };
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800346 int rc = 0;
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700347 int fd;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800348
Ken Sumrall96e11b52013-04-03 13:45:10 -0700349 struct log_info log_info;
350
Rom Lemarchand113bd472013-01-10 15:21:18 -0800351 int a = 0; // start index of unprocessed data
352 int b = 0; // end index of unprocessed data
353 int sz;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800354 bool found_child = false;
Tom Cherry01328932019-09-24 18:58:38 -0700355 // 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 Sumrall96e11b52013-04-03 13:45:10 -0700360 char tmpbuf[256];
Rom Lemarchand113bd472013-01-10 15:21:18 -0800361
Ken Sumrall96e11b52013-04-03 13:45:10 -0700362 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 Sumrall4eaf9052013-09-18 17:49:21 -0700374 if (log_target & LOG_KLOG) {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700375 snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt),
Greg Hartman465f75f2015-02-03 09:05:29 -0800376 "<6>%.*s: %%s\n", MAX_KLOG_TAG, log_info.btag);
Ken Sumrall96e11b52013-04-03 13:45:10 -0700377 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800378
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700379 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 Lemarchand0cc2cab2013-01-16 12:08:04 -0800398 while (!found_child) {
Tom Cherry01328932019-09-24 18:58:38 -0700399 int timeout = received_messages ? -1 : 1000;
400 if (TEMP_FAILURE_RETRY(poll(poll_fds, ARRAY_SIZE(poll_fds), timeout)) < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800401 ERROR("poll failed\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800402 rc = -1;
403 goto err_poll;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800404 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800405
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700406 if (poll_fds[0].revents & POLLIN) {
Tom Cherry01328932019-09-24 18:58:38 -0700407 received_messages = true;
Yusuke Sato0df08272015-07-08 14:57:07 -0700408 sz = TEMP_FAILURE_RETRY(
409 read(parent_read, &buffer[b], sizeof(buffer) - 1 - b));
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800410
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700411 sz += b;
412 // Log one line at a time
413 for (b = 0; b < sz; b++) {
414 if (buffer[b] == '\r') {
Ken Sumrall96e11b52013-04-03 13:45:10 -0700415 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 Lemarchand6ad53df2013-03-20 13:46:53 -0700427 } else if (buffer[b] == '\n') {
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800428 buffer[b] = '\0';
Ken Sumrall96e11b52013-04-03 13:45:10 -0700429 log_line(&log_info, &buffer[a], b - a);
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700430 a = b + 1;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800431 }
432 }
433
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700434 if (a == 0 && b == sizeof(buffer) - 1) {
435 // buffer is full, flush
436 buffer[b] = '\0';
Ken Sumrall96e11b52013-04-03 13:45:10 -0700437 log_line(&log_info, &buffer[a], b - a);
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700438 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 Lemarchand113bd472013-01-10 15:21:18 -0800447 }
448 }
449
Tom Cherry01328932019-09-24 18:58:38 -0700450 if (!received_messages || (poll_fds[0].revents & POLLHUP)) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800451 int ret;
Tom Cherry01328932019-09-24 18:58:38 -0700452 sigset_t oldset;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800453
Tom Cherry01328932019-09-24 18:58:38 -0700454 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 Lemarchand6ad53df2013-03-20 13:46:53 -0700464 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 Lemarchandb58a8222013-01-09 21:31:25 -0800471 }
Tom Cherry01328932019-09-24 18:58:38 -0700472
473 if (forward_signals) {
474 if (found_child) {
475 restore_signal_handlers();
476 }
477 unblock_signals(&oldset);
478 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800479 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800480 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800481
JP Abgralla689d132013-02-13 16:31:58 -0800482 if (chld_sts != NULL) {
483 *chld_sts = status;
484 } else {
485 if (WIFEXITED(status))
486 rc = WEXITSTATUS(status);
487 else
488 rc = -ECHILD;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800489 }
490
Ken Sumrall96e11b52013-04-03 13:45:10 -0700491 // 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 Abgralla689d132013-02-13 16:31:58 -0800507 }
Ken Sumrall96e11b52013-04-03 13:45:10 -0700508 } 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 Abgralla689d132013-02-13 16:31:58 -0800517 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800518 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800519
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700520err_waitpid:
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800521err_poll:
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700522 if (log_target & LOG_FILE) {
523 fclose(log_info.fp); /* Also closes underlying fd */
524 }
Ken Sumrall96e11b52013-04-03 13:45:10 -0700525 if (abbreviated) {
526 free_abbr_buf(&log_info.a_buf);
527 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800528 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800529}
530
Ken Sumrall96e11b52013-04-03 13:45:10 -0700531static void child(int argc, char* argv[]) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800532 // 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 Lemarchand2a46bfa2013-01-29 11:44:59 -0800538 FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
Rom Lemarchand99e19662013-01-07 15:50:02 -0800539 strerror(errno));
Rom Lemarchand113bd472013-01-10 15:21:18 -0800540 }
541}
542
Tom Cherry01328932019-09-24 18:58:38 -0700543int android_fork_execvp_ext2(int argc, char* argv[], int* status, bool forward_signals,
544 int log_target, bool abbreviated, char* file_path) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800545 pid_t pid;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800546 int parent_ptty;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800547 sigset_t blockset;
548 sigset_t oldset;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800549 int rc = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800550
Rom Lemarchand74a7b912013-03-11 14:00:58 -0700551 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 Lemarchand113bd472013-01-10 15:21:18 -0800557 /* Use ptty instead of socketpair so that STDOUT is not buffered */
Tom Cherry01328932019-09-24 18:58:38 -0700558 parent_ptty = TEMP_FAILURE_RETRY(posix_openpt(O_RDWR | O_CLOEXEC));
Rom Lemarchand113bd472013-01-10 15:21:18 -0800559 if (parent_ptty < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800560 ERROR("Cannot create parent ptty\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800561 rc = -1;
562 goto err_open;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800563 }
564
Elliott Hughes84cfd102014-07-29 11:05:18 -0700565 char child_devname[64];
Rom Lemarchand113bd472013-01-10 15:21:18 -0800566 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
Elliott Hughes84cfd102014-07-29 11:05:18 -0700567 ptsname_r(parent_ptty, child_devname, sizeof(child_devname)) != 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800568 ERROR("Problem with /dev/ptmx\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800569 rc = -1;
570 goto err_ptty;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800571 }
572
Tom Cherry01328932019-09-24 18:58:38 -0700573 if (forward_signals) {
574 // Block these signals until we have the child pid and our signal handlers set up.
575 block_signals(&oldset);
Rom Lemarchand6ad53df2013-03-20 13:46:53 -0700576 }
577
Rom Lemarchand113bd472013-01-10 15:21:18 -0800578 pid = fork();
579 if (pid < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800580 ERROR("Failed to fork\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800581 rc = -1;
582 goto err_fork;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800583 } else if (pid == 0) {
Rom Lemarchand74a7b912013-03-11 14:00:58 -0700584 pthread_mutex_unlock(&fd_mutex);
Tom Cherry01328932019-09-24 18:58:38 -0700585 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 Lemarchand113bd472013-01-10 15:21:18 -0800593 close(parent_ptty);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800594
Rom Lemarchand113bd472013-01-10 15:21:18 -0800595 dup2(child_ptty, 1);
596 dup2(child_ptty, 2);
597 close(child_ptty);
598
Ken Sumrall96e11b52013-04-03 13:45:10 -0700599 child(argc, argv);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800600 } else {
Tom Cherry01328932019-09-24 18:58:38 -0700601 if (forward_signals) {
602 setup_signal_handlers(pid);
603 unblock_signals(&oldset);
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800604 }
605
Tom Cherry01328932019-09-24 18:58:38 -0700606 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 Lemarchandb58a8222013-01-09 21:31:25 -0800612 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800613
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800614err_fork:
Tom Cherry01328932019-09-24 18:58:38 -0700615 if (forward_signals) {
616 unblock_signals(&oldset);
617 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800618err_ptty:
619 close(parent_ptty);
620err_open:
Rom Lemarchand74a7b912013-03-11 14:00:58 -0700621 pthread_mutex_unlock(&fd_mutex);
622err_lock:
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800623 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800624}