blob: cd2652c717c1614f8791733d688fc3b1c48ee92e [file] [log] [blame]
Felipe Leme59f5af02016-07-21 20:10:57 -07001/*
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
Dieter Hsu031c9572020-09-29 15:23:33 +080029static constexpr char VERSION[] = "1.2";
Felipe Leme59f5af02016-07-21 20:10:57 -070030
31static void show_usage() {
32 fprintf(stderr,
Dieter Hsu031c9572020-09-29 15:23:33 +080033 "usage: bugreportz [-hpsv]\n"
Felipe Leme59f5af02016-07-21 20:10:57 -070034 " -h: to display this help message\n"
Felipe Leme02b7e002016-07-22 12:03:20 -070035 " -p: display progress\n"
Dieter Hsu031c9572020-09-29 15:23:33 +080036 " -s: stream content to standard output\n"
Felipe Leme59f5af02016-07-21 20:10:57 -070037 " -v: to display the version\n"
38 " or no arguments to generate a zipped bugreport\n");
39}
40
41static void show_version() {
42 fprintf(stderr, "%s\n", VERSION);
43}
44
45int main(int argc, char* argv[]) {
Felipe Leme02b7e002016-07-22 12:03:20 -070046 bool show_progress = false;
Dieter Hsu031c9572020-09-29 15:23:33 +080047 bool stream_data = false;
Felipe Leme59f5af02016-07-21 20:10:57 -070048 if (argc > 1) {
49 /* parse arguments */
50 int c;
Dieter Hsu031c9572020-09-29 15:23:33 +080051 while ((c = getopt(argc, argv, "hpsv")) != -1) {
Felipe Leme59f5af02016-07-21 20:10:57 -070052 switch (c) {
53 case 'h':
54 show_usage();
55 return EXIT_SUCCESS;
Felipe Leme02b7e002016-07-22 12:03:20 -070056 case 'p':
57 show_progress = true;
58 break;
Dieter Hsu031c9572020-09-29 15:23:33 +080059 case 's':
60 stream_data = true;
61 break;
Felipe Leme59f5af02016-07-21 20:10:57 -070062 case 'v':
63 show_version();
64 return EXIT_SUCCESS;
65 default:
66 show_usage();
67 return EXIT_FAILURE;
68 }
69 }
Felipe Leme59f5af02016-07-21 20:10:57 -070070 }
71
Elliott Hughes0550c722020-06-10 09:51:57 -070072 // We don't support any non-option arguments.
73 if (optind != argc) {
74 show_usage();
75 return EXIT_FAILURE;
76 }
77
Felipe Leme59f5af02016-07-21 20:10:57 -070078 // 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.
Dieter Hsu031c9572020-09-29 15:23:33 +080083 if (stream_data) {
84 property_set("ctl.start", "dumpstate");
85 } else {
86 property_set("ctl.start", "dumpstatez");
87 }
Felipe Leme59f5af02016-07-21 20:10:57 -070088
89 // Socket will not be available until service starts.
Stephen Hines6896f062019-05-09 13:03:40 -070090 int s = -1;
Felipe Leme59f5af02016-07-21 20:10:57 -070091 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));
Felipe Leme64e1f5b2018-08-02 16:16:23 -0700100 return EXIT_FAILURE;
Felipe Leme59f5af02016-07-21 20:10:57 -0700101 }
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) {
Felipe Leme64e1f5b2018-08-02 16:16:23 -0700110 fprintf(stderr,
111 "WARNING: Cannot set socket timeout, bugreportz might hang indefinitely: %s\n",
112 strerror(errno));
Felipe Leme59f5af02016-07-21 20:10:57 -0700113 }
114
Dieter Hsu031c9572020-09-29 15:23:33 +0800115 int ret;
116 if (stream_data) {
117 ret = bugreportz_stream(s);
118 } else {
119 ret = bugreportz(s, show_progress);
120 }
Felipe Leme64e1f5b2018-08-02 16:16:23 -0700121
122 if (close(s) == -1) {
123 fprintf(stderr, "WARNING: error closing socket: %s\n", strerror(errno));
124 ret = EXIT_FAILURE;
125 }
126 return ret;
Felipe Leme59f5af02016-07-21 20:10:57 -0700127}