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 | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 19 | #include <sys/socket.h> |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 20 | #include <poll.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 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> |
| 27 | #include <libgen.h> |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 28 | #include <stdbool.h> |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 29 | #include <pthread.h> |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 30 | |
| 31 | #include <logwrap/logwrap.h> |
| 32 | #include "private/android_filesystem_config.h" |
| 33 | #include "cutils/log.h" |
| 34 | |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 35 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 36 | |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 37 | static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 38 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 39 | #define ERROR(fmt, args...) \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 40 | do { \ |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 41 | fprintf(stderr, fmt, ## args); \ |
| 42 | ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 43 | } while(0) |
| 44 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 45 | #define FATAL_CHILD(fmt, args...) \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 46 | do { \ |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 47 | ERROR(fmt, ## args); \ |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 48 | _exit(-1); \ |
| 49 | } while(0) |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 50 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 51 | static int parent(const char *tag, int parent_read, pid_t pid, int *chld_sts, |
| 52 | bool logwrap) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 53 | int status = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 54 | char buffer[4096]; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 55 | struct pollfd poll_fds[] = { |
| 56 | [0] = { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 57 | .fd = parent_read, |
| 58 | .events = POLLIN, |
| 59 | }, |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 60 | }; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 61 | int rc = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 62 | |
| 63 | int a = 0; // start index of unprocessed data |
| 64 | int b = 0; // end index of unprocessed data |
| 65 | int sz; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 66 | bool found_child = false; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 67 | |
| 68 | char *btag = basename(tag); |
| 69 | if (!btag) btag = (char*) tag; |
| 70 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 71 | while (!found_child) { |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 72 | if (TEMP_FAILURE_RETRY(poll(poll_fds, ARRAY_SIZE(poll_fds), -1)) < 0) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 73 | ERROR("poll failed\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 74 | rc = -1; |
| 75 | goto err_poll; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 76 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 77 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 78 | if (poll_fds[0].revents & POLLIN) { |
| 79 | sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 80 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 81 | sz += b; |
| 82 | // Log one line at a time |
| 83 | for (b = 0; b < sz; b++) { |
| 84 | if (buffer[b] == '\r') { |
| 85 | buffer[b] = '\0'; |
| 86 | } else if (buffer[b] == '\n') { |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 87 | buffer[b] = '\0'; |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 88 | if (logwrap) |
Rom Lemarchand | f5200c0 | 2013-01-24 10:57:44 -0800 | [diff] [blame] | 89 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 90 | a = b + 1; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 94 | if (a == 0 && b == sizeof(buffer) - 1) { |
| 95 | // buffer is full, flush |
| 96 | buffer[b] = '\0'; |
| 97 | if (logwrap) |
| 98 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
| 99 | b = 0; |
| 100 | } else if (a != b) { |
| 101 | // Keep left-overs |
| 102 | b -= a; |
| 103 | memmove(buffer, &buffer[a], b); |
| 104 | a = 0; |
| 105 | } else { |
| 106 | a = 0; |
| 107 | b = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 111 | if (poll_fds[0].revents & POLLHUP) { |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 112 | int ret; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 113 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 114 | ret = waitpid(pid, &status, WNOHANG); |
| 115 | if (ret < 0) { |
| 116 | rc = errno; |
| 117 | ALOG(LOG_ERROR, "logwrap", "waitpid failed with %s\n", strerror(errno)); |
| 118 | goto err_waitpid; |
| 119 | } |
| 120 | if (ret > 0) { |
| 121 | found_child = true; |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 122 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 123 | } |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 124 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 125 | |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 126 | if (chld_sts != NULL) { |
| 127 | *chld_sts = status; |
| 128 | } else { |
| 129 | if (WIFEXITED(status)) |
| 130 | rc = WEXITSTATUS(status); |
| 131 | else |
| 132 | rc = -ECHILD; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 133 | } |
| 134 | |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 135 | if (logwrap) { |
| 136 | // Flush remaining data |
| 137 | if (a != b) { |
| 138 | buffer[b] = '\0'; |
| 139 | ALOG(LOG_INFO, btag, "%s", &buffer[a]); |
| 140 | } |
| 141 | if (WIFEXITED(status)) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 142 | if (WEXITSTATUS(status)) |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 143 | ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", btag, |
| 144 | WEXITSTATUS(status)); |
| 145 | } else { |
Rom Lemarchand | 2f34c50 | 2013-02-07 15:43:09 -0800 | [diff] [blame] | 146 | if (WIFSIGNALED(status)) |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 147 | ALOG(LOG_INFO, "logwrapper", "%s terminated by signal %d", btag, |
| 148 | WTERMSIG(status)); |
Rom Lemarchand | 2f34c50 | 2013-02-07 15:43:09 -0800 | [diff] [blame] | 149 | else if (WIFSTOPPED(status)) |
JP Abgrall | a689d13 | 2013-02-13 16:31:58 -0800 | [diff] [blame] | 150 | ALOG(LOG_INFO, "logwrapper", "%s stopped by signal %d", btag, |
| 151 | WSTOPSIG(status)); |
| 152 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 153 | } |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 154 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 155 | err_waitpid: |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 156 | err_poll: |
| 157 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 160 | static void child(int argc, char* argv[], bool logwrap) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 161 | // create null terminated argv_child array |
| 162 | char* argv_child[argc + 1]; |
| 163 | memcpy(argv_child, argv, argc * sizeof(char *)); |
| 164 | argv_child[argc] = NULL; |
| 165 | |
| 166 | if (execvp(argv_child[0], argv_child)) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 167 | FATAL_CHILD("executing %s failed: %s\n", argv_child[0], |
Rom Lemarchand | 99e1966 | 2013-01-07 15:50:02 -0800 | [diff] [blame] | 168 | strerror(errno)); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 172 | int android_fork_execvp(int argc, char* argv[], int *status, bool ignore_int_quit, |
| 173 | bool logwrap) { |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 174 | pid_t pid; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 175 | int parent_ptty; |
| 176 | int child_ptty; |
| 177 | char *child_devname = NULL; |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 178 | struct sigaction intact; |
| 179 | struct sigaction quitact; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 180 | sigset_t blockset; |
| 181 | sigset_t oldset; |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 182 | int rc = 0; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 183 | |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 184 | rc = pthread_mutex_lock(&fd_mutex); |
| 185 | if (rc) { |
| 186 | ERROR("failed to lock signal_fd mutex\n"); |
| 187 | goto err_lock; |
| 188 | } |
| 189 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 190 | /* Use ptty instead of socketpair so that STDOUT is not buffered */ |
| 191 | parent_ptty = open("/dev/ptmx", O_RDWR); |
| 192 | if (parent_ptty < 0) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 193 | ERROR("Cannot create parent ptty\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 194 | rc = -1; |
| 195 | goto err_open; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (grantpt(parent_ptty) || unlockpt(parent_ptty) || |
| 199 | ((child_devname = (char*)ptsname(parent_ptty)) == 0)) { |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 200 | ERROR("Problem with /dev/ptmx\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 201 | rc = -1; |
| 202 | goto err_ptty; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 205 | child_ptty = open(child_devname, O_RDWR); |
| 206 | if (child_ptty < 0) { |
| 207 | ERROR("Cannot open child_ptty\n"); |
| 208 | rc = -1; |
| 209 | goto err_child_ptty; |
| 210 | } |
| 211 | |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 212 | sigemptyset(&blockset); |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 213 | sigaddset(&blockset, SIGINT); |
| 214 | sigaddset(&blockset, SIGQUIT); |
Rom Lemarchand | ed179d2 | 2013-01-16 15:07:30 -0800 | [diff] [blame] | 215 | pthread_sigmask(SIG_BLOCK, &blockset, &oldset); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 216 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 217 | pid = fork(); |
| 218 | if (pid < 0) { |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 219 | close(child_ptty); |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 220 | ERROR("Failed to fork\n"); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 221 | rc = -1; |
| 222 | goto err_fork; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 223 | } else if (pid == 0) { |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 224 | pthread_mutex_unlock(&fd_mutex); |
Rom Lemarchand | ed179d2 | 2013-01-16 15:07:30 -0800 | [diff] [blame] | 225 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 226 | close(parent_ptty); |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 227 | |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 228 | // redirect stdout and stderr |
| 229 | dup2(child_ptty, 1); |
| 230 | dup2(child_ptty, 2); |
| 231 | close(child_ptty); |
| 232 | |
Rom Lemarchand | 2a46bfa | 2013-01-29 11:44:59 -0800 | [diff] [blame] | 233 | child(argc, argv, logwrap); |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 234 | } else { |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 235 | close(child_ptty); |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 236 | if (ignore_int_quit) { |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 237 | struct sigaction ignact; |
| 238 | |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 239 | memset(&ignact, 0, sizeof(ignact)); |
| 240 | ignact.sa_handler = SIG_IGN; |
| 241 | sigaction(SIGINT, &ignact, &intact); |
| 242 | sigaction(SIGQUIT, &ignact, &quitact); |
| 243 | } |
| 244 | |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 245 | rc = parent(argv[0], parent_ptty, pid, status, logwrap); |
Rom Lemarchand | b58a822 | 2013-01-09 21:31:25 -0800 | [diff] [blame] | 246 | } |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 247 | |
Rom Lemarchand | 75c289a | 2013-01-09 10:20:25 -0800 | [diff] [blame] | 248 | if (ignore_int_quit) { |
| 249 | sigaction(SIGINT, &intact, NULL); |
| 250 | sigaction(SIGQUIT, &quitact, NULL); |
| 251 | } |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 252 | err_fork: |
Rom Lemarchand | ed179d2 | 2013-01-16 15:07:30 -0800 | [diff] [blame] | 253 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
Rom Lemarchand | 6ad53df | 2013-03-20 13:46:53 -0700 | [diff] [blame^] | 254 | err_child_ptty: |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 255 | err_ptty: |
| 256 | close(parent_ptty); |
| 257 | err_open: |
Rom Lemarchand | 74a7b91 | 2013-03-11 14:00:58 -0700 | [diff] [blame] | 258 | pthread_mutex_unlock(&fd_mutex); |
| 259 | err_lock: |
Rom Lemarchand | 0cc2cab | 2013-01-16 12:08:04 -0800 | [diff] [blame] | 260 | return rc; |
Rom Lemarchand | 113bd47 | 2013-01-10 15:21:18 -0800 | [diff] [blame] | 261 | } |