blob: 790556ce5a3115617801e04c92d79a243f50e178 [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
Emil Bengtssonb8b08a32023-02-09 13:46:40 +010078 // Wait a little while for dumpstatez to stop if it is running
79 bool dumpstate_running = false;
80 for (int i = 0; i < 20; i++) {
81 char buf[PROPERTY_VALUE_MAX];
82 property_get("init.svc.dumpstatez", buf, "");
83 dumpstate_running = strcmp(buf, "running") == 0;
84
85 if (!dumpstate_running) break;
86
87 sleep(1);
88 }
89
90 if (dumpstate_running) {
91 fprintf(stderr, "FAIL:dumpstatez service is already running\n");
92 return EXIT_FAILURE;
93 }
94
Felipe Leme59f5af02016-07-21 20:10:57 -070095 // TODO: code below was copy-and-pasted from bugreport.cpp (except by the
96 // timeout value);
97 // should be reused instead.
98
99 // Start the dumpstatez service.
Dieter Hsu031c9572020-09-29 15:23:33 +0800100 if (stream_data) {
101 property_set("ctl.start", "dumpstate");
102 } else {
103 property_set("ctl.start", "dumpstatez");
104 }
Felipe Leme59f5af02016-07-21 20:10:57 -0700105
106 // Socket will not be available until service starts.
Stephen Hines6896f062019-05-09 13:03:40 -0700107 int s = -1;
Felipe Leme59f5af02016-07-21 20:10:57 -0700108 for (int i = 0; i < 20; i++) {
109 s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
110 if (s >= 0) break;
111 // Try again in 1 second.
112 sleep(1);
113 }
114
115 if (s == -1) {
116 printf("FAIL:Failed to connect to dumpstatez service: %s\n", strerror(errno));
Felipe Leme64e1f5b2018-08-02 16:16:23 -0700117 return EXIT_FAILURE;
Felipe Leme59f5af02016-07-21 20:10:57 -0700118 }
119
120 // Set a timeout so that if nothing is read in 10 minutes, we'll stop
121 // reading and quit. No timeout in dumpstate is longer than 60 seconds,
122 // so this gives lots of leeway in case of unforeseen time outs.
123 struct timeval tv;
124 tv.tv_sec = 10 * 60;
125 tv.tv_usec = 0;
126 if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
Felipe Leme64e1f5b2018-08-02 16:16:23 -0700127 fprintf(stderr,
128 "WARNING: Cannot set socket timeout, bugreportz might hang indefinitely: %s\n",
129 strerror(errno));
Felipe Leme59f5af02016-07-21 20:10:57 -0700130 }
131
Dieter Hsu031c9572020-09-29 15:23:33 +0800132 int ret;
133 if (stream_data) {
134 ret = bugreportz_stream(s);
135 } else {
136 ret = bugreportz(s, show_progress);
137 }
Felipe Leme64e1f5b2018-08-02 16:16:23 -0700138
139 if (close(s) == -1) {
140 fprintf(stderr, "WARNING: error closing socket: %s\n", strerror(errno));
141 ret = EXIT_FAILURE;
142 }
143 return ret;
Felipe Leme59f5af02016-07-21 20:10:57 -0700144}