Home | History | Annotate | Download | only in bugreport
      1 /*
      2  * Copyright (C) 2009 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 <stdio.h>
     19 #include <sys/socket.h>
     20 #include <sys/types.h>
     21 #include <unistd.h>
     22 
     23 #include <cutils/properties.h>
     24 #include <cutils/sockets.h>
     25 
     26 // This program will trigger the dumpstate service to start a call to
     27 // dumpstate, then connect to the dumpstate local client to read the
     28 // output. All of the dumpstate output is written to stdout, including
     29 // any errors encountered while reading/writing the output.
     30 int main() {
     31 
     32   fprintf(stderr, "=============================================================================\n");
     33   fprintf(stderr, "WARNING: flat bugreports are deprecated, use adb bugreport <zip_file> instead\n");
     34   fprintf(stderr, "=============================================================================\n\n\n");
     35 
     36   // Start the dumpstate service.
     37   property_set("ctl.start", "dumpstate");
     38 
     39   // Socket will not be available until service starts.
     40   int s;
     41   for (int i = 0; i < 20; i++) {
     42     s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED,
     43                             SOCK_STREAM);
     44     if (s >= 0)
     45       break;
     46     // Try again in 1 second.
     47     sleep(1);
     48   }
     49 
     50   if (s == -1) {
     51     printf("Failed to connect to dumpstate service: %s\n", strerror(errno));
     52     return 1;
     53   }
     54 
     55   // Set a timeout so that if nothing is read in 3 minutes, we'll stop
     56   // reading and quit. No timeout in dumpstate is longer than 60 seconds,
     57   // so this gives lots of leeway in case of unforeseen time outs.
     58   struct timeval tv;
     59   tv.tv_sec = 3 * 60;
     60   tv.tv_usec = 0;
     61   if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
     62     printf("WARNING: Cannot set socket timeout: %s\n", strerror(errno));
     63   }
     64 
     65   while (1) {
     66     char buffer[65536];
     67     ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)));
     68     if (bytes_read == 0) {
     69       break;
     70     } else if (bytes_read == -1) {
     71       // EAGAIN really means time out, so change the errno.
     72       if (errno == EAGAIN) {
     73         errno = ETIMEDOUT;
     74       }
     75       printf("\nBugreport read terminated abnormally (%s).\n", strerror(errno));
     76       break;
     77     }
     78 
     79     ssize_t bytes_to_send = bytes_read;
     80     ssize_t bytes_written;
     81     do {
     82       bytes_written = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
     83                                                buffer + bytes_read - bytes_to_send,
     84                                                bytes_to_send));
     85       if (bytes_written == -1) {
     86         printf("Failed to write data to stdout: read %zd, trying to send %zd (%s)\n",
     87                bytes_read, bytes_to_send, strerror(errno));
     88         return 1;
     89       }
     90       bytes_to_send -= bytes_written;
     91     } while (bytes_written != 0 && bytes_to_send > 0);
     92   }
     93 
     94   close(s);
     95   return 0;
     96 }
     97