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 | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame^] | 23 | #include "bugreportz.h" |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 24 | |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame^] | 25 | int bugreportz(int s) { |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 26 | while (1) { |
| 27 | char buffer[65536]; |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame^] | 28 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer))); |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 29 | 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 Leme | 1634d84 | 2016-06-08 11:11:01 -0700 | [diff] [blame] | 36 | printf("FAIL:Bugreport read terminated abnormally (%s)\n", strerror(errno)); |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 37 | 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 Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame^] | 44 | write(STDOUT_FILENO, buffer + bytes_read - bytes_to_send, bytes_to_send)); |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 45 | if (bytes_written == -1) { |
Felipe Leme | 1634d84 | 2016-06-08 11:11:01 -0700 | [diff] [blame] | 46 | fprintf(stderr, |
Felipe Leme | 59f5af0 | 2016-07-21 20:10:57 -0700 | [diff] [blame^] | 47 | "Failed to write data to stdout: read %zd, trying to send %zd " |
| 48 | "(%s)\n", |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 49 | bytes_read, bytes_to_send, strerror(errno)); |
Felipe Leme | 1634d84 | 2016-06-08 11:11:01 -0700 | [diff] [blame] | 50 | break; |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 51 | } |
| 52 | bytes_to_send -= bytes_written; |
| 53 | } while (bytes_written != 0 && bytes_to_send > 0); |
| 54 | } |
| 55 | |
Felipe Leme | 1634d84 | 2016-06-08 11:11:01 -0700 | [diff] [blame] | 56 | if (close(s) == -1) { |
| 57 | fprintf(stderr, "WARNING: error closing socket: %s\n", strerror(errno)); |
| 58 | } |
Felipe Leme | ceeb642 | 2016-04-15 09:31:33 -0700 | [diff] [blame] | 59 | return EXIT_SUCCESS; |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 60 | } |