2013-01-11 00:21:18 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
2016-09-28 19:07:20 +02:00
|
|
|
#include <poll.h>
|
2013-03-11 22:00:58 +01:00
|
|
|
#include <pthread.h>
|
2016-09-28 19:07:20 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <android-base/macros.h>
|
2013-04-03 22:45:10 +02:00
|
|
|
#include <cutils/klog.h>
|
2017-01-10 22:19:54 +01:00
|
|
|
#include <log/log.h>
|
2016-09-28 19:07:20 +02:00
|
|
|
#include <logwrap/logwrap.h>
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-03-11 22:00:58 +01:00
|
|
|
static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
// Protected by fd_mutex. These signals must be blocked while modifying as well.
|
|
|
|
static pid_t child_pid;
|
|
|
|
static struct sigaction old_int;
|
|
|
|
static struct sigaction old_quit;
|
|
|
|
static struct sigaction old_hup;
|
2013-01-16 21:08:04 +01:00
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
#define ERROR(fmt, args...) \
|
|
|
|
do { \
|
|
|
|
fprintf(stderr, fmt, ##args); \
|
|
|
|
ALOG(LOG_ERROR, "logwrapper", fmt, ##args); \
|
|
|
|
} while (0)
|
2013-01-08 00:50:02 +01:00
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
#define FATAL_CHILD(fmt, args...) \
|
|
|
|
do { \
|
|
|
|
ERROR(fmt, ##args); \
|
|
|
|
_exit(-1); \
|
|
|
|
} while (0)
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-04-03 22:45:10 +02:00
|
|
|
#define MAX_KLOG_TAG 16
|
|
|
|
|
|
|
|
/* This is a simple buffer that holds up to the first beginning_buf->buf_size
|
|
|
|
* bytes of output from a command.
|
|
|
|
*/
|
|
|
|
#define BEGINNING_BUF_SIZE 0x1000
|
|
|
|
struct beginning_buf {
|
2019-09-26 00:01:58 +02:00
|
|
|
char* buf;
|
2013-04-03 22:45:10 +02:00
|
|
|
size_t alloc_len;
|
|
|
|
/* buf_size is the usable space, which is one less than the allocated size */
|
|
|
|
size_t buf_size;
|
|
|
|
size_t used_len;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This is a circular buf that holds up to the last ending_buf->buf_size bytes
|
|
|
|
* of output from a command after the first beginning_buf->buf_size bytes
|
|
|
|
* (which are held in beginning_buf above).
|
|
|
|
*/
|
|
|
|
#define ENDING_BUF_SIZE 0x1000
|
|
|
|
struct ending_buf {
|
2019-09-26 00:01:58 +02:00
|
|
|
char* buf;
|
2013-04-03 22:45:10 +02:00
|
|
|
ssize_t alloc_len;
|
|
|
|
/* buf_size is the usable space, which is one less than the allocated size */
|
|
|
|
ssize_t buf_size;
|
|
|
|
ssize_t used_len;
|
|
|
|
/* read and write offsets into the circular buffer */
|
|
|
|
int read;
|
|
|
|
int write;
|
|
|
|
};
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
/* A structure to hold all the abbreviated buf data */
|
2013-04-03 22:45:10 +02:00
|
|
|
struct abbr_buf {
|
|
|
|
struct beginning_buf b_buf;
|
|
|
|
struct ending_buf e_buf;
|
|
|
|
int beginning_buf_full;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Collect all the various bits of info needed for logging in one place. */
|
|
|
|
struct log_info {
|
|
|
|
int log_target;
|
|
|
|
char klog_fmt[MAX_KLOG_TAG * 2];
|
2019-09-26 00:01:58 +02:00
|
|
|
const char* btag;
|
2013-04-03 22:45:10 +02:00
|
|
|
bool abbreviated;
|
2019-09-26 00:01:58 +02:00
|
|
|
FILE* fp;
|
2013-04-03 22:45:10 +02:00
|
|
|
struct abbr_buf a_buf;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Forware declaration */
|
2019-09-26 00:01:58 +02:00
|
|
|
static void add_line_to_abbr_buf(struct abbr_buf* a_buf, char* linebuf, int linelen);
|
2013-04-03 22:45:10 +02:00
|
|
|
|
|
|
|
/* Return 0 on success, and 1 when full */
|
2019-09-26 00:01:58 +02:00
|
|
|
static int add_line_to_linear_buf(struct beginning_buf* b_buf, char* line, ssize_t line_len) {
|
2013-04-03 22:45:10 +02:00
|
|
|
int full = 0;
|
|
|
|
|
|
|
|
if ((line_len + b_buf->used_len) > b_buf->buf_size) {
|
|
|
|
full = 1;
|
|
|
|
} else {
|
|
|
|
/* Add to the end of the buf */
|
|
|
|
memcpy(b_buf->buf + b_buf->used_len, line, line_len);
|
|
|
|
b_buf->used_len += line_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return full;
|
|
|
|
}
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
static void add_line_to_circular_buf(struct ending_buf* e_buf, char* line, ssize_t line_len) {
|
2013-04-03 22:45:10 +02:00
|
|
|
ssize_t free_len;
|
|
|
|
ssize_t needed_space;
|
|
|
|
int cnt;
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
if (e_buf->buf == nullptr) {
|
2013-04-03 22:45:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
if (line_len > e_buf->buf_size) {
|
|
|
|
return;
|
|
|
|
}
|
2013-04-03 22:45:10 +02:00
|
|
|
|
|
|
|
free_len = e_buf->buf_size - e_buf->used_len;
|
|
|
|
|
|
|
|
if (line_len > free_len) {
|
|
|
|
/* remove oldest entries at read, and move read to make
|
|
|
|
* room for the new string */
|
|
|
|
needed_space = line_len - free_len;
|
|
|
|
e_buf->read = (e_buf->read + needed_space) % e_buf->buf_size;
|
|
|
|
e_buf->used_len -= needed_space;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the line into the circular buffer, dealing with possible
|
|
|
|
* wraparound.
|
|
|
|
*/
|
2019-09-26 00:01:58 +02:00
|
|
|
cnt = std::min(line_len, e_buf->buf_size - e_buf->write);
|
2013-04-03 22:45:10 +02:00
|
|
|
memcpy(e_buf->buf + e_buf->write, line, cnt);
|
|
|
|
if (cnt < line_len) {
|
|
|
|
memcpy(e_buf->buf, line + cnt, line_len - cnt);
|
|
|
|
}
|
|
|
|
e_buf->used_len += line_len;
|
|
|
|
e_buf->write = (e_buf->write + line_len) % e_buf->buf_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Log directly to the specified log */
|
2019-09-26 00:01:58 +02:00
|
|
|
static void do_log_line(struct log_info* log_info, const char* line) {
|
2013-09-19 02:49:21 +02:00
|
|
|
if (log_info->log_target & LOG_KLOG) {
|
2013-04-03 22:45:10 +02:00
|
|
|
klog_write(6, log_info->klog_fmt, line);
|
2013-09-19 02:49:21 +02:00
|
|
|
}
|
|
|
|
if (log_info->log_target & LOG_ALOG) {
|
2013-04-03 22:45:10 +02:00
|
|
|
ALOG(LOG_INFO, log_info->btag, "%s", line);
|
|
|
|
}
|
2013-09-19 02:49:21 +02:00
|
|
|
if (log_info->log_target & LOG_FILE) {
|
|
|
|
fprintf(log_info->fp, "%s\n", line);
|
|
|
|
}
|
2013-04-03 22:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Log to either the abbreviated buf, or directly to the specified log
|
|
|
|
* via do_log_line() above.
|
|
|
|
*/
|
2019-09-26 00:01:58 +02:00
|
|
|
static void log_line(struct log_info* log_info, char* line, int len) {
|
2013-04-03 22:45:10 +02:00
|
|
|
if (log_info->abbreviated) {
|
|
|
|
add_line_to_abbr_buf(&log_info->a_buf, line, len);
|
|
|
|
} else {
|
|
|
|
do_log_line(log_info, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The kernel will take a maximum of 1024 bytes in any single write to
|
|
|
|
* the kernel logging device file, so find and print each line one at
|
|
|
|
* a time. The allocated size for buf should be at least 1 byte larger
|
|
|
|
* than buf_size (the usable size of the buffer) to make sure there is
|
|
|
|
* room to temporarily stuff a null byte to terminate a line for logging.
|
|
|
|
*/
|
2019-09-26 00:01:58 +02:00
|
|
|
static void print_buf_lines(struct log_info* log_info, char* buf, int buf_size) {
|
|
|
|
char* line_start;
|
2013-04-03 22:45:10 +02:00
|
|
|
char c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
line_start = buf;
|
|
|
|
for (i = 0; i < buf_size; i++) {
|
|
|
|
if (*(buf + i) == '\n') {
|
|
|
|
/* Found a line ending, print the line and compute new line_start */
|
|
|
|
/* Save the next char and replace with \0 */
|
|
|
|
c = *(buf + i + 1);
|
|
|
|
*(buf + i + 1) = '\0';
|
|
|
|
do_log_line(log_info, line_start);
|
|
|
|
/* Restore the saved char */
|
|
|
|
*(buf + i + 1) = c;
|
|
|
|
line_start = buf + i + 1;
|
|
|
|
} else if (*(buf + i) == '\0') {
|
|
|
|
/* The end of the buffer, print the last bit */
|
|
|
|
do_log_line(log_info, line_start);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If the buffer was completely full, and didn't end with a newline, just
|
|
|
|
* ignore the partial last line.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
static void init_abbr_buf(struct abbr_buf* a_buf) {
|
|
|
|
char* new_buf;
|
2013-04-03 22:45:10 +02:00
|
|
|
|
|
|
|
memset(a_buf, 0, sizeof(struct abbr_buf));
|
2019-09-26 00:01:58 +02:00
|
|
|
new_buf = static_cast<char*>(malloc(BEGINNING_BUF_SIZE));
|
2013-04-03 22:45:10 +02:00
|
|
|
if (new_buf) {
|
|
|
|
a_buf->b_buf.buf = new_buf;
|
|
|
|
a_buf->b_buf.alloc_len = BEGINNING_BUF_SIZE;
|
|
|
|
a_buf->b_buf.buf_size = BEGINNING_BUF_SIZE - 1;
|
|
|
|
}
|
2019-09-26 00:01:58 +02:00
|
|
|
new_buf = static_cast<char*>(malloc(ENDING_BUF_SIZE));
|
2013-04-03 22:45:10 +02:00
|
|
|
if (new_buf) {
|
|
|
|
a_buf->e_buf.buf = new_buf;
|
|
|
|
a_buf->e_buf.alloc_len = ENDING_BUF_SIZE;
|
|
|
|
a_buf->e_buf.buf_size = ENDING_BUF_SIZE - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
static void free_abbr_buf(struct abbr_buf* a_buf) {
|
2013-04-03 22:45:10 +02:00
|
|
|
free(a_buf->b_buf.buf);
|
|
|
|
free(a_buf->e_buf.buf);
|
|
|
|
}
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
static void add_line_to_abbr_buf(struct abbr_buf* a_buf, char* linebuf, int linelen) {
|
2013-04-03 22:45:10 +02:00
|
|
|
if (!a_buf->beginning_buf_full) {
|
2019-09-26 00:01:58 +02:00
|
|
|
a_buf->beginning_buf_full = add_line_to_linear_buf(&a_buf->b_buf, linebuf, linelen);
|
2013-04-03 22:45:10 +02:00
|
|
|
}
|
|
|
|
if (a_buf->beginning_buf_full) {
|
|
|
|
add_line_to_circular_buf(&a_buf->e_buf, linebuf, linelen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
static void print_abbr_buf(struct log_info* log_info) {
|
|
|
|
struct abbr_buf* a_buf = &log_info->a_buf;
|
2013-04-03 22:45:10 +02:00
|
|
|
|
|
|
|
/* Add the abbreviated output to the kernel log */
|
|
|
|
if (a_buf->b_buf.alloc_len) {
|
|
|
|
print_buf_lines(log_info, a_buf->b_buf.buf, a_buf->b_buf.used_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print an ellipsis to indicate that the buffer has wrapped or
|
|
|
|
* is full, and some data was not logged.
|
|
|
|
*/
|
|
|
|
if (a_buf->e_buf.used_len == a_buf->e_buf.buf_size) {
|
|
|
|
do_log_line(log_info, "...\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a_buf->e_buf.used_len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Simplest way to print the circular buffer is allocate a second buf
|
|
|
|
* of the same size, and memcpy it so it's a simple linear buffer,
|
|
|
|
* and then cal print_buf_lines on it */
|
|
|
|
if (a_buf->e_buf.read < a_buf->e_buf.write) {
|
|
|
|
/* no wrap around, just print it */
|
2019-09-26 00:01:58 +02:00
|
|
|
print_buf_lines(log_info, a_buf->e_buf.buf + a_buf->e_buf.read, a_buf->e_buf.used_len);
|
2013-04-03 22:45:10 +02:00
|
|
|
} else {
|
|
|
|
/* The circular buffer will always have at least 1 byte unused,
|
|
|
|
* so by allocating alloc_len here we will have at least
|
|
|
|
* 1 byte of space available as required by print_buf_lines().
|
|
|
|
*/
|
2019-09-26 00:01:58 +02:00
|
|
|
char* nbuf = static_cast<char*>(malloc(a_buf->e_buf.alloc_len));
|
2013-04-03 22:45:10 +02:00
|
|
|
if (!nbuf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int first_chunk_len = a_buf->e_buf.buf_size - a_buf->e_buf.read;
|
|
|
|
memcpy(nbuf, a_buf->e_buf.buf + a_buf->e_buf.read, first_chunk_len);
|
|
|
|
/* copy second chunk */
|
|
|
|
memcpy(nbuf + first_chunk_len, a_buf->e_buf.buf, a_buf->e_buf.write);
|
|
|
|
print_buf_lines(log_info, nbuf, first_chunk_len + a_buf->e_buf.write);
|
|
|
|
free(nbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
static void signal_handler(int signal_num);
|
|
|
|
|
|
|
|
static void block_signals(sigset_t* oldset) {
|
|
|
|
sigset_t blockset;
|
|
|
|
|
|
|
|
sigemptyset(&blockset);
|
|
|
|
sigaddset(&blockset, SIGINT);
|
|
|
|
sigaddset(&blockset, SIGQUIT);
|
|
|
|
sigaddset(&blockset, SIGHUP);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &blockset, oldset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unblock_signals(sigset_t* oldset) {
|
2019-09-26 00:01:58 +02:00
|
|
|
pthread_sigmask(SIG_SETMASK, oldset, nullptr);
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_signal_handlers(pid_t pid) {
|
|
|
|
struct sigaction handler = {.sa_handler = signal_handler};
|
|
|
|
|
|
|
|
child_pid = pid;
|
|
|
|
sigaction(SIGINT, &handler, &old_int);
|
|
|
|
sigaction(SIGQUIT, &handler, &old_quit);
|
|
|
|
sigaction(SIGHUP, &handler, &old_hup);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void restore_signal_handlers() {
|
2019-09-26 00:01:58 +02:00
|
|
|
sigaction(SIGINT, &old_int, nullptr);
|
|
|
|
sigaction(SIGQUIT, &old_quit, nullptr);
|
|
|
|
sigaction(SIGHUP, &old_hup, nullptr);
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
child_pid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_handler(int signal_num) {
|
|
|
|
if (child_pid == 0 || kill(child_pid, signal_num) != 0) {
|
|
|
|
restore_signal_handlers();
|
|
|
|
raise(signal_num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, int log_target,
|
2019-09-26 00:01:58 +02:00
|
|
|
bool abbreviated, const char* file_path, bool forward_signals) {
|
2013-01-16 21:08:04 +01:00
|
|
|
int status = 0;
|
2013-01-11 00:21:18 +01:00
|
|
|
char buffer[4096];
|
2013-01-10 06:31:25 +01:00
|
|
|
struct pollfd poll_fds[] = {
|
2019-09-26 00:01:58 +02:00
|
|
|
{
|
|
|
|
.fd = parent_read,
|
|
|
|
.events = POLLIN,
|
|
|
|
},
|
2013-01-10 06:31:25 +01:00
|
|
|
};
|
2013-01-16 21:08:04 +01:00
|
|
|
int rc = 0;
|
2013-09-19 02:49:21 +02:00
|
|
|
int fd;
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-04-03 22:45:10 +02:00
|
|
|
struct log_info log_info;
|
|
|
|
|
2013-01-11 00:21:18 +01:00
|
|
|
int a = 0; // start index of unprocessed data
|
|
|
|
int b = 0; // end index of unprocessed data
|
|
|
|
int sz;
|
2013-01-16 21:08:04 +01:00
|
|
|
bool found_child = false;
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
// There is a very small chance that opening child_ptty in the child will fail, but in this case
|
|
|
|
// POLLHUP will not be generated below. Therefore, we use a 1 second timeout for poll() until
|
|
|
|
// we receive a message from child_ptty. If this times out, we call waitpid() with WNOHANG to
|
|
|
|
// check the status of the child process and exit appropriately if it has terminated.
|
|
|
|
bool received_messages = false;
|
2013-04-03 22:45:10 +02:00
|
|
|
char tmpbuf[256];
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-04-03 22:45:10 +02:00
|
|
|
log_info.btag = basename(tag);
|
|
|
|
if (!log_info.btag) {
|
2019-09-26 00:01:58 +02:00
|
|
|
log_info.btag = tag;
|
2013-04-03 22:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (abbreviated && (log_target == LOG_NONE)) {
|
|
|
|
abbreviated = 0;
|
|
|
|
}
|
|
|
|
if (abbreviated) {
|
|
|
|
init_abbr_buf(&log_info.a_buf);
|
|
|
|
}
|
|
|
|
|
2013-09-19 02:49:21 +02:00
|
|
|
if (log_target & LOG_KLOG) {
|
2019-09-26 00:01:58 +02:00
|
|
|
snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt), "<6>%.*s: %%s\n", MAX_KLOG_TAG,
|
|
|
|
log_info.btag);
|
2013-04-03 22:45:10 +02:00
|
|
|
}
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-09-19 02:49:21 +02:00
|
|
|
if ((log_target & LOG_FILE) && !file_path) {
|
|
|
|
/* No file_path specified, clear the LOG_FILE bit */
|
|
|
|
log_target &= ~LOG_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (log_target & LOG_FILE) {
|
|
|
|
fd = open(file_path, O_WRONLY | O_CREAT, 0664);
|
|
|
|
if (fd < 0) {
|
|
|
|
ERROR("Cannot log to file %s\n", file_path);
|
|
|
|
log_target &= ~LOG_FILE;
|
|
|
|
} else {
|
|
|
|
lseek(fd, 0, SEEK_END);
|
|
|
|
log_info.fp = fdopen(fd, "a");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info.log_target = log_target;
|
|
|
|
log_info.abbreviated = abbreviated;
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
while (!found_child) {
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
int timeout = received_messages ? -1 : 1000;
|
2019-09-26 00:01:58 +02:00
|
|
|
if (TEMP_FAILURE_RETRY(poll(poll_fds, arraysize(poll_fds), timeout)) < 0) {
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR("poll failed\n");
|
2013-01-16 21:08:04 +01:00
|
|
|
rc = -1;
|
|
|
|
goto err_poll;
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-03-20 21:46:53 +01:00
|
|
|
if (poll_fds[0].revents & POLLIN) {
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
received_messages = true;
|
2019-09-26 00:01:58 +02:00
|
|
|
sz = TEMP_FAILURE_RETRY(read(parent_read, &buffer[b], sizeof(buffer) - 1 - b));
|
2013-01-10 06:31:25 +01:00
|
|
|
|
2013-03-20 21:46:53 +01:00
|
|
|
sz += b;
|
|
|
|
// Log one line at a time
|
|
|
|
for (b = 0; b < sz; b++) {
|
|
|
|
if (buffer[b] == '\r') {
|
2013-04-03 22:45:10 +02:00
|
|
|
if (abbreviated) {
|
|
|
|
/* The abbreviated logging code uses newline as
|
|
|
|
* the line separator. Lucikly, the pty layer
|
|
|
|
* helpfully cooks the output of the command
|
|
|
|
* being run and inserts a CR before NL. So
|
|
|
|
* I just change it to NL here when doing
|
|
|
|
* abbreviated logging.
|
|
|
|
*/
|
|
|
|
buffer[b] = '\n';
|
|
|
|
} else {
|
|
|
|
buffer[b] = '\0';
|
|
|
|
}
|
2013-03-20 21:46:53 +01:00
|
|
|
} else if (buffer[b] == '\n') {
|
2013-01-10 06:31:25 +01:00
|
|
|
buffer[b] = '\0';
|
2013-04-03 22:45:10 +02:00
|
|
|
log_line(&log_info, &buffer[a], b - a);
|
2013-03-20 21:46:53 +01:00
|
|
|
a = b + 1;
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-20 21:46:53 +01:00
|
|
|
if (a == 0 && b == sizeof(buffer) - 1) {
|
|
|
|
// buffer is full, flush
|
|
|
|
buffer[b] = '\0';
|
2013-04-03 22:45:10 +02:00
|
|
|
log_line(&log_info, &buffer[a], b - a);
|
2013-03-20 21:46:53 +01:00
|
|
|
b = 0;
|
|
|
|
} else if (a != b) {
|
|
|
|
// Keep left-overs
|
|
|
|
b -= a;
|
|
|
|
memmove(buffer, &buffer[a], b);
|
|
|
|
a = 0;
|
|
|
|
} else {
|
|
|
|
a = 0;
|
|
|
|
b = 0;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
if (!received_messages || (poll_fds[0].revents & POLLHUP)) {
|
2013-01-16 21:08:04 +01:00
|
|
|
int ret;
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
sigset_t oldset;
|
|
|
|
|
|
|
|
if (forward_signals) {
|
|
|
|
// Our signal handlers forward these signals to 'child_pid', but waitpid() may reap
|
|
|
|
// the child, so we must block these signals until we either 1) conclude that the
|
|
|
|
// child is still running or 2) determine the child has been reaped and we have
|
|
|
|
// reset the signals to their original disposition.
|
|
|
|
block_signals(&oldset);
|
|
|
|
}
|
2013-01-11 00:21:18 +01:00
|
|
|
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
int flags = (poll_fds[0].revents & POLLHUP) ? 0 : WNOHANG;
|
|
|
|
ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, flags));
|
2013-03-20 21:46:53 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
rc = errno;
|
|
|
|
ALOG(LOG_ERROR, "logwrap", "waitpid failed with %s\n", strerror(errno));
|
|
|
|
goto err_waitpid;
|
|
|
|
}
|
|
|
|
if (ret > 0) {
|
|
|
|
found_child = true;
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
|
|
|
|
if (forward_signals) {
|
|
|
|
if (found_child) {
|
|
|
|
restore_signal_handlers();
|
|
|
|
}
|
|
|
|
unblock_signals(&oldset);
|
|
|
|
}
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
2013-01-10 06:31:25 +01:00
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
if (chld_sts != nullptr) {
|
2013-02-14 01:31:58 +01:00
|
|
|
*chld_sts = status;
|
|
|
|
} else {
|
2019-09-26 00:01:58 +02:00
|
|
|
if (WIFEXITED(status))
|
|
|
|
rc = WEXITSTATUS(status);
|
|
|
|
else
|
|
|
|
rc = -ECHILD;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
|
2013-04-03 22:45:10 +02:00
|
|
|
// Flush remaining data
|
|
|
|
if (a != b) {
|
2019-09-26 00:01:58 +02:00
|
|
|
buffer[b] = '\0';
|
|
|
|
log_line(&log_info, &buffer[a], b - a);
|
2013-04-03 22:45:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* All the output has been processed, time to dump the abbreviated output */
|
|
|
|
if (abbreviated) {
|
|
|
|
print_abbr_buf(&log_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
2019-09-26 00:01:58 +02:00
|
|
|
if (WEXITSTATUS(status)) {
|
|
|
|
snprintf(tmpbuf, sizeof(tmpbuf), "%s terminated by exit(%d)\n", log_info.btag,
|
|
|
|
WEXITSTATUS(status));
|
|
|
|
do_log_line(&log_info, tmpbuf);
|
|
|
|
}
|
2013-04-03 22:45:10 +02:00
|
|
|
} else {
|
2019-09-26 00:01:58 +02:00
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
snprintf(tmpbuf, sizeof(tmpbuf), "%s terminated by signal %d\n", log_info.btag,
|
|
|
|
WTERMSIG(status));
|
|
|
|
do_log_line(&log_info, tmpbuf);
|
|
|
|
} else if (WIFSTOPPED(status)) {
|
|
|
|
snprintf(tmpbuf, sizeof(tmpbuf), "%s stopped by signal %d\n", log_info.btag,
|
|
|
|
WSTOPSIG(status));
|
|
|
|
do_log_line(&log_info, tmpbuf);
|
|
|
|
}
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
|
|
|
|
2013-03-20 21:46:53 +01:00
|
|
|
err_waitpid:
|
2013-01-16 21:08:04 +01:00
|
|
|
err_poll:
|
2013-09-19 02:49:21 +02:00
|
|
|
if (log_target & LOG_FILE) {
|
|
|
|
fclose(log_info.fp); /* Also closes underlying fd */
|
|
|
|
}
|
2013-04-03 22:45:10 +02:00
|
|
|
if (abbreviated) {
|
|
|
|
free_abbr_buf(&log_info.a_buf);
|
|
|
|
}
|
2013-01-16 21:08:04 +01:00
|
|
|
return rc;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
static void child(int argc, const char* const* argv) {
|
2013-01-11 00:21:18 +01:00
|
|
|
// create null terminated argv_child array
|
|
|
|
char* argv_child[argc + 1];
|
2019-09-26 00:01:58 +02:00
|
|
|
memcpy(argv_child, argv, argc * sizeof(char*));
|
|
|
|
argv_child[argc] = nullptr;
|
2013-01-11 00:21:18 +01:00
|
|
|
|
|
|
|
if (execvp(argv_child[0], argv_child)) {
|
2019-09-26 00:01:58 +02:00
|
|
|
FATAL_CHILD("executing %s failed: %s\n", argv_child[0], strerror(errno));
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 00:01:58 +02:00
|
|
|
int logwrap_fork_execvp(int argc, const char* const* argv, int* status, bool forward_signals,
|
|
|
|
int log_target, bool abbreviated, const char* file_path) {
|
2013-01-11 00:21:18 +01:00
|
|
|
pid_t pid;
|
|
|
|
int parent_ptty;
|
2013-01-16 21:08:04 +01:00
|
|
|
sigset_t oldset;
|
|
|
|
int rc = 0;
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-03-11 22:00:58 +01:00
|
|
|
rc = pthread_mutex_lock(&fd_mutex);
|
|
|
|
if (rc) {
|
|
|
|
ERROR("failed to lock signal_fd mutex\n");
|
|
|
|
goto err_lock;
|
|
|
|
}
|
|
|
|
|
2013-01-11 00:21:18 +01:00
|
|
|
/* Use ptty instead of socketpair so that STDOUT is not buffered */
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
parent_ptty = TEMP_FAILURE_RETRY(posix_openpt(O_RDWR | O_CLOEXEC));
|
2013-01-11 00:21:18 +01:00
|
|
|
if (parent_ptty < 0) {
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR("Cannot create parent ptty\n");
|
2013-01-16 21:08:04 +01:00
|
|
|
rc = -1;
|
|
|
|
goto err_open;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 20:05:18 +02:00
|
|
|
char child_devname[64];
|
2013-01-11 00:21:18 +01:00
|
|
|
if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
|
2019-09-26 00:01:58 +02:00
|
|
|
ptsname_r(parent_ptty, child_devname, sizeof(child_devname)) != 0) {
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR("Problem with /dev/ptmx\n");
|
2013-01-16 21:08:04 +01:00
|
|
|
rc = -1;
|
|
|
|
goto err_ptty;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
if (forward_signals) {
|
|
|
|
// Block these signals until we have the child pid and our signal handlers set up.
|
|
|
|
block_signals(&oldset);
|
2013-03-20 21:46:53 +01:00
|
|
|
}
|
|
|
|
|
2013-01-11 00:21:18 +01:00
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR("Failed to fork\n");
|
2013-01-16 21:08:04 +01:00
|
|
|
rc = -1;
|
|
|
|
goto err_fork;
|
2013-01-11 00:21:18 +01:00
|
|
|
} else if (pid == 0) {
|
2013-03-11 22:00:58 +01:00
|
|
|
pthread_mutex_unlock(&fd_mutex);
|
2019-09-26 00:01:58 +02:00
|
|
|
if (forward_signals) {
|
|
|
|
unblock_signals(&oldset);
|
|
|
|
}
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
|
|
|
|
setsid();
|
|
|
|
|
|
|
|
int child_ptty = TEMP_FAILURE_RETRY(open(child_devname, O_RDWR | O_CLOEXEC));
|
|
|
|
if (child_ptty < 0) {
|
|
|
|
FATAL_CHILD("Cannot open child_ptty: %s\n", strerror(errno));
|
|
|
|
}
|
2013-01-11 00:21:18 +01:00
|
|
|
close(parent_ptty);
|
2013-01-16 21:08:04 +01:00
|
|
|
|
2013-01-11 00:21:18 +01:00
|
|
|
dup2(child_ptty, 1);
|
|
|
|
dup2(child_ptty, 2);
|
|
|
|
close(child_ptty);
|
|
|
|
|
2013-04-03 22:45:10 +02:00
|
|
|
child(argc, argv);
|
2013-01-11 00:21:18 +01:00
|
|
|
} else {
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
if (forward_signals) {
|
|
|
|
setup_signal_handlers(pid);
|
|
|
|
unblock_signals(&oldset);
|
2013-01-09 19:20:25 +01:00
|
|
|
}
|
|
|
|
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
rc = parent(argv[0], parent_ptty, pid, status, log_target, abbreviated, file_path,
|
|
|
|
forward_signals);
|
2013-01-16 21:08:04 +01:00
|
|
|
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
if (forward_signals) {
|
|
|
|
restore_signal_handlers();
|
|
|
|
}
|
2013-01-09 19:20:25 +01:00
|
|
|
}
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
err_fork:
|
logwrapper: open child_ptty in child process and remove ignore_int_quit
Opening child_ptty before fork() means that the parent process will
use child_ptty as the controlling terminal if it doesn't already have
one. This is a problem, because when the parent_ptty closes, SIGHUP
will be sent to the parent process, since its controlling terminal has
closed.
It's better to have the child process start its own session and then
open child_ptty as its controlling terminal. In this case, the child
process will get SIGHUP if the parent process exits, but the parent
process avoids the original issue.
There is a concern that the child_ptty will never be opened and
POLLHUP will never be generated, but there is a work-around in place
with a timeout to handle that extremely rare situation.
Secondly, remove the ignore_int_quit logic. It is described to
totally ignore these signals, similar to `nohup` which should be
preferred. However, it regressed shortly after being introduced in
2013 and serves essentially no purpose currently.
Thirdly, add a forward_signals option that does what we should have
done the whole time with signals: it forwards SIGHUP, SIGQUIT, and
SIGINT to the child process and let that process handle them. This
only needs to be enabled in the `logwrapper` case itself. Other
processes should not need to change any signals. This fixes case 3)
below.
Lastly, add O_CLOEXEC where appropriate.
Test: launch process as `adb shell logwrapper yes`
1) The both processes exit when given SIGINT
2) The process prints when abbreviated is not enabled
3) The process does print when abbreviated is enabled;
this was previously broken
Test: launch process as `adb shell` then `logwrapper yes`
4) The both processes exit when given SIGINT
5) The process prints if abbreviated is enabled or not.
6) Ctrl-c then Ctrl-c again rapidly and observe that the
logwrapper process is terminated by the second Ctrl-c.
Test: simulate a failure in child() before opening child_tty and
observe logwrapper and the child exiting appropriately.
Change-Id: Ia76cd17e16535500170d8f5e2183b3bbbf843df7
2019-09-25 03:58:38 +02:00
|
|
|
if (forward_signals) {
|
|
|
|
unblock_signals(&oldset);
|
|
|
|
}
|
2013-01-16 21:08:04 +01:00
|
|
|
err_ptty:
|
|
|
|
close(parent_ptty);
|
|
|
|
err_open:
|
2013-03-11 22:00:58 +01:00
|
|
|
pthread_mutex_unlock(&fd_mutex);
|
|
|
|
err_lock:
|
2013-01-16 21:08:04 +01:00
|
|
|
return rc;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|