Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -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 <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 | // TODO: code below was copy-and-pasted from bugreport.cpp (except by the timeout value); |
| 27 | // should be reused instead. |
| 28 | int main() { |
| 29 | |
| 30 | // Start the dumpstatez service. |
| 31 | property_set("ctl.start", "dumpstatez"); |
| 32 | |
| 33 | // Socket will not be available until service starts. |
| 34 | int s; |
| 35 | for (int i = 0; i < 20; i++) { |
| 36 | s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); |
| 37 | if (s >= 0) |
| 38 | break; |
| 39 | // Try again in 1 second. |
| 40 | sleep(1); |
| 41 | } |
| 42 | |
| 43 | if (s == -1) { |
| 44 | printf("Failed to connect to dumpstatez service: %s\n", strerror(errno)); |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | // Set a timeout so that if nothing is read in 10 minutes, we'll stop |
| 49 | // reading and quit. No timeout in dumpstate is longer than 60 seconds, |
| 50 | // so this gives lots of leeway in case of unforeseen time outs. |
| 51 | struct timeval tv; |
| 52 | tv.tv_sec = 10 * 60; |
| 53 | tv.tv_usec = 0; |
| 54 | if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { |
| 55 | printf("WARNING: Cannot set socket timeout: %s\n", strerror(errno)); |
| 56 | } |
| 57 | |
| 58 | while (1) { |
| 59 | char buffer[65536]; |
| 60 | ssize_t bytes_read = TEMP_FAILURE_RETRY( |
| 61 | read(s, buffer, sizeof(buffer))); |
| 62 | if (bytes_read == 0) { |
| 63 | break; |
| 64 | } else if (bytes_read == -1) { |
| 65 | // EAGAIN really means time out, so change the errno. |
| 66 | if (errno == EAGAIN) { |
| 67 | errno = ETIMEDOUT; |
| 68 | } |
| 69 | printf("\nBugreport read terminated abnormally (%s).\n", |
| 70 | strerror(errno)); |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | ssize_t bytes_to_send = bytes_read; |
| 75 | ssize_t bytes_written; |
| 76 | do { |
| 77 | bytes_written = TEMP_FAILURE_RETRY( |
| 78 | write(STDOUT_FILENO, buffer + bytes_read - bytes_to_send, |
| 79 | bytes_to_send)); |
| 80 | if (bytes_written == -1) { |
| 81 | printf( |
| 82 | "Failed to write data to stdout: read %zd, trying to send %zd (%s)\n", |
| 83 | bytes_read, bytes_to_send, strerror(errno)); |
| 84 | return 1; |
| 85 | } |
| 86 | bytes_to_send -= bytes_written; |
| 87 | } while (bytes_written != 0 && bytes_to_send > 0); |
| 88 | } |
| 89 | |
| 90 | close(s); |
| 91 | return 0; |
| 92 | |
| 93 | } |