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> |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 18 | #include <sys/select.h> |
| 19 | #include <sys/socket.h> |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/wait.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
| 25 | #include <errno.h> |
| 26 | #include <fcntl.h> |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 27 | #include <pthread.h> |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 28 | |
| 29 | #include "private/android_filesystem_config.h" |
| 30 | #include "cutils/log.h" |
Glenn Kasten | 1b4807b | 2012-03-05 15:14:33 -0800 | [diff] [blame] | 31 | #include "cutils/sched_policy.h" |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 32 | |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 33 | struct monitor_data { |
| 34 | const char *tag; |
| 35 | int fd; |
| 36 | }; |
| 37 | |
| 38 | static int parent(const char *tag, int parent_read, int monitor_read) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 39 | int status; |
| 40 | char buffer[4096]; |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 41 | fd_set fds; |
| 42 | int maxfd; |
| 43 | int rc = -EAGAIN; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 44 | |
| 45 | int a = 0; // start index of unprocessed data |
| 46 | int b = 0; // end index of unprocessed data |
| 47 | int sz; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 48 | |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 49 | maxfd = 1 + (parent_read > monitor_read ? parent_read : monitor_read); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 50 | |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 51 | do { |
| 52 | FD_ZERO(&fds); |
| 53 | FD_SET(parent_read, &fds); |
| 54 | FD_SET(monitor_read, &fds); |
| 55 | |
| 56 | if (select(maxfd, &fds, NULL, NULL, NULL) <= 0) { |
| 57 | ALOG(LOG_INFO, "logwrapper", "select failed"); |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | if (FD_ISSET(parent_read, &fds)) { |
| 62 | sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b); |
| 63 | |
| 64 | sz += b; |
| 65 | // Log one line at a time |
| 66 | for (b = 0; b < sz; b++) { |
| 67 | if (buffer[b] == '\r') { |
| 68 | buffer[b] = '\0'; |
| 69 | } else if (buffer[b] == '\n') { |
| 70 | buffer[b] = '\0'; |
| 71 | |
| 72 | ALOG(LOG_INFO, tag, "%s", &buffer[a]); |
| 73 | a = b + 1; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (a == 0 && b == sizeof(buffer) - 1) { |
| 78 | // buffer is full, flush |
| 79 | buffer[b] = '\0'; |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 80 | ALOG(LOG_INFO, tag, "%s", &buffer[a]); |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 81 | b = 0; |
| 82 | } else if (a != b) { |
| 83 | // Keep left-overs |
| 84 | b -= a; |
| 85 | memmove(buffer, &buffer[a], b); |
| 86 | a = 0; |
| 87 | } else { |
| 88 | a = 0; |
| 89 | b = 0; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 93 | // Child exited, get return status and exit loop |
| 94 | if (FD_ISSET(monitor_read, &fds)) { |
| 95 | if (read(monitor_read, &rc, sizeof(rc)) != sizeof(rc)) { |
| 96 | ALOG(LOG_ERROR, "logwrapper", "Unable to read child return " |
| 97 | "status"); |
| 98 | rc = -ECHILD; |
| 99 | } |
| 100 | break; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 101 | } |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 102 | } while (1); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 103 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 104 | // Flush remaining data |
| 105 | if (a != b) { |
| 106 | buffer[b] = '\0'; |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 107 | ALOG(LOG_INFO, tag, "%s", &buffer[a]); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 108 | } |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 109 | |
| 110 | return rc; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 113 | static void child(int argc, const char**argv) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 114 | // create null terminated argv_child array |
| 115 | char* argv_child[argc + 1]; |
| 116 | memcpy(argv_child, argv, argc * sizeof(char *)); |
| 117 | argv_child[argc] = NULL; |
| 118 | |
| 119 | // XXX: PROTECT FROM VIKING KILLER |
| 120 | if (execv(argv_child[0], argv_child)) { |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 121 | ALOG(LOG_ERROR, "logwrapper", |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 122 | "executing %s failed: %s", argv_child[0], strerror(errno)); |
Rom Lemarchand | e90c174 | 2012-12-21 11:35:43 -0800 | [diff] [blame] | 123 | _exit(-1); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 127 | static void *monitor(void *arg) |
| 128 | { |
| 129 | struct monitor_data *data = arg; |
| 130 | int status; |
| 131 | int rc = -EAGAIN; |
| 132 | |
| 133 | if (wait(&status) != -1) { // Wait for child |
| 134 | if (WIFEXITED(status)) { |
| 135 | if (WEXITSTATUS(status) != 0) { |
| 136 | ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", |
| 137 | data->tag, WEXITSTATUS(status)); |
| 138 | } |
| 139 | rc = WEXITSTATUS(status); |
| 140 | } else if (WIFSIGNALED(status)) |
| 141 | ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", |
| 142 | data->tag, WTERMSIG(status)); |
| 143 | else if (WIFSTOPPED(status)) |
| 144 | ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", data->tag, |
| 145 | WSTOPSIG(status)); |
| 146 | } else |
| 147 | ALOG(LOG_INFO, "logwrapper", "%s wait() failed: %s (%d)", data->tag, |
| 148 | strerror(errno), errno); |
| 149 | |
| 150 | write(data->fd, &rc, sizeof(rc)); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 154 | int logwrap(int argc, const char* argv[], int background) |
| 155 | { |
| 156 | pid_t pid; |
| 157 | |
| 158 | int parent_ptty; |
| 159 | int child_ptty; |
| 160 | char *child_devname = NULL; |
| 161 | |
| 162 | /* Use ptty instead of socketpair so that STDOUT is not buffered */ |
| 163 | parent_ptty = open("/dev/ptmx", O_RDWR); |
| 164 | if (parent_ptty < 0) { |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 165 | ALOG(LOG_ERROR, "logwrapper", "Cannot create parent ptty"); |
| 166 | return -errno; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (grantpt(parent_ptty) || unlockpt(parent_ptty) || |
| 170 | ((child_devname = (char*)ptsname(parent_ptty)) == 0)) { |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 171 | close(parent_ptty); |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 172 | ALOG(LOG_ERROR, "logwrapper", "Problem with /dev/ptmx"); |
| 173 | return -1; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | pid = fork(); |
| 177 | if (pid < 0) { |
Brad Fitzpatrick | 1b15d46 | 2010-09-20 11:11:46 -0700 | [diff] [blame] | 178 | close(parent_ptty); |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 179 | ALOG(LOG_ERROR, "logwrapper", "Failed to fork"); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 180 | return -errno; |
| 181 | } else if (pid == 0) { |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 182 | /* |
| 183 | * Child |
| 184 | */ |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 185 | close(parent_ptty); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 186 | child_ptty = open(child_devname, O_RDWR); |
| 187 | if (child_ptty < 0) { |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 188 | ALOG(LOG_ERROR, "logwrapper", "Problem with child ptty"); |
Rom Lemarchand | e90c174 | 2012-12-21 11:35:43 -0800 | [diff] [blame] | 189 | _exit(-errno); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // redirect stdout and stderr |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 193 | dup2(child_ptty, 1); |
| 194 | dup2(child_ptty, 2); |
| 195 | close(child_ptty); |
| 196 | |
| 197 | if (background) { |
Glenn Kasten | 1b4807b | 2012-03-05 15:14:33 -0800 | [diff] [blame] | 198 | int err = set_sched_policy(getpid(), SP_BACKGROUND); |
| 199 | if (err < 0) { |
Steve Block | 8c48733 | 2011-10-12 17:28:59 +0100 | [diff] [blame] | 200 | ALOG(LOG_WARN, "logwrapper", |
Glenn Kasten | 1b4807b | 2012-03-05 15:14:33 -0800 | [diff] [blame] | 201 | "Unable to background process (%s)", strerror(-err)); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | child(argc, argv); |
| 206 | } else { |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 207 | /* |
| 208 | * Parent |
| 209 | */ |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 210 | int rc, err; |
| 211 | int sockets[2]; |
| 212 | pthread_t thread_id; |
| 213 | struct monitor_data data; |
| 214 | |
| 215 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets)) { |
| 216 | err = errno; |
| 217 | ALOG(LOG_ERROR, "logwrapper", "Unable to create monitoring " |
| 218 | "socket: %s (%d)", strerror(err), err); |
| 219 | exit(-err); |
| 220 | } |
| 221 | data.tag = argv[0]; |
| 222 | data.fd = sockets[1]; |
| 223 | err = pthread_create(&thread_id, NULL, monitor, &data); |
| 224 | if (err != 0) { |
| 225 | ALOG(LOG_ERROR, "logwrapper", "Unable to create monitoring " |
| 226 | "thread: %s (%d)", strerror(err), err); |
| 227 | exit(-err); |
| 228 | } |
| 229 | rc = parent(argv[0], parent_ptty, sockets[0]); |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 230 | close(parent_ptty); |
Rom Lemarchand | b9dcde7 | 2012-12-27 10:45:02 -0800 | [diff] [blame] | 231 | close(sockets[0]); |
| 232 | close(sockets[1]); |
San Mehat | 8c940ef | 2010-02-13 14:19:53 -0800 | [diff] [blame] | 233 | return rc; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |