logwrapper.c revision 75c289aab9b81dc2235680cf141a4b183ee49391
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 17#include <stdio.h> 18#include <stdlib.h> 19#include <sys/wait.h> 20 21#include <logwrap/logwrap.h> 22 23#include "cutils/log.h" 24 25void fatal(const char *msg) { 26 fprintf(stderr, "%s", msg); 27 ALOG(LOG_ERROR, "logwrapper", "%s", msg); 28 exit(-1); 29} 30 31void usage() { 32 fatal( 33 "Usage: logwrapper [-d] BINARY [ARGS ...]\n" 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" 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"); 41} 42 43int main(int argc, char* argv[]) { 44 int seg_fault_on_exit = 0; 45 int status = 0xAAAA; 46 int rc; 47 48 if (argc < 2) { 49 usage(); 50 } 51 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 62 rc = logwrap(argc - 1, &argv[1], &status, true); 63 if (!rc) { 64 if (WIFEXITED(status)) 65 rc = WEXITSTATUS(status); 66 else 67 rc = -ECHILD; 68 } 69 70 if (seg_fault_on_exit) { 71 *(int *)status = 0; // causes SIGSEGV with fault_address = status 72 } 73 74 return rc; 75} 76