blob: dd2fd1f05729fc0d13894f1bf6fa6941405c2690 [file] [log] [blame]
David Klempner78dc6cd2015-01-26 15:02:51 -08001/*
2 *
Craig Tiller749f10b2016-03-07 20:11:46 -08003 * Copyright 2015-2016, Google Inc.
David Klempner78dc6cd2015-01-26 15:02:51 -08004 * 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" Noblec8fd2d12015-01-30 20:23:27 +010034#include <grpc/support/port_platform.h>
35
36#ifdef GPR_POSIX_WAKEUP_FD
37
Craig Tillere1a03a62015-02-02 07:46:55 -080038#include "src/core/iomgr/wakeup_fd_posix.h"
David Klempner78dc6cd2015-01-26 15:02:51 -080039
40#include <errno.h>
41#include <string.h>
42#include <unistd.h>
43
David Klempner78dc6cd2015-01-26 15:02:51 -080044#include <grpc/support/log.h>
45
Craig Tiller1f375d72016-03-17 07:47:18 -070046#include "src/core/iomgr/socket_utils_posix.h"
47
Craig Tillera82950e2015-09-22 12:33:20 -070048static void pipe_init(grpc_wakeup_fd* fd_info) {
David Klempner78dc6cd2015-01-26 15:02:51 -080049 int pipefd[2];
50 /* TODO(klempner): Make this nonfatal */
Craig Tiller9474c4a2016-02-25 07:02:39 -080051 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 Tillera82950e2015-09-22 12:33:20 -070056 GPR_ASSERT(grpc_set_socket_nonblocking(pipefd[0], 1));
57 GPR_ASSERT(grpc_set_socket_nonblocking(pipefd[1], 1));
David Klempner78dc6cd2015-01-26 15:02:51 -080058 fd_info->read_fd = pipefd[0];
59 fd_info->write_fd = pipefd[1];
60}
61
Craig Tillera82950e2015-09-22 12:33:20 -070062static void pipe_consume(grpc_wakeup_fd* fd_info) {
David Klempner78dc6cd2015-01-26 15:02:51 -080063 char buf[128];
Craig Tillerf96dfc32015-09-10 14:43:18 -070064 ssize_t r;
David Klempner78dc6cd2015-01-26 15:02:51 -080065
Craig Tillera82950e2015-09-22 12:33:20 -070066 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 Klempner78dc6cd2015-01-26 15:02:51 -080078 }
Craig Tillera82950e2015-09-22 12:33:20 -070079 }
David Klempner78dc6cd2015-01-26 15:02:51 -080080}
81
Craig Tillera82950e2015-09-22 12:33:20 -070082static void pipe_wakeup(grpc_wakeup_fd* fd_info) {
David Klempner78dc6cd2015-01-26 15:02:51 -080083 char c = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070084 while (write(fd_info->write_fd, &c, 1) != 1 && errno == EINTR)
David Klempner78dc6cd2015-01-26 15:02:51 -080085 ;
86}
87
Craig Tillera82950e2015-09-22 12:33:20 -070088static 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 Klempner78dc6cd2015-01-26 15:02:51 -080091}
92
Craig Tillera82950e2015-09-22 12:33:20 -070093static int pipe_check_availability(void) {
David Klempner78dc6cd2015-01-26 15:02:51 -080094 /* Assume that pipes are always available. */
95 return 1;
96}
97
Craig Tillere1a03a62015-02-02 07:46:55 -080098const grpc_wakeup_fd_vtable grpc_pipe_wakeup_fd_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -070099 pipe_init, pipe_consume, pipe_wakeup, pipe_destroy,
100 pipe_check_availability};
David Klempner78dc6cd2015-01-26 15:02:51 -0800101
Craig Tillerd6c98df2015-08-18 09:33:44 -0700102#endif /* GPR_POSIX_WAKUP_FD */