summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRom Lemarchand <romlem@google.com>2013-01-29 11:44:59 -0800
committerRom Lemarchand <romlem@google.com>2013-01-30 12:26:05 -0800
commit2a46bfa6b9401b847a3c0609e247428564d91121 (patch)
tree3996ef0c290e72951e54c42f8c61b1689ec9c608
parent49f0f776932ec6120d41d8e144247f9d36a01fb5 (diff)
downloadsystem_core-2a46bfa6b9401b847a3c0609e247428564d91121.zip
system_core-2a46bfa6b9401b847a3c0609e247428564d91121.tar.gz
system_core-2a46bfa6b9401b847a3c0609e247428564d91121.tar.bz2
logwrapper: rename logwrap() to android_fork_execvp()
Also change the quiet flag to a logwrap flag (inverses the meaning of the boolean). Change-Id: I76047a7b460f4c28d52f26bfe3f65889d96047f8
-rw-r--r--logwrapper/include/logwrap/logwrap.h6
-rw-r--r--logwrapper/logwrap.c68
-rw-r--r--logwrapper/logwrapper.c2
3 files changed, 36 insertions, 40 deletions
diff --git a/logwrapper/include/logwrap/logwrap.h b/logwrapper/include/logwrap/logwrap.h
index 6597ef5..44523e3 100644
--- a/logwrapper/include/logwrap/logwrap.h
+++ b/logwrapper/include/logwrap/logwrap.h
@@ -42,15 +42,15 @@ __BEGIN_DECLS
* 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)
- * quiet: when true, don't display log messages
+ * 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 logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
- bool quiet);
+int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit,
+ bool logwrap);
__END_DECLS
diff --git a/logwrapper/logwrap.c b/logwrapper/logwrap.c
index 5af1671..a04097d 100644
--- a/logwrapper/logwrap.c
+++ b/logwrapper/logwrap.c
@@ -36,22 +36,20 @@
static int signal_fd_write;
-#define ERROR(fmt, quiet, args...) \
+#define ERROR(fmt, args...) \
do { \
- if (!quiet) { \
- fprintf(stderr, fmt, ## args); \
- ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
- } \
+ fprintf(stderr, fmt, ## args); \
+ ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
} while(0)
-#define FATAL_CHILD(fmt, quiet, args...) \
+#define FATAL_CHILD(fmt, args...) \
do { \
- ERROR(fmt, quiet, ## args); \
+ 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 quiet) {
+ int *chld_sts, bool logwrap) {
int status = 0;
char buffer[4096];
struct pollfd poll_fds[] = {
@@ -84,7 +82,7 @@ static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
if (poll(poll_fds, remote_hung ? 1 : 2, -1) < 0) {
if (errno == EINTR)
continue;
- ERROR("poll failed\n", quiet);
+ ERROR("poll failed\n");
rc = -1;
goto err_poll;
}
@@ -100,7 +98,7 @@ static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
buffer[b] = '\0';
} else if (buffer[b] == '\n') {
buffer[b] = '\0';
- if (!quiet)
+ if (logwrap)
ALOG(LOG_INFO, btag, "%s", &buffer[a]);
a = b + 1;
}
@@ -109,7 +107,7 @@ static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
if (a == 0 && b == sizeof(buffer) - 1) {
// buffer is full, flush
buffer[b] = '\0';
- if (!quiet)
+ if (logwrap)
ALOG(LOG_INFO, btag, "%s", &buffer[a]);
b = 0;
} else if (a != b) {
@@ -149,22 +147,20 @@ static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
// Flush remaining data
if (a != b) {
buffer[b] = '\0';
- if (!quiet)
+ if (logwrap)
ALOG(LOG_INFO, btag, "%s", &buffer[a]);
}
- if (!quiet) {
- 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 (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;
@@ -173,24 +169,24 @@ err_poll:
return rc;
}
-static void child(int argc, char* argv[], bool quiet) {
+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", quiet, argv_child[0],
+ FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
strerror(errno));
}
}
-void sigchld_handler(int sig) {
+static void sigchld_handler(int sig) {
write(signal_fd_write, &sig, 1);
}
-int logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
- bool quiet) {
+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;
@@ -207,14 +203,14 @@ int logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
/* 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", quiet);
+ 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", quiet);
+ ERROR("Problem with /dev/ptmx\n");
rc = -1;
goto err_ptty;
}
@@ -227,7 +223,7 @@ int logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
pid = fork();
if (pid < 0) {
- ERROR("Failed to fork\n", quiet);
+ ERROR("Failed to fork\n");
rc = -1;
goto err_fork;
} else if (pid == 0) {
@@ -236,7 +232,7 @@ int logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
child_ptty = open(child_devname, O_RDWR);
if (child_ptty < 0) {
- FATAL_CHILD("Problem with child ptty\n", quiet);
+ FATAL_CHILD("Problem with child ptty\n");
return -1;
}
@@ -245,7 +241,7 @@ int logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
dup2(child_ptty, 2);
close(child_ptty);
- child(argc, argv, quiet);
+ child(argc, argv, logwrap);
} else {
struct sigaction ignact;
@@ -272,7 +268,7 @@ int logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
if (rc == -1) {
- ERROR("socketpair failed: %s\n", quiet, strerror(errno));
+ ERROR("socketpair failed: %s\n", strerror(errno));
goto err_socketpair;
}
@@ -283,7 +279,7 @@ int logwrap(int argc, char* argv[], int *status, bool ignore_int_quit,
signal_fd_write = sockets[0];
- rc = parent(argv[0], parent_ptty, sockets[1], pid, status, quiet);
+ rc = parent(argv[0], parent_ptty, sockets[1], pid, status, logwrap);
}
close(sockets[0]);
diff --git a/logwrapper/logwrapper.c b/logwrapper/logwrapper.c
index cba69d9..ed71a29 100644
--- a/logwrapper/logwrapper.c
+++ b/logwrapper/logwrapper.c
@@ -59,7 +59,7 @@ int main(int argc, char* argv[]) {
usage();
}
- rc = logwrap(argc - 1, &argv[1], &status, true, false);
+ rc = android_fork_execvp(argc - 1, &argv[1], &status, true, true);
if (!rc) {
if (WIFEXITED(status))
rc = WEXITSTATUS(status);