Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [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> |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 19 | #include <sys/signalfd.h> |
| 20 | #include <signal.h> |
| 21 | #include <poll.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 22 | #include <sys/wait.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <errno.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <libgen.h> |
| 29 | |
| 30 | #include <logwrap/logwrap.h> |
| 31 | #include "private/android_filesystem_config.h" |
| 32 | #include "cutils/log.h" |
| 33 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 34 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
| 35 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 36 | static int fatal(const char *msg) { |
| 37 | fprintf(stderr, "%s", msg); |
| 38 | ALOG(LOG_ERROR, "logwrapper", "%s", msg); |
| 39 | return -1; |
| 40 | } |
| 41 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 42 | static int parent(const char *tag, int parent_read, int signal_fd, pid_t pid, |
| 43 | int *chld_sts) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 44 | int status; |
| 45 | char buffer[4096]; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 46 | struct pollfd poll_fds[] = { |
| 47 | [0] = { |
| 48 | .fd = parent_read, |
| 49 | .events = POLLIN, |
| 50 | }, |
| 51 | [1] = { |
| 52 | .fd = signal_fd, |
| 53 | .events = POLLIN, |
| 54 | }, |
| 55 | }; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 56 | |
| 57 | int a = 0; // start index of unprocessed data |
| 58 | int b = 0; // end index of unprocessed data |
| 59 | int sz; |
| 60 | |
| 61 | char *btag = basename(tag); |
| 62 | if (!btag) btag = (char*) tag; |
| 63 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 64 | while (1) { |
| 65 | if (poll(poll_fds, ARRAY_SIZE(poll_fds), -1) <= 0) { |
| 66 | return fatal("poll failed\n"); |
| 67 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 68 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 69 | if (poll_fds[0].revents & POLLIN) { |
| 70 | sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b); |
| 71 | |
| 72 | sz += b; |
| 73 | // Log one line at a time |
| 74 | for (b = 0; b < sz; b++) { |
| 75 | if (buffer[b] == '\r') { |
| 76 | buffer[b] = '\0'; |
| 77 | } else if (buffer[b] == '\n') { |
| 78 | buffer[b] = '\0'; |
| 79 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
| 80 | a = b + 1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (a == 0 && b == sizeof(buffer) - 1) { |
| 85 | // buffer is full, flush |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 86 | buffer[b] = '\0'; |
| 87 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 88 | b = 0; |
| 89 | } else if (a != b) { |
| 90 | // Keep left-overs |
| 91 | b -= a; |
| 92 | memmove(buffer, &buffer[a], b); |
| 93 | a = 0; |
| 94 | } else { |
| 95 | a = 0; |
| 96 | b = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 100 | if (poll_fds[1].revents & POLLIN) { |
| 101 | struct signalfd_siginfo sfd_info; |
| 102 | pid_t wpid; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 103 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 104 | // Clear all pending signals before reading the child's status |
| 105 | while (read(signal_fd, &sfd_info, sizeof(sfd_info)) > 0) { |
| 106 | if ((pid_t)sfd_info.ssi_pid != pid) |
| 107 | ALOG(LOG_WARN, "logwrapper", "cleared SIGCHLD for pid %u\n", |
| 108 | sfd_info.ssi_pid); |
| 109 | } |
| 110 | wpid = waitpid(pid, &status, WNOHANG); |
| 111 | if (wpid > 0) |
| 112 | break; |
| 113 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 114 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 115 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 116 | // Flush remaining data |
| 117 | if (a != b) { |
| 118 | buffer[b] = '\0'; |
| 119 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
| 120 | } |
| 121 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 122 | if (WIFEXITED(status)) { |
| 123 | if (WEXITSTATUS(status)) |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 124 | ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag, |
| 125 | WEXITSTATUS(status)); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 126 | } else if (WIFSIGNALED(status)) { |
| 127 | ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", tag, |
| 128 | WTERMSIG(status)); |
| 129 | } else if (WIFSTOPPED(status)) { |
| 130 | ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", tag, |
| 131 | WSTOPSIG(status)); |
| 132 | } |
| 133 | if (chld_sts != NULL) |
| 134 | *chld_sts = status; |
| 135 | |
| 136 | return 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 139 | static void child(int argc, char* argv[]) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 140 | // create null terminated argv_child array |
| 141 | char* argv_child[argc + 1]; |
| 142 | memcpy(argv_child, argv, argc * sizeof(char *)); |
| 143 | argv_child[argc] = NULL; |
| 144 | |
| 145 | if (execvp(argv_child[0], argv_child)) { |
| 146 | ALOG(LOG_ERROR, "logwrapper", |
| 147 | "executing %s failed: %s\n", argv_child[0], strerror(errno)); |
| 148 | exit(-1); |
| 149 | } |
| 150 | } |
| 151 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 152 | int logwrap(int argc, char* argv[], int *status) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 153 | pid_t pid; |
| 154 | |
| 155 | int parent_ptty; |
| 156 | int child_ptty; |
| 157 | char *child_devname = NULL; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 158 | sigset_t chldset; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 159 | |
| 160 | /* Use ptty instead of socketpair so that STDOUT is not buffered */ |
| 161 | parent_ptty = open("/dev/ptmx", O_RDWR); |
| 162 | if (parent_ptty < 0) { |
| 163 | return fatal("Cannot create parent ptty\n"); |
| 164 | } |
| 165 | |
| 166 | if (grantpt(parent_ptty) || unlockpt(parent_ptty) || |
| 167 | ((child_devname = (char*)ptsname(parent_ptty)) == 0)) { |
| 168 | return fatal("Problem with /dev/ptmx\n"); |
| 169 | } |
| 170 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 171 | sigemptyset(&chldset); |
| 172 | sigaddset(&chldset, SIGCHLD); |
| 173 | sigprocmask(SIG_BLOCK, &chldset, NULL); |
| 174 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 175 | pid = fork(); |
| 176 | if (pid < 0) { |
| 177 | close(parent_ptty); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 178 | sigprocmask(SIG_UNBLOCK, &chldset, NULL); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 179 | return fatal("Failed to fork\n"); |
| 180 | } else if (pid == 0) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 181 | close(parent_ptty); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 182 | sigprocmask(SIG_UNBLOCK, &chldset, NULL); |
| 183 | child_ptty = open(child_devname, O_RDWR); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 184 | if (child_ptty < 0) { |
| 185 | return fatal("Problem with child ptty\n"); |
| 186 | } |
| 187 | |
| 188 | // redirect stdout and stderr |
| 189 | dup2(child_ptty, 1); |
| 190 | dup2(child_ptty, 2); |
| 191 | close(child_ptty); |
| 192 | |
| 193 | child(argc - 1, &argv[1]); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 194 | return fatal("This should never happen\n"); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 195 | |
| 196 | } else { |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 197 | int rc; |
| 198 | int fd; |
| 199 | |
| 200 | fd = signalfd(-1, &chldset, SFD_NONBLOCK); |
| 201 | if (fd == -1) { |
| 202 | char msg[40]; |
| 203 | |
| 204 | snprintf(msg, sizeof(msg), "signalfd failed: %d\n", errno); |
| 205 | |
| 206 | close(parent_ptty); |
| 207 | sigprocmask(SIG_UNBLOCK, &chldset, NULL); |
| 208 | return fatal(msg); |
| 209 | } |
| 210 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 211 | // switch user and group to "log" |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 212 | // this may fail if we are not root, |
| 213 | // but in that case switching user/group is unnecessary |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 214 | setgid(AID_LOG); |
| 215 | setuid(AID_LOG); |
| 216 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 217 | rc = parent(argv[1], parent_ptty, fd, pid, status); |
| 218 | close(parent_ptty); |
| 219 | close(fd); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 220 | |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame^] | 221 | sigprocmask(SIG_UNBLOCK, &chldset, NULL); |
| 222 | |
| 223 | return rc; |
| 224 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 225 | } |