Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
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 :
34 : #include <grpc/support/port_platform.h>
35 :
36 : #ifdef GPR_POSIX_WAKEUP_FD
37 :
38 : #include "src/core/iomgr/wakeup_fd_posix.h"
39 :
40 : #include <errno.h>
41 : #include <string.h>
42 : #include <unistd.h>
43 :
44 : #include "src/core/iomgr/socket_utils_posix.h"
45 : #include <grpc/support/log.h>
46 :
47 0 : static void pipe_init(grpc_wakeup_fd* fd_info) {
48 : int pipefd[2];
49 : /* TODO(klempner): Make this nonfatal */
50 0 : GPR_ASSERT(0 == pipe(pipefd));
51 0 : GPR_ASSERT(grpc_set_socket_nonblocking(pipefd[0], 1));
52 0 : GPR_ASSERT(grpc_set_socket_nonblocking(pipefd[1], 1));
53 0 : fd_info->read_fd = pipefd[0];
54 0 : fd_info->write_fd = pipefd[1];
55 0 : }
56 :
57 0 : static void pipe_consume(grpc_wakeup_fd* fd_info) {
58 : char buf[128];
59 : ssize_t r;
60 :
61 : for (;;) {
62 0 : r = read(fd_info->read_fd, buf, sizeof(buf));
63 0 : if (r > 0) continue;
64 0 : if (r == 0) return;
65 0 : switch (errno) {
66 : case EAGAIN:
67 0 : return;
68 : case EINTR:
69 0 : continue;
70 : default:
71 0 : gpr_log(GPR_ERROR, "error reading pipe: %s", strerror(errno));
72 0 : return;
73 : }
74 0 : }
75 : }
76 :
77 0 : static void pipe_wakeup(grpc_wakeup_fd* fd_info) {
78 0 : char c = 0;
79 0 : while (write(fd_info->write_fd, &c, 1) != 1 && errno == EINTR)
80 : ;
81 0 : }
82 :
83 0 : static void pipe_destroy(grpc_wakeup_fd* fd_info) {
84 0 : if (fd_info->read_fd != 0) close(fd_info->read_fd);
85 0 : if (fd_info->write_fd != 0) close(fd_info->write_fd);
86 0 : }
87 :
88 0 : static int pipe_check_availability(void) {
89 : /* Assume that pipes are always available. */
90 0 : return 1;
91 : }
92 :
93 : const grpc_wakeup_fd_vtable grpc_pipe_wakeup_fd_vtable = {
94 : pipe_init, pipe_consume, pipe_wakeup, pipe_destroy,
95 : pipe_check_availability};
96 :
97 : #endif /* GPR_POSIX_WAKUP_FD */
|