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