2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
19 #include <sys/socket.h>
31 #include <logwrap/logwrap.h>
32 #include "private/android_filesystem_config.h"
33 #include "cutils/log.h"
34 #include <cutils/klog.h>
36 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
37 #define MIN(a,b) (((a)<(b))?(a):(b))
39 static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
41 #define ERROR(fmt, args...) \
43 fprintf(stderr, fmt, ## args); \
44 ALOG(LOG_ERROR, "logwrapper", fmt, ## args); \
47 #define FATAL_CHILD(fmt, args...) \
49 ERROR(fmt, ## args); \
53 #define MAX_KLOG_TAG 16
55 /* This is a simple buffer that holds up to the first beginning_buf->buf_size
56 * bytes of output from a command.
58 #define BEGINNING_BUF_SIZE 0x1000
59 struct beginning_buf {
62 /* buf_size is the usable space, which is one less than the allocated size */
67 /* This is a circular buf that holds up to the last ending_buf->buf_size bytes
68 * of output from a command after the first beginning_buf->buf_size bytes
69 * (which are held in beginning_buf above).
71 #define ENDING_BUF_SIZE 0x1000
75 /* buf_size is the usable space, which is one less than the allocated size */
78 /* read and write offsets into the circular buffer */
83 /* A structure to hold all the abbreviated buf data */
85 struct beginning_buf b_buf;
86 struct ending_buf e_buf;
87 int beginning_buf_full;
90 /* Collect all the various bits of info needed for logging in one place. */
93 char klog_fmt[MAX_KLOG_TAG * 2];
97 struct abbr_buf a_buf;
100 /* Forware declaration */
101 static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen);
103 /* Return 0 on success, and 1 when full */
104 static int add_line_to_linear_buf(struct beginning_buf *b_buf,
105 char *line, ssize_t line_len)
111 if ((line_len + b_buf->used_len) > b_buf->buf_size) {
114 /* Add to the end of the buf */
115 memcpy(b_buf->buf + b_buf->used_len, line, line_len);
116 b_buf->used_len += line_len;
122 static void add_line_to_circular_buf(struct ending_buf *e_buf,
123 char *line, ssize_t line_len)
126 ssize_t needed_space;
130 if (e_buf->buf == NULL) {
134 if (line_len > e_buf->buf_size) {
138 free_len = e_buf->buf_size - e_buf->used_len;
140 if (line_len > free_len) {
141 /* remove oldest entries at read, and move read to make
142 * room for the new string */
143 needed_space = line_len - free_len;
144 e_buf->read = (e_buf->read + needed_space) % e_buf->buf_size;
145 e_buf->used_len -= needed_space;
148 /* Copy the line into the circular buffer, dealing with possible
151 cnt = MIN(line_len, e_buf->buf_size - e_buf->write);
152 memcpy(e_buf->buf + e_buf->write, line, cnt);
153 if (cnt < line_len) {
154 memcpy(e_buf->buf, line + cnt, line_len - cnt);
156 e_buf->used_len += line_len;
157 e_buf->write = (e_buf->write + line_len) % e_buf->buf_size;
160 /* Log directly to the specified log */
161 static void do_log_line(struct log_info *log_info, char *line) {
162 if (log_info->log_target & LOG_KLOG) {
163 klog_write(6, log_info->klog_fmt, line);
165 if (log_info->log_target & LOG_ALOG) {
166 ALOG(LOG_INFO, log_info->btag, "%s", line);
168 if (log_info->log_target & LOG_FILE) {
169 fprintf(log_info->fp, "%s\n", line);
173 /* Log to either the abbreviated buf, or directly to the specified log
174 * via do_log_line() above.
176 static void log_line(struct log_info *log_info, char *line, int len) {
177 if (log_info->abbreviated) {
178 add_line_to_abbr_buf(&log_info->a_buf, line, len);
180 do_log_line(log_info, line);
185 * The kernel will take a maximum of 1024 bytes in any single write to
186 * the kernel logging device file, so find and print each line one at
187 * a time. The allocated size for buf should be at least 1 byte larger
188 * than buf_size (the usable size of the buffer) to make sure there is
189 * room to temporarily stuff a null byte to terminate a line for logging.
191 static void print_buf_lines(struct log_info *log_info, char *buf, int buf_size)
199 for (i = 0; i < buf_size; i++) {
200 if (*(buf + i) == '\n') {
201 /* Found a line ending, print the line and compute new line_start */
202 /* Save the next char and replace with \0 */
204 *(buf + i + 1) = '\0';
205 do_log_line(log_info, line_start);
206 /* Restore the saved char */
208 line_start = buf + i + 1;
209 } else if (*(buf + i) == '\0') {
210 /* The end of the buffer, print the last bit */
211 do_log_line(log_info, line_start);
215 /* If the buffer was completely full, and didn't end with a newline, just
216 * ignore the partial last line.
220 static void init_abbr_buf(struct abbr_buf *a_buf) {
223 memset(a_buf, 0, sizeof(struct abbr_buf));
224 new_buf = malloc(BEGINNING_BUF_SIZE);
226 a_buf->b_buf.buf = new_buf;
227 a_buf->b_buf.alloc_len = BEGINNING_BUF_SIZE;
228 a_buf->b_buf.buf_size = BEGINNING_BUF_SIZE - 1;
230 new_buf = malloc(ENDING_BUF_SIZE);
232 a_buf->e_buf.buf = new_buf;
233 a_buf->e_buf.alloc_len = ENDING_BUF_SIZE;
234 a_buf->e_buf.buf_size = ENDING_BUF_SIZE - 1;
238 static void free_abbr_buf(struct abbr_buf *a_buf) {
239 free(a_buf->b_buf.buf);
240 free(a_buf->e_buf.buf);
243 static void add_line_to_abbr_buf(struct abbr_buf *a_buf, char *linebuf, int linelen) {
244 if (!a_buf->beginning_buf_full) {
245 a_buf->beginning_buf_full =
246 add_line_to_linear_buf(&a_buf->b_buf, linebuf, linelen);
248 if (a_buf->beginning_buf_full) {
249 add_line_to_circular_buf(&a_buf->e_buf, linebuf, linelen);
253 static void print_abbr_buf(struct log_info *log_info) {
254 struct abbr_buf *a_buf = &log_info->a_buf;
256 /* Add the abbreviated output to the kernel log */
257 if (a_buf->b_buf.alloc_len) {
258 print_buf_lines(log_info, a_buf->b_buf.buf, a_buf->b_buf.used_len);
261 /* Print an ellipsis to indicate that the buffer has wrapped or
262 * is full, and some data was not logged.
264 if (a_buf->e_buf.used_len == a_buf->e_buf.buf_size) {
265 do_log_line(log_info, "...\n");
268 if (a_buf->e_buf.used_len == 0) {
272 /* Simplest way to print the circular buffer is allocate a second buf
273 * of the same size, and memcpy it so it's a simple linear buffer,
274 * and then cal print_buf_lines on it */
275 if (a_buf->e_buf.read < a_buf->e_buf.write) {
276 /* no wrap around, just print it */
277 print_buf_lines(log_info, a_buf->e_buf.buf + a_buf->e_buf.read,
278 a_buf->e_buf.used_len);
280 /* The circular buffer will always have at least 1 byte unused,
281 * so by allocating alloc_len here we will have at least
282 * 1 byte of space available as required by print_buf_lines().
284 char * nbuf = malloc(a_buf->e_buf.alloc_len);
288 int first_chunk_len = a_buf->e_buf.buf_size - a_buf->e_buf.read;
289 memcpy(nbuf, a_buf->e_buf.buf + a_buf->e_buf.read, first_chunk_len);
290 /* copy second chunk */
291 memcpy(nbuf + first_chunk_len, a_buf->e_buf.buf, a_buf->e_buf.write);
292 print_buf_lines(log_info, nbuf, first_chunk_len + a_buf->e_buf.write);
297 static int parent(const char *tag, int parent_read, pid_t pid,
298 int *chld_sts, int log_target, bool abbreviated, char *file_path) {
301 struct pollfd poll_fds[] = {
310 struct log_info log_info;
312 int a = 0; // start index of unprocessed data
313 int b = 0; // end index of unprocessed data
315 bool found_child = false;
318 log_info.btag = basename(tag);
319 if (!log_info.btag) {
320 log_info.btag = (char*) tag;
323 if (abbreviated && (log_target == LOG_NONE)) {
327 init_abbr_buf(&log_info.a_buf);
330 if (log_target & LOG_KLOG) {
331 snprintf(log_info.klog_fmt, sizeof(log_info.klog_fmt),
332 "<6>%.*s: %%s", MAX_KLOG_TAG, log_info.btag);
335 if ((log_target & LOG_FILE) && !file_path) {
336 /* No file_path specified, clear the LOG_FILE bit */
337 log_target &= ~LOG_FILE;
340 if (log_target & LOG_FILE) {
341 fd = open(file_path, O_WRONLY | O_CREAT, 0664);
343 ERROR("Cannot log to file %s\n", file_path);
344 log_target &= ~LOG_FILE;
346 lseek(fd, 0, SEEK_END);
347 log_info.fp = fdopen(fd, "a");
351 log_info.log_target = log_target;
352 log_info.abbreviated = abbreviated;
354 while (!found_child) {
355 if (TEMP_FAILURE_RETRY(poll(poll_fds, ARRAY_SIZE(poll_fds), -1)) < 0) {
356 ERROR("poll failed\n");
361 if (poll_fds[0].revents & POLLIN) {
362 sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b);
365 // Log one line at a time
366 for (b = 0; b < sz; b++) {
367 if (buffer[b] == '\r') {
369 /* The abbreviated logging code uses newline as
370 * the line separator. Lucikly, the pty layer
371 * helpfully cooks the output of the command
372 * being run and inserts a CR before NL. So
373 * I just change it to NL here when doing
374 * abbreviated logging.
380 } else if (buffer[b] == '\n') {
382 log_line(&log_info, &buffer[a], b - a);
387 if (a == 0 && b == sizeof(buffer) - 1) {
388 // buffer is full, flush
390 log_line(&log_info, &buffer[a], b - a);
395 memmove(buffer, &buffer[a], b);
403 if (poll_fds[0].revents & POLLHUP) {
406 ret = waitpid(pid, &status, WNOHANG);
409 ALOG(LOG_ERROR, "logwrap", "waitpid failed with %s\n", strerror(errno));
418 if (chld_sts != NULL) {
421 if (WIFEXITED(status))
422 rc = WEXITSTATUS(status);
427 // Flush remaining data
430 log_line(&log_info, &buffer[a], b - a);
433 /* All the output has been processed, time to dump the abbreviated output */
435 print_abbr_buf(&log_info);
438 if (WIFEXITED(status)) {
439 if (WEXITSTATUS(status)) {
440 snprintf(tmpbuf, sizeof(tmpbuf),
441 "%s terminated by exit(%d)\n", log_info.btag, WEXITSTATUS(status));
442 do_log_line(&log_info, tmpbuf);
445 if (WIFSIGNALED(status)) {
446 snprintf(tmpbuf, sizeof(tmpbuf),
447 "%s terminated by signal %d\n", log_info.btag, WTERMSIG(status));
448 do_log_line(&log_info, tmpbuf);
449 } else if (WIFSTOPPED(status)) {
450 snprintf(tmpbuf, sizeof(tmpbuf),
451 "%s stopped by signal %d\n", log_info.btag, WSTOPSIG(status));
452 do_log_line(&log_info, tmpbuf);
458 if (log_target & LOG_FILE) {
459 fclose(log_info.fp); /* Also closes underlying fd */
462 free_abbr_buf(&log_info.a_buf);
467 static void child(int argc, char* argv[]) {
468 // create null terminated argv_child array
469 char* argv_child[argc + 1];
470 memcpy(argv_child, argv, argc * sizeof(char *));
471 argv_child[argc] = NULL;
473 if (execvp(argv_child[0], argv_child)) {
474 FATAL_CHILD("executing %s failed: %s\n", argv_child[0],
479 int android_fork_execvp_ext(int argc, char* argv[], int *status, bool ignore_int_quit,
480 int log_target, bool abbreviated, char *file_path) {
484 char *child_devname = NULL;
485 struct sigaction intact;
486 struct sigaction quitact;
491 rc = pthread_mutex_lock(&fd_mutex);
493 ERROR("failed to lock signal_fd mutex\n");
497 /* Use ptty instead of socketpair so that STDOUT is not buffered */
498 parent_ptty = open("/dev/ptmx", O_RDWR);
499 if (parent_ptty < 0) {
500 ERROR("Cannot create parent ptty\n");
505 if (grantpt(parent_ptty) || unlockpt(parent_ptty) ||
506 ((child_devname = (char*)ptsname(parent_ptty)) == 0)) {
507 ERROR("Problem with /dev/ptmx\n");
512 child_ptty = open(child_devname, O_RDWR);
513 if (child_ptty < 0) {
514 ERROR("Cannot open child_ptty\n");
519 sigemptyset(&blockset);
520 sigaddset(&blockset, SIGINT);
521 sigaddset(&blockset, SIGQUIT);
522 pthread_sigmask(SIG_BLOCK, &blockset, &oldset);
527 ERROR("Failed to fork\n");
530 } else if (pid == 0) {
531 pthread_mutex_unlock(&fd_mutex);
532 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
535 // redirect stdout and stderr
543 if (ignore_int_quit) {
544 struct sigaction ignact;
546 memset(&ignact, 0, sizeof(ignact));
547 ignact.sa_handler = SIG_IGN;
548 sigaction(SIGINT, &ignact, &intact);
549 sigaction(SIGQUIT, &ignact, &quitact);
552 rc = parent(argv[0], parent_ptty, pid, status, log_target,
553 abbreviated, file_path);
556 if (ignore_int_quit) {
557 sigaction(SIGINT, &intact, NULL);
558 sigaction(SIGQUIT, &quitact, NULL);
561 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
566 pthread_mutex_unlock(&fd_mutex);