blob: ded0ed35cba93d4a82865b99218d3ebbbb95af03 [file] [log] [blame]
Felipe Leme2628e9e2016-04-12 16:36:51 -07001/*
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 Leme59f5af02016-07-21 20:10:57 -070019#include <stdlib.h>
20#include <string.h>
Felipe Leme2628e9e2016-04-12 16:36:51 -070021#include <unistd.h>
22
Felipe Leme02b7e002016-07-22 12:03:20 -070023#include <string>
24
25#include <android-base/file.h>
26#include <android-base/strings.h>
27
Felipe Leme59f5af02016-07-21 20:10:57 -070028#include "bugreportz.h"
Felipe Leme2628e9e2016-04-12 16:36:51 -070029
Felipe Lemeaabfcae2016-07-29 09:49:04 -070030static constexpr char BEGIN_PREFIX[] = "BEGIN:";
Felipe Leme02b7e002016-07-22 12:03:20 -070031static constexpr char PROGRESS_PREFIX[] = "PROGRESS:";
32
33static void write_line(const std::string& line, bool show_progress) {
34 if (line.empty()) return;
35
Felipe Lemeaabfcae2016-07-29 09:49:04 -070036 // When not invoked with the -p option, it must skip BEGIN and PROGRESS lines otherwise it
Felipe Leme02b7e002016-07-22 12:03:20 -070037 // will break adb (which is expecting either OK or FAIL).
Felipe Lemeaabfcae2016-07-29 09:49:04 -070038 if (!show_progress && (android::base::StartsWith(line, PROGRESS_PREFIX) ||
39 android::base::StartsWith(line, BEGIN_PREFIX)))
40 return;
Felipe Leme02b7e002016-07-22 12:03:20 -070041
42 android::base::WriteStringToFd(line, STDOUT_FILENO);
43}
44
45int bugreportz(int s, bool show_progress) {
46 std::string line;
Felipe Leme2628e9e2016-04-12 16:36:51 -070047 while (1) {
48 char buffer[65536];
Felipe Leme59f5af02016-07-21 20:10:57 -070049 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)));
Felipe Leme2628e9e2016-04-12 16:36:51 -070050 if (bytes_read == 0) {
51 break;
52 } else if (bytes_read == -1) {
53 // EAGAIN really means time out, so change the errno.
54 if (errno == EAGAIN) {
55 errno = ETIMEDOUT;
56 }
Felipe Leme1634d842016-06-08 11:11:01 -070057 printf("FAIL:Bugreport read terminated abnormally (%s)\n", strerror(errno));
Felipe Leme64e1f5b2018-08-02 16:16:23 -070058 return EXIT_FAILURE;
Felipe Leme2628e9e2016-04-12 16:36:51 -070059 }
60
Felipe Leme02b7e002016-07-22 12:03:20 -070061 // Writes line by line.
62 for (int i = 0; i < bytes_read; i++) {
63 char c = buffer[i];
64 line.append(1, c);
65 if (c == '\n') {
66 write_line(line, show_progress);
67 line.clear();
Felipe Leme2628e9e2016-04-12 16:36:51 -070068 }
Felipe Leme02b7e002016-07-22 12:03:20 -070069 }
Felipe Leme2628e9e2016-04-12 16:36:51 -070070 }
Felipe Leme02b7e002016-07-22 12:03:20 -070071 // Process final line, in case it didn't finish with newline
72 write_line(line, show_progress);
Felipe Leme2628e9e2016-04-12 16:36:51 -070073
Felipe Lemeceeb6422016-04-15 09:31:33 -070074 return EXIT_SUCCESS;
Felipe Leme2628e9e2016-04-12 16:36:51 -070075}