1 /*
2 * Copyright (C) 2016 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 <getopt.h>
19 #include <stdio.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include <cutils/properties.h>
25 #include <cutils/sockets.h>
26
27 #include "bugreportz.h"
28
29 static constexpr char VERSION[] = "1.2";
30
show_usage()31 static void show_usage() {
32 fprintf(stderr,
33 "usage: bugreportz [-hpsv]\n"
34 " -h: to display this help message\n"
35 " -p: display progress\n"
36 " -s: stream content to standard output\n"
37 " -v: to display the version\n"
38 " or no arguments to generate a zipped bugreport\n");
39 }
40
show_version()41 static void show_version() {
42 fprintf(stderr, "%s\n", VERSION);
43 }
44
main(int argc,char * argv[])45 int main(int argc, char* argv[]) {
46 bool show_progress = false;
47 bool stream_data = false;
48 if (argc > 1) {
49 /* parse arguments */
50 int c;
51 while ((c = getopt(argc, argv, "hpsv")) != -1) {
52 switch (c) {
53 case 'h':
54 show_usage();
55 return EXIT_SUCCESS;
56 case 'p':
57 show_progress = true;
58 break;
59 case 's':
60 stream_data = true;
61 break;
62 case 'v':
63 show_version();
64 return EXIT_SUCCESS;
65 default:
66 show_usage();
67 return EXIT_FAILURE;
68 }
69 }
70 }
71
72 // We don't support any non-option arguments.
73 if (optind != argc) {
74 show_usage();
75 return EXIT_FAILURE;
76 }
77
78 // TODO: code below was copy-and-pasted from bugreport.cpp (except by the
79 // timeout value);
80 // should be reused instead.
81
82 // Start the dumpstatez service.
83 if (stream_data) {
84 property_set("ctl.start", "dumpstate");
85 } else {
86 property_set("ctl.start", "dumpstatez");
87 }
88
89 // Socket will not be available until service starts.
90 int s = -1;
91 for (int i = 0; i < 20; i++) {
92 s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
93 if (s >= 0) break;
94 // Try again in 1 second.
95 sleep(1);
96 }
97
98 if (s == -1) {
99 printf("FAIL:Failed to connect to dumpstatez service: %s\n", strerror(errno));
100 return EXIT_FAILURE;
101 }
102
103 // Set a timeout so that if nothing is read in 10 minutes, we'll stop
104 // reading and quit. No timeout in dumpstate is longer than 60 seconds,
105 // so this gives lots of leeway in case of unforeseen time outs.
106 struct timeval tv;
107 tv.tv_sec = 10 * 60;
108 tv.tv_usec = 0;
109 if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
110 fprintf(stderr,
111 "WARNING: Cannot set socket timeout, bugreportz might hang indefinitely: %s\n",
112 strerror(errno));
113 }
114
115 int ret;
116 if (stream_data) {
117 ret = bugreportz_stream(s);
118 } else {
119 ret = bugreportz(s, show_progress);
120 }
121
122 if (close(s) == -1) {
123 fprintf(stderr, "WARNING: error closing socket: %s\n", strerror(errno));
124 ret = EXIT_FAILURE;
125 }
126 return ret;
127 }
128