| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 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 |  | 
| Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 29 | static constexpr char VERSION[] = "1.2"; | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | static void show_usage() { | 
|  | 32 | fprintf(stderr, | 
| Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 33 | "usage: bugreportz [-hpsv]\n" | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 34 | "  -h: to display this help message\n" | 
| Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 35 | "  -p: display progress\n" | 
| Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 36 | "  -s: stream content to standard output\n" | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 37 | "  -v: to display the version\n" | 
|  | 38 | "  or no arguments to generate a zipped bugreport\n"); | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | static void show_version() { | 
|  | 42 | fprintf(stderr, "%s\n", VERSION); | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | int main(int argc, char* argv[]) { | 
| Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 46 | bool show_progress = false; | 
| Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 47 | bool stream_data = false; | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 48 | if (argc > 1) { | 
|  | 49 | /* parse arguments */ | 
|  | 50 | int c; | 
| Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 51 | while ((c = getopt(argc, argv, "hpsv")) != -1) { | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 52 | switch (c) { | 
|  | 53 | case 'h': | 
|  | 54 | show_usage(); | 
|  | 55 | return EXIT_SUCCESS; | 
| Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 56 | case 'p': | 
|  | 57 | show_progress = true; | 
|  | 58 | break; | 
| Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 59 | case 's': | 
|  | 60 | stream_data = true; | 
|  | 61 | break; | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 62 | case 'v': | 
|  | 63 | show_version(); | 
|  | 64 | return EXIT_SUCCESS; | 
|  | 65 | default: | 
|  | 66 | show_usage(); | 
|  | 67 | return EXIT_FAILURE; | 
|  | 68 | } | 
|  | 69 | } | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
| Elliott Hughes | 0550c72 | 2020-06-10 09:51:57 -0700 | [diff] [blame] | 72 | // We don't support any non-option arguments. | 
|  | 73 | if (optind != argc) { | 
|  | 74 | show_usage(); | 
|  | 75 | return EXIT_FAILURE; | 
|  | 76 | } | 
|  | 77 |  | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 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. | 
| Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 83 | if (stream_data) { | 
|  | 84 | property_set("ctl.start", "dumpstate"); | 
|  | 85 | } else { | 
|  | 86 | property_set("ctl.start", "dumpstatez"); | 
|  | 87 | } | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 88 |  | 
|  | 89 | // Socket will not be available until service starts. | 
| Stephen Hines | 6896f06 | 2019-05-09 13:03:40 -0700 | [diff] [blame] | 90 | int s = -1; | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 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)); | 
| Felipe Leme | 64e1f5b | 2018-08-02 16:16:23 -0700 | [diff] [blame] | 100 | return EXIT_FAILURE; | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 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) { | 
| Felipe Leme | 64e1f5b | 2018-08-02 16:16:23 -0700 | [diff] [blame] | 110 | fprintf(stderr, | 
|  | 111 | "WARNING: Cannot set socket timeout, bugreportz might hang indefinitely: %s\n", | 
|  | 112 | strerror(errno)); | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 115 | int ret; | 
|  | 116 | if (stream_data) { | 
|  | 117 | ret = bugreportz_stream(s); | 
|  | 118 | } else { | 
|  | 119 | ret = bugreportz(s, show_progress); | 
|  | 120 | } | 
| Felipe Leme | 64e1f5b | 2018-08-02 16:16:23 -0700 | [diff] [blame] | 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; | 
| Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 127 | } |