logwrapper.c 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * 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/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#include "private/android_filesystem_config.h"
#include "cutils/log.h"
28
#include "cutils/sched_policy.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

int parent(const char *tag, 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;
    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';

47
                ALOG(LOG_INFO, tag, "%s", &buffer[a]);
48 49 50 51 52 53 54
                a = b + 1;
            }
        }

        if (a == 0 && b == sizeof(buffer) - 1) {
            // buffer is full, flush
            buffer[b] = '\0';
55
            ALOG(LOG_INFO, tag, "%s", &buffer[a]);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
            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';
71
        ALOG(LOG_INFO, tag, "%s", &buffer[a]);
72 73 74 75
    }
    status = 0xAAAA;
    if (wait(&status) != -1) {  // Wait for child
        if (WIFEXITED(status)) {
76
            if (WEXITSTATUS(status) != 0) {
77
                ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
78 79
                        WEXITSTATUS(status));
            }
80 81
            return WEXITSTATUS(status);
        } else if (WIFSIGNALED(status))
82
            ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag,
83 84
                    WTERMSIG(status));
        else if (WIFSTOPPED(status))
85
            ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag,
86 87
                    WSTOPSIG(status));
    } else
88
        ALOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag,
89 90 91 92 93 94 95 96 97 98 99 100
                strerror(errno), errno);
    return -EAGAIN;
}

void child(int argc, const char**argv) {
    // create null terminated argv_child array
    char* argv_child[argc + 1];
    memcpy(argv_child, argv, argc * sizeof(char *));
    argv_child[argc] = NULL;

    // XXX: PROTECT FROM VIKING KILLER
    if (execv(argv_child[0], argv_child)) {
101
        ALOG(LOG_ERROR, "logwrapper",
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            "executing %s failed: %s", argv_child[0], strerror(errno));
        exit(-1);
    }
}

int logwrap(int argc, const char* argv[], int background)
{
    pid_t pid;

    int parent_ptty;
    int child_ptty;
    char *child_devname = NULL;

    /* Use ptty instead of socketpair so that STDOUT is not buffered */
    parent_ptty = open("/dev/ptmx", O_RDWR);
    if (parent_ptty < 0) {
118 119
        ALOG(LOG_ERROR, "logwrapper", "Cannot create parent ptty");
        return -errno;
120 121 122 123
    }

    if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
            ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
124
        close(parent_ptty);
125 126
        ALOG(LOG_ERROR, "logwrapper", "Problem with /dev/ptmx");
        return -1;
127 128 129 130
    }

    pid = fork();
    if (pid < 0) {
131
        close(parent_ptty);
132
        ALOG(LOG_ERROR, "logwrapper", "Failed to fork");
133 134
        return -errno;
    } else if (pid == 0) {
135 136 137
        /*
         * Child
         */
138 139
        child_ptty = open(child_devname, O_RDWR);
        if (child_ptty < 0) {
140
            close(parent_ptty);
141
            ALOG(LOG_ERROR, "logwrapper", "Problem with child ptty");
142 143 144 145 146 147 148 149 150 151
            return -errno;
        }

        // redirect stdout and stderr
        close(parent_ptty);
        dup2(child_ptty, 1);
        dup2(child_ptty, 2);
        close(child_ptty);

        if (background) {
152 153
            int err = set_sched_policy(getpid(), SP_BACKGROUND);
            if (err < 0) {
154
                ALOG(LOG_WARN, "logwrapper",
155
                    "Unable to background process (%s)", strerror(-err));
156 157 158 159 160
            }
        }

        child(argc, argv);
    } else {
161 162 163 164 165 166
        /*
         * Parent
         */
        int rc = parent(argv[0], parent_ptty);
        close(parent_ptty);
        return rc;
167 168 169 170
    }

    return 0;
}