blob: b2abfe2ec55d2966db3b4cbeaf49d15778d919eb [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
JP Abgralla689d132013-02-13 16:31:58 -080035#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
Rom Lemarchandb58a8222013-01-09 21:31:25 -080036
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) {
Rom Lemarchand451dd852013-03-11 13:19:17 -070082 if (TEMP_FAILURE_RETRY(poll(poll_fds, remote_hung ? 1 : 2, -1)) < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080083 ERROR("poll failed\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080084 rc = -1;
85 goto err_poll;
Rom Lemarchandb58a8222013-01-09 21:31:25 -080086 }
Rom Lemarchand113bd472013-01-10 15:21:18 -080087
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080088 if (!remote_hung) {
89 if (poll_fds[1].revents & POLLIN) {
90 sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b);
Rom Lemarchandb58a8222013-01-09 21:31:25 -080091
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -080092 sz += b;
93 // Log one line at a time
94 for (b = 0; b < sz; b++) {
95 if (buffer[b] == '\r') {
96 buffer[b] = '\0';
97 } else if (buffer[b] == '\n') {
98 buffer[b] = '\0';
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -080099 if (logwrap)
Rom Lemarchandf5200c02013-01-24 10:57:44 -0800100 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800101 a = b + 1;
102 }
103 }
104
105 if (a == 0 && b == sizeof(buffer) - 1) {
106 // buffer is full, flush
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800107 buffer[b] = '\0';
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800108 if (logwrap)
Rom Lemarchandf5200c02013-01-24 10:57:44 -0800109 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) {
Rom Lemarchand451dd852013-03-11 13:19:17 -0700133 ret = TEMP_FAILURE_RETRY(waitpid(-1, &status, WNOHANG));
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800134
135 if (ret <= 0)
136 break;
137
138 found_child = (pid == ret);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800139 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800140 }
Rom Lemarchand113bd472013-01-10 15:21:18 -0800141 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800142
JP Abgralla689d132013-02-13 16:31:58 -0800143 if (chld_sts != NULL) {
144 *chld_sts = status;
145 } else {
146 if (WIFEXITED(status))
147 rc = WEXITSTATUS(status);
148 else
149 rc = -ECHILD;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800150 }
151
JP Abgralla689d132013-02-13 16:31:58 -0800152 if (logwrap) {
153 // Flush remaining data
154 if (a != b) {
155 buffer[b] = '\0';
156 ALOG(LOG_INFO, btag, "%s", &buffer[a]);
157 }
158 if (WIFEXITED(status)) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800159 if (WEXITSTATUS(status))
JP Abgralla689d132013-02-13 16:31:58 -0800160 ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
161 WEXITSTATUS(status));
162 } else {
Rom Lemarchand2f34c502013-02-07 15:43:09 -0800163 if (WIFSIGNALED(status))
JP Abgralla689d132013-02-13 16:31:58 -0800164 ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag,
165 WTERMSIG(status));
Rom Lemarchand2f34c502013-02-07 15:43:09 -0800166 else if (WIFSTOPPED(status))
JP Abgralla689d132013-02-13 16:31:58 -0800167 ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag,
168 WSTOPSIG(status));
169 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800170 }
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800171
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800172err_poll:
173 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800174}
175
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800176static void child(int argc, char* argv[], bool logwrap) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800177 // create null terminated argv_child array
178 char* argv_child[argc + 1];
179 memcpy(argv_child, argv, argc * sizeof(char *));
180 argv_child[argc] = NULL;
181
182 if (execvp(argv_child[0], argv_child)) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800183 FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
Rom Lemarchand99e19662013-01-07 15:50:02 -0800184 strerror(errno));
Rom Lemarchand113bd472013-01-10 15:21:18 -0800185 }
186}
187
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800188static void sigchld_handler(int sig) {
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800189 write(signal_fd_write, &sig, 1);
190}
191
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800192int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
193 bool logwrap) {
Rom Lemarchand113bd472013-01-10 15:21:18 -0800194 pid_t pid;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800195 int parent_ptty;
196 int child_ptty;
197 char *child_devname = NULL;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800198 struct sigaction chldact;
199 struct sigaction oldchldact;
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800200 struct sigaction intact;
201 struct sigaction quitact;
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800202 sigset_t blockset;
203 sigset_t oldset;
204 int sockets[2];
205 int rc = 0;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800206
207 /* Use ptty instead of socketpair so that STDOUT is not buffered */
208 parent_ptty = open("/dev/ptmx", O_RDWR);
209 if (parent_ptty < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800210 ERROR("Cannot create parent ptty\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800211 rc = -1;
212 goto err_open;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800213 }
214
215 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
216 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800217 ERROR("Problem with /dev/ptmx\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800218 rc = -1;
219 goto err_ptty;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800220 }
221
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800222 sigemptyset(&blockset);
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800223 sigaddset(&blockset, SIGINT);
224 sigaddset(&blockset, SIGQUIT);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800225 sigaddset(&blockset, SIGCHLD);
Rom Lemarchanded179d22013-01-16 15:07:30 -0800226 pthread_sigmask(SIG_BLOCK, &blockset, &oldset);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800227
Rom Lemarchand113bd472013-01-10 15:21:18 -0800228 pid = fork();
229 if (pid < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800230 ERROR("Failed to fork\n");
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800231 rc = -1;
232 goto err_fork;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800233 } else if (pid == 0) {
Rom Lemarchanded179d22013-01-16 15:07:30 -0800234 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800235 close(parent_ptty);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800236
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800237 child_ptty = open(child_devname, O_RDWR);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800238 if (child_ptty < 0) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800239 FATAL_CHILD("Problem with child ptty\n");
Rom Lemarchandf5200c02013-01-24 10:57:44 -0800240 return -1;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800241 }
242
243 // redirect stdout and stderr
244 dup2(child_ptty, 1);
245 dup2(child_ptty, 2);
246 close(child_ptty);
247
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800248 child(argc, argv, logwrap);
Rom Lemarchand113bd472013-01-10 15:21:18 -0800249 } else {
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800250 struct sigaction ignact;
251
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800252 memset(&chldact, 0, sizeof(chldact));
253 chldact.sa_handler = sigchld_handler;
254 chldact.sa_flags = SA_NOCLDSTOP;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800255
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800256 sigaction(SIGCHLD, &chldact, &oldchldact);
257 if ((!(oldchldact.sa_flags & SA_SIGINFO) &&
258 oldchldact.sa_handler != SIG_DFL &&
259 oldchldact.sa_handler != SIG_IGN) ||
260 ((oldchldact.sa_flags & SA_SIGINFO) &&
261 oldchldact.sa_sigaction != NULL)) {
262 ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD "
263 "handler and might cause interaction issues");
264 }
265
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800266 if (ignore_int_quit) {
267 memset(&ignact, 0, sizeof(ignact));
268 ignact.sa_handler = SIG_IGN;
269 sigaction(SIGINT, &ignact, &intact);
270 sigaction(SIGQUIT, &ignact, &quitact);
271 }
272
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800273 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
274 if (rc == -1) {
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800275 ERROR("socketpair failed: %s\n", strerror(errno));
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800276 goto err_socketpair;
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800277 }
278
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800279 fcntl(sockets[0], F_SETFD, FD_CLOEXEC);
280 fcntl(sockets[0], F_SETFL, O_NONBLOCK);
281 fcntl(sockets[1], F_SETFD, FD_CLOEXEC);
282 fcntl(sockets[1], F_SETFL, O_NONBLOCK);
283
284 signal_fd_write = sockets[0];
285
Rom Lemarchand2a46bfa2013-01-29 11:44:59 -0800286 rc = parent(argv[0], parent_ptty, sockets[1], pid, status, logwrap);
Rom Lemarchandb58a8222013-01-09 21:31:25 -0800287 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800288
289 close(sockets[0]);
290 close(sockets[1]);
291err_socketpair:
Rom Lemarchand75c289a2013-01-09 10:20:25 -0800292 if (ignore_int_quit) {
293 sigaction(SIGINT, &intact, NULL);
294 sigaction(SIGQUIT, &quitact, NULL);
295 }
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800296 sigaction(SIGCHLD, &oldchldact, NULL);
297err_fork:
Rom Lemarchanded179d22013-01-16 15:07:30 -0800298 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
Rom Lemarchand0cc2cab2013-01-16 12:08:04 -0800299err_ptty:
300 close(parent_ptty);
301err_open:
302 return rc;
Rom Lemarchand113bd472013-01-10 15:21:18 -0800303}