blob: 4b81c910dcd28c58471898cac491c526dddbc7f5 [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 Leme59f5af02016-07-21 20:10:57 -070023#include "bugreportz.h"
Felipe Leme2628e9e2016-04-12 16:36:51 -070024
Felipe Leme59f5af02016-07-21 20:10:57 -070025int bugreportz(int s) {
Felipe Leme2628e9e2016-04-12 16:36:51 -070026 while (1) {
27 char buffer[65536];
Felipe Leme59f5af02016-07-21 20:10:57 -070028 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)));
Felipe Leme2628e9e2016-04-12 16:36:51 -070029 if (bytes_read == 0) {
30 break;
31 } else if (bytes_read == -1) {
32 // EAGAIN really means time out, so change the errno.
33 if (errno == EAGAIN) {
34 errno = ETIMEDOUT;
35 }
Felipe Leme1634d842016-06-08 11:11:01 -070036 printf("FAIL:Bugreport read terminated abnormally (%s)\n", strerror(errno));
Felipe Leme2628e9e2016-04-12 16:36:51 -070037 break;
38 }
39
40 ssize_t bytes_to_send = bytes_read;
41 ssize_t bytes_written;
42 do {
43 bytes_written = TEMP_FAILURE_RETRY(
Felipe Leme59f5af02016-07-21 20:10:57 -070044 write(STDOUT_FILENO, buffer + bytes_read - bytes_to_send, bytes_to_send));
Felipe Leme2628e9e2016-04-12 16:36:51 -070045 if (bytes_written == -1) {
Felipe Leme1634d842016-06-08 11:11:01 -070046 fprintf(stderr,
Felipe Leme59f5af02016-07-21 20:10:57 -070047 "Failed to write data to stdout: read %zd, trying to send %zd "
48 "(%s)\n",
Felipe Leme2628e9e2016-04-12 16:36:51 -070049 bytes_read, bytes_to_send, strerror(errno));
Felipe Leme1634d842016-06-08 11:11:01 -070050 break;
Felipe Leme2628e9e2016-04-12 16:36:51 -070051 }
52 bytes_to_send -= bytes_written;
53 } while (bytes_written != 0 && bytes_to_send > 0);
54 }
55
Felipe Leme1634d842016-06-08 11:11:01 -070056 if (close(s) == -1) {
57 fprintf(stderr, "WARNING: error closing socket: %s\n", strerror(errno));
58 }
Felipe Lemeceeb6422016-04-15 09:31:33 -070059 return EXIT_SUCCESS;
Felipe Leme2628e9e2016-04-12 16:36:51 -070060}