The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Roberts | 5605cda | 2013-11-21 07:00:38 -0800 | [diff] [blame] | 17 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 20 | #include <sys/wait.h> |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 23 | #include <logwrap/logwrap.h> |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 24 | #include <cutils/klog.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 25 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include "cutils/log.h" |
| 27 | |
| 28 | void fatal(const char *msg) { |
Nick Kralevich | dd26bb3 | 2010-05-13 15:38:49 -0700 | [diff] [blame] | 29 | fprintf(stderr, "%s", msg); |
Steve Block | 61fbcbe | 2011-10-12 17:22:43 +0100 | [diff] [blame] | 30 | ALOG(LOG_ERROR, "logwrapper", "%s", msg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | exit(-1); |
| 32 | } |
| 33 | |
| 34 | void usage() { |
| 35 | fatal( |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 36 | "Usage: logwrapper [-a] [-d] [-k] BINARY [ARGS ...]\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | "\n" |
| 38 | "Forks and executes BINARY ARGS, redirecting stdout and stderr to\n" |
| 39 | "the Android logging system. Tag is set to BINARY, priority is\n" |
Rom Lemarchand | b10c7b4 | 2013-01-04 16:20:36 -0800 | [diff] [blame] | 40 | "always LOG_INFO.\n" |
| 41 | "\n" |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 42 | "-a: Causes logwrapper to do abbreviated logging.\n" |
| 43 | " This logs up to the first 4K and last 4K of the command\n" |
| 44 | " being run, and logs the output when the command exits\n" |
Rom Lemarchand | b10c7b4 | 2013-01-04 16:20:36 -0800 | [diff] [blame] | 45 | "-d: Causes logwrapper to SIGSEGV when BINARY terminates\n" |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 46 | " fault address is set to the status of wait()\n" |
| 47 | "-k: Causes logwrapper to log to the kernel log instead of\n" |
| 48 | " the Android system log\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | } |
| 50 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | int main(int argc, char* argv[]) { |
Rom Lemarchand | b10c7b4 | 2013-01-04 16:20:36 -0800 | [diff] [blame] | 52 | int seg_fault_on_exit = 0; |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 53 | int log_target = LOG_ALOG; |
| 54 | bool abbreviated = false; |
| 55 | int ch; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 56 | int status = 0xAAAA; |
| 57 | int rc; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 59 | while ((ch = getopt(argc, argv, "adk")) != -1) { |
| 60 | switch (ch) { |
| 61 | case 'a': |
| 62 | abbreviated = true; |
| 63 | break; |
| 64 | case 'd': |
| 65 | seg_fault_on_exit = 1; |
| 66 | break; |
| 67 | case 'k': |
| 68 | log_target = LOG_KLOG; |
| 69 | klog_set_level(6); |
| 70 | break; |
| 71 | case '?': |
| 72 | default: |
| 73 | usage(); |
| 74 | } |
| 75 | } |
| 76 | argc -= optind; |
| 77 | argv += optind; |
| 78 | |
| 79 | if (argc < 1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | usage(); |
| 81 | } |
| 82 | |
Ken Sumrall | 96e11b5 | 2013-04-03 13:45:10 -0700 | [diff] [blame] | 83 | rc = android_fork_execvp_ext(argc, &argv[0], &status, true, |
Yusuke Sato | d81c3c6 | 2015-08-14 01:22:53 -0700 | [diff] [blame] | 84 | log_target, abbreviated, NULL, NULL, 0); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 85 | if (!rc) { |
| 86 | if (WIFEXITED(status)) |
| 87 | rc = WEXITSTATUS(status); |
| 88 | else |
| 89 | rc = -ECHILD; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 92 | if (seg_fault_on_exit) { |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 93 | uintptr_t fault_address = (uintptr_t) status; |
| 94 | *(int *) fault_address = 0; // causes SIGSEGV with fault_address = status |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 97 | return rc; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | } |