2013-01-11 00:21:18 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2013-01-16 21:08:04 +01:00
|
|
|
#include <sys/socket.h>
|
2013-01-10 06:31:25 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <poll.h>
|
2013-01-11 00:21:18 +01:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
2013-01-16 21:08:04 +01:00
|
|
|
#include <stdbool.h>
|
2013-01-11 00:21:18 +01:00
|
|
|
|
|
|
|
#include <logwrap/logwrap.h>
|
|
|
|
#include "private/android_filesystem_config.h"
|
|
|
|
#include "cutils/log.h"
|
|
|
|
|
2013-01-10 06:31:25 +01:00
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
static int signal_fd_write;
|
|
|
|
|
2013-01-29 20:44:59 +01:00
|
|
|
#define ERROR(fmt, args...) \
|
2013-01-08 00:50:02 +01:00
|
|
|
do { \
|
2013-01-29 20:44:59 +01:00
|
|
|
fprintf(stderr, fmt, ## args); \
|
|
|
|
ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
|
2013-01-08 00:50:02 +01:00
|
|
|
} while(0)
|
|
|
|
|
2013-01-29 20:44:59 +01:00
|
|
|
#define FATAL_CHILD(fmt, args...) \
|
2013-01-08 00:50:02 +01:00
|
|
|
do { \
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR(fmt, ## args); \
|
2013-01-08 00:50:02 +01:00
|
|
|
_exit(-1); \
|
|
|
|
} while(0)
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-01-10 06:31:25 +01:00
|
|
|
static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
|
2013-01-29 20:44:59 +01:00
|
|
|
int *chld_sts, bool logwrap) {
|
2013-01-16 21:08:04 +01:00
|
|
|
int status = 0;
|
2013-01-11 00:21:18 +01:00
|
|
|
char buffer[4096];
|
2013-01-10 06:31:25 +01:00
|
|
|
struct pollfd poll_fds[] = {
|
|
|
|
[0] = {
|
2013-01-16 21:08:04 +01:00
|
|
|
.fd = signal_fd,
|
2013-01-10 06:31:25 +01:00
|
|
|
.events = POLLIN,
|
|
|
|
},
|
|
|
|
[1] = {
|
2013-01-16 21:08:04 +01:00
|
|
|
.fd = parent_read,
|
2013-01-10 06:31:25 +01:00
|
|
|
.events = POLLIN,
|
|
|
|
},
|
|
|
|
};
|
2013-01-16 21:08:04 +01:00
|
|
|
int rc = 0;
|
|
|
|
sigset_t chldset;
|
2013-01-11 00:21:18 +01:00
|
|
|
|
|
|
|
int a = 0; // start index of unprocessed data
|
|
|
|
int b = 0; // end index of unprocessed data
|
|
|
|
int sz;
|
2013-01-16 21:08:04 +01:00
|
|
|
bool remote_hung = false;
|
|
|
|
bool found_child = false;
|
2013-01-11 00:21:18 +01:00
|
|
|
|
|
|
|
char *btag = basename(tag);
|
|
|
|
if (!btag) btag = (char*) tag;
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
sigemptyset(&chldset);
|
|
|
|
sigaddset(&chldset, SIGCHLD);
|
2013-01-17 00:07:30 +01:00
|
|
|
pthread_sigmask(SIG_UNBLOCK, &chldset, NULL);
|
2013-01-16 21:08:04 +01:00
|
|
|
|
|
|
|
while (!found_child) {
|
|
|
|
if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR("poll failed\n");
|
2013-01-16 21:08:04 +01:00
|
|
|
rc = -1;
|
|
|
|
goto err_poll;
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
if (!remote_hung) {
|
|
|
|
if (poll_fds[1].revents & POLLIN) {
|
|
|
|
sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b);
|
|
|
|
|
|
|
|
sz += b;
|
|
|
|
// Log one line at a time
|
|
|
|
for (b = 0; b < sz; b++) {
|
|
|
|
if (buffer[b] == '\r') {
|
|
|
|
buffer[b] = '\0';
|
|
|
|
} else if (buffer[b] == '\n') {
|
|
|
|
buffer[b] = '\0';
|
2013-01-29 20:44:59 +01:00
|
|
|
if (logwrap)
|
2013-01-24 19:57:44 +01:00
|
|
|
ALOG(LOG_INFO, btag, "%s", &buffer[a]);
|
2013-01-16 21:08:04 +01:00
|
|
|
a = b + 1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-10 06:31:25 +01:00
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
if (a == 0 && b == sizeof(buffer) - 1) {
|
|
|
|
// buffer is full, flush
|
2013-01-10 06:31:25 +01:00
|
|
|
buffer[b] = '\0';
|
2013-01-29 20:44:59 +01:00
|
|
|
if (logwrap)
|
2013-01-24 19:57:44 +01:00
|
|
|
ALOG(LOG_INFO, btag, "%s", &buffer[a]);
|
2013-01-16 21:08:04 +01:00
|
|
|
b = 0;
|
|
|
|
} else if (a != b) {
|
|
|
|
// Keep left-overs
|
|
|
|
b -= a;
|
|
|
|
memmove(buffer, &buffer[a], b);
|
|
|
|
a = 0;
|
|
|
|
} else {
|
|
|
|
a = 0;
|
|
|
|
b = 0;
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
if (poll_fds[1].revents & POLLHUP) {
|
|
|
|
remote_hung = true;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
if (poll_fds[0].revents & POLLIN) {
|
|
|
|
char tmp[32];
|
|
|
|
int ret;
|
2013-01-11 00:21:18 +01:00
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
read(signal_fd, tmp, sizeof(tmp));
|
|
|
|
while (!found_child) {
|
|
|
|
do {
|
|
|
|
ret = waitpid(-1, &status, WNOHANG);
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
found_child = (pid == ret);
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
2013-01-10 06:31:25 +01:00
|
|
|
|
2013-01-11 00:21:18 +01:00
|
|
|
// Flush remaining data
|
|
|
|
if (a != b) {
|
|
|
|
buffer[b] = '\0';
|
2013-01-29 20:44:59 +01:00
|
|
|
if (logwrap)
|
2013-01-24 19:57:44 +01:00
|
|
|
ALOG(LOG_INFO, btag, "%s", &buffer[a]);
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 20:44:59 +01:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
if (WEXITSTATUS(status))
|
|
|
|
ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
|
|
|
|
WEXITSTATUS(status));
|
2013-02-08 00:43:09 +01:00
|
|
|
if (chld_sts == NULL)
|
|
|
|
rc = WEXITSTATUS(status);
|
|
|
|
} else {
|
|
|
|
if (chld_sts == NULL)
|
|
|
|
rc = -ECHILD;
|
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag,
|
|
|
|
WTERMSIG(status));
|
|
|
|
else if (WIFSTOPPED(status))
|
|
|
|
ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag,
|
|
|
|
WSTOPSIG(status));
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
|
|
|
if (chld_sts != NULL)
|
|
|
|
*chld_sts = status;
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
err_poll:
|
|
|
|
return rc;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 20:44:59 +01:00
|
|
|
static void child(int argc, char* argv[], bool logwrap) {
|
2013-01-11 00:21:18 +01:00
|
|
|
// create null terminated argv_child array
|
|
|
|
char* argv_child[argc + 1];
|
|
|
|
memcpy(argv_child, argv, argc * sizeof(char *));
|
|
|
|
argv_child[argc] = NULL;
|
|
|
|
|
|
|
|
if (execvp(argv_child[0], argv_child)) {
|
2013-01-29 20:44:59 +01:00
|
|
|
FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
|
2013-01-08 00:50:02 +01:00
|
|
|
strerror(errno));
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 20:44:59 +01:00
|
|
|
static void sigchld_handler(int sig) {
|
2013-01-16 21:08:04 +01:00
|
|
|
write(signal_fd_write, &sig, 1);
|
|
|
|
}
|
|
|
|
|
2013-01-29 20:44:59 +01:00
|
|
|
int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
|
|
|
|
bool logwrap) {
|
2013-01-11 00:21:18 +01:00
|
|
|
pid_t pid;
|
|
|
|
int parent_ptty;
|
|
|
|
int child_ptty;
|
|
|
|
char *child_devname = NULL;
|
2013-01-16 21:08:04 +01:00
|
|
|
struct sigaction chldact;
|
|
|
|
struct sigaction oldchldact;
|
2013-01-09 19:20:25 +01:00
|
|
|
struct sigaction intact;
|
|
|
|
struct sigaction quitact;
|
2013-01-16 21:08:04 +01:00
|
|
|
sigset_t blockset;
|
|
|
|
sigset_t oldset;
|
|
|
|
int sockets[2];
|
|
|
|
int rc = 0;
|
2013-01-11 00:21:18 +01:00
|
|
|
|
|
|
|
/* Use ptty instead of socketpair so that STDOUT is not buffered */
|
|
|
|
parent_ptty = open("/dev/ptmx", O_RDWR);
|
|
|
|
if (parent_ptty < 0) {
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR("Cannot create parent ptty\n");
|
2013-01-16 21:08:04 +01:00
|
|
|
rc = -1;
|
|
|
|
goto err_open;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
|
|
|
|
((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR("Problem with /dev/ptmx\n");
|
2013-01-16 21:08:04 +01:00
|
|
|
rc = -1;
|
|
|
|
goto err_ptty;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
sigemptyset(&blockset);
|
2013-01-09 19:20:25 +01:00
|
|
|
sigaddset(&blockset, SIGINT);
|
|
|
|
sigaddset(&blockset, SIGQUIT);
|
2013-01-16 21:08:04 +01:00
|
|
|
sigaddset(&blockset, SIGCHLD);
|
2013-01-17 00:07:30 +01:00
|
|
|
pthread_sigmask(SIG_BLOCK, &blockset, &oldset);
|
2013-01-10 06:31:25 +01:00
|
|
|
|
2013-01-11 00:21:18 +01:00
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR("Failed to fork\n");
|
2013-01-16 21:08:04 +01:00
|
|
|
rc = -1;
|
|
|
|
goto err_fork;
|
2013-01-11 00:21:18 +01:00
|
|
|
} else if (pid == 0) {
|
2013-01-17 00:07:30 +01:00
|
|
|
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
|
2013-01-11 00:21:18 +01:00
|
|
|
close(parent_ptty);
|
2013-01-16 21:08:04 +01:00
|
|
|
|
2013-01-10 06:31:25 +01:00
|
|
|
child_ptty = open(child_devname, O_RDWR);
|
2013-01-11 00:21:18 +01:00
|
|
|
if (child_ptty < 0) {
|
2013-01-29 20:44:59 +01:00
|
|
|
FATAL_CHILD("Problem with child ptty\n");
|
2013-01-24 19:57:44 +01:00
|
|
|
return -1;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// redirect stdout and stderr
|
|
|
|
dup2(child_ptty, 1);
|
|
|
|
dup2(child_ptty, 2);
|
|
|
|
close(child_ptty);
|
|
|
|
|
2013-01-29 20:44:59 +01:00
|
|
|
child(argc, argv, logwrap);
|
2013-01-11 00:21:18 +01:00
|
|
|
} else {
|
2013-01-09 19:20:25 +01:00
|
|
|
struct sigaction ignact;
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
memset(&chldact, 0, sizeof(chldact));
|
|
|
|
chldact.sa_handler = sigchld_handler;
|
|
|
|
chldact.sa_flags = SA_NOCLDSTOP;
|
|
|
|
|
|
|
|
sigaction(SIGCHLD, &chldact, &oldchldact);
|
|
|
|
if ((!(oldchldact.sa_flags & SA_SIGINFO) &&
|
|
|
|
oldchldact.sa_handler != SIG_DFL &&
|
|
|
|
oldchldact.sa_handler != SIG_IGN) ||
|
|
|
|
((oldchldact.sa_flags & SA_SIGINFO) &&
|
|
|
|
oldchldact.sa_sigaction != NULL)) {
|
|
|
|
ALOG(LOG_WARN, "logwrapper", "logwrap replaced the SIGCHLD "
|
|
|
|
"handler and might cause interaction issues");
|
|
|
|
}
|
2013-01-10 06:31:25 +01:00
|
|
|
|
2013-01-09 19:20:25 +01:00
|
|
|
if (ignore_int_quit) {
|
|
|
|
memset(&ignact, 0, sizeof(ignact));
|
|
|
|
ignact.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGINT, &ignact, &intact);
|
|
|
|
sigaction(SIGQUIT, &ignact, &quitact);
|
|
|
|
}
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
|
|
|
|
if (rc == -1) {
|
2013-01-29 20:44:59 +01:00
|
|
|
ERROR("socketpair failed: %s\n", strerror(errno));
|
2013-01-16 21:08:04 +01:00
|
|
|
goto err_socketpair;
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 21:08:04 +01:00
|
|
|
fcntl(sockets[0], F_SETFD, FD_CLOEXEC);
|
|
|
|
fcntl(sockets[0], F_SETFL, O_NONBLOCK);
|
|
|
|
fcntl(sockets[1], F_SETFD, FD_CLOEXEC);
|
|
|
|
fcntl(sockets[1], F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
signal_fd_write = sockets[0];
|
|
|
|
|
2013-01-29 20:44:59 +01:00
|
|
|
rc = parent(argv[0], parent_ptty, sockets[1], pid, status, logwrap);
|
2013-01-10 06:31:25 +01:00
|
|
|
}
|
2013-01-16 21:08:04 +01:00
|
|
|
|
|
|
|
close(sockets[0]);
|
|
|
|
close(sockets[1]);
|
|
|
|
err_socketpair:
|
2013-01-09 19:20:25 +01:00
|
|
|
if (ignore_int_quit) {
|
|
|
|
sigaction(SIGINT, &intact, NULL);
|
|
|
|
sigaction(SIGQUIT, &quitact, NULL);
|
|
|
|
}
|
2013-01-16 21:08:04 +01:00
|
|
|
sigaction(SIGCHLD, &oldchldact, NULL);
|
|
|
|
err_fork:
|
2013-01-17 00:07:30 +01:00
|
|
|
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
|
2013-01-16 21:08:04 +01:00
|
|
|
err_ptty:
|
|
|
|
close(parent_ptty);
|
|
|
|
err_open:
|
|
|
|
return rc;
|
2013-01-11 00:21:18 +01:00
|
|
|
}
|