bugreport.cpp 2.9 KB

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