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 | |
Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 17 | #include "bugreportz.h" |
| 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/strings.h> |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <stdio.h> |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 27 | #include <string> |
| 28 | |
Felipe Leme | aabfcae | 2016-07-29 09:49:04 -0700 | [diff] [blame] | 29 | static constexpr char BEGIN_PREFIX[] = "BEGIN:"; |
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 | |
Felipe Leme | aabfcae | 2016-07-29 09:49:04 -0700 | [diff] [blame] | 35 | // When not invoked with the -p option, it must skip BEGIN and PROGRESS lines otherwise it |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 36 | // will break adb (which is expecting either OK or FAIL). |
Felipe Leme | aabfcae | 2016-07-29 09:49:04 -0700 | [diff] [blame] | 37 | if (!show_progress && (android::base::StartsWith(line, PROGRESS_PREFIX) || |
| 38 | android::base::StartsWith(line, BEGIN_PREFIX))) |
| 39 | return; |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 40 | |
| 41 | android::base::WriteStringToFd(line, STDOUT_FILENO); |
| 42 | } |
| 43 | |
| 44 | int bugreportz(int s, bool show_progress) { |
| 45 | std::string line; |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 46 | while (1) { |
| 47 | char buffer[65536]; |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame] | 48 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer))); |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 49 | if (bytes_read == 0) { |
| 50 | break; |
| 51 | } else if (bytes_read == -1) { |
| 52 | // EAGAIN really means time out, so change the errno. |
| 53 | if (errno == EAGAIN) { |
| 54 | errno = ETIMEDOUT; |
| 55 | } |
Felipe Leme | 1634d84 | 2016-06-08 11:11:01 -0700 | [diff] [blame] | 56 | printf("FAIL:Bugreport read terminated abnormally (%s)\n", strerror(errno)); |
Felipe Leme | 64e1f5b | 2018-08-02 16:16:23 -0700 | [diff] [blame] | 57 | return EXIT_FAILURE; |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 60 | // Writes line by line. |
| 61 | for (int i = 0; i < bytes_read; i++) { |
| 62 | char c = buffer[i]; |
| 63 | line.append(1, c); |
| 64 | if (c == '\n') { |
| 65 | write_line(line, show_progress); |
| 66 | line.clear(); |
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 | } |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 69 | } |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 70 | // Process final line, in case it didn't finish with newline |
| 71 | write_line(line, show_progress); |
Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 72 | return EXIT_SUCCESS; |
| 73 | } |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 74 | |
Dieter Hsu | 031c957 | 2020-09-29 15:23:33 +0800 | [diff] [blame] | 75 | int bugreportz_stream(int s) { |
| 76 | while (1) { |
| 77 | char buffer[65536]; |
| 78 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer))); |
| 79 | if (bytes_read == 0) { |
| 80 | break; |
| 81 | } else if (bytes_read == -1) { |
| 82 | // EAGAIN really means time out, so change the errno. |
| 83 | if (errno == EAGAIN) { |
| 84 | errno = ETIMEDOUT; |
| 85 | } |
| 86 | printf("FAIL:Bugreport read terminated abnormally (%s)\n", strerror(errno)); |
| 87 | return EXIT_FAILURE; |
| 88 | } |
| 89 | |
| 90 | if (!android::base::WriteFully(android::base::borrowed_fd(STDOUT_FILENO), buffer, |
| 91 | bytes_read)) { |
| 92 | printf("Failed to write data to stdout: trying to send %zd bytes (%s)\n", bytes_read, |
| 93 | strerror(errno)); |
| 94 | return EXIT_FAILURE; |
| 95 | } |
| 96 | } |
Felipe Leme | ceeb642 | 2016-04-15 09:31:33 -0700 | [diff] [blame] | 97 | return EXIT_SUCCESS; |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 98 | } |