blob: a04097d8e2181ea8860fd09969315db85aa39aa3 [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 Lemarchand2a46bfa2013-01-29 11:44:59 -080039#define ERROR(fmt, args...) \
Rom Lemarchand99e19662013-01-07 15:50:02 -080040do { \
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080041 fprintf(stderr, fmt, ## args); \
42 ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
Rom Lemarchand99e19662013-01-07 15:50:02 -080043} while(0)
44
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080045#define FATAL_CHILD(fmt, args...) \
Rom Lemarchand99e19662013-01-07 15:50:02 -080046do { \
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080047 ERROR(fmt, ## args); \
Rom Lemarchand99e19662013-01-07 15:50:02 -080048 _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,
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080052 int *chld_sts, bool logwrap) {
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);
Rom Lemarchanded179d22013-01-16 15:07:30 -080079 pthread_sigmask(SIG_UNBLOCK, &chldset, NULL);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080080
81 while (!found_child) {
82 if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) {
83 if (errno == EINTR)
84 continue;
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) {
135 do {
136 ret = waitpid(-1, &status, WNOHANG);
137 } while (ret < 0 && errno == EINTR);
138
139 if (ret <= 0)
140 break;
141
142 found_child = (pid == ret);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800143 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800144 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800145 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800146
Rom Lemarchand113bd472013-01-10 15:21:18 -0800147 // Flush remaining data
148 if (a != b) {
149 buffer[b] = '\0';
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800150 if (logwrap)
Rom Lemarchandf5200c02013-01-24 10:57:44 -0800151 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800152 }
153
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800154 if (WIFEXITED(status)) {
155 if (WEXITSTATUS(status))
156 ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
157 WEXITSTATUS(status));
158 } else if (WIFSIGNALED(status)) {
159 ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag,
160 WTERMSIG(status));
161 } else if (WIFSTOPPED(status)) {
162 ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag,
163 WSTOPSIG(status));
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800164 }
165 if (chld_sts != NULL)
166 *chld_sts = status;
167
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800168err_poll:
169 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800170}
171
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800172static void child(int argc, char* argv[], bool logwrap) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800173 // create null terminated argv_child array
174 char* argv_child[argc + 1];
175 memcpy(argv_child, argv, argc * sizeof(char *));
176 argv_child[argc] = NULL;
177
178 if (execvp(argv_child[0], argv_child)) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800179 FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
Rom Lemarchand99e19662013-01-07 15:50:02 -0800180 strerror(errno));
Rom Lemarchand113bd472013-01-10 15:21:18 -0800181 }
182}
183
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800184static void sigchld_handler(int sig) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800185 write(signal_fd_write, &sig, 1);
186}
187
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800188int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
189 bool logwrap) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800190 pid_t pid;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800191 int parent_ptty;
192 int child_ptty;
193 char *child_devname = NULL;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800194 struct sigaction chldact;
195 struct sigaction oldchldact;
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800196 struct sigaction intact;
197 struct sigaction quitact;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800198 sigset_t blockset;
199 sigset_t oldset;
200 int sockets[2];
201 int rc = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800202
203 /* Use ptty instead of socketpair so that STDOUT is not buffered */
204 parent_ptty = open("/dev/ptmx", O_RDWR);
205 if (parent_ptty < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800206 ERROR("Cannot create parent ptty\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800207 rc = -1;
208 goto err_open;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800209 }
210
211 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
212 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800213 ERROR("Problem with /dev/ptmx\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800214 rc = -1;
215 goto err_ptty;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800216 }
217
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800218 sigemptyset(&blockset);
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800219 sigaddset(&blockset, SIGINT);
220 sigaddset(&blockset, SIGQUIT);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800221 sigaddset(&blockset, SIGCHLD);
Rom Lemarchanded179d22013-01-16 15:07:30 -0800222 pthread_sigmask(SIG_BLOCK, &blockset, &oldset);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800223
Rom Lemarchand113bd472013-01-10 15:21:18 -0800224 pid = fork();
225 if (pid < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800226 ERROR("Failed to fork\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800227 rc = -1;
228 goto err_fork;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800229 } else if (pid == 0) {
Rom Lemarchanded179d22013-01-16 15:07:30 -0800230 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800231 close(parent_ptty);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800232
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800233 child_ptty = open(child_devname, O_RDWR);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800234 if (child_ptty < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800235 FATAL_CHILD("Problem with child ptty\n");
Rom Lemarchandf5200c02013-01-24 10:57:44 -0800236 return -1;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800237 }
238
239 // redirect stdout and stderr
240 dup2(child_ptty, 1);
241 dup2(child_ptty, 2);
242 close(child_ptty);
243
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800244 child(argc, argv, logwrap);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800245 } else {
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800246 struct sigaction ignact;
247
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800248 memset(&chldact, 0, sizeof(chldact));
249 chldact.sa_handler = sigchld_handler;
250 chldact.sa_flags = SA_NOCLDSTOP;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800251
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800252 sigaction(SIGCHLD, &chldact, &oldchldact);
253 if ((!(oldchldact.sa_flags & SA_SIGINFO) &&
254 oldchldact.sa_handler != SIG_DFL &&
255 oldchldact.sa_handler != SIG_IGN) ||
256 ((oldchldact.sa_flags & SA_SIGINFO) &&
257 oldchldact.sa_sigaction != NULL)) {
258 ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD "
259 "handler and might cause interaction issues");
260 }
261
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800262 if (ignore_int_quit) {
263 memset(&ignact, 0, sizeof(ignact));
264 ignact.sa_handler = SIG_IGN;
265 sigaction(SIGINT, &ignact, &intact);
266 sigaction(SIGQUIT, &ignact, &quitact);
267 }
268
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800269 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
270 if (rc == -1) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800271 ERROR("socketpair failed: %s\n", strerror(errno));
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800272 goto err_socketpair;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800273 }
274
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800275 fcntl(sockets[0], F_SETFD, FD_CLOEXEC);
276 fcntl(sockets[0], F_SETFL, O_NONBLOCK);
277 fcntl(sockets[1], F_SETFD, FD_CLOEXEC);
278 fcntl(sockets[1], F_SETFL, O_NONBLOCK);
279
280 signal_fd_write = sockets[0];
281
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800282 rc = parent(argv[0], parent_ptty, sockets[1], pid, status, logwrap);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800283 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800284
285 close(sockets[0]);
286 close(sockets[1]);
287err_socketpair:
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800288 if (ignore_int_quit) {
289 sigaction(SIGINT, &intact, NULL);
290 sigaction(SIGQUIT, &quitact, NULL);
291 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800292 sigaction(SIGCHLD, &oldchldact, NULL);
293err_fork:
Rom Lemarchanded179d22013-01-16 15:07:30 -0800294 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800295err_ptty:
296 close(parent_ptty);
297err_open:
298 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800299}