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 | |
| 29 | static constexpr char VERSION[] = "1.0"; |
| 30 | |
| 31 | static void show_usage() { |
| 32 | fprintf(stderr, |
| 33 | "usage: bugreportz [-h | -v]\n" |
| 34 | " -h: to display this help message\n" |
| 35 | " -v: to display the version\n" |
| 36 | " or no arguments to generate a zipped bugreport\n"); |
| 37 | } |
| 38 | |
| 39 | static void show_version() { |
| 40 | fprintf(stderr, "%s\n", VERSION); |
| 41 | } |
| 42 | |
| 43 | int main(int argc, char* argv[]) { |
| 44 | if (argc > 1) { |
| 45 | /* parse arguments */ |
| 46 | int c; |
| 47 | while ((c = getopt(argc, argv, "vh")) != -1) { |
| 48 | switch (c) { |
| 49 | case 'h': |
| 50 | show_usage(); |
| 51 | return EXIT_SUCCESS; |
| 52 | case 'v': |
| 53 | show_version(); |
| 54 | return EXIT_SUCCESS; |
| 55 | default: |
| 56 | show_usage(); |
| 57 | return EXIT_FAILURE; |
| 58 | } |
| 59 | } |
| 60 | // passed an argument not starting with - |
| 61 | if (optind > 1 || argv[optind] != nullptr) { |
| 62 | show_usage(); |
| 63 | return EXIT_FAILURE; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // TODO: code below was copy-and-pasted from bugreport.cpp (except by the |
| 68 | // timeout value); |
| 69 | // should be reused instead. |
| 70 | |
| 71 | // Start the dumpstatez service. |
| 72 | property_set("ctl.start", "dumpstatez"); |
| 73 | |
| 74 | // Socket will not be available until service starts. |
| 75 | int s; |
| 76 | for (int i = 0; i < 20; i++) { |
| 77 | s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); |
| 78 | if (s >= 0) break; |
| 79 | // Try again in 1 second. |
| 80 | sleep(1); |
| 81 | } |
| 82 | |
| 83 | if (s == -1) { |
| 84 | printf("FAIL:Failed to connect to dumpstatez service: %s\n", strerror(errno)); |
| 85 | return EXIT_SUCCESS; |
| 86 | } |
| 87 | |
| 88 | // Set a timeout so that if nothing is read in 10 minutes, we'll stop |
| 89 | // reading and quit. No timeout in dumpstate is longer than 60 seconds, |
| 90 | // so this gives lots of leeway in case of unforeseen time outs. |
| 91 | struct timeval tv; |
| 92 | tv.tv_sec = 10 * 60; |
| 93 | tv.tv_usec = 0; |
| 94 | if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { |
| 95 | fprintf(stderr, "WARNING: Cannot set socket timeout: %s\n", strerror(errno)); |
| 96 | } |
| 97 | |
| 98 | bugreportz(s); |
| 99 | } |