2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
26 #include "private/android_filesystem_config.h"
27 #include "cutils/log.h"
29 int parent(const char *tag, int parent_read) {
33 int a = 0; // start index of unprocessed data
34 int b = 0; // end index of unprocessed data
36 while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) {
39 // Log one line at a time
40 for (b = 0; b < sz; b++) {
41 if (buffer[b] == '\r') {
43 } else if (buffer[b] == '\n') {
46 LOG(LOG_INFO, tag, "%s", &buffer[a]);
51 if (a == 0 && b == sizeof(buffer) - 1) {
52 // buffer is full, flush
54 LOG(LOG_INFO, tag, "%s", &buffer[a]);
59 memmove(buffer, &buffer[a], b);
67 // Flush remaining data
70 LOG(LOG_INFO, tag, "%s", &buffer[a]);
73 if (wait(&status) != -1) { // Wait for child
74 if (WIFEXITED(status)) {
75 if (WEXITSTATUS(status) != 0) {
76 LOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
79 return WEXITSTATUS(status);
80 } else if (WIFSIGNALED(status))
81 LOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
83 else if (WIFSTOPPED(status))
84 LOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
87 LOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag,
88 strerror(errno), errno);
92 void child(int argc, const char**argv) {
93 // create null terminated argv_child array
94 char* argv_child[argc + 1];
95 memcpy(argv_child, argv, argc * sizeof(char *));
96 argv_child[argc] = NULL;
98 // XXX: PROTECT FROM VIKING KILLER
99 if (execv(argv_child[0], argv_child)) {
100 LOG(LOG_ERROR, "logwrapper",
101 "executing %s failed: %s", argv_child[0], strerror(errno));
106 int logwrap(int argc, const char* argv[], int background)
112 char *child_devname = NULL;
114 /* Use ptty instead of socketpair so that STDOUT is not buffered */
115 parent_ptty = open("/dev/ptmx", O_RDWR);
116 if (parent_ptty < 0) {
117 LOG(LOG_ERROR, "logwrapper", "Cannot create parent ptty");
121 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
122 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
124 LOG(LOG_ERROR, "logwrapper", "Problem with /dev/ptmx");
131 LOG(LOG_ERROR, "logwrapper", "Failed to fork");
133 } else if (pid == 0) {
137 child_ptty = open(child_devname, O_RDWR);
138 if (child_ptty < 0) {
140 LOG(LOG_ERROR, "logwrapper", "Problem with child ptty");
144 // redirect stdout and stderr
151 int fd = open("/dev/cpuctl/bg_non_interactive/tasks", O_WRONLY);
154 sprintf(text, "%d", getpid());
155 if (write(fd, text, strlen(text)) < 0) {
156 LOG(LOG_WARN, "logwrapper",
157 "Unable to background process (%s)", strerror(errno));
161 LOG(LOG_WARN, "logwrapper",
162 "Unable to background process (%s)", strerror(errno));
171 int rc = parent(argv[0], parent_ptty);