summaryrefslogtreecommitdiffstats
path: root/logwrapper
diff options
context:
space:
mode:
Diffstat (limited to 'logwrapper')
-rw-r--r--logwrapper/Android.mk29
-rw-r--r--logwrapper/include/logwrap/logwrap.h57
-rw-r--r--logwrapper/logwrap.c299
-rw-r--r--logwrapper/logwrapper.c139
4 files changed, 398 insertions, 126 deletions
diff --git a/logwrapper/Android.mk b/logwrapper/Android.mk
index 5fd6356..b4b6b29 100644
--- a/logwrapper/Android.mk
+++ b/logwrapper/Android.mk
@@ -1,7 +1,34 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
+
+# ========================================================
+# Static library
+# ========================================================
+include $(CLEAR_VARS)
+LOCAL_MODULE := liblogwrap
+LOCAL_SRC_FILES := logwrap.c
+LOCAL_SHARED_LIBRARIES := libcutils liblog
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+include $(BUILD_STATIC_LIBRARY)
+
+# ========================================================
+# Shared library
+# ========================================================
+include $(CLEAR_VARS)
+LOCAL_MODULE := liblogwrap
+LOCAL_SHARED_LIBRARIES := libcutils liblog
+LOCAL_WHOLE_STATIC_LIBRARIES := liblogwrap
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+include $(BUILD_SHARED_LIBRARY)
+
+# ========================================================
+# Executable
+# ========================================================
+include $(CLEAR_VARS)
LOCAL_SRC_FILES:= logwrapper.c
LOCAL_MODULE := logwrapper
-LOCAL_STATIC_LIBRARIES := liblog
+LOCAL_STATIC_LIBRARIES := liblog liblogwrap
include $(BUILD_EXECUTABLE)
diff --git a/logwrapper/include/logwrap/logwrap.h b/logwrapper/include/logwrap/logwrap.h
new file mode 100644
index 0000000..44523e3
--- /dev/null
+++ b/logwrapper/include/logwrap/logwrap.h
@@ -0,0 +1,57 @@
+/* system/core/include/logwrap/logwrap.h
+ *
+ * Copyright 2013, 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.
+ */
+
+#ifndef __LIBS_LOGWRAP_H
+#define __LIBS_LOGWRAP_H
+
+#include <stdbool.h>
+
+__BEGIN_DECLS
+
+/*
+ * Run a command while logging its stdout and stderr
+ *
+ * WARNING: while this function is running it will clear all SIGCHLD handlers
+ * if you rely on SIGCHLD in the caller there is a chance zombies will be
+ * created if you're not calling waitpid after calling this. This function will
+ * log a warning when it clears SIGCHLD for processes other than the child it
+ * created.
+ *
+ * Arguments:
+ * argc: the number of elements in argv
+ * argv: an array of strings containing the command to be executed and its
+ * arguments as separate strings. argv does not need to be
+ * NULL-terminated
+ * status: the equivalent child status as populated by wait(status). This
+ * value is only valid when logwrap successfully completes
+ * ignore_int_quit: set to true if you want to completely ignore SIGINT and
+ * SIGQUIT while logwrap is running. This may force the end-user to
+ * send a signal twice to signal the caller (once for the child, and
+ * once for the caller)
+ * logwrap: when true, log messages from the child
+ *
+ * Return value:
+ * 0 when logwrap successfully run the child process and captured its status
+ * -1 when an internal error occurred
+ *
+ */
+int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
+ bool logwrap);
+
+__END_DECLS
+
+#endif /* __LIBS_LOGWRAP_H */
diff --git a/logwrapper/logwrap.c b/logwrapper/logwrap.c
new file mode 100644
index 0000000..a04097d
--- /dev/null
+++ b/logwrapper/logwrap.c
@@ -0,0 +1,299 @@
+/*
+ * 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>
+#include <sys/socket.h>
+#include <signal.h>
+#include <poll.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <stdbool.h>
+
+#include <logwrap/logwrap.h>
+#include "private/android_filesystem_config.h"
+#include "cutils/log.h"
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
+
+static int signal_fd_write;
+
+#define ERROR(fmt, args...) \
+do { \
+ fprintf(stderr, fmt, ## args); \
+ ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
+} while(0)
+
+#define FATAL_CHILD(fmt, args...) \
+do { \
+ ERROR(fmt, ## args); \
+ _exit(-1); \
+} while(0)
+
+static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
+ int *chld_sts, bool logwrap) {
+ int status = 0;
+ char buffer[4096];
+ struct pollfd poll_fds[] = {
+ [0] = {
+ .fd = signal_fd,
+ .events = POLLIN,
+ },
+ [1] = {
+ .fd = parent_read,
+ .events = POLLIN,
+ },
+ };
+ int rc = 0;
+ sigset_t chldset;
+
+ int a = 0; // start index of unprocessed data
+ int b = 0; // end index of unprocessed data
+ int sz;
+ bool remote_hung = false;
+ bool found_child = false;
+
+ char *btag = basename(tag);
+ if (!btag) btag = (char*) tag;
+
+ sigemptyset(&chldset);
+ sigaddset(&chldset, SIGCHLD);
+ pthread_sigmask(SIG_UNBLOCK, &chldset, NULL);
+
+ while (!found_child) {
+ if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) {
+ if (errno == EINTR)
+ continue;
+ ERROR("poll failed\n");
+ rc = -1;
+ goto err_poll;
+ }
+
+ 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';
+ if (logwrap)
+ ALOG(LOG_INFO, btag, "%s", &buffer[a]);
+ a = b + 1;
+ }
+ }
+
+ if (a == 0 && b == sizeof(buffer) - 1) {
+ // buffer is full, flush
+ buffer[b] = '\0';
+ if (logwrap)
+ ALOG(LOG_INFO, btag, "%s", &buffer[a]);
+ b = 0;
+ } else if (a != b) {
+ // Keep left-overs
+ b -= a;
+ memmove(buffer, &buffer[a], b);
+ a = 0;
+ } else {
+ a = 0;
+ b = 0;
+ }
+ }
+
+ if (poll_fds[1].revents & POLLHUP) {
+ remote_hung = true;
+ }
+ }
+
+ if (poll_fds[0].revents & POLLIN) {
+ char tmp[32];
+ int ret;
+
+ 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);
+ }
+ }
+ }
+
+ // Flush remaining data
+ if (a != b) {
+ buffer[b] = '\0';
+ if (logwrap)
+ ALOG(LOG_INFO, btag, "%s", &buffer[a]);
+ }
+
+ if (WIFEXITED(status)) {
+ if (WEXITSTATUS(status))
+ ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag,
+ WEXITSTATUS(status));
+ } else 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));
+ }
+ if (chld_sts != NULL)
+ *chld_sts = status;
+
+err_poll:
+ return rc;
+}
+
+static void child(int argc, char* argv[], bool logwrap) {
+ // 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)) {
+ FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
+ strerror(errno));
+ }
+}
+
+static void sigchld_handler(int sig) {
+ write(signal_fd_write, &sig, 1);
+}
+
+int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
+ bool logwrap) {
+ pid_t pid;
+ int parent_ptty;
+ int child_ptty;
+ char *child_devname = NULL;
+ struct sigaction chldact;
+ struct sigaction oldchldact;
+ struct sigaction intact;
+ struct sigaction quitact;
+ sigset_t blockset;
+ sigset_t oldset;
+ int sockets[2];
+ int rc = 0;
+
+ /* Use ptty instead of socketpair so that STDOUT is not buffered */
+ parent_ptty = open("/dev/ptmx", O_RDWR);
+ if (parent_ptty < 0) {
+ ERROR("Cannot create parent ptty\n");
+ rc = -1;
+ goto err_open;
+ }
+
+ if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
+ ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
+ ERROR("Problem with /dev/ptmx\n");
+ rc = -1;
+ goto err_ptty;
+ }
+
+ sigemptyset(&blockset);
+ sigaddset(&blockset, SIGINT);
+ sigaddset(&blockset, SIGQUIT);
+ sigaddset(&blockset, SIGCHLD);
+ pthread_sigmask(SIG_BLOCK, &blockset, &oldset);
+
+ pid = fork();
+ if (pid < 0) {
+ ERROR("Failed to fork\n");
+ rc = -1;
+ goto err_fork;
+ } else if (pid == 0) {
+ pthread_sigmask(SIG_SETMASK, &oldset, NULL);
+ close(parent_ptty);
+
+ child_ptty = open(child_devname, O_RDWR);
+ if (child_ptty < 0) {
+ FATAL_CHILD("Problem with child ptty\n");
+ return -1;
+ }
+
+ // redirect stdout and stderr
+ dup2(child_ptty, 1);
+ dup2(child_ptty, 2);
+ close(child_ptty);
+
+ child(argc, argv, logwrap);
+ } else {
+ struct sigaction ignact;
+
+ 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");
+ }
+
+ if (ignore_int_quit) {
+ memset(&ignact, 0, sizeof(ignact));
+ ignact.sa_handler = SIG_IGN;
+ sigaction(SIGINT, &ignact, &intact);
+ sigaction(SIGQUIT, &ignact, &quitact);
+ }
+
+ rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
+ if (rc == -1) {
+ ERROR("socketpair failed: %s\n", strerror(errno));
+ goto err_socketpair;
+ }
+
+ 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];
+
+ rc = parent(argv[0], parent_ptty, sockets[1], pid, status, logwrap);
+ }
+
+ close(sockets[0]);
+ close(sockets[1]);
+err_socketpair:
+ if (ignore_int_quit) {
+ sigaction(SIGINT, &intact, NULL);
+ sigaction(SIGQUIT, &quitact, NULL);
+ }
+ sigaction(SIGCHLD, &oldchldact, NULL);
+err_fork:
+ pthread_sigmask(SIG_SETMASK, &oldset, NULL);
+err_ptty:
+ close(parent_ptty);
+err_open:
+ return rc;
+}
diff --git a/logwrapper/logwrapper.c b/logwrapper/logwrapper.c
index dd777c0..ed71a29 100644
--- a/logwrapper/logwrapper.c
+++ b/logwrapper/logwrapper.c
@@ -14,17 +14,12 @@
* limitations under the License.
*/
-#include <string.h>
-#include <sys/types.h>
-#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <libgen.h>
+#include <sys/wait.h>
+
+#include <logwrap/logwrap.h>
-#include "private/android_filesystem_config.h"
#include "cutils/log.h"
void fatal(const char *msg) {
@@ -45,90 +40,10 @@ void usage() {
" fault address is set to the status of wait()\n");
}
-void parent(const char *tag, int seg_fault_on_exit, int parent_read) {
- int status;
- char buffer[4096];
-
- int a = 0; // start index of unprocessed data
- int b = 0; // end index of unprocessed data
- int sz;
-
- char *btag = basename(tag);
- if (!btag) btag = (char*) tag;
-
- while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) {
-
- 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';
- ALOG(LOG_INFO, btag, "%s", &buffer[a]);
- a = b + 1;
- }
- }
-
- if (a == 0 && b == sizeof(buffer) - 1) {
- // buffer is full, flush
- buffer[b] = '\0';
- ALOG(LOG_INFO, btag, "%s", &buffer[a]);
- b = 0;
- } else if (a != b) {
- // Keep left-overs
- b -= a;
- memmove(buffer, &buffer[a], b);
- a = 0;
- } else {
- a = 0;
- b = 0;
- }
-
- }
- // Flush remaining data
- if (a != b) {
- buffer[b] = '\0';
- ALOG(LOG_INFO, btag, "%s", &buffer[a]);
- }
- status = 0xAAAA;
- if (wait(&status) != -1) { // Wait for child
- if (WIFEXITED(status) && WEXITSTATUS(status))
- ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
- WEXITSTATUS(status));
- else if (WIFSIGNALED(status))
- ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
- WTERMSIG(status));
- else if (WIFSTOPPED(status))
- ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
- WSTOPSIG(status));
- } else
- ALOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag,
- strerror(errno), errno);
- if (seg_fault_on_exit)
- *(int *)status = 0; // causes SIGSEGV with fault_address = status
-}
-
-void child(int argc, char* argv[]) {
- // 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)) {
- ALOG(LOG_ERROR, "logwrapper",
- "executing %s failed: %s\n", argv_child[0], strerror(errno));
- exit(-1);
- }
-}
-
int main(int argc, char* argv[]) {
- pid_t pid;
int seg_fault_on_exit = 0;
-
- int parent_ptty;
- int child_ptty;
- char *child_devname = NULL;
+ int status = 0xAAAA;
+ int rc;
if (argc < 2) {
usage();
@@ -144,43 +59,17 @@ int main(int argc, char* argv[]) {
usage();
}
- /* Use ptty instead of socketpair so that STDOUT is not buffered */
- parent_ptty = open("/dev/ptmx", O_RDWR);
- if (parent_ptty < 0) {
- fatal("Cannot create parent ptty\n");
+ rc = android_fork_execvp(argc - 1, &argv[1], &status, true, true);
+ if (!rc) {
+ if (WIFEXITED(status))
+ rc = WEXITSTATUS(status);
+ else
+ rc = -ECHILD;
}
- if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
- ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
- fatal("Problem with /dev/ptmx\n");
- }
-
- pid = fork();
- if (pid < 0) {
- fatal("Failed to fork\n");
- } else if (pid == 0) {
- child_ptty = open(child_devname, O_RDWR);
- if (child_ptty < 0) {
- fatal("Problem with child ptty\n");
- }
-
- // redirect stdout and stderr
- close(parent_ptty);
- dup2(child_ptty, 1);
- dup2(child_ptty, 2);
- close(child_ptty);
-
- child(argc - 1, &argv[1]);
-
- } else {
- // switch user and group to "log"
- // this may fail if we are not root,
- // but in that case switching user/group is unnecessary
- setgid(AID_LOG);
- setuid(AID_LOG);
-
- parent(argv[1], seg_fault_on_exit, parent_ptty);
+ if (seg_fault_on_exit) {
+ *(int *)status = 0; // causes SIGSEGV with fault_address = status
}
- return 0;
+ return rc;
}