blob: ed71a29e60e9a8761388c90143ead0accdd34c68 [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017#include <stdio.h>
18#include <stdlib.h>
Rom Lemarchand113bd472013-01-10 15:21:18 -080019#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080020
Rom Lemarchand113bd472013-01-10 15:21:18 -080021#include <logwrap/logwrap.h>
22
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include "cutils/log.h"
24
25void fatal(const char *msg) {
Nick Kralevichdd26bb32010-05-13 15:38:49 -070026 fprintf(stderr, "%s", msg);
Steve Block61fbcbe2011-10-12 17:22:43 +010027 ALOG(LOG_ERROR, "logwrapper", "%s", msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028 exit(-1);
29}
30
31void usage() {
32 fatal(
Rom Lemarchandb10c7b42013-01-04 16:20:36 -080033 "Usage: logwrapper [-d] BINARY [ARGS ...]\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034 "\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 Lemarchandb10c7b42013-01-04 16:20:36 -080037 "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 Projectdd7bc332009-03-03 19:32:55 -080041}
42
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043int main(int argc, char* argv[]) {
Rom Lemarchandb10c7b42013-01-04 16:20:36 -080044 int seg_fault_on_exit = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -080045 int status = 0xAAAA;
46 int rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
48 if (argc < 2) {
49 usage();
50 }
51
Rom Lemarchandb10c7b42013-01-04 16:20:36 -080052 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 Lemarchand2a46bfa2013-01-29 11:44:59 -080062 rc = android_fork_execvp(argc - 1, &argv[1], &status, true, true);
Rom Lemarchand113bd472013-01-10 15:21:18 -080063 if (!rc) {
64 if (WIFEXITED(status))
65 rc = WEXITSTATUS(status);
66 else
67 rc = -ECHILD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068 }
69
Rom Lemarchand113bd472013-01-10 15:21:18 -080070 if (seg_fault_on_exit) {
71 *(int *)status = 0; // causes SIGSEGV with fault_address = status
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 }
73
Rom Lemarchand113bd472013-01-10 15:21:18 -080074 return rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075}