blob: ef457def5ec95f829cd19db44637258bd62bf0de [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
39static void fatal(const char *msg) {
Rom Lemarchand113bd472013-01-10 15:21:18 -080040 fprintf(stderr, "%s", msg);
41 ALOG(LOG_ERROR, "logwrapper", "%s", msg);
Rom Lemarchand113bd472013-01-10 15:21:18 -080042}
43
Rom Lemarchandb58a8222013-01-09 21:31:25 -080044static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
45 int *chld_sts) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080046 int status = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -080047 char buffer[4096];
Rom Lemarchandb58a8222013-01-09 21:31:25 -080048 struct pollfd poll_fds[] = {
49 [0] = {
Rom Lemarchandb58a8222013-01-09 21:31:25 -080050 .fd = signal_fd,
51 .events = POLLIN,
52 },
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080053 [1] = {
54 .fd = parent_read,
55 .events = POLLIN,
56 },
Rom Lemarchandb58a8222013-01-09 21:31:25 -080057 };
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080058 int rc = 0;
59 sigset_t chldset;
Rom Lemarchand113bd472013-01-10 15:21:18 -080060
61 int a = 0; // start index of unprocessed data
62 int b = 0; // end index of unprocessed data
63 int sz;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080064 bool remote_hung = false;
65 bool found_child = false;
Rom Lemarchand113bd472013-01-10 15:21:18 -080066
67 char *btag = basename(tag);
68 if (!btag) btag = (char*) tag;
69
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080070 sigemptyset(&chldset);
71 sigaddset(&chldset, SIGCHLD);
72 sigprocmask(SIG_UNBLOCK, &chldset, NULL);
73
74 while (!found_child) {
75 if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) {
76 if (errno == EINTR)
77 continue;
78 fatal("poll failed\n");
79 rc = -1;
80 goto err_poll;
Rom Lemarchandb58a8222013-01-09 21:31:25 -080081 }
Rom Lemarchand113bd472013-01-10 15:21:18 -080082
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080083 if (!remote_hung) {
84 if (poll_fds[1].revents & POLLIN) {
85 sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b);
Rom Lemarchandb58a8222013-01-09 21:31:25 -080086
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080087 sz += b;
88 // Log one line at a time
89 for (b = 0; b < sz; b++) {
90 if (buffer[b] == '\r') {
91 buffer[b] = '\0';
92 } else if (buffer[b] == '\n') {
93 buffer[b] = '\0';
94 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
95 a = b + 1;
96 }
97 }
98
99 if (a == 0 && b == sizeof(buffer) - 1) {
100 // buffer is full, flush
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800101 buffer[b] = '\0';
102 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800103 b = 0;
104 } else if (a != b) {
105 // Keep left-overs
106 b -= a;
107 memmove(buffer, &buffer[a], b);
108 a = 0;
109 } else {
110 a = 0;
111 b = 0;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800112 }
113 }
114
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800115 if (poll_fds[1].revents & POLLHUP) {
116 remote_hung = true;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800117 }
118 }
119
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800120 if (poll_fds[0].revents & POLLIN) {
121 char tmp[32];
122 int ret;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800123
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800124 read(signal_fd, tmp, sizeof(tmp));
125 while (!found_child) {
126 do {
127 ret = waitpid(-1, &status, WNOHANG);
128 } while (ret < 0 && errno == EINTR);
129
130 if (ret <= 0)
131 break;
132
133 found_child = (pid == ret);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800134 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800135 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800136 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800137
Rom Lemarchand113bd472013-01-10 15:21:18 -0800138 // Flush remaining data
139 if (a != b) {
140 buffer[b] = '\0';
141 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
142 }
143
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800144 if (WIFEXITED(status)) {
145 if (WEXITSTATUS(status))
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800146 ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
Rom Lemarchand113bd472013-01-10 15:21:18 -0800147 WEXITSTATUS(status));
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800148 } else if (WIFSIGNALED(status)) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800149 ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag,
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800150 WTERMSIG(status));
151 } else if (WIFSTOPPED(status)) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800152 ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag,
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800153 WSTOPSIG(status));
154 }
155 if (chld_sts != NULL)
156 *chld_sts = status;
157
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800158err_poll:
159 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800160}
161
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800162static void child(int argc, char* argv[]) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800163 // create null terminated argv_child array
164 char* argv_child[argc + 1];
165 memcpy(argv_child, argv, argc * sizeof(char *));
166 argv_child[argc] = NULL;
167
168 if (execvp(argv_child[0], argv_child)) {
169 ALOG(LOG_ERROR, "logwrapper",
170 "executing %s failed: %s\n", argv_child[0], strerror(errno));
171 exit(-1);
172 }
173}
174
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800175void sigchld_handler(int sig) {
176 write(signal_fd_write, &sig, 1);
177}
178
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800179int logwrap(int argc, char* argv[], int *status) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800180 pid_t pid;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800181 int parent_ptty;
182 int child_ptty;
183 char *child_devname = NULL;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800184 struct sigaction chldact;
185 struct sigaction oldchldact;
186 sigset_t blockset;
187 sigset_t oldset;
188 int sockets[2];
189 int rc = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800190
191 /* Use ptty instead of socketpair so that STDOUT is not buffered */
192 parent_ptty = open("/dev/ptmx", O_RDWR);
193 if (parent_ptty < 0) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800194 fatal("Cannot create parent ptty\n");
195 rc = -1;
196 goto err_open;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800197 }
198
199 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
200 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800201 fatal("Problem with /dev/ptmx\n");
202 rc = -1;
203 goto err_ptty;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800204 }
205
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800206 sigemptyset(&blockset);
207 sigaddset(&blockset, SIGCHLD);
208 sigprocmask(SIG_BLOCK, &blockset, &oldset);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800209
Rom Lemarchand113bd472013-01-10 15:21:18 -0800210 pid = fork();
211 if (pid < 0) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800212 fatal("Failed to fork\n");
213 rc = -1;
214 goto err_fork;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800215 } else if (pid == 0) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800216 sigprocmask(SIG_SETMASK, &oldset, NULL);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800217 close(parent_ptty);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800218
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800219 child_ptty = open(child_devname, O_RDWR);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800220 if (child_ptty < 0) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800221 fatal("Problem with child ptty\n");
222 return -1;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800223 }
224
225 // redirect stdout and stderr
226 dup2(child_ptty, 1);
227 dup2(child_ptty, 2);
228 close(child_ptty);
229
Rom Lemarchand611f5b42013-01-14 14:11:01 -0800230 child(argc, argv);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800231 fatal("This should never happen\n");
232 return -1;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800233 } else {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800234 memset(&chldact, 0, sizeof(chldact));
235 chldact.sa_handler = sigchld_handler;
236 chldact.sa_flags = SA_NOCLDSTOP;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800237
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800238 sigaction(SIGCHLD, &chldact, &oldchldact);
239 if ((!(oldchldact.sa_flags & SA_SIGINFO) &&
240 oldchldact.sa_handler != SIG_DFL &&
241 oldchldact.sa_handler != SIG_IGN) ||
242 ((oldchldact.sa_flags & SA_SIGINFO) &&
243 oldchldact.sa_sigaction != NULL)) {
244 ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD "
245 "handler and might cause interaction issues");
246 }
247
248 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
249 if (rc == -1) {
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800250 char msg[40];
251
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800252 snprintf(msg, sizeof(msg), "socketpair failed: %d\n", errno);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800253
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800254 fatal(msg);
255 goto err_socketpair;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800256 }
257
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800258 fcntl(sockets[0], F_SETFD, FD_CLOEXEC);
259 fcntl(sockets[0], F_SETFL, O_NONBLOCK);
260 fcntl(sockets[1], F_SETFD, FD_CLOEXEC);
261 fcntl(sockets[1], F_SETFL, O_NONBLOCK);
262
263 signal_fd_write = sockets[0];
264
Rom Lemarchand611f5b42013-01-14 14:11:01 -0800265 rc = parent(argv[0], parent_ptty, sockets[1], pid, status);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800266 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800267
268 close(sockets[0]);
269 close(sockets[1]);
270err_socketpair:
271 sigaction(SIGCHLD, &oldchldact, NULL);
272err_fork:
273 sigprocmask(SIG_SETMASK, &oldset, NULL);
274err_ptty:
275 close(parent_ptty);
276err_open:
277 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800278}