blob: 129ffb13124b943bf79b69917cedf48146bc455d [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 Lemarchand74a7b912013-03-11 14:00:58 -070030#include <pthread.h>
Rom Lemarchand113bd472013-01-10 15:21:18 -080031
32#include <logwrap/logwrap.h>
33#include "private/android_filesystem_config.h"
34#include "cutils/log.h"
35
JP Abgralla689d132013-02-13 16:31:58 -080036#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
Rom Lemarchandb58a8222013-01-09 21:31:25 -080037
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080038static int signal_fd_write;
Rom Lemarchand74a7b912013-03-11 14:00:58 -070039static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080040
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080041#define ERROR(fmt, args...) \
Rom Lemarchand99e19662013-01-07 15:50:02 -080042do { \
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080043 fprintf(stderr, fmt, ## args); \
44 ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
Rom Lemarchand99e19662013-01-07 15:50:02 -080045} while(0)
46
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080047#define FATAL_CHILD(fmt, args...) \
Rom Lemarchand99e19662013-01-07 15:50:02 -080048do { \
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080049 ERROR(fmt, ## args); \
Rom Lemarchand99e19662013-01-07 15:50:02 -080050 _exit(-1); \
51} while(0)
Rom Lemarchand113bd472013-01-10 15:21:18 -080052
Rom Lemarchandb58a8222013-01-09 21:31:25 -080053static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080054 int *chld_sts, bool logwrap) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080055 int status = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -080056 char buffer[4096];
Rom Lemarchandb58a8222013-01-09 21:31:25 -080057 struct pollfd poll_fds[] = {
58 [0] = {
Rom Lemarchandb58a8222013-01-09 21:31:25 -080059 .fd = signal_fd,
60 .events = POLLIN,
61 },
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080062 [1] = {
63 .fd = parent_read,
64 .events = POLLIN,
65 },
Rom Lemarchandb58a8222013-01-09 21:31:25 -080066 };
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080067 int rc = 0;
68 sigset_t chldset;
Rom Lemarchand113bd472013-01-10 15:21:18 -080069
70 int a = 0; // start index of unprocessed data
71 int b = 0; // end index of unprocessed data
72 int sz;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080073 bool remote_hung = false;
74 bool found_child = false;
Rom Lemarchand113bd472013-01-10 15:21:18 -080075
76 char *btag = basename(tag);
77 if (!btag) btag = (char*) tag;
78
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080079 sigemptyset(&chldset);
80 sigaddset(&chldset, SIGCHLD);
Rom Lemarchanded179d22013-01-16 15:07:30 -080081 pthread_sigmask(SIG_UNBLOCK, &chldset, NULL);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080082
83 while (!found_child) {
Rom Lemarchand451dd852013-03-11 13:19:17 -070084 if (TEMP_FAILURE_RETRY(poll(poll_fds, remote_hung ? 1 : 2, -1)) < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -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';
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800101 if (logwrap)
Rom Lemarchandf5200c02013-01-24 10:57:44 -0800102 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800103 a = b + 1;
104 }
105 }
106
107 if (a == 0 && b == sizeof(buffer) - 1) {
108 // buffer is full, flush
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800109 buffer[b] = '\0';
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800110 if (logwrap)
Rom Lemarchandf5200c02013-01-24 10:57:44 -0800111 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800112 b = 0;
113 } else if (a != b) {
114 // Keep left-overs
115 b -= a;
116 memmove(buffer, &buffer[a], b);
117 a = 0;
118 } else {
119 a = 0;
120 b = 0;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800121 }
122 }
123
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800124 if (poll_fds[1].revents & POLLHUP) {
125 remote_hung = true;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800126 }
127 }
128
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800129 if (poll_fds[0].revents & POLLIN) {
130 char tmp[32];
131 int ret;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800132
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800133 read(signal_fd, tmp, sizeof(tmp));
134 while (!found_child) {
Rom Lemarchand451dd852013-03-11 13:19:17 -0700135 ret = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG));
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800136
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
JP Abgralla689d132013-02-13 16:31:58 -0800145 if (chld_sts != NULL) {
146 *chld_sts = status;
147 } else {
148 if (WIFEXITED(status))
149 rc = WEXITSTATUS(status);
150 else
151 rc = -ECHILD;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800152 }
153
JP Abgralla689d132013-02-13 16:31:58 -0800154 if (logwrap) {
155 // Flush remaining data
156 if (a != b) {
157 buffer[b] = '\0';
158 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
159 }
160 if (WIFEXITED(status)) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800161 if (WEXITSTATUS(status))
JP Abgralla689d132013-02-13 16:31:58 -0800162 ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
163 WEXITSTATUS(status));
164 } else {
Rom Lemarchand2f34c502013-02-07 15:43:09 -0800165 if (WIFSIGNALED(status))
JP Abgralla689d132013-02-13 16:31:58 -0800166 ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag,
167 WTERMSIG(status));
Rom Lemarchand2f34c502013-02-07 15:43:09 -0800168 else if (WIFSTOPPED(status))
JP Abgralla689d132013-02-13 16:31:58 -0800169 ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag,
170 WSTOPSIG(status));
171 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800172 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800173
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800174err_poll:
175 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800176}
177
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800178static void child(int argc, char* argv[], bool logwrap) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800179 // create null terminated argv_child array
180 char* argv_child[argc + 1];
181 memcpy(argv_child, argv, argc * sizeof(char *));
182 argv_child[argc] = NULL;
183
184 if (execvp(argv_child[0], argv_child)) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800185 FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
Rom Lemarchand99e19662013-01-07 15:50:02 -0800186 strerror(errno));
Rom Lemarchand113bd472013-01-10 15:21:18 -0800187 }
188}
189
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800190static void sigchld_handler(int sig) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800191 write(signal_fd_write, &sig, 1);
192}
193
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800194int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
195 bool logwrap) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800196 pid_t pid;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800197 int parent_ptty;
198 int child_ptty;
199 char *child_devname = NULL;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800200 struct sigaction chldact;
201 struct sigaction oldchldact;
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800202 struct sigaction intact;
203 struct sigaction quitact;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800204 sigset_t blockset;
205 sigset_t oldset;
206 int sockets[2];
207 int rc = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800208
Rom Lemarchand74a7b912013-03-11 14:00:58 -0700209 rc = pthread_mutex_lock(&fd_mutex);
210 if (rc) {
211 ERROR("failed to lock signal_fd mutex\n");
212 goto err_lock;
213 }
214
Rom Lemarchand113bd472013-01-10 15:21:18 -0800215 /* Use ptty instead of socketpair so that STDOUT is not buffered */
216 parent_ptty = open("/dev/ptmx", O_RDWR);
217 if (parent_ptty < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800218 ERROR("Cannot create parent ptty\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800219 rc = -1;
220 goto err_open;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800221 }
222
223 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
224 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800225 ERROR("Problem with /dev/ptmx\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800226 rc = -1;
227 goto err_ptty;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800228 }
229
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800230 sigemptyset(&blockset);
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800231 sigaddset(&blockset, SIGINT);
232 sigaddset(&blockset, SIGQUIT);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800233 sigaddset(&blockset, SIGCHLD);
Rom Lemarchanded179d22013-01-16 15:07:30 -0800234 pthread_sigmask(SIG_BLOCK, &blockset, &oldset);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800235
Rom Lemarchand113bd472013-01-10 15:21:18 -0800236 pid = fork();
237 if (pid < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800238 ERROR("Failed to fork\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800239 rc = -1;
240 goto err_fork;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800241 } else if (pid == 0) {
Rom Lemarchand74a7b912013-03-11 14:00:58 -0700242 pthread_mutex_unlock(&fd_mutex);
Rom Lemarchanded179d22013-01-16 15:07:30 -0800243 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800244 close(parent_ptty);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800245
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800246 child_ptty = open(child_devname, O_RDWR);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800247 if (child_ptty < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800248 FATAL_CHILD("Problem with child ptty\n");
Rom Lemarchandf5200c02013-01-24 10:57:44 -0800249 return -1;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800250 }
251
252 // redirect stdout and stderr
253 dup2(child_ptty, 1);
254 dup2(child_ptty, 2);
255 close(child_ptty);
256
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800257 child(argc, argv, logwrap);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800258 } else {
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800259 struct sigaction ignact;
260
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800261 memset(&chldact, 0, sizeof(chldact));
262 chldact.sa_handler = sigchld_handler;
263 chldact.sa_flags = SA_NOCLDSTOP;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800264
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800265 sigaction(SIGCHLD, &chldact, &oldchldact);
266 if ((!(oldchldact.sa_flags & SA_SIGINFO) &&
267 oldchldact.sa_handler != SIG_DFL &&
268 oldchldact.sa_handler != SIG_IGN) ||
269 ((oldchldact.sa_flags & SA_SIGINFO) &&
270 oldchldact.sa_sigaction != NULL)) {
271 ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD "
272 "handler and might cause interaction issues");
273 }
274
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800275 if (ignore_int_quit) {
276 memset(&ignact, 0, sizeof(ignact));
277 ignact.sa_handler = SIG_IGN;
278 sigaction(SIGINT, &ignact, &intact);
279 sigaction(SIGQUIT, &ignact, &quitact);
280 }
281
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800282 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
283 if (rc == -1) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800284 ERROR("socketpair failed: %s\n", strerror(errno));
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800285 goto err_socketpair;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800286 }
287
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800288 fcntl(sockets[0], F_SETFD, FD_CLOEXEC);
289 fcntl(sockets[0], F_SETFL, O_NONBLOCK);
290 fcntl(sockets[1], F_SETFD, FD_CLOEXEC);
291 fcntl(sockets[1], F_SETFL, O_NONBLOCK);
292
293 signal_fd_write = sockets[0];
294
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800295 rc = parent(argv[0], parent_ptty, sockets[1], pid, status, logwrap);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800296 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800297
298 close(sockets[0]);
299 close(sockets[1]);
300err_socketpair:
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800301 if (ignore_int_quit) {
302 sigaction(SIGINT, &intact, NULL);
303 sigaction(SIGQUIT, &quitact, NULL);
304 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800305 sigaction(SIGCHLD, &oldchldact, NULL);
306err_fork:
Rom Lemarchanded179d22013-01-16 15:07:30 -0800307 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800308err_ptty:
309 close(parent_ptty);
310err_open:
Rom Lemarchand74a7b912013-03-11 14:00:58 -0700311 pthread_mutex_unlock(&fd_mutex);
312err_lock:
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800313 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800314}