blob: 34a1e5aa2ff1de7da4590c900bb89e159d915762 [file] [log] [blame]
San Mehat49e2bce2009-10-12 16:29:01 -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>
24#include <fcntl.h>
25
26#include "private/android_filesystem_config.h"
27#include "cutils/log.h"
Glenn Kasten1b4807b2012-03-05 15:14:33 -080028#include "cutils/sched_policy.h"
San Mehat49e2bce2009-10-12 16:29:01 -070029
30int parent(const char *tag, int parent_read) {
31 int status;
32 char buffer[4096];
33
34 int a = 0; // start index of unprocessed data
35 int b = 0; // end index of unprocessed data
36 int sz;
37 while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) {
38
39 sz += b;
40 // Log one line at a time
41 for (b = 0; b < sz; b++) {
42 if (buffer[b] == '\r') {
43 buffer[b] = '\0';
44 } else if (buffer[b] == '\n') {
45 buffer[b] = '\0';
46
Steve Block8c487332011-10-12 17:28:59 +010047 ALOG(LOG_INFO, tag, "%s", &buffer[a]);
San Mehat49e2bce2009-10-12 16:29:01 -070048 a = b + 1;
49 }
50 }
51
52 if (a == 0 && b == sizeof(buffer) - 1) {
53 // buffer is full, flush
54 buffer[b] = '\0';
Steve Block8c487332011-10-12 17:28:59 +010055 ALOG(LOG_INFO, tag, "%s", &buffer[a]);
San Mehat49e2bce2009-10-12 16:29:01 -070056 b = 0;
57 } else if (a != b) {
58 // Keep left-overs
59 b -= a;
60 memmove(buffer, &buffer[a], b);
61 a = 0;
62 } else {
63 a = 0;
64 b = 0;
65 }
66
67 }
68 // Flush remaining data
69 if (a != b) {
70 buffer[b] = '\0';
Steve Block8c487332011-10-12 17:28:59 +010071 ALOG(LOG_INFO, tag, "%s", &buffer[a]);
San Mehat49e2bce2009-10-12 16:29:01 -070072 }
73 status = 0xAAAA;
74 if (wait(&status) != -1) { // Wait for child
75 if (WIFEXITED(status)) {
Brad Fitzpatrick1b15d462010-09-20 11:11:46 -070076 if (WEXITSTATUS(status) != 0) {
Steve Block8c487332011-10-12 17:28:59 +010077 ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
Brad Fitzpatrick1b15d462010-09-20 11:11:46 -070078 WEXITSTATUS(status));
79 }
San Mehat49e2bce2009-10-12 16:29:01 -070080 return WEXITSTATUS(status);
81 } else if (WIFSIGNALED(status))
Steve Block8c487332011-10-12 17:28:59 +010082 ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
San Mehat49e2bce2009-10-12 16:29:01 -070083 WTERMSIG(status));
84 else if (WIFSTOPPED(status))
Steve Block8c487332011-10-12 17:28:59 +010085 ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
San Mehat49e2bce2009-10-12 16:29:01 -070086 WSTOPSIG(status));
87 } else
Steve Block8c487332011-10-12 17:28:59 +010088 ALOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag,
San Mehat49e2bce2009-10-12 16:29:01 -070089 strerror(errno), errno);
90 return -EAGAIN;
91}
92
93void child(int argc, const char**argv) {
94 // create null terminated argv_child array
95 char* argv_child[argc + 1];
96 memcpy(argv_child, argv, argc * sizeof(char *));
97 argv_child[argc] = NULL;
98
99 // XXX: PROTECT FROM VIKING KILLER
100 if (execv(argv_child[0], argv_child)) {
Steve Block8c487332011-10-12 17:28:59 +0100101 ALOG(LOG_ERROR, "logwrapper",
San Mehat49e2bce2009-10-12 16:29:01 -0700102 "executing %s failed: %s", argv_child[0], strerror(errno));
103 exit(-1);
104 }
105}
106
107int logwrap(int argc, const char* argv[], int background)
108{
109 pid_t pid;
110
111 int parent_ptty;
112 int child_ptty;
113 char *child_devname = NULL;
114
115 /* Use ptty instead of socketpair so that STDOUT is not buffered */
116 parent_ptty = open("/dev/ptmx", O_RDWR);
117 if (parent_ptty < 0) {
Steve Block8c487332011-10-12 17:28:59 +0100118 ALOG(LOG_ERROR, "logwrapper", "Cannot create parent ptty");
119 return -errno;
San Mehat49e2bce2009-10-12 16:29:01 -0700120 }
121
122 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
123 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
San Mehat8c940ef2010-02-13 14:19:53 -0800124 close(parent_ptty);
Steve Block8c487332011-10-12 17:28:59 +0100125 ALOG(LOG_ERROR, "logwrapper", "Problem with /dev/ptmx");
126 return -1;
San Mehat49e2bce2009-10-12 16:29:01 -0700127 }
128
129 pid = fork();
130 if (pid < 0) {
Brad Fitzpatrick1b15d462010-09-20 11:11:46 -0700131 close(parent_ptty);
Steve Block8c487332011-10-12 17:28:59 +0100132 ALOG(LOG_ERROR, "logwrapper", "Failed to fork");
San Mehat49e2bce2009-10-12 16:29:01 -0700133 return -errno;
134 } else if (pid == 0) {
San Mehat8c940ef2010-02-13 14:19:53 -0800135 /*
136 * Child
137 */
San Mehat49e2bce2009-10-12 16:29:01 -0700138 child_ptty = open(child_devname, O_RDWR);
139 if (child_ptty < 0) {
Brad Fitzpatrick1b15d462010-09-20 11:11:46 -0700140 close(parent_ptty);
Steve Block8c487332011-10-12 17:28:59 +0100141 ALOG(LOG_ERROR, "logwrapper", "Problem with child ptty");
San Mehat49e2bce2009-10-12 16:29:01 -0700142 return -errno;
143 }
144
145 // redirect stdout and stderr
146 close(parent_ptty);
147 dup2(child_ptty, 1);
148 dup2(child_ptty, 2);
149 close(child_ptty);
150
151 if (background) {
Glenn Kasten1b4807b2012-03-05 15:14:33 -0800152 int err = set_sched_policy(getpid(), SP_BACKGROUND);
153 if (err < 0) {
Steve Block8c487332011-10-12 17:28:59 +0100154 ALOG(LOG_WARN, "logwrapper",
Glenn Kasten1b4807b2012-03-05 15:14:33 -0800155 "Unable to background process (%s)", strerror(-err));
San Mehat49e2bce2009-10-12 16:29:01 -0700156 }
157 }
158
159 child(argc, argv);
160 } else {
San Mehat8c940ef2010-02-13 14:19:53 -0800161 /*
162 * Parent
163 */
164 int rc = parent(argv[0], parent_ptty);
165 close(parent_ptty);
166 return rc;
San Mehat49e2bce2009-10-12 16:29:01 -0700167 }
168
169 return 0;
170}