blob: 3b876d4e6aaf0ea22215dd919d81f16ebabbfb43 [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
17#include <string.h>
18#include <sys/types.h>
19#include <sys/wait.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23#include <errno.h>
24#include <fcntl.h>
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010025#include <libgen.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
27#include "private/android_filesystem_config.h"
28#include "cutils/log.h"
29
30void fatal(const char *msg) {
Nick Kralevichdd26bb32010-05-13 15:38:49 -070031 fprintf(stderr, "%s", msg);
Steve Block61fbcbe2011-10-12 17:22:43 +010032 ALOG(LOG_ERROR, "logwrapper", "%s", msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033 exit(-1);
34}
35
36void usage() {
37 fatal(
Rom Lemarchand4d74bcf2013-01-03 14:37:57 -080038 "Usage: logwrapper BINARY [ARGS ...]\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039 "\n"
40 "Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
41 "the Android logging system. Tag is set to BINARY, priority is\n"
Rom Lemarchand4d74bcf2013-01-03 14:37:57 -080042 "always LOG_INFO.\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043}
44
Rom Lemarchand4d74bcf2013-01-03 14:37:57 -080045void parent(const char *tag, int parent_read) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046 int status;
47 char buffer[4096];
48
49 int a = 0; // start index of unprocessed data
50 int b = 0; // end index of unprocessed data
51 int sz;
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010052
53 char *btag = basename(tag);
54 if (!btag) btag = (char*) tag;
55
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056 while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) {
57
58 sz += b;
59 // Log one line at a time
60 for (b = 0; b < sz; b++) {
61 if (buffer[b] == '\r') {
62 buffer[b] = '\0';
63 } else if (buffer[b] == '\n') {
64 buffer[b] = '\0';
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010065 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066 a = b + 1;
67 }
68 }
69
70 if (a == 0 && b == sizeof(buffer) - 1) {
71 // buffer is full, flush
72 buffer[b] = '\0';
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010073 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 b = 0;
75 } else if (a != b) {
76 // Keep left-overs
77 b -= a;
78 memmove(buffer, &buffer[a], b);
79 a = 0;
80 } else {
81 a = 0;
82 b = 0;
83 }
84
85 }
86 // Flush remaining data
87 if (a != b) {
88 buffer[b] = '\0';
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010089 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090 }
91 status = 0xAAAA;
92 if (wait(&status) != -1) { // Wait for child
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010093 if (WIFEXITED(status) && WEXITSTATUS(status))
Steve Block61fbcbe2011-10-12 17:22:43 +010094 ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095 WEXITSTATUS(status));
96 else if (WIFSIGNALED(status))
Steve Block61fbcbe2011-10-12 17:22:43 +010097 ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 WTERMSIG(status));
99 else if (WIFSTOPPED(status))
Steve Block61fbcbe2011-10-12 17:22:43 +0100100 ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 WSTOPSIG(status));
102 } else
Steve Block61fbcbe2011-10-12 17:22:43 +0100103 ALOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 strerror(errno), errno);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105}
106
107void child(int argc, char* argv[]) {
108 // create null terminated argv_child array
109 char* argv_child[argc + 1];
110 memcpy(argv_child, argv, argc * sizeof(char *));
111 argv_child[argc] = NULL;
112
113 if (execvp(argv_child[0], argv_child)) {
Steve Block61fbcbe2011-10-12 17:22:43 +0100114 ALOG(LOG_ERROR, "logwrapper",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 "executing %s failed: %s\n", argv_child[0], strerror(errno));
116 exit(-1);
117 }
118}
119
120int main(int argc, char* argv[]) {
121 pid_t pid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122
123 int parent_ptty;
124 int child_ptty;
125 char *child_devname = NULL;
126
127 if (argc < 2) {
128 usage();
129 }
130
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 /* Use ptty instead of socketpair so that STDOUT is not buffered */
132 parent_ptty = open("/dev/ptmx", O_RDWR);
133 if (parent_ptty < 0) {
134 fatal("Cannot create parent ptty\n");
135 }
136
137 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
138 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
139 fatal("Problem with /dev/ptmx\n");
140 }
141
142 pid = fork();
143 if (pid < 0) {
144 fatal("Failed to fork\n");
145 } else if (pid == 0) {
146 child_ptty = open(child_devname, O_RDWR);
147 if (child_ptty < 0) {
148 fatal("Problem with child ptty\n");
149 }
150
151 // redirect stdout and stderr
152 close(parent_ptty);
153 dup2(child_ptty, 1);
154 dup2(child_ptty, 2);
155 close(child_ptty);
156
157 child(argc - 1, &argv[1]);
158
159 } else {
160 // switch user and group to "log"
161 // this may fail if we are not root,
162 // but in that case switching user/group is unnecessary
163 setgid(AID_LOG);
164 setuid(AID_LOG);
165
Rom Lemarchand4d74bcf2013-01-03 14:37:57 -0800166 parent(argv[1], parent_ptty);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 }
168
169 return 0;
170}