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> |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame^] | 23 | #include <string> |
| 24 | |
| 25 | #include <android-base/file.h> |
| 26 | #include <android-base/strings.h> |
| 27 | |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 28 | #include "bugreportz.h" |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 29 | |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame^] | 30 | static constexpr char PROGRESS_PREFIX[] = "PROGRESS:"; |
| 31 | |
| 32 | static void write_line(const std::string& line, bool show_progress) { |
| 33 | if (line.empty()) return; |
| 34 | |
| 35 | // When not invoked with the -p option, it must skip PROGRESS lines otherwise it |
| 36 | // will break adb (which is expecting either OK or FAIL). |
| 37 | if (!show_progress && android::base::StartsWith(line, PROGRESS_PREFIX)) return; |
| 38 | |
| 39 | android::base::WriteStringToFd(line, STDOUT_FILENO); |
| 40 | } |
| 41 | |
| 42 | int bugreportz(int s, bool show_progress) { |
| 43 | std::string line; |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 44 | while (1) { |
| 45 | char buffer[65536]; |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 46 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer))); |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 47 | if (bytes_read == 0) { |
| 48 | break; |
| 49 | } else if (bytes_read == -1) { |
| 50 | // EAGAIN really means time out, so change the errno. |
| 51 | if (errno == EAGAIN) { |
| 52 | errno = ETIMEDOUT; |
| 53 | } |
Felipe Leme | 1634d84 | 2016-06-08 11:11:01 -0700 | [diff] [blame] | 54 | printf("FAIL:Bugreport read terminated abnormally (%s)\n", strerror(errno)); |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 55 | break; |
| 56 | } |
| 57 | |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame^] | 58 | // Writes line by line. |
| 59 | for (int i = 0; i < bytes_read; i++) { |
| 60 | char c = buffer[i]; |
| 61 | line.append(1, c); |
| 62 | if (c == '\n') { |
| 63 | write_line(line, show_progress); |
| 64 | line.clear(); |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 65 | } |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame^] | 66 | } |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 67 | } |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame^] | 68 | // Process final line, in case it didn't finish with newline |
| 69 | write_line(line, show_progress); |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 70 | |
Felipe Leme | 1634d84 | 2016-06-08 11:11:01 -0700 | [diff] [blame] | 71 | if (close(s) == -1) { |
| 72 | fprintf(stderr, "WARNING: error closing socket: %s\n", strerror(errno)); |
| 73 | } |
Felipe Leme | ceeb642 | 2016-04-15 09:31:33 -0700 | [diff] [blame] | 74 | return EXIT_SUCCESS; |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 75 | } |