blob: dd777c032b050591a184c4be7b30611e2ee2d482 [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 Lemarchandb10c7b42013-01-04 16:20:36 -080038 "Usage: logwrapper [-d] 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 Lemarchandb10c7b42013-01-04 16:20:36 -080042 "always LOG_INFO.\n"
43 "\n"
44 "-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
45 " fault address is set to the status of wait()\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046}
47
Rom Lemarchandb10c7b42013-01-04 16:20:36 -080048void parent(const char *tag, int seg_fault_on_exit, int parent_read) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049 int status;
50 char buffer[4096];
51
52 int a = 0; // start index of unprocessed data
53 int b = 0; // end index of unprocessed data
54 int sz;
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010055
56 char *btag = basename(tag);
57 if (!btag) btag = (char*) tag;
58
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059 while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) {
60
61 sz += b;
62 // Log one line at a time
63 for (b = 0; b < sz; b++) {
64 if (buffer[b] == '\r') {
65 buffer[b] = '\0';
66 } else if (buffer[b] == '\n') {
67 buffer[b] = '\0';
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010068 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069 a = b + 1;
70 }
71 }
72
73 if (a == 0 && b == sizeof(buffer) - 1) {
74 // buffer is full, flush
75 buffer[b] = '\0';
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010076 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 b = 0;
78 } else if (a != b) {
79 // Keep left-overs
80 b -= a;
81 memmove(buffer, &buffer[a], b);
82 a = 0;
83 } else {
84 a = 0;
85 b = 0;
86 }
87
88 }
89 // Flush remaining data
90 if (a != b) {
91 buffer[b] = '\0';
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010092 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 }
94 status = 0xAAAA;
95 if (wait(&status) != -1) { // Wait for child
Tanguy Pruvot1f28b772011-12-19 03:53:49 +010096 if (WIFEXITED(status) && WEXITSTATUS(status))
Steve Block61fbcbe2011-10-12 17:22:43 +010097 ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 WEXITSTATUS(status));
99 else if (WIFSIGNALED(status))
Steve Block61fbcbe2011-10-12 17:22:43 +0100100 ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 WTERMSIG(status));
102 else if (WIFSTOPPED(status))
Steve Block61fbcbe2011-10-12 17:22:43 +0100103 ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 WSTOPSIG(status));
105 } else
Steve Block61fbcbe2011-10-12 17:22:43 +0100106 ALOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 strerror(errno), errno);
Rom Lemarchandb10c7b42013-01-04 16:20:36 -0800108 if (seg_fault_on_exit)
109 *(int *)status = 0; // causes SIGSEGV with fault_address = status
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110}
111
112void child(int argc, char* argv[]) {
113 // create null terminated argv_child array
114 char* argv_child[argc + 1];
115 memcpy(argv_child, argv, argc * sizeof(char *));
116 argv_child[argc] = NULL;
117
118 if (execvp(argv_child[0], argv_child)) {
Steve Block61fbcbe2011-10-12 17:22:43 +0100119 ALOG(LOG_ERROR, "logwrapper",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 "executing %s failed: %s\n", argv_child[0], strerror(errno));
121 exit(-1);
122 }
123}
124
125int main(int argc, char* argv[]) {
126 pid_t pid;
Rom Lemarchandb10c7b42013-01-04 16:20:36 -0800127 int seg_fault_on_exit = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128
129 int parent_ptty;
130 int child_ptty;
131 char *child_devname = NULL;
132
133 if (argc < 2) {
134 usage();
135 }
136
Rom Lemarchandb10c7b42013-01-04 16:20:36 -0800137 if (strncmp(argv[1], "-d", 2) == 0) {
138 seg_fault_on_exit = 1;
139 argc--;
140 argv++;
141 }
142
143 if (argc < 2) {
144 usage();
145 }
146
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 /* Use ptty instead of socketpair so that STDOUT is not buffered */
148 parent_ptty = open("/dev/ptmx", O_RDWR);
149 if (parent_ptty < 0) {
150 fatal("Cannot create parent ptty\n");
151 }
152
153 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
154 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
155 fatal("Problem with /dev/ptmx\n");
156 }
157
158 pid = fork();
159 if (pid < 0) {
160 fatal("Failed to fork\n");
161 } else if (pid == 0) {
162 child_ptty = open(child_devname, O_RDWR);
163 if (child_ptty < 0) {
164 fatal("Problem with child ptty\n");
165 }
166
167 // redirect stdout and stderr
168 close(parent_ptty);
169 dup2(child_ptty, 1);
170 dup2(child_ptty, 2);
171 close(child_ptty);
172
173 child(argc - 1, &argv[1]);
174
175 } else {
176 // switch user and group to "log"
177 // this may fail if we are not root,
178 // but in that case switching user/group is unnecessary
179 setgid(AID_LOG);
180 setuid(AID_LOG);
181
Rom Lemarchandb10c7b42013-01-04 16:20:36 -0800182 parent(argv[1], seg_fault_on_exit, parent_ptty);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 }
184
185 return 0;
186}