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 | |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 29 | static constexpr char VERSION[] = "1.1"; |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 30 | |
| 31 | static void show_usage() { |
| 32 | fprintf(stderr, |
Elliott Hughes | 0550c72 | 2020-06-10 09:51:57 -0700 | [diff] [blame] | 33 | "usage: bugreportz [-hpv]\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" |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 36 | " -v: to display the version\n" |
| 37 | " or no arguments to generate a zipped bugreport\n"); |
| 38 | } |
| 39 | |
| 40 | static void show_version() { |
| 41 | fprintf(stderr, "%s\n", VERSION); |
| 42 | } |
| 43 | |
| 44 | int main(int argc, char* argv[]) { |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 45 | bool show_progress = false; |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 46 | if (argc > 1) { |
| 47 | /* parse arguments */ |
| 48 | int c; |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 49 | while ((c = getopt(argc, argv, "hpv")) != -1) { |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 50 | switch (c) { |
| 51 | case 'h': |
| 52 | show_usage(); |
| 53 | return EXIT_SUCCESS; |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 54 | case 'p': |
| 55 | show_progress = true; |
| 56 | break; |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 57 | case 'v': |
| 58 | show_version(); |
| 59 | return EXIT_SUCCESS; |
| 60 | default: |
| 61 | show_usage(); |
| 62 | return EXIT_FAILURE; |
| 63 | } |
| 64 | } |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Elliott Hughes | 0550c72 | 2020-06-10 09:51:57 -0700 | [diff] [blame] | 67 | // We don't support any non-option arguments. |
| 68 | if (optind != argc) { |
| 69 | show_usage(); |
| 70 | return EXIT_FAILURE; |
| 71 | } |
| 72 | |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 73 | // 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 Hines | 6896f06 | 2019-05-09 13:03:40 -0700 | [diff] [blame] | 81 | int s = -1; |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 82 | 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 Leme | 64e1f5b | 2018-08-02 16:16:23 -0700 | [diff] [blame] | 91 | return EXIT_FAILURE; |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 92 | } |
| 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 Leme | 64e1f5b | 2018-08-02 16:16:23 -0700 | [diff] [blame] | 101 | fprintf(stderr, |
| 102 | "WARNING: Cannot set socket timeout, bugreportz might hang indefinitely: %s\n", |
| 103 | strerror(errno)); |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Felipe Leme | 64e1f5b | 2018-08-02 16:16:23 -0700 | [diff] [blame] | 106 | 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 Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 113 | } |