Merge changes from topic "logwrapper-api-update"

* changes:
  Update init/fs_mgr for new logwrapper function
  logwrap: convert to C++, rename function logwrap_fork_execvp().
This commit is contained in:
Tom Cherry 2019-09-27 15:31:04 +00:00 committed by Gerrit Code Review
commit aef269f11b
7 changed files with 155 additions and 193 deletions

View file

@ -222,13 +222,11 @@ static void check_fs(const std::string& blk_device, const std::string& fs_type,
} else {
LINFO << "Running " << E2FSCK_BIN << " on " << realpath(blk_device);
if (should_force_check(*fs_stat)) {
ret = android_fork_execvp_ext(
ARRAY_SIZE(e2fsck_forced_argv), const_cast<char**>(e2fsck_forced_argv), &status,
true, LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
ret = logwrap_fork_execvp(ARRAY_SIZE(e2fsck_forced_argv), e2fsck_forced_argv,
&status, false, LOG_KLOG | LOG_FILE, true, FSCK_LOG_FILE);
} else {
ret = android_fork_execvp_ext(
ARRAY_SIZE(e2fsck_argv), const_cast<char**>(e2fsck_argv), &status, true,
LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
ret = logwrap_fork_execvp(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, &status, false,
LOG_KLOG | LOG_FILE, true, FSCK_LOG_FILE);
}
if (ret < 0) {
@ -246,14 +244,12 @@ static void check_fs(const std::string& blk_device, const std::string& fs_type,
if (should_force_check(*fs_stat)) {
LINFO << "Running " << F2FS_FSCK_BIN << " -f " << realpath(blk_device);
ret = android_fork_execvp_ext(
ARRAY_SIZE(f2fs_fsck_forced_argv), const_cast<char**>(f2fs_fsck_forced_argv), &status,
true, LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
ret = logwrap_fork_execvp(ARRAY_SIZE(f2fs_fsck_forced_argv), f2fs_fsck_forced_argv,
&status, false, LOG_KLOG | LOG_FILE, true, FSCK_LOG_FILE);
} else {
LINFO << "Running " << F2FS_FSCK_BIN << " -a " << realpath(blk_device);
ret = android_fork_execvp_ext(
ARRAY_SIZE(f2fs_fsck_argv), const_cast<char**>(f2fs_fsck_argv), &status, true,
LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
ret = logwrap_fork_execvp(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv, &status, false,
LOG_KLOG | LOG_FILE, true, FSCK_LOG_FILE);
}
if (ret < 0) {
/* No need to check for error in fork, we can't really handle it now */
@ -331,8 +327,7 @@ static bool tune2fs_available(void) {
static bool run_tune2fs(const char* argv[], int argc) {
int ret;
ret = android_fork_execvp_ext(argc, const_cast<char**>(argv), nullptr, true,
LOG_KLOG | LOG_FILE, true, nullptr, nullptr, 0);
ret = logwrap_fork_execvp(argc, argv, nullptr, false, LOG_KLOG, true, nullptr);
return ret == 0;
}
@ -852,37 +847,22 @@ static int handle_encryptable(const FstabEntry& entry) {
}
}
static bool call_vdc(const std::vector<std::string>& args) {
static bool call_vdc(const std::vector<std::string>& args, int* ret) {
std::vector<char const*> argv;
argv.emplace_back("/system/bin/vdc");
for (auto& arg : args) {
argv.emplace_back(arg.c_str());
}
LOG(INFO) << "Calling: " << android::base::Join(argv, ' ');
int ret =
android_fork_execvp(argv.size(), const_cast<char**>(argv.data()), nullptr, false, true);
if (ret != 0) {
LOG(ERROR) << "vdc returned error code: " << ret;
return false;
}
LOG(DEBUG) << "vdc finished successfully";
return true;
}
static bool call_vdc_ret(const std::vector<std::string>& args, int* ret) {
std::vector<char const*> argv;
argv.emplace_back("/system/bin/vdc");
for (auto& arg : args) {
argv.emplace_back(arg.c_str());
}
LOG(INFO) << "Calling: " << android::base::Join(argv, ' ');
int err = android_fork_execvp(argv.size(), const_cast<char**>(argv.data()), ret, false, true);
int err = logwrap_fork_execvp(argv.size(), argv.data(), ret, false, LOG_ALOG, false, nullptr);
if (err != 0) {
LOG(ERROR) << "vdc call failed with error code: " << err;
return false;
}
LOG(DEBUG) << "vdc finished successfully";
*ret = WEXITSTATUS(*ret);
if (ret != nullptr) {
*ret = WEXITSTATUS(*ret);
}
return true;
}
@ -914,11 +894,11 @@ class CheckpointManager {
}
if (entry->fs_mgr_flags.checkpoint_blk) {
call_vdc({"checkpoint", "restoreCheckpoint", entry->blk_device});
call_vdc({"checkpoint", "restoreCheckpoint", entry->blk_device}, nullptr);
}
if (needs_checkpoint_ == UNKNOWN &&
!call_vdc_ret({"checkpoint", "needsCheckpoint"}, &needs_checkpoint_)) {
!call_vdc({"checkpoint", "needsCheckpoint"}, &needs_checkpoint_)) {
LERROR << "Failed to find if checkpointing is needed. Assuming no.";
needs_checkpoint_ = NO;
}
@ -1193,7 +1173,8 @@ int fs_mgr_mount_all(Fstab* fstab, int mount_mode) {
encryptable = status;
if (status == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) {
if (!call_vdc({"cryptfs", "encryptFstab", attempted_entry.blk_device,
attempted_entry.mount_point})) {
attempted_entry.mount_point},
nullptr)) {
LERROR << "Encryption failed";
return FS_MGR_MNTALL_FAIL;
}
@ -1265,7 +1246,8 @@ int fs_mgr_mount_all(Fstab* fstab, int mount_mode) {
} else if (mount_errno != EBUSY && mount_errno != EACCES &&
should_use_metadata_encryption(attempted_entry)) {
if (!call_vdc({"cryptfs", "mountFstab", attempted_entry.blk_device,
attempted_entry.mount_point})) {
attempted_entry.mount_point},
nullptr)) {
++error_count;
}
encryptable = FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED;
@ -1615,10 +1597,8 @@ bool fs_mgr_swapon_all(const Fstab& fstab) {
MKSWAP_BIN,
entry.blk_device.c_str(),
};
int err = 0;
int status;
err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), const_cast<char**>(mkswap_argv),
&status, true, LOG_KLOG, false, nullptr, nullptr, 0);
int err = logwrap_fork_execvp(ARRAY_SIZE(mkswap_argv), mkswap_argv, nullptr, false,
LOG_KLOG, false, nullptr);
if (err) {
LERROR << "mkswap failed for " << entry.blk_device;
ret = false;

View file

@ -76,8 +76,8 @@ static int format_ext4(const std::string& fs_blkdev, const std::string& fs_mnt_p
"/system/bin/mke2fs", "-t", "ext4", "-b", "4096", fs_blkdev.c_str(),
size_str.c_str(), nullptr};
rc = android_fork_execvp_ext(arraysize(mke2fs_args), const_cast<char**>(mke2fs_args), NULL,
true, LOG_KLOG, true, nullptr, nullptr, 0);
rc = logwrap_fork_execvp(arraysize(mke2fs_args), mke2fs_args, nullptr, false, LOG_KLOG, true,
nullptr);
if (rc) {
LERROR << "mke2fs returned " << rc;
return rc;
@ -86,8 +86,8 @@ static int format_ext4(const std::string& fs_blkdev, const std::string& fs_mnt_p
const char* const e2fsdroid_args[] = {
"/system/bin/e2fsdroid", "-e", "-a", fs_mnt_point.c_str(), fs_blkdev.c_str(), nullptr};
rc = android_fork_execvp_ext(arraysize(e2fsdroid_args), const_cast<char**>(e2fsdroid_args),
NULL, true, LOG_KLOG, true, nullptr, nullptr, 0);
rc = logwrap_fork_execvp(arraysize(e2fsdroid_args), e2fsdroid_args, nullptr, false, LOG_KLOG,
true, nullptr);
if (rc) {
LERROR << "e2fsdroid returned " << rc;
}
@ -119,8 +119,7 @@ static int format_f2fs(const std::string& fs_blkdev, uint64_t dev_sz, bool crypt
};
// clang-format on
return android_fork_execvp_ext(arraysize(args), const_cast<char**>(args), NULL, true,
LOG_KLOG, true, nullptr, nullptr, 0);
return logwrap_fork_execvp(arraysize(args), args, nullptr, false, LOG_KLOG, true, nullptr);
}
int fs_mgr_do_format(const FstabEntry& entry, bool crypt_footer) {

View file

@ -116,16 +116,16 @@ class MountEntry {
"-a",
mnt_fsname_.c_str(),
};
android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
true, nullptr, nullptr, 0);
logwrap_fork_execvp(arraysize(f2fs_argv), f2fs_argv, &st, false, LOG_KLOG, true,
nullptr);
} else if (IsExt4()) {
const char* ext4_argv[] = {
"/system/bin/e2fsck",
"-y",
mnt_fsname_.c_str(),
};
android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
true, nullptr, nullptr, 0);
logwrap_fork_execvp(arraysize(ext4_argv), ext4_argv, &st, false, LOG_KLOG, true,
nullptr);
}
}
@ -163,8 +163,7 @@ static void TurnOffBacklight() {
static void ShutdownVold() {
const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
int status;
android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
nullptr, nullptr, 0);
logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, true, nullptr);
}
static void LogShutdownTime(UmountStat stat, Timer* t) {
@ -221,8 +220,8 @@ static void DumpUmountDebuggingInfo() {
if (!security_getenforce()) {
LOG(INFO) << "Run lsof";
const char* lsof_argv[] = {"/system/bin/lsof"};
android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
true, nullptr, nullptr, 0);
logwrap_fork_execvp(arraysize(lsof_argv), lsof_argv, &status, false, LOG_KLOG, true,
nullptr);
}
FindPartitionsToUmount(nullptr, nullptr, true);
// dump current CPU stack traces and uninterruptible tasks
@ -317,8 +316,8 @@ void RebootMonitorThread(unsigned int cmd, const std::string& rebootTarget, sem_
LOG(INFO) << "Try to dump init process call trace:";
const char* vdc_argv[] = {"/system/bin/debuggerd", "-b", "1"};
int status;
android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true,
LOG_KLOG, true, nullptr, nullptr, 0);
logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG,
true, nullptr);
}
LOG(INFO) << "Show stack for all active CPU:";
WriteStringToFile("l", PROC_SYSRQ);

View file

@ -13,11 +13,12 @@ cc_library {
name: "liblogwrap",
defaults: ["logwrapper_defaults"],
recovery_available: true,
srcs: ["logwrap.c"],
srcs: ["logwrap.cpp"],
shared_libs: [
"libcutils",
"liblog",
],
header_libs: ["libbase_headers"],
export_include_dirs: ["include"],
local_include_dirs: ["include"],
}
@ -31,9 +32,10 @@ cc_defaults {
defaults: ["logwrapper_defaults"],
local_include_dirs: ["include"],
srcs: [
"logwrap.c",
"logwrapper.c",
"logwrap.cpp",
"logwrapper.cpp",
],
header_libs: ["libbase_headers"],
shared_libs: ["libcutils", "liblog"],
}

View file

@ -17,11 +17,6 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
__BEGIN_DECLS
/*
* Run a command while logging its stdout and stderr
*
@ -56,35 +51,32 @@ __BEGIN_DECLS
*
*/
/* Values for the log_target parameter android_fork_execvp_ext() */
/* Values for the log_target parameter logwrap_fork_execvp() */
#define LOG_NONE 0
#define LOG_ALOG 1
#define LOG_KLOG 2
#define LOG_FILE 4
int android_fork_execvp_ext2(int argc, char* argv[], int* status, bool forward_signals,
int log_target, bool abbreviated, char* file_path);
int logwrap_fork_execvp(int argc, const char* const* argv, int* status, bool forward_signals,
int log_target, bool abbreviated, const char* file_path);
// TODO: Actually deprecate this and the below.
static inline int android_fork_execvp_ext(int argc, char* argv[], int* status, bool ignore_int_quit,
int log_target, bool abbreviated, char* file_path,
int log_target, bool abbreviated, const char* file_path,
void* unused_opts, int unused_opts_len) {
(void)ignore_int_quit;
(void)unused_opts;
(void)unused_opts_len;
return android_fork_execvp_ext2(argc, argv, status, false, log_target, abbreviated, file_path);
return logwrap_fork_execvp(argc, argv, status, false, log_target, abbreviated, file_path);
}
/* Similar to above, except abbreviated logging is not available, and if logwrap
* is true, logging is to the Android system log, and if false, there is no
* logging.
*/
static inline int android_fork_execvp(int argc, char* argv[], int *status,
bool ignore_int_quit, bool logwrap)
{
return android_fork_execvp_ext(argc, argv, status, ignore_int_quit,
(logwrap ? LOG_ALOG : LOG_NONE), false, NULL,
NULL, 0);
static inline int android_fork_execvp(int argc, char* argv[], int* status, bool ignore_int_quit,
bool logwrap) {
(void)ignore_int_quit;
return logwrap_fork_execvp(argc, argv, status, false, (logwrap ? LOG_ALOG : LOG_NONE), false,
nullptr);
}
__END_DECLS

View file

@ -19,7 +19,6 @@
#include <libgen.h>
#include <poll.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -28,13 +27,13 @@
#include <sys/wait.h>
#include <unistd.h>
#include <algorithm>
#include <android-base/macros.h>
#include <cutils/klog.h>
#include <log/log.h>
#include <logwrap/logwrap.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
#define MIN(a,b) (((a)<(b))?(a):(b))
static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
// Protected by fd_mutex. These signals must be blocked while modifying as well.
static pid_t child_pid;
@ -42,17 +41,17 @@ static struct sigaction old_int;
static struct sigaction old_quit;
static struct sigaction old_hup;
#define ERROR(fmt, args...) \
do { \
fprintf(stderr, fmt, ## args); \
ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
} while(0)
#define ERROR(fmt, args...) \
do { \
fprintf(stderr, fmt, ##args); \
ALOG(LOG_ERROR, "logwrapper", fmt, ##args); \
} while (0)
#define FATAL_CHILD(fmt, args...) \
do { \
ERROR(fmt, ## args); \
_exit(-1); \
} while(0)
#define FATAL_CHILD(fmt, args...) \
do { \
ERROR(fmt, ##args); \
_exit(-1); \
} while (0)
#define MAX_KLOG_TAG 16
@ -61,7 +60,7 @@ do { \
*/
#define BEGINNING_BUF_SIZE 0x1000
struct beginning_buf {
char *buf;
char* buf;
size_t alloc_len;
/* buf_size is the usable space, which is one less than the allocated size */
size_t buf_size;
@ -74,7 +73,7 @@ struct beginning_buf {
*/
#define ENDING_BUF_SIZE 0x1000
struct ending_buf {
char *buf;
char* buf;
ssize_t alloc_len;
/* buf_size is the usable space, which is one less than the allocated size */
ssize_t buf_size;
@ -84,7 +83,7 @@ struct ending_buf {
int write;
};
/* A structure to hold all the abbreviated buf data */
/* A structure to hold all the abbreviated buf data */
struct abbr_buf {
struct beginning_buf b_buf;
struct ending_buf e_buf;
@ -95,19 +94,17 @@ struct abbr_buf {
struct log_info {
int log_target;
char klog_fmt[MAX_KLOG_TAG * 2];
char *btag;
const char* btag;
bool abbreviated;
FILE *fp;
FILE* fp;
struct abbr_buf a_buf;
};
/* Forware declaration */
static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen);
static void add_line_to_abbr_buf(struct abbr_buf* a_buf, char* linebuf, int linelen);
/* Return 0 on success, and 1 when full */
static int add_line_to_linear_buf(struct beginning_buf *b_buf,
char *line, ssize_t line_len)
{
static int add_line_to_linear_buf(struct beginning_buf* b_buf, char* line, ssize_t line_len) {
int full = 0;
if ((line_len + b_buf->used_len) > b_buf->buf_size) {
@ -121,20 +118,18 @@ static int add_line_to_linear_buf(struct beginning_buf *b_buf,
return full;
}
static void add_line_to_circular_buf(struct ending_buf *e_buf,
char *line, ssize_t line_len)
{
static void add_line_to_circular_buf(struct ending_buf* e_buf, char* line, ssize_t line_len) {
ssize_t free_len;
ssize_t needed_space;
int cnt;
if (e_buf->buf == NULL) {
if (e_buf->buf == nullptr) {
return;
}
if (line_len > e_buf->buf_size) {
return;
}
if (line_len > e_buf->buf_size) {
return;
}
free_len = e_buf->buf_size - e_buf->used_len;
@ -149,7 +144,7 @@ static void add_line_to_circular_buf(struct ending_buf *e_buf,
/* Copy the line into the circular buffer, dealing with possible
* wraparound.
*/
cnt = MIN(line_len, e_buf->buf_size - e_buf->write);
cnt = std::min(line_len, e_buf->buf_size - e_buf->write);
memcpy(e_buf->buf + e_buf->write, line, cnt);
if (cnt < line_len) {
memcpy(e_buf->buf, line + cnt, line_len - cnt);
@ -159,7 +154,7 @@ static void add_line_to_circular_buf(struct ending_buf *e_buf,
}
/* Log directly to the specified log */
static void do_log_line(struct log_info *log_info, char *line) {
static void do_log_line(struct log_info* log_info, const char* line) {
if (log_info->log_target & LOG_KLOG) {
klog_write(6, log_info->klog_fmt, line);
}
@ -174,7 +169,7 @@ static void do_log_line(struct log_info *log_info, char *line) {
/* Log to either the abbreviated buf, or directly to the specified log
* via do_log_line() above.
*/
static void log_line(struct log_info *log_info, char *line, int len) {
static void log_line(struct log_info* log_info, char* line, int len) {
if (log_info->abbreviated) {
add_line_to_abbr_buf(&log_info->a_buf, line, len);
} else {
@ -189,9 +184,8 @@ static void log_line(struct log_info *log_info, char *line, int len) {
* 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.
*/
static void print_buf_lines(struct log_info *log_info, char *buf, int buf_size)
{
char *line_start;
static void print_buf_lines(struct log_info* log_info, char* buf, int buf_size) {
char* line_start;
char c;
int i;
@ -217,17 +211,17 @@ static void print_buf_lines(struct log_info *log_info, char *buf, int buf_size)
*/
}
static void init_abbr_buf(struct abbr_buf *a_buf) {
char *new_buf;
static void init_abbr_buf(struct abbr_buf* a_buf) {
char* new_buf;
memset(a_buf, 0, sizeof(struct abbr_buf));
new_buf = malloc(BEGINNING_BUF_SIZE);
new_buf = static_cast<char*>(malloc(BEGINNING_BUF_SIZE));
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;
}
new_buf = malloc(ENDING_BUF_SIZE);
new_buf = static_cast<char*>(malloc(ENDING_BUF_SIZE));
if (new_buf) {
a_buf->e_buf.buf = new_buf;
a_buf->e_buf.alloc_len = ENDING_BUF_SIZE;
@ -235,23 +229,22 @@ static void init_abbr_buf(struct abbr_buf *a_buf) {
}
}
static void free_abbr_buf(struct abbr_buf *a_buf) {
static void free_abbr_buf(struct abbr_buf* a_buf) {
free(a_buf->b_buf.buf);
free(a_buf->e_buf.buf);
}
static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen) {
static void add_line_to_abbr_buf(struct abbr_buf* a_buf, char* linebuf, int linelen) {
if (!a_buf->beginning_buf_full) {
a_buf->beginning_buf_full =
add_line_to_linear_buf(&a_buf->b_buf, linebuf, linelen);
a_buf->beginning_buf_full = add_line_to_linear_buf(&a_buf->b_buf, linebuf, linelen);
}
if (a_buf->beginning_buf_full) {
add_line_to_circular_buf(&a_buf->e_buf, linebuf, linelen);
}
}
static void print_abbr_buf(struct log_info *log_info) {
struct abbr_buf *a_buf = &log_info->a_buf;
static void print_abbr_buf(struct log_info* log_info) {
struct abbr_buf* a_buf = &log_info->a_buf;
/* Add the abbreviated output to the kernel log */
if (a_buf->b_buf.alloc_len) {
@ -274,14 +267,13 @@ static void print_abbr_buf(struct log_info *log_info) {
* 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 */
print_buf_lines(log_info, a_buf->e_buf.buf + a_buf->e_buf.read,
a_buf->e_buf.used_len);
print_buf_lines(log_info, a_buf->e_buf.buf + a_buf->e_buf.read, a_buf->e_buf.used_len);
} 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().
*/
char * nbuf = malloc(a_buf->e_buf.alloc_len);
char* nbuf = static_cast<char*>(malloc(a_buf->e_buf.alloc_len));
if (!nbuf) {
return;
}
@ -307,7 +299,7 @@ static void block_signals(sigset_t* oldset) {
}
static void unblock_signals(sigset_t* oldset) {
pthread_sigmask(SIG_SETMASK, oldset, NULL);
pthread_sigmask(SIG_SETMASK, oldset, nullptr);
}
static void setup_signal_handlers(pid_t pid) {
@ -320,9 +312,9 @@ static void setup_signal_handlers(pid_t pid) {
}
static void restore_signal_handlers() {
sigaction(SIGINT, &old_int, NULL);
sigaction(SIGQUIT, &old_quit, NULL);
sigaction(SIGHUP, &old_hup, NULL);
sigaction(SIGINT, &old_int, nullptr);
sigaction(SIGQUIT, &old_quit, nullptr);
sigaction(SIGHUP, &old_hup, nullptr);
child_pid = 0;
}
@ -334,14 +326,14 @@ static void signal_handler(int signal_num) {
}
static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, int log_target,
bool abbreviated, char* file_path, bool forward_signals) {
bool abbreviated, const char* file_path, bool forward_signals) {
int status = 0;
char buffer[4096];
struct pollfd poll_fds[] = {
[0] = {
.fd = parent_read,
.events = POLLIN,
},
{
.fd = parent_read,
.events = POLLIN,
},
};
int rc = 0;
int fd;
@ -361,7 +353,7 @@ static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, in
log_info.btag = basename(tag);
if (!log_info.btag) {
log_info.btag = (char*) tag;
log_info.btag = tag;
}
if (abbreviated && (log_target == LOG_NONE)) {
@ -372,8 +364,8 @@ static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, in
}
if (log_target & LOG_KLOG) {
snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt),
"<6>%.*s: %%s\n", MAX_KLOG_TAG, log_info.btag);
snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt), "<6>%.*s: %%s\n", MAX_KLOG_TAG,
log_info.btag);
}
if ((log_target & LOG_FILE) && !file_path) {
@ -397,7 +389,7 @@ static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, in
while (!found_child) {
int timeout = received_messages ? -1 : 1000;
if (TEMP_FAILURE_RETRY(poll(poll_fds, ARRAY_SIZE(poll_fds), timeout)) < 0) {
if (TEMP_FAILURE_RETRY(poll(poll_fds, arraysize(poll_fds), timeout)) < 0) {
ERROR("poll failed\n");
rc = -1;
goto err_poll;
@ -405,8 +397,7 @@ static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, in
if (poll_fds[0].revents & POLLIN) {
received_messages = true;
sz = TEMP_FAILURE_RETRY(
read(parent_read, &buffer[b], sizeof(buffer) - 1 - b));
sz = TEMP_FAILURE_RETRY(read(parent_read, &buffer[b], sizeof(buffer) - 1 - b));
sz += b;
// Log one line at a time
@ -479,19 +470,19 @@ static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, in
}
}
if (chld_sts != NULL) {
if (chld_sts != nullptr) {
*chld_sts = status;
} else {
if (WIFEXITED(status))
rc = WEXITSTATUS(status);
else
rc = -ECHILD;
if (WIFEXITED(status))
rc = WEXITSTATUS(status);
else
rc = -ECHILD;
}
// Flush remaining data
if (a != b) {
buffer[b] = '\0';
log_line(&log_info, &buffer[a], b - a);
buffer[b] = '\0';
log_line(&log_info, &buffer[a], b - a);
}
/* All the output has been processed, time to dump the abbreviated output */
@ -500,21 +491,21 @@ static int parent(const char* tag, int parent_read, pid_t pid, int* chld_sts, in
}
if (WIFEXITED(status)) {
if (WEXITSTATUS(status)) {
snprintf(tmpbuf, sizeof(tmpbuf),
"%s terminated by exit(%d)\n", log_info.btag, WEXITSTATUS(status));
do_log_line(&log_info, tmpbuf);
}
if (WEXITSTATUS(status)) {
snprintf(tmpbuf, sizeof(tmpbuf), "%s terminated by exit(%d)\n", log_info.btag,
WEXITSTATUS(status));
do_log_line(&log_info, tmpbuf);
}
} else {
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);
}
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);
}
}
err_waitpid:
@ -528,23 +519,21 @@ err_poll:
return rc;
}
static void child(int argc, char* argv[]) {
static void child(int argc, const char* const* argv) {
// create null terminated argv_child array
char* argv_child[argc + 1];
memcpy(argv_child, argv, argc * sizeof(char *));
argv_child[argc] = NULL;
memcpy(argv_child, argv, argc * sizeof(char*));
argv_child[argc] = nullptr;
if (execvp(argv_child[0], argv_child)) {
FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
strerror(errno));
FATAL_CHILD("executing %s failed: %s\n", argv_child[0], strerror(errno));
}
}
int android_fork_execvp_ext2(int argc, char* argv[], int* status, bool forward_signals,
int log_target, bool abbreviated, char* file_path) {
int logwrap_fork_execvp(int argc, const char* const* argv, int* status, bool forward_signals,
int log_target, bool abbreviated, const char* file_path) {
pid_t pid;
int parent_ptty;
sigset_t blockset;
sigset_t oldset;
int rc = 0;
@ -564,7 +553,7 @@ int android_fork_execvp_ext2(int argc, char* argv[], int* status, bool forward_s
char child_devname[64];
if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
ptsname_r(parent_ptty, child_devname, sizeof(child_devname)) != 0) {
ptsname_r(parent_ptty, child_devname, sizeof(child_devname)) != 0) {
ERROR("Problem with /dev/ptmx\n");
rc = -1;
goto err_ptty;
@ -582,7 +571,9 @@ int android_fork_execvp_ext2(int argc, char* argv[], int* status, bool forward_s
goto err_fork;
} else if (pid == 0) {
pthread_mutex_unlock(&fd_mutex);
unblock_signals(&oldset);
if (forward_signals) {
unblock_signals(&oldset);
}
setsid();

View file

@ -24,27 +24,26 @@
#include <log/log.h>
#include <logwrap/logwrap.h>
void fatal(const char *msg) {
void fatal(const char* msg) {
fprintf(stderr, "%s", msg);
ALOG(LOG_ERROR, "logwrapper", "%s", msg);
exit(-1);
}
void usage() {
fatal(
"Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n"
"\n"
"Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
"the Android logging system. Tag is set to BINARY, priority is\n"
"always LOG_INFO.\n"
"\n"
"-a: Causes logwrapper to do abbreviated logging.\n"
" This logs up to the first 4K and last 4K of the command\n"
" being run, and logs the output when the command exits\n"
"-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
" fault address is set to the status of wait()\n"
"-k: Causes logwrapper to log to the kernel log instead of\n"
" the Android system log\n");
fatal("Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n"
"\n"
"Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
"the Android logging system. Tag is set to BINARY, priority is\n"
"always LOG_INFO.\n"
"\n"
"-a: Causes logwrapper to do abbreviated logging.\n"
" This logs up to the first 4K and last 4K of the command\n"
" being run, and logs the output when the command exits\n"
"-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
" fault address is set to the status of wait()\n"
"-k: Causes logwrapper to log to the kernel log instead of\n"
" the Android system log\n");
}
int main(int argc, char* argv[]) {
@ -69,7 +68,7 @@ int main(int argc, char* argv[]) {
break;
case '?':
default:
usage();
usage();
}
}
argc -= optind;
@ -79,7 +78,7 @@ int main(int argc, char* argv[]) {
usage();
}
rc = android_fork_execvp_ext2(argc, &argv[0], &status, true, log_target, abbreviated, NULL);
rc = logwrap_fork_execvp(argc, &argv[0], &status, true, log_target, abbreviated, nullptr);
if (!rc) {
if (WIFEXITED(status))
rc = WEXITSTATUS(status);
@ -88,8 +87,8 @@ int main(int argc, char* argv[]) {
}
if (seg_fault_on_exit) {
uintptr_t fault_address = (uintptr_t) status;
*(int *) fault_address = 0; // causes SIGSEGV with fault_address = status
uintptr_t fault_address = (uintptr_t)status;
*(int*)fault_address = 0; // causes SIGSEGV with fault_address = status
}
return rc;