2009-03-04 04:32:55 +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.
|
|
|
|
*/
|
|
|
|
|
2013-11-21 16:00:38 +01:00
|
|
|
#include <errno.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-01-11 00:21:18 +01:00
|
|
|
#include <sys/wait.h>
|
2013-04-03 22:45:10 +02:00
|
|
|
#include <unistd.h>
|
2013-01-11 00:21:18 +01:00
|
|
|
|
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>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
void fatal(const char *msg) {
|
2010-05-14 00:38:49 +02:00
|
|
|
fprintf(stderr, "%s", msg);
|
2011-10-12 18:22:43 +02:00
|
|
|
ALOG(LOG_ERROR, "logwrapper", "%s", msg);
|
2009-03-04 04:32:55 +01:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage() {
|
|
|
|
fatal(
|
2013-04-03 22:45:10 +02:00
|
|
|
"Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n"
|
2009-03-04 04:32:55 +01:00
|
|
|
"\n"
|
|
|
|
"Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
|
|
|
|
"the Android logging system. Tag is set to BINARY, priority is\n"
|
2013-01-05 01:20:36 +01:00
|
|
|
"always LOG_INFO.\n"
|
|
|
|
"\n"
|
2013-04-03 22:45:10 +02:00
|
|
|
"-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"
|
2013-01-05 01:20:36 +01:00
|
|
|
"-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
|
2013-04-03 22:45:10 +02:00
|
|
|
" 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");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2013-01-05 01:20:36 +01:00
|
|
|
int seg_fault_on_exit = 0;
|
2013-04-03 22:45:10 +02:00
|
|
|
int log_target = LOG_ALOG;
|
|
|
|
bool abbreviated = false;
|
|
|
|
int ch;
|
2013-01-11 00:21:18 +01:00
|
|
|
int status = 0xAAAA;
|
|
|
|
int rc;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-04-03 22:45:10 +02:00
|
|
|
while ((ch = getopt(argc, argv, "adk")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
abbreviated = true;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
seg_fault_on_exit = 1;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
log_target = LOG_KLOG;
|
|
|
|
klog_set_level(6);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
2013-01-05 01:20:36 +01:00
|
|
|
}
|
2013-04-03 22:45:10 +02:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2013-01-05 01:20:36 +01:00
|
|
|
|
2013-04-03 22:45:10 +02:00
|
|
|
if (argc < 1) {
|
2013-01-05 01:20:36 +01:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2013-04-03 22:45:10 +02:00
|
|
|
rc = android_fork_execvp_ext(argc, &argv[0], &status, true,
|
2015-08-14 10:22:53 +02:00
|
|
|
log_target, abbreviated, NULL, NULL, 0);
|
2013-01-11 00:21:18 +01:00
|
|
|
if (!rc) {
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
rc = WEXITSTATUS(status);
|
|
|
|
else
|
|
|
|
rc = -ECHILD;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2013-01-11 00:21:18 +01:00
|
|
|
if (seg_fault_on_exit) {
|
2014-01-16 19:53:11 +01:00
|
|
|
uintptr_t fault_address = (uintptr_t) status;
|
|
|
|
*(int *) fault_address = 0; // causes SIGSEGV with fault_address = status
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2013-01-11 00:21:18 +01:00
|
|
|
return rc;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|