blob: 016e12a2d6c30f426d723440189c4ec3d724ebe1 [file] [log] [blame]
Rom Lemarchand113bd472013-01-10 15:21:18 -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>
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080019#include <sys/socket.h>
Rom Lemarchandb58a8222013-01-09 21:31:25 -080020#include <signal.h>
21#include <poll.h>
Rom Lemarchand113bd472013-01-10 15:21:18 -080022#include <sys/wait.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <unistd.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <libgen.h>
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080029#include <stdbool.h>
Rom Lemarchand113bd472013-01-10 15:21:18 -080030
31#include <logwrap/logwrap.h>
32#include "private/android_filesystem_config.h"
33#include "cutils/log.h"
34
Rom Lemarchandb58a8222013-01-09 21:31:25 -080035#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
36
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080037static int signal_fd_write;
38
Rom Lemarchand99e19662013-01-07 15:50:02 -080039#define ERROR(fmt, args...) \
40do { \
41 fprintf(stderr, fmt, ## args); \
42 ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
43} while(0)
44
45#define FATAL_CHILD(fmt, args...) \
46do { \
47 ERROR(fmt, ## args); \
48 _exit(-1); \
49} while(0)
Rom Lemarchand113bd472013-01-10 15:21:18 -080050
Rom Lemarchandb58a8222013-01-09 21:31:25 -080051static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
52 int *chld_sts) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080053 int status = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -080054 char buffer[4096];
Rom Lemarchandb58a8222013-01-09 21:31:25 -080055 struct pollfd poll_fds[] = {
56 [0] = {
Rom Lemarchandb58a8222013-01-09 21:31:25 -080057 .fd = signal_fd,
58 .events = POLLIN,
59 },
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080060 [1] = {
61 .fd = parent_read,
62 .events = POLLIN,
63 },
Rom Lemarchandb58a8222013-01-09 21:31:25 -080064 };
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080065 int rc = 0;
66 sigset_t chldset;
Rom Lemarchand113bd472013-01-10 15:21:18 -080067
68 int a = 0; // start index of unprocessed data
69 int b = 0; // end index of unprocessed data
70 int sz;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080071 bool remote_hung = false;
72 bool found_child = false;
Rom Lemarchand113bd472013-01-10 15:21:18 -080073
74 char *btag = basename(tag);
75 if (!btag) btag = (char*) tag;
76
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080077 sigemptyset(&chldset);
78 sigaddset(&chldset, SIGCHLD);
79 sigprocmask(SIG_UNBLOCK, &chldset, NULL);
80
81 while (!found_child) {
82 if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) {
83 if (errno == EINTR)
84 continue;
Rom Lemarchand99e19662013-01-07 15:50:02 -080085 ERROR("poll failed\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080086 rc = -1;
87 goto err_poll;
Rom Lemarchandb58a8222013-01-09 21:31:25 -080088 }
Rom Lemarchand113bd472013-01-10 15:21:18 -080089
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080090 if (!remote_hung) {
91 if (poll_fds[1].revents & POLLIN) {
92 sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b);
Rom Lemarchandb58a8222013-01-09 21:31:25 -080093
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080094 sz += b;
95 // Log one line at a time
96 for (b = 0; b < sz; b++) {
97 if (buffer[b] == '\r') {
98 buffer[b] = '\0';
99 } else if (buffer[b] == '\n') {
100 buffer[b] = '\0';
101 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
102 a = b + 1;
103 }
104 }
105
106 if (a == 0 && b == sizeof(buffer) - 1) {
107 // buffer is full, flush
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800108 buffer[b] = '\0';
109 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800110 b = 0;
111 } else if (a != b) {
112 // Keep left-overs
113 b -= a;
114 memmove(buffer, &buffer[a], b);
115 a = 0;
116 } else {
117 a = 0;
118 b = 0;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800119 }
120 }
121
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800122 if (poll_fds[1].revents & POLLHUP) {
123 remote_hung = true;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800124 }
125 }
126
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800127 if (poll_fds[0].revents & POLLIN) {
128 char tmp[32];
129 int ret;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800130
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800131 read(signal_fd, tmp, sizeof(tmp));
132 while (!found_child) {
133 do {
134 ret = waitpid(-1, &status, WNOHANG);
135 } while (ret < 0 && errno == EINTR);
136
137 if (ret <= 0)
138 break;
139
140 found_child = (pid == ret);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800141 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800142 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800143 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800144
Rom Lemarchand113bd472013-01-10 15:21:18 -0800145 // Flush remaining data
146 if (a != b) {
147 buffer[b] = '\0';
148 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
149 }
150
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800151 if (WIFEXITED(status)) {
152 if (WEXITSTATUS(status))
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800153 ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
Rom Lemarchand113bd472013-01-10 15:21:18 -0800154 WEXITSTATUS(status));
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800155 } else if (WIFSIGNALED(status)) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800156 ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag,
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800157 WTERMSIG(status));
158 } else if (WIFSTOPPED(status)) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800159 ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag,
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800160 WSTOPSIG(status));
161 }
162 if (chld_sts != NULL)
163 *chld_sts = status;
164
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800165err_poll:
166 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800167}
168
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800169static void child(int argc, char* argv[]) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800170 // create null terminated argv_child array
171 char* argv_child[argc + 1];
172 memcpy(argv_child, argv, argc * sizeof(char *));
173 argv_child[argc] = NULL;
174
175 if (execvp(argv_child[0], argv_child)) {
Rom Lemarchand99e19662013-01-07 15:50:02 -0800176 FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
177 strerror(errno));
Rom Lemarchand113bd472013-01-10 15:21:18 -0800178 }
179}
180
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800181void sigchld_handler(int sig) {
182 write(signal_fd_write, &sig, 1);
183}
184
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800185int logwrap(int argc, char* argv[], int *status) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800186 pid_t pid;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800187 int parent_ptty;
188 int child_ptty;
189 char *child_devname = NULL;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800190 struct sigaction chldact;
191 struct sigaction oldchldact;
192 sigset_t blockset;
193 sigset_t oldset;
194 int sockets[2];
195 int rc = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800196
197 /* Use ptty instead of socketpair so that STDOUT is not buffered */
198 parent_ptty = open("/dev/ptmx", O_RDWR);
199 if (parent_ptty < 0) {
Rom Lemarchand99e19662013-01-07 15:50:02 -0800200 ERROR("Cannot create parent ptty\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800201 rc = -1;
202 goto err_open;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800203 }
204
205 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
206 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
Rom Lemarchand99e19662013-01-07 15:50:02 -0800207 ERROR("Problem with /dev/ptmx\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800208 rc = -1;
209 goto err_ptty;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800210 }
211
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800212 sigemptyset(&blockset);
213 sigaddset(&blockset, SIGCHLD);
214 sigprocmask(SIG_BLOCK, &blockset, &oldset);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800215
Rom Lemarchand113bd472013-01-10 15:21:18 -0800216 pid = fork();
217 if (pid < 0) {
Rom Lemarchand99e19662013-01-07 15:50:02 -0800218 ERROR("Failed to fork\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800219 rc = -1;
220 goto err_fork;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800221 } else if (pid == 0) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800222 sigprocmask(SIG_SETMASK, &oldset, NULL);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800223 close(parent_ptty);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800224
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800225 child_ptty = open(child_devname, O_RDWR);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800226 if (child_ptty < 0) {
Rom Lemarchand99e19662013-01-07 15:50:02 -0800227 FATAL_CHILD("Problem with child ptty\n");
Rom Lemarchand113bd472013-01-10 15:21:18 -0800228 }
229
230 // redirect stdout and stderr
231 dup2(child_ptty, 1);
232 dup2(child_ptty, 2);
233 close(child_ptty);
234
Rom Lemarchand611f5b42013-01-14 14:11:01 -0800235 child(argc, argv);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800236 } else {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800237 memset(&chldact, 0, sizeof(chldact));
238 chldact.sa_handler = sigchld_handler;
239 chldact.sa_flags = SA_NOCLDSTOP;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800240
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800241 sigaction(SIGCHLD, &chldact, &oldchldact);
242 if ((!(oldchldact.sa_flags & SA_SIGINFO) &&
243 oldchldact.sa_handler != SIG_DFL &&
244 oldchldact.sa_handler != SIG_IGN) ||
245 ((oldchldact.sa_flags & SA_SIGINFO) &&
246 oldchldact.sa_sigaction != NULL)) {
247 ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD "
248 "handler and might cause interaction issues");
249 }
250
251 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
252 if (rc == -1) {
Rom Lemarchand99e19662013-01-07 15:50:02 -0800253 ERROR("socketpair failed: %s\n", strerror(errno));
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800254 goto err_socketpair;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800255 }
256
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800257 fcntl(sockets[0], F_SETFD, FD_CLOEXEC);
258 fcntl(sockets[0], F_SETFL, O_NONBLOCK);
259 fcntl(sockets[1], F_SETFD, FD_CLOEXEC);
260 fcntl(sockets[1], F_SETFL, O_NONBLOCK);
261
262 signal_fd_write = sockets[0];
263
Rom Lemarchand611f5b42013-01-14 14:11:01 -0800264 rc = parent(argv[0], parent_ptty, sockets[1], pid, status);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800265 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800266
267 close(sockets[0]);
268 close(sockets[1]);
269err_socketpair:
270 sigaction(SIGCHLD, &oldchldact, NULL);
271err_fork:
272 sigprocmask(SIG_SETMASK, &oldset, NULL);
273err_ptty:
274 close(parent_ptty);
275err_open:
276 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800277}