blob: 1d48e08f3e12c4f810ebddeac5bb862b0ceb3ae5 [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
Felipe Leme02b7e002016-07-22 12:03:20 -070029static constexpr char VERSION[] = "1.1";
Felipe Leme59f5af02016-07-21 20:10:57 -070030
31static void show_usage() {
32 fprintf(stderr,
Elliott Hughes0550c722020-06-10 09:51:57 -070033 "usage: bugreportz [-hpv]\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"
Felipe Leme59f5af02016-07-21 20:10:57 -070036 " -v: to display the version\n"
37 " or no arguments to generate a zipped bugreport\n");
38}
39
40static void show_version() {
41 fprintf(stderr, "%s\n", VERSION);
42}
43
44int main(int argc, char* argv[]) {
Felipe Leme02b7e002016-07-22 12:03:20 -070045 bool show_progress = false;
Felipe Leme59f5af02016-07-21 20:10:57 -070046 if (argc > 1) {
47 /* parse arguments */
48 int c;
Felipe Leme02b7e002016-07-22 12:03:20 -070049 while ((c = getopt(argc, argv, "hpv")) != -1) {
Felipe Leme59f5af02016-07-21 20:10:57 -070050 switch (c) {
51 case 'h':
52 show_usage();
53 return EXIT_SUCCESS;
Felipe Leme02b7e002016-07-22 12:03:20 -070054 case 'p':
55 show_progress = true;
56 break;
Felipe Leme59f5af02016-07-21 20:10:57 -070057 case 'v':
58 show_version();
59 return EXIT_SUCCESS;
60 default:
61 show_usage();
62 return EXIT_FAILURE;
63 }
64 }
Felipe Leme59f5af02016-07-21 20:10:57 -070065 }
66
Elliott Hughes0550c722020-06-10 09:51:57 -070067 // We don't support any non-option arguments.
68 if (optind != argc) {
69 show_usage();
70 return EXIT_FAILURE;
71 }
72
Felipe Leme59f5af02016-07-21 20:10:57 -070073 // TODO: code below was copy-and-pasted from bugreport.cpp (except by the
74 // timeout value);
75 // should be reused instead.
76
77 // Start the dumpstatez service.
78 property_set("ctl.start", "dumpstatez");
79
80 // Socket will not be available until service starts.
Stephen Hines6896f062019-05-09 13:03:40 -070081 int s = -1;
Felipe Leme59f5af02016-07-21 20:10:57 -070082 for (int i = 0; i < 20; i++) {
83 s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
84 if (s >= 0) break;
85 // Try again in 1 second.
86 sleep(1);
87 }
88
89 if (s == -1) {
90 printf("FAIL:Failed to connect to dumpstatez service: %s\n", strerror(errno));
Felipe Leme64e1f5b2018-08-02 16:16:23 -070091 return EXIT_FAILURE;
Felipe Leme59f5af02016-07-21 20:10:57 -070092 }
93
94 // Set a timeout so that if nothing is read in 10 minutes, we'll stop
95 // reading and quit. No timeout in dumpstate is longer than 60 seconds,
96 // so this gives lots of leeway in case of unforeseen time outs.
97 struct timeval tv;
98 tv.tv_sec = 10 * 60;
99 tv.tv_usec = 0;
100 if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
Felipe Leme64e1f5b2018-08-02 16:16:23 -0700101 fprintf(stderr,
102 "WARNING: Cannot set socket timeout, bugreportz might hang indefinitely: %s\n",
103 strerror(errno));
Felipe Leme59f5af02016-07-21 20:10:57 -0700104 }
105
Felipe Leme64e1f5b2018-08-02 16:16:23 -0700106 int ret = bugreportz(s, show_progress);
107
108 if (close(s) == -1) {
109 fprintf(stderr, "WARNING: error closing socket: %s\n", strerror(errno));
110 ret = EXIT_FAILURE;
111 }
112 return ret;
Felipe Leme59f5af02016-07-21 20:10:57 -0700113}