blob: 35f061e70fc0d044544f2ad307d9e8498ebdca0c [file] [log] [blame]
Christopher Ferris20303f82014-01-10 16:33:16 -08001/*
2 * Copyright 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 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016
Jeff Brown053b8652012-06-06 16:25:03 -070017#include <stddef.h>
Jeff Brown053b8652012-06-06 16:25:03 -070018#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <string.h>
Jeff Brown053b8652012-06-06 16:25:03 -070020#include <errno.h>
21#include <unistd.h>
22#include <signal.h>
Colin Cross9227bd32013-07-23 16:59:20 -070023#include <log/logd.h>
Jeff Brown13e715b2011-10-21 12:14:56 -070024#include <sys/ptrace.h>
Jeff Brown053b8652012-06-06 16:25:03 -070025#include <sys/wait.h>
Christopher Tateded2e5a2013-03-19 13:12:23 -070026#include <arpa/inet.h>
27#include <assert.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
29#include "utility.h"
30
Christopher Ferris20303f82014-01-10 16:33:16 -080031const int sleep_time_usec = 50000; // 0.05 seconds
32const int max_total_sleep_usec = 10000000; // 10 seconds
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Christopher Tateded2e5a2013-03-19 13:12:23 -070034static int write_to_am(int fd, const char* buf, int len) {
Christopher Ferris20303f82014-01-10 16:33:16 -080035 int to_write = len;
36 while (to_write > 0) {
37 int written = TEMP_FAILURE_RETRY( write(fd, buf + len - to_write, to_write) );
38 if (written < 0) {
39 // hard failure
40 LOG("AM write failure (%d / %s)\n", errno, strerror(errno));
41 return -1;
Christopher Tateded2e5a2013-03-19 13:12:23 -070042 }
Christopher Ferris20303f82014-01-10 16:33:16 -080043 to_write -= written;
44 }
45 return len;
Christopher Tateded2e5a2013-03-19 13:12:23 -070046}
47
Christopher Ferris20303f82014-01-10 16:33:16 -080048void _LOG(log_t* log, int scopeFlags, const char* fmt, ...) {
49 char buf[512];
50 bool want_tfd_write;
51 bool want_log_write;
52 bool want_amfd_write;
53 int len = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Christopher Ferris20303f82014-01-10 16:33:16 -080055 va_list ap;
56 va_start(ap, fmt);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Christopher Ferris20303f82014-01-10 16:33:16 -080058 // where is the information going to go?
59 want_tfd_write = log && log->tfd >= 0;
60 want_log_write = IS_AT_FAULT(scopeFlags) && (!log || !log->quiet);
61 want_amfd_write = IS_AT_FAULT(scopeFlags) && !IS_SENSITIVE(scopeFlags) && log && log->amfd >= 0;
Christopher Tateded2e5a2013-03-19 13:12:23 -070062
Christopher Ferris20303f82014-01-10 16:33:16 -080063 // if we're going to need the literal string, generate it once here
64 if (want_tfd_write || want_amfd_write) {
65 vsnprintf(buf, sizeof(buf), fmt, ap);
66 len = strlen(buf);
67 }
68
69 if (want_tfd_write) {
70 write(log->tfd, buf, len);
71 }
72
73 if (want_log_write) {
74 // whatever goes to logcat also goes to the Activity Manager
75 __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
76 if (want_amfd_write && len > 0) {
77 int written = write_to_am(log->amfd, buf, len);
78 if (written <= 0) {
79 // timeout or other failure on write; stop informing the activity manager
80 log->amfd = -1;
81 }
Christopher Tateded2e5a2013-03-19 13:12:23 -070082 }
Christopher Ferris20303f82014-01-10 16:33:16 -080083 }
84 va_end(ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085}
86
Jeff Brown053b8652012-06-06 16:25:03 -070087int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -080088 for (;;) {
89 int status;
90 pid_t n = waitpid(tid, &status, __WALL | WNOHANG);
91 if (n < 0) {
92 if (errno == EAGAIN)
93 continue;
94 LOG("waitpid failed: %s\n", strerror(errno));
95 return -1;
96 } else if (n > 0) {
97 XLOG("waitpid: n=%d status=%08x\n", n, status);
98 if (WIFSTOPPED(status)) {
99 return WSTOPSIG(status);
100 } else {
101 LOG("unexpected waitpid response: n=%d, status=%08x\n", n, status);
102 return -1;
103 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700104 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800105
106 if (*total_sleep_time_usec > max_total_sleep_usec) {
107 LOG("timed out waiting for tid=%d to die\n", tid);
108 return -1;
109 }
110
111 // not ready yet
112 XLOG("not ready yet\n");
113 usleep(sleep_time_usec);
114 *total_sleep_time_usec += sleep_time_usec;
115 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700116}
117
Jeff Brown053b8652012-06-06 16:25:03 -0700118void wait_for_stop(pid_t tid, int* total_sleep_time_usec) {
Christopher Ferris20303f82014-01-10 16:33:16 -0800119 siginfo_t si;
120 while (TEMP_FAILURE_RETRY(ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) < 0 && errno == ESRCH) {
121 if (*total_sleep_time_usec > max_total_sleep_usec) {
122 LOG("timed out waiting for tid=%d to stop\n", tid);
123 break;
Jeff Brown13e715b2011-10-21 12:14:56 -0700124 }
Christopher Ferris20303f82014-01-10 16:33:16 -0800125
126 usleep(sleep_time_usec);
127 *total_sleep_time_usec += sleep_time_usec;
128 }
Jeff Brown13e715b2011-10-21 12:14:56 -0700129}