19b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris/*
29b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris * Copyright (C) 2009 The Android Open Source Project
39b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris *
49b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris * Licensed under the Apache License, Version 2.0 (the "License");
59b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris * you may not use this file except in compliance with the License.
69b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris * You may obtain a copy of the License at
79b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris *
89b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris *      http://www.apache.org/licenses/LICENSE-2.0
99b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris *
109b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris * Unless required by applicable law or agreed to in writing, software
119b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris * distributed under the License is distributed on an "AS IS" BASIS,
129b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris * See the License for the specific language governing permissions and
149b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris * limitations under the License.
159b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris */
169b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris
173b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme#include <errno.h>
189b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris#include <stdio.h>
193b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme#include <sys/socket.h>
203b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme#include <sys/types.h>
213b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme#include <unistd.h>
229b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris
233b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme#include <cutils/properties.h>
243b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme#include <cutils/sockets.h>
253b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme
263b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme// This program will trigger the dumpstate service to start a call to
273b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme// dumpstate, then connect to the dumpstate local client to read the
283b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme// output. All of the dumpstate output is written to stdout, including
293b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme// any errors encountered while reading/writing the output.
309b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferrisint main() {
313b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme
323b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  fprintf(stderr, "=============================================================================\n");
333b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  fprintf(stderr, "WARNING: flat bugreports are deprecated, use adb bugreport <zip_file> instead\n");
343b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  fprintf(stderr, "=============================================================================\n\n\n");
353b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme
363b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  // Start the dumpstate service.
373b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  property_set("ctl.start", "dumpstate");
383b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme
393b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  // Socket will not be available until service starts.
403b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  int s;
413b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  for (int i = 0; i < 20; i++) {
423b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED,
433b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme                            SOCK_STREAM);
443b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    if (s >= 0)
453b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      break;
463b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    // Try again in 1 second.
473b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    sleep(1);
483b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  }
493b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme
503b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  if (s == -1) {
513b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    printf("Failed to connect to dumpstate service: %s\n", strerror(errno));
523b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    return 1;
533b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  }
543b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme
553b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  // Set a timeout so that if nothing is read in 3 minutes, we'll stop
563b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  // reading and quit. No timeout in dumpstate is longer than 60 seconds,
573b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  // so this gives lots of leeway in case of unforeseen time outs.
583b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  struct timeval tv;
593b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  tv.tv_sec = 3 * 60;
603b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  tv.tv_usec = 0;
613b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
623b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    printf("WARNING: Cannot set socket timeout: %s\n", strerror(errno));
633b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  }
643b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme
653b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  while (1) {
663b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    char buffer[65536];
673b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)));
683b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    if (bytes_read == 0) {
693b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      break;
703b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    } else if (bytes_read == -1) {
713b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      // EAGAIN really means time out, so change the errno.
723b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      if (errno == EAGAIN) {
733b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme        errno = ETIMEDOUT;
743b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      }
753b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      printf("\nBugreport read terminated abnormally (%s).\n", strerror(errno));
763b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      break;
773b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    }
783b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme
793b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    ssize_t bytes_to_send = bytes_read;
803b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    ssize_t bytes_written;
813b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    do {
823b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      bytes_written = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
833b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme                                               buffer + bytes_read - bytes_to_send,
843b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme                                               bytes_to_send));
853b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      if (bytes_written == -1) {
863b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme        printf("Failed to write data to stdout: read %zd, trying to send %zd (%s)\n",
873b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme               bytes_read, bytes_to_send, strerror(errno));
883b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme        return 1;
893b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      }
903b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme      bytes_to_send -= bytes_written;
913b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme    } while (bytes_written != 0 && bytes_to_send > 0);
923b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  }
933b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme
943b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  close(s);
953b650731d31f5ebc131068ae6cde811cb118672eFelipe Leme  return 0;
969b73bf07d73dbab5b792632e1e233edbad77f5fdChristopher Ferris}
97