blob: 25d22819b8505267f835489c149d72960fe240d2 [file] [log] [blame]
The Android Open Source Project8ac3a132009-01-20 14:04:01 -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>
25
26#include "private/android_filesystem_config.h"
27#include "cutils/log.h"
28
29int parent(const char *tag, int parent_read) {
30 int status;
31 char buffer[4096];
32
33 int a = 0; // start index of unprocessed data
34 int b = 0; // end index of unprocessed data
35 int sz;
36 while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) {
37
38 sz += b;
39 // Log one line at a time
40 for (b = 0; b < sz; b++) {
41 if (buffer[b] == '\r') {
42 buffer[b] = '\0';
43 } else if (buffer[b] == '\n') {
44 buffer[b] = '\0';
45 LOG(LOG_INFO, tag, &buffer[a]);
46 a = b + 1;
47 }
48 }
49
50 if (a == 0 && b == sizeof(buffer) - 1) {
51 // buffer is full, flush
52 buffer[b] = '\0';
53 LOG(LOG_INFO, tag, &buffer[a]);
54 b = 0;
55 } else if (a != b) {
56 // Keep left-overs
57 b -= a;
58 memmove(buffer, &buffer[a], b);
59 a = 0;
60 } else {
61 a = 0;
62 b = 0;
63 }
64
65 }
66 // Flush remaining data
67 if (a != b) {
68 buffer[b] = '\0';
69 LOG(LOG_INFO, tag, &buffer[a]);
70 }
71 status = 0xAAAA;
72 if (wait(&status) != -1) { // Wait for child
73 if (WIFEXITED(status)) {
74 LOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
75 WEXITSTATUS(status));
76 return WEXITSTATUS(status);
77 } else if (WIFSIGNALED(status))
78 LOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
79 WTERMSIG(status));
80 else if (WIFSTOPPED(status))
81 LOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
82 WSTOPSIG(status));
83 } else
84 LOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag,
85 strerror(errno), errno);
86 return -EAGAIN;
87}
88
89void child(int argc, char* argv[]) {
90 // create null terminated argv_child array
91 char* argv_child[argc + 1];
92 memcpy(argv_child, argv, argc * sizeof(char *));
93 argv_child[argc] = NULL;
94
95 // XXX: PROTECT FROM VIKING KILLER
96 if (execvp(argv_child[0], argv_child)) {
97 LOG(LOG_ERROR, "logwrapper",
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080098 "executing %s failed: %s", argv_child[0], strerror(errno));
The Android Open Source Project8ac3a132009-01-20 14:04:01 -080099 exit(-1);
100 }
101}
102
103int logwrap(int argc, char* argv[], pid_t *childPid)
104{
105 pid_t pid;
106
107 int parent_ptty;
108 int child_ptty;
109 char *child_devname = NULL;
110
111 /* Use ptty instead of socketpair so that STDOUT is not buffered */
112 parent_ptty = open("/dev/ptmx", O_RDWR);
113 if (parent_ptty < 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800114 LOG(LOG_ERROR, "logwrapper", "Cannot create parent ptty");
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800115 return -errno;
116 }
117
118 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
119 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800120 LOG(LOG_ERROR, "logwrapper", "Problem with /dev/ptmx");
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800121 return -1;
122 }
123
124 pid = fork();
125 if (pid < 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800126 LOG(LOG_ERROR, "logwrapper", "Failed to fork");
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800127 return -errno;
128 } else if (pid == 0) {
129 child_ptty = open(child_devname, O_RDWR);
130 if (child_ptty < 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800131 LOG(LOG_ERROR, "logwrapper", "Problem with child ptty");
The Android Open Source Project8ac3a132009-01-20 14:04:01 -0800132 return -errno;
133 }
134
135 // redirect stdout and stderr
136 close(parent_ptty);
137 dup2(child_ptty, 1);
138 dup2(child_ptty, 2);
139 close(child_ptty);
140
141 child(argc, argv);
142 } else {
143 return parent(argv[0], parent_ptty);
144 }
145
146 return 0;
147}