2009-03-04 11:32:55 +08: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 23:00:38 +08:00
|
|
|
#include <errno.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-01-11 07:21:18 +08:00
|
|
|
#include <sys/wait.h>
|
2013-04-04 04:45:10 +08:00
|
|
|
#include <unistd.h>
|
2013-01-11 07:21:18 +08:00
|
|
|
|
|
|
|
#include <logwrap/logwrap.h>
|
2013-04-04 04:45:10 +08:00
|
|
|
#include <cutils/klog.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
#include "cutils/log.h"
|
|
|
|
|
|
|
|
void fatal(const char *msg) {
|
2010-05-14 06:38:49 +08:00
|
|
|
fprintf(stderr, "%s", msg);
|
2011-10-13 00:22:43 +08:00
|
|
|
ALOG(LOG_ERROR, "logwrapper", "%s", msg);
|
2009-03-04 11:32:55 +08:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage() {
|
|
|
|
fatal(
|
2013-04-04 04:45:10 +08:00
|
|
|
"Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n"
|
2009-03-04 11:32:55 +08: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 08:20:36 +08:00
|
|
|
"always LOG_INFO.\n"
|
|
|
|
"\n"
|
2013-04-04 04:45:10 +08: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 08:20:36 +08:00
|
|
|
"-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
|
2013-04-04 04:45:10 +08: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 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2013-01-05 08:20:36 +08:00
|
|
|
int seg_fault_on_exit = 0;
|
2013-04-04 04:45:10 +08:00
|
|
|
int log_target = LOG_ALOG;
|
|
|
|
bool abbreviated = false;
|
|
|
|
int ch;
|
2013-01-11 07:21:18 +08:00
|
|
|
int status = 0xAAAA;
|
|
|
|
int rc;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2013-04-04 04:45:10 +08: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 08:20:36 +08:00
|
|
|
}
|
2013-04-04 04:45:10 +08:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2013-01-05 08:20:36 +08:00
|
|
|
|
2013-04-04 04:45:10 +08:00
|
|
|
if (argc < 1) {
|
2013-01-05 08:20:36 +08:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2013-04-04 04:45:10 +08:00
|
|
|
rc = android_fork_execvp_ext(argc, &argv[0], &status, true,
|
2013-09-19 08:49:21 +08:00
|
|
|
log_target, abbreviated, NULL);
|
2013-01-11 07:21:18 +08:00
|
|
|
if (!rc) {
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
rc = WEXITSTATUS(status);
|
|
|
|
else
|
|
|
|
rc = -ECHILD;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 07:21:18 +08:00
|
|
|
if (seg_fault_on_exit) {
|
2014-01-17 02:53:11 +08:00
|
|
|
uintptr_t fault_address = (uintptr_t) status;
|
|
|
|
*(int *) fault_address = 0; // causes SIGSEGV with fault_address = status
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 07:21:18 +08:00
|
|
|
return rc;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|