1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/iomgr/port.h"
22
23 #ifdef GRPC_POSIX_WAKEUP_FD
24
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "absl/log/log.h"
30 #include "src/core/lib/iomgr/socket_utils_posix.h"
31 #include "src/core/lib/iomgr/wakeup_fd_pipe.h"
32 #include "src/core/lib/iomgr/wakeup_fd_posix.h"
33 #include "src/core/util/crash.h"
34 #include "src/core/util/strerror.h"
35
pipe_init(grpc_wakeup_fd * fd_info)36 static grpc_error_handle pipe_init(grpc_wakeup_fd* fd_info) {
37 int pipefd[2];
38 int r = pipe(pipefd);
39 if (0 != r) {
40 LOG(ERROR) << "pipe creation failed (" << errno
41 << "): " << grpc_core::StrError(errno);
42 return GRPC_OS_ERROR(errno, "pipe");
43 }
44 grpc_error_handle err;
45 err = grpc_set_socket_nonblocking(pipefd[0], 1);
46 if (!err.ok()) return err;
47 err = grpc_set_socket_nonblocking(pipefd[1], 1);
48 if (!err.ok()) return err;
49 fd_info->read_fd = pipefd[0];
50 fd_info->write_fd = pipefd[1];
51 return absl::OkStatus();
52 }
53
pipe_consume(grpc_wakeup_fd * fd_info)54 static grpc_error_handle pipe_consume(grpc_wakeup_fd* fd_info) {
55 char buf[128];
56 ssize_t r;
57
58 for (;;) {
59 r = read(fd_info->read_fd, buf, sizeof(buf));
60 if (r > 0) continue;
61 if (r == 0) return absl::OkStatus();
62 switch (errno) {
63 case EAGAIN:
64 return absl::OkStatus();
65 case EINTR:
66 continue;
67 default:
68 return GRPC_OS_ERROR(errno, "read");
69 }
70 }
71 }
72
pipe_wakeup(grpc_wakeup_fd * fd_info)73 static grpc_error_handle pipe_wakeup(grpc_wakeup_fd* fd_info) {
74 char c = 0;
75 while (write(fd_info->write_fd, &c, 1) != 1 && errno == EINTR) {
76 }
77 return absl::OkStatus();
78 }
79
pipe_destroy(grpc_wakeup_fd * fd_info)80 static void pipe_destroy(grpc_wakeup_fd* fd_info) {
81 if (fd_info->read_fd != 0) close(fd_info->read_fd);
82 if (fd_info->write_fd != 0) close(fd_info->write_fd);
83 }
84
pipe_check_availability(void)85 static int pipe_check_availability(void) {
86 grpc_wakeup_fd fd;
87 fd.read_fd = fd.write_fd = -1;
88
89 if (pipe_init(&fd) == absl::OkStatus()) {
90 pipe_destroy(&fd);
91 return 1;
92 } else {
93 return 0;
94 }
95 }
96
97 const grpc_wakeup_fd_vtable grpc_pipe_wakeup_fd_vtable = {
98 pipe_init, pipe_consume, pipe_wakeup, pipe_destroy,
99 pipe_check_availability};
100
101 #endif // GRPC_POSIX_WAKEUP_FD
102