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.1";
30
show_usage()31 static void show_usage() {
32 fprintf(stderr,
33 "usage: bugreportz [-h | -v]\n"
34 " -h: to display this help message\n"
35 " -p: display progress\n"
36 " -v: to display the version\n"
37 " or no arguments to generate a zipped bugreport\n");
38 }
39
show_version()40 static void show_version() {
41 fprintf(stderr, "%s\n", VERSION);
42 }
43
main(int argc,char * argv[])44 int main(int argc, char* argv[]) {
45 bool show_progress = false;
46 if (argc > 1) {
47 /* parse arguments */
48 int c;
49 while ((c = getopt(argc, argv, "hpv")) != -1) {
50 switch (c) {
51 case 'h':
52 show_usage();
53 return EXIT_SUCCESS;
54 case 'p':
55 show_progress = true;
56 break;
57 case 'v':
58 show_version();
59 return EXIT_SUCCESS;
60 default:
61 show_usage();
62 return EXIT_FAILURE;
63 }
64 }
65 }
66
67 // TODO: code below was copy-and-pasted from bugreport.cpp (except by the
68 // timeout value);
69 // should be reused instead.
70
71 // Start the dumpstatez service.
72 property_set("ctl.start", "dumpstatez");
73
74 // Socket will not be available until service starts.
75 int s;
76 for (int i = 0; i < 20; i++) {
77 s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
78 if (s >= 0) break;
79 // Try again in 1 second.
80 sleep(1);
81 }
82
83 if (s == -1) {
84 printf("FAIL:Failed to connect to dumpstatez service: %s\n", strerror(errno));
85 return EXIT_SUCCESS;
86 }
87
88 // Set a timeout so that if nothing is read in 10 minutes, we'll stop
89 // reading and quit. No timeout in dumpstate is longer than 60 seconds,
90 // so this gives lots of leeway in case of unforeseen time outs.
91 struct timeval tv;
92 tv.tv_sec = 10 * 60;
93 tv.tv_usec = 0;
94 if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
95 fprintf(stderr, "WARNING: Cannot set socket timeout: %s\n", strerror(errno));
96 }
97
98 bugreportz(s, show_progress);
99 }
100