blob: 33454c6a68230419fde1a4b2ef8824a675155e12 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
William Roberts5605cda2013-11-21 07:00:38 -080017#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#include <stdio.h>
19#include <stdlib.h>
Rom Lemarchand113bd472013-01-10 15:21:18 -080020#include <sys/wait.h>
Ken Sumrall96e11b52013-04-03 13:45:10 -070021#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022
Ken Sumrall96e11b52013-04-03 13:45:10 -070023#include <cutils/klog.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080024#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070025#include <logwrap/logwrap.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
27void fatal(const char *msg) {
Nick Kralevichdd26bb32010-05-13 15:38:49 -070028 fprintf(stderr, "%s", msg);
Steve Block61fbcbe2011-10-12 17:22:43 +010029 ALOG(LOG_ERROR, "logwrapper", "%s", msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030 exit(-1);
31}
32
33void usage() {
34 fatal(
Ken Sumrall96e11b52013-04-03 13:45:10 -070035 "Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036 "\n"
37 "Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
38 "the Android logging system. Tag is set to BINARY, priority is\n"
Rom Lemarchandb10c7b42013-01-04 16:20:36 -080039 "always LOG_INFO.\n"
40 "\n"
Ken Sumrall96e11b52013-04-03 13:45:10 -070041 "-a: Causes logwrapper to do abbreviated logging.\n"
42 " This logs up to the first 4K and last 4K of the command\n"
43 " being run, and logs the output when the command exits\n"
Rom Lemarchandb10c7b42013-01-04 16:20:36 -080044 "-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
Ken Sumrall96e11b52013-04-03 13:45:10 -070045 " fault address is set to the status of wait()\n"
46 "-k: Causes logwrapper to log to the kernel log instead of\n"
47 " the Android system log\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048}
49
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050int main(int argc, char* argv[]) {
Rom Lemarchandb10c7b42013-01-04 16:20:36 -080051 int seg_fault_on_exit = 0;
Ken Sumrall96e11b52013-04-03 13:45:10 -070052 int log_target = LOG_ALOG;
53 bool abbreviated = false;
54 int ch;
Rom Lemarchand113bd472013-01-10 15:21:18 -080055 int status = 0xAAAA;
56 int rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Ken Sumrall96e11b52013-04-03 13:45:10 -070058 while ((ch = getopt(argc, argv, "adk")) != -1) {
59 switch (ch) {
60 case 'a':
61 abbreviated = true;
62 break;
63 case 'd':
64 seg_fault_on_exit = 1;
65 break;
66 case 'k':
67 log_target = LOG_KLOG;
68 klog_set_level(6);
69 break;
70 case '?':
71 default:
72 usage();
73 }
74 }
75 argc -= optind;
76 argv += optind;
77
78 if (argc < 1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 usage();
80 }
81
Ken Sumrall96e11b52013-04-03 13:45:10 -070082 rc = android_fork_execvp_ext(argc, &argv[0], &status, true,
Yusuke Satod81c3c62015-08-14 01:22:53 -070083 log_target, abbreviated, NULL, NULL, 0);
Rom Lemarchand113bd472013-01-10 15:21:18 -080084 if (!rc) {
85 if (WIFEXITED(status))
86 rc = WEXITSTATUS(status);
87 else
88 rc = -ECHILD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089 }
90
Rom Lemarchand113bd472013-01-10 15:21:18 -080091 if (seg_fault_on_exit) {
Elliott Hughesccecf142014-01-16 10:53:11 -080092 uintptr_t fault_address = (uintptr_t) status;
93 *(int *) fault_address = 0; // causes SIGSEGV with fault_address = status
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 }
95
Rom Lemarchand113bd472013-01-10 15:21:18 -080096 return rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097}