David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 749f10b | 2016-03-07 20:11:46 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Nicolas "Pixel" Noble | c8fd2d1 | 2015-01-30 20:23:27 +0100 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_POSIX_WAKEUP_FD |
| 37 | |
Craig Tiller | e1a03a6 | 2015-02-02 07:46:55 -0800 | [diff] [blame] | 38 | #include "src/core/iomgr/wakeup_fd_posix.h" |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 39 | |
| 40 | #include <errno.h> |
| 41 | #include <string.h> |
| 42 | #include <unistd.h> |
| 43 | |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 44 | #include <grpc/support/log.h> |
| 45 | |
Craig Tiller | 1f375d7 | 2016-03-17 07:47:18 -0700 | [diff] [blame] | 46 | #include "src/core/iomgr/socket_utils_posix.h" |
| 47 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 48 | static void pipe_init(grpc_wakeup_fd* fd_info) { |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 49 | int pipefd[2]; |
| 50 | /* TODO(klempner): Make this nonfatal */ |
Craig Tiller | 9474c4a | 2016-02-25 07:02:39 -0800 | [diff] [blame] | 51 | int r = pipe(pipefd); |
| 52 | if (0 != r) { |
| 53 | gpr_log(GPR_ERROR, "pipe creation failed (%d): %s", errno, strerror(errno)); |
| 54 | abort(); |
| 55 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | GPR_ASSERT(grpc_set_socket_nonblocking(pipefd[0], 1)); |
| 57 | GPR_ASSERT(grpc_set_socket_nonblocking(pipefd[1], 1)); |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 58 | fd_info->read_fd = pipefd[0]; |
| 59 | fd_info->write_fd = pipefd[1]; |
| 60 | } |
| 61 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 62 | static void pipe_consume(grpc_wakeup_fd* fd_info) { |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 63 | char buf[128]; |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 64 | ssize_t r; |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 65 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 66 | for (;;) { |
| 67 | r = read(fd_info->read_fd, buf, sizeof(buf)); |
| 68 | if (r > 0) continue; |
| 69 | if (r == 0) return; |
| 70 | switch (errno) { |
| 71 | case EAGAIN: |
| 72 | return; |
| 73 | case EINTR: |
| 74 | continue; |
| 75 | default: |
| 76 | gpr_log(GPR_ERROR, "error reading pipe: %s", strerror(errno)); |
| 77 | return; |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 78 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 79 | } |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | static void pipe_wakeup(grpc_wakeup_fd* fd_info) { |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 83 | char c = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 84 | while (write(fd_info->write_fd, &c, 1) != 1 && errno == EINTR) |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 85 | ; |
| 86 | } |
| 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | static void pipe_destroy(grpc_wakeup_fd* fd_info) { |
| 89 | if (fd_info->read_fd != 0) close(fd_info->read_fd); |
| 90 | if (fd_info->write_fd != 0) close(fd_info->write_fd); |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | static int pipe_check_availability(void) { |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 94 | /* Assume that pipes are always available. */ |
| 95 | return 1; |
| 96 | } |
| 97 | |
Craig Tiller | e1a03a6 | 2015-02-02 07:46:55 -0800 | [diff] [blame] | 98 | const grpc_wakeup_fd_vtable grpc_pipe_wakeup_fd_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | pipe_init, pipe_consume, pipe_wakeup, pipe_destroy, |
| 100 | pipe_check_availability}; |
David Klempner | 78dc6cd | 2015-01-26 15:02:51 -0800 | [diff] [blame] | 101 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 102 | #endif /* GPR_POSIX_WAKUP_FD */ |