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 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame^] | 19 | #include <sys/wait.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 20 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame^] | 21 | #include <logwrap/logwrap.h> |
| 22 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include "cutils/log.h" |
| 24 | |
| 25 | void fatal(const char *msg) { |
Nick Kralevich | dd26bb3 | 2010-05-13 15:38:49 -0700 | [diff] [blame] | 26 | fprintf(stderr, "%s", msg); |
Steve Block | 61fbcbe | 2011-10-12 17:22:43 +0100 | [diff] [blame] | 27 | ALOG(LOG_ERROR, "logwrapper", "%s", msg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | exit(-1); |
| 29 | } |
| 30 | |
| 31 | void usage() { |
| 32 | fatal( |
Rom Lemarchand | b10c7b4 | 2013-01-04 16:20:36 -0800 | [diff] [blame] | 33 | "Usage: logwrapper [-d] BINARY [ARGS ...]\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | "\n" |
| 35 | "Forks and executes BINARY ARGS, redirecting stdout and stderr to\n" |
| 36 | "the Android logging system. Tag is set to BINARY, priority is\n" |
Rom Lemarchand | b10c7b4 | 2013-01-04 16:20:36 -0800 | [diff] [blame] | 37 | "always LOG_INFO.\n" |
| 38 | "\n" |
| 39 | "-d: Causes logwrapper to SIGSEGV when BINARY terminates\n" |
| 40 | " fault address is set to the status of wait()\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | } |
| 42 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | int main(int argc, char* argv[]) { |
Rom Lemarchand | b10c7b4 | 2013-01-04 16:20:36 -0800 | [diff] [blame] | 44 | int seg_fault_on_exit = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame^] | 45 | int status = 0xAAAA; |
| 46 | int rc; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | |
| 48 | if (argc < 2) { |
| 49 | usage(); |
| 50 | } |
| 51 | |
Rom Lemarchand | b10c7b4 | 2013-01-04 16:20:36 -0800 | [diff] [blame] | 52 | if (strncmp(argv[1], "-d", 2) == 0) { |
| 53 | seg_fault_on_exit = 1; |
| 54 | argc--; |
| 55 | argv++; |
| 56 | } |
| 57 | |
| 58 | if (argc < 2) { |
| 59 | usage(); |
| 60 | } |
| 61 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame^] | 62 | rc = logwrap(argc, argv, &status); |
| 63 | if (!rc) { |
| 64 | if (WIFEXITED(status)) |
| 65 | rc = WEXITSTATUS(status); |
| 66 | else |
| 67 | rc = -ECHILD; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame^] | 70 | if (seg_fault_on_exit) { |
| 71 | *(int *)status = 0; // causes SIGSEGV with fault_address = status |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame^] | 74 | return rc; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | } |