blob: f00bfbf3d0bff76fea2544aa6919e4e0323b979f [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
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>
The Android Open Source Project35237d12008-12-17 18:08:08 -080024#include <fcntl.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070025
The Android Open Source Project35237d12008-12-17 18:08:08 -080026#include "private/android_filesystem_config.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include "cutils/log.h"
28
29void fatal(const char *msg) {
30 fprintf(stderr, msg);
31 LOG(LOG_ERROR, "logwrapper", msg);
32 exit(-1);
33}
34
35void usage() {
36 fatal(
The Android Open Source Project35237d12008-12-17 18:08:08 -080037 "Usage: logwrapper [-x] BINARY [ARGS ...]\n"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038 "\n"
39 "Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
40 "the Android logging system. Tag is set to BINARY, priority is\n"
The Android Open Source Project35237d12008-12-17 18:08:08 -080041 "always LOG_INFO.\n"
42 "\n"
43 "-x: Causes logwrapper to SIGSEGV when BINARY terminates\n"
44 " fault address is set to the status of wait()\n");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045}
46
The Android Open Source Project35237d12008-12-17 18:08:08 -080047void parent(const char *tag, int seg_fault_on_exit, int parent_read) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048 int status;
The Android Open Source Project35237d12008-12-17 18:08:08 -080049 char buffer[4096];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050
51 int a = 0; // start index of unprocessed data
52 int b = 0; // end index of unprocessed data
53 int sz;
The Android Open Source Project35237d12008-12-17 18:08:08 -080054 while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) {
55
56 sz += b;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057 // Log one line at a time
The Android Open Source Project35237d12008-12-17 18:08:08 -080058 for (b = 0; b < sz; b++) {
59 if (buffer[b] == '\r') {
60 buffer[b] = '\0';
61 } else if (buffer[b] == '\n') {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062 buffer[b] = '\0';
63 LOG(LOG_INFO, tag, &buffer[a]);
64 a = b + 1;
65 }
66 }
67
The Android Open Source Project35237d12008-12-17 18:08:08 -080068 if (a == 0 && b == sizeof(buffer) - 1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069 // buffer is full, flush
70 buffer[b] = '\0';
71 LOG(LOG_INFO, tag, &buffer[a]);
72 b = 0;
The Android Open Source Project35237d12008-12-17 18:08:08 -080073 } else if (a != b) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074 // Keep left-overs
The Android Open Source Project35237d12008-12-17 18:08:08 -080075 b -= a;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 memmove(buffer, &buffer[a], b);
77 a = 0;
The Android Open Source Project35237d12008-12-17 18:08:08 -080078 } else {
79 a = 0;
80 b = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081 }
The Android Open Source Project35237d12008-12-17 18:08:08 -080082
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083 }
84 // Flush remaining data
85 if (a != b) {
86 buffer[b] = '\0';
87 LOG(LOG_INFO, tag, &buffer[a]);
88 }
The Android Open Source Project35237d12008-12-17 18:08:08 -080089 status = 0xAAAA;
90 if (wait(&status) != -1) { // Wait for child
91 if (WIFEXITED(status))
92 LOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
93 WEXITSTATUS(status));
94 else if (WIFSIGNALED(status))
95 LOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
96 WTERMSIG(status));
97 else if (WIFSTOPPED(status))
98 LOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
99 WSTOPSIG(status));
100 } else
101 LOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag,
102 strerror(errno), errno);
103 if (seg_fault_on_exit)
104 *(int *)status = 0; // causes SIGSEGV with fault_address = status
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105}
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)) {
114 LOG(LOG_ERROR, "logwrapper",
115 "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 Project35237d12008-12-17 18:08:08 -0800122 int seg_fault_on_exit = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700123
The Android Open Source Project35237d12008-12-17 18:08:08 -0800124 int parent_ptty;
125 int child_ptty;
126 char *child_devname = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127
128 if (argc < 2) {
129 usage();
130 }
131
The Android Open Source Project35237d12008-12-17 18:08:08 -0800132 if (strncmp(argv[1], "-d", 2) == 0) {
133 seg_fault_on_exit = 1;
134 argc--;
135 argv++;
136 }
137
138 if (argc < 2) {
139 usage();
140 }
141
142 /* Use ptty instead of socketpair so that STDOUT is not buffered */
143 parent_ptty = open("/dev/ptmx", O_RDWR);
144 if (parent_ptty < 0) {
145 fatal("Cannot create parent ptty\n");
146 }
147
148 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
149 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
150 fatal("Problem with /dev/ptmx\n");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700151 }
152
153 pid = fork();
154 if (pid < 0) {
155 fatal("Failed to fork\n");
156 } else if (pid == 0) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800157 child_ptty = open(child_devname, O_RDWR);
158 if (child_ptty < 0) {
159 fatal("Problem with child ptty\n");
160 }
161
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700162 // redirect stdout and stderr
The Android Open Source Project35237d12008-12-17 18:08:08 -0800163 close(parent_ptty);
164 dup2(child_ptty, 1);
165 dup2(child_ptty, 2);
166 close(child_ptty);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700167
168 child(argc - 1, &argv[1]);
169
170 } else {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800171 // switch user and group to "log"
172 // this may fail if we are not root,
173 // but in that case switching user/group is unnecessary
174 setgid(AID_LOG);
175 setuid(AID_LOG);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700176
The Android Open Source Project35237d12008-12-17 18:08:08 -0800177 parent(argv[1], seg_fault_on_exit, parent_ptty);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178 }
179
180 return 0;
181}