San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/wait.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | |
| 26 | #include "private/android_filesystem_config.h" |
| 27 | #include "cutils/log.h" |
Glenn Kasten | 1b4807b | 2012-03-05 15:14:33 -0800 | [diff] [blame] | 28 | #include "cutils/sched_policy.h" |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 29 | |
| 30 | int parent(const char *tag, int parent_read) { |
| 31 | int status; |
| 32 | char buffer[4096]; |
| 33 | |
| 34 | int a = 0; // start index of unprocessed data |
| 35 | int b = 0; // end index of unprocessed data |
| 36 | int sz; |
| 37 | while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) { |
| 38 | |
| 39 | sz += b; |
| 40 | // Log one line at a time |
| 41 | for (b = 0; b < sz; b++) { |
| 42 | if (buffer[b] == '\r') { |
| 43 | buffer[b] = '\0'; |
| 44 | } else if (buffer[b] == '\n') { |
| 45 | buffer[b] = '\0'; |
| 46 | |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 47 | ALOG(LOG_INFO, tag, "%s", &buffer[a]); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 48 | a = b + 1; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (a == 0 && b == sizeof(buffer) - 1) { |
| 53 | // buffer is full, flush |
| 54 | buffer[b] = '\0'; |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 55 | ALOG(LOG_INFO, tag, "%s", &buffer[a]); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 56 | b = 0; |
| 57 | } else if (a != b) { |
| 58 | // Keep left-overs |
| 59 | b -= a; |
| 60 | memmove(buffer, &buffer[a], b); |
| 61 | a = 0; |
| 62 | } else { |
| 63 | a = 0; |
| 64 | b = 0; |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | // Flush remaining data |
| 69 | if (a != b) { |
| 70 | buffer[b] = '\0'; |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 71 | ALOG(LOG_INFO, tag, "%s", &buffer[a]); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 72 | } |
| 73 | status = 0xAAAA; |
| 74 | if (wait(&status) != -1) { // Wait for child |
| 75 | if (WIFEXITED(status)) { |
Brad Fitzpatrick | 1b15d46 | 2010-09-20 11:11:46 -0700 | [diff] [blame] | 76 | if (WEXITSTATUS(status) != 0) { |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 77 | ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag, |
Brad Fitzpatrick | 1b15d46 | 2010-09-20 11:11:46 -0700 | [diff] [blame] | 78 | WEXITSTATUS(status)); |
| 79 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 80 | return WEXITSTATUS(status); |
| 81 | } else if (WIFSIGNALED(status)) |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 82 | ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag, |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 83 | WTERMSIG(status)); |
| 84 | else if (WIFSTOPPED(status)) |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 85 | ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag, |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 86 | WSTOPSIG(status)); |
| 87 | } else |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 88 | ALOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", tag, |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 89 | strerror(errno), errno); |
| 90 | return -EAGAIN; |
| 91 | } |
| 92 | |
| 93 | void child(int argc, const char**argv) { |
| 94 | // create null terminated argv_child array |
| 95 | char* argv_child[argc + 1]; |
| 96 | memcpy(argv_child, argv, argc * sizeof(char *)); |
| 97 | argv_child[argc] = NULL; |
| 98 | |
| 99 | // XXX: PROTECT FROM VIKING KILLER |
| 100 | if (execv(argv_child[0], argv_child)) { |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 101 | ALOG(LOG_ERROR, "logwrapper", |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 102 | "executing %s failed: %s", argv_child[0], strerror(errno)); |
| 103 | exit(-1); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | int logwrap(int argc, const char* argv[], int background) |
| 108 | { |
| 109 | pid_t pid; |
| 110 | |
| 111 | int parent_ptty; |
| 112 | int child_ptty; |
| 113 | char *child_devname = NULL; |
| 114 | |
| 115 | /* Use ptty instead of socketpair so that STDOUT is not buffered */ |
| 116 | parent_ptty = open("/dev/ptmx", O_RDWR); |
| 117 | if (parent_ptty < 0) { |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 118 | ALOG(LOG_ERROR, "logwrapper", "Cannot create parent ptty"); |
| 119 | return -errno; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | if (grantpt(parent_ptty) || unlockpt(parent_ptty) || |
| 123 | ((child_devname = (char*)ptsname(parent_ptty)) == 0)) { |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 124 | close(parent_ptty); |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 125 | ALOG(LOG_ERROR, "logwrapper", "Problem with /dev/ptmx"); |
| 126 | return -1; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | pid = fork(); |
| 130 | if (pid < 0) { |
Brad Fitzpatrick | 1b15d46 | 2010-09-20 11:11:46 -0700 | [diff] [blame] | 131 | close(parent_ptty); |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 132 | ALOG(LOG_ERROR, "logwrapper", "Failed to fork"); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 133 | return -errno; |
| 134 | } else if (pid == 0) { |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 135 | /* |
| 136 | * Child |
| 137 | */ |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 138 | child_ptty = open(child_devname, O_RDWR); |
| 139 | if (child_ptty < 0) { |
Brad Fitzpatrick | 1b15d46 | 2010-09-20 11:11:46 -0700 | [diff] [blame] | 140 | close(parent_ptty); |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 141 | ALOG(LOG_ERROR, "logwrapper", "Problem with child ptty"); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 142 | return -errno; |
| 143 | } |
| 144 | |
| 145 | // redirect stdout and stderr |
| 146 | close(parent_ptty); |
| 147 | dup2(child_ptty, 1); |
| 148 | dup2(child_ptty, 2); |
| 149 | close(child_ptty); |
| 150 | |
| 151 | if (background) { |
Glenn Kasten | 1b4807b | 2012-03-05 15:14:33 -0800 | [diff] [blame] | 152 | int err = set_sched_policy(getpid(), SP_BACKGROUND); |
| 153 | if (err < 0) { |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 154 | ALOG(LOG_WARN, "logwrapper", |
Glenn Kasten | 1b4807b | 2012-03-05 15:14:33 -0800 | [diff] [blame] | 155 | "Unable to background process (%s)", strerror(-err)); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | child(argc, argv); |
| 160 | } else { |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 161 | /* |
| 162 | * Parent |
| 163 | */ |
| 164 | int rc = parent(argv[0], parent_ptty); |
| 165 | close(parent_ptty); |
| 166 | return rc; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |