1 /*
2 * Copyright (C) 2009 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 <errno.h>
18 #include <stdio.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include <cutils/properties.h>
24 #include <cutils/sockets.h>
25
26 // This program will trigger the dumpstate service to start a call to
27 // dumpstate, then connect to the dumpstate local client to read the
28 // output. All of the dumpstate output is written to stdout, including
29 // any errors encountered while reading/writing the output.
main(int argc,char * [])30 int main(int argc, char* /*argv*/[]) {
31 fprintf(stderr, "=============================================================================\n");
32 fprintf(stderr, "WARNING: Flat (text file, non-zipped) bugreports are deprecated.\n");
33 fprintf(stderr, "WARNING: Please generate zipped bugreports instead.\n");
34 fprintf(stderr, "WARNING: On the host use: adb bugreport filename.zip\n");
35 fprintf(stderr, "WARNING: On the device use: bugreportz\n");
36 fprintf(stderr, "WARNING: bugreportz will output the filename to use with adb pull.\n");
37 fprintf(stderr, "=============================================================================\n\n\n");
38
39 if (argc != 1) {
40 fprintf(stderr, "usage: bugreport\n");
41 exit(1);
42 }
43
44 // Start the dumpstate service.
45 property_set("ctl.start", "dumpstate");
46
47 // Socket will not be available until service starts.
48 int s = -1;
49 for (int i = 0; i < 20; i++) {
50 s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED,
51 SOCK_STREAM);
52 if (s >= 0)
53 break;
54 // Try again in 1 second.
55 sleep(1);
56 }
57
58 if (s == -1) {
59 printf("Failed to connect to dumpstate service: %s\n", strerror(errno));
60 return 1;
61 }
62
63 // Set a timeout so that if nothing is read in 3 minutes, we'll stop
64 // reading and quit. No timeout in dumpstate is longer than 60 seconds,
65 // so this gives lots of leeway in case of unforeseen time outs.
66 struct timeval tv;
67 tv.tv_sec = 3 * 60;
68 tv.tv_usec = 0;
69 if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
70 printf("WARNING: Cannot set socket timeout: %s\n", strerror(errno));
71 }
72
73 while (1) {
74 char buffer[65536];
75 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)));
76 if (bytes_read == 0) {
77 break;
78 } else if (bytes_read == -1) {
79 // EAGAIN really means time out, so change the errno.
80 if (errno == EAGAIN) {
81 errno = ETIMEDOUT;
82 }
83 printf("\nBugreport read terminated abnormally (%s).\n", strerror(errno));
84 break;
85 }
86
87 ssize_t bytes_to_send = bytes_read;
88 ssize_t bytes_written;
89 do {
90 bytes_written = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
91 buffer + bytes_read - bytes_to_send,
92 bytes_to_send));
93 if (bytes_written == -1) {
94 printf("Failed to write data to stdout: read %zd, trying to send %zd (%s)\n",
95 bytes_read, bytes_to_send, strerror(errno));
96 return 1;
97 }
98 bytes_to_send -= bytes_written;
99 } while (bytes_written != 0 && bytes_to_send > 0);
100 }
101
102 close(s);
103 return 0;
104 }
105