Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a46bfa6 authored by Rom Lemarchand's avatar Rom Lemarchand
Browse files

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
parent 49f0f776
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -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

+32 −36
Original line number Diff line number Diff line
@@ -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);                              \
    }                                                                         \
} 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,11 +147,10 @@ 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,
@@ -165,7 +162,6 @@ static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid,
        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]);
+1 −1
Original line number Diff line number Diff line
@@ -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);