wakeup_fd_pipe.cc
Go to the documentation of this file.
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 
20 
22 
23 #ifdef GRPC_POSIX_WAKEUP_FD
24 
25 #include <errno.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include <grpc/support/log.h>
30 
34 
35 static grpc_error_handle pipe_init(grpc_wakeup_fd* fd_info) {
36  int pipefd[2];
37  int r = pipe(pipefd);
38  if (0 != r) {
39  gpr_log(GPR_ERROR, "pipe creation failed (%d): %s", errno, strerror(errno));
40  return GRPC_OS_ERROR(errno, "pipe");
41  }
43  err = grpc_set_socket_nonblocking(pipefd[0], 1);
44  if (!GRPC_ERROR_IS_NONE(err)) return err;
45  err = grpc_set_socket_nonblocking(pipefd[1], 1);
46  if (!GRPC_ERROR_IS_NONE(err)) return err;
47  fd_info->read_fd = pipefd[0];
48  fd_info->write_fd = pipefd[1];
49  return GRPC_ERROR_NONE;
50 }
51 
52 static grpc_error_handle pipe_consume(grpc_wakeup_fd* fd_info) {
53  char buf[128];
54  ssize_t r;
55 
56  for (;;) {
57  r = read(fd_info->read_fd, buf, sizeof(buf));
58  if (r > 0) continue;
59  if (r == 0) return GRPC_ERROR_NONE;
60  switch (errno) {
61  case EAGAIN:
62  return GRPC_ERROR_NONE;
63  case EINTR:
64  continue;
65  default:
66  return GRPC_OS_ERROR(errno, "read");
67  }
68  }
69 }
70 
71 static grpc_error_handle pipe_wakeup(grpc_wakeup_fd* fd_info) {
72  char c = 0;
73  while (write(fd_info->write_fd, &c, 1) != 1 && errno == EINTR) {
74  }
75  return GRPC_ERROR_NONE;
76 }
77 
78 static void pipe_destroy(grpc_wakeup_fd* fd_info) {
79  if (fd_info->read_fd != 0) close(fd_info->read_fd);
80  if (fd_info->write_fd != 0) close(fd_info->write_fd);
81 }
82 
83 static int pipe_check_availability(void) {
84  grpc_wakeup_fd fd;
85  fd.read_fd = fd.write_fd = -1;
86 
87  if (pipe_init(&fd) == GRPC_ERROR_NONE) {
88  pipe_destroy(&fd);
89  return 1;
90  } else {
91  return 0;
92  }
93 }
94 
96  pipe_init, pipe_consume, pipe_wakeup, pipe_destroy,
97  pipe_check_availability};
98 
99 #endif /* GPR_POSIX_WAKUP_FD */
grpc_wakeup_fd::read_fd
int read_fd
Definition: wakeup_fd_posix.h:75
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
wakeup_fd_posix.h
grpc_wakeup_fd
Definition: wakeup_fd_posix.h:74
write
#define write
Definition: test-fs.c:47
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
error_ref_leak.err
err
Definition: error_ref_leak.py:35
wakeup_fd_pipe.h
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
GRPC_OS_ERROR
#define GRPC_OS_ERROR(err, call_name)
create an error associated with errno!=0 (an 'operating system' error)
Definition: error.h:352
ssize_t
intptr_t ssize_t
Definition: win.h:27
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
close
#define close
Definition: test-fs.c:48
grpc_wakeup_fd::write_fd
int write_fd
Definition: wakeup_fd_posix.h:76
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
port.h
fix_build_deps.r
r
Definition: fix_build_deps.py:491
socket_utils_posix.h
grpc_set_socket_nonblocking
grpc_error_handle grpc_set_socket_nonblocking(int fd, int non_blocking)
grpc_error
Definition: error_internal.h:42
grpc_pipe_wakeup_fd_vtable
const grpc_wakeup_fd_vtable grpc_pipe_wakeup_fd_vtable
grpc_wakeup_fd_vtable
Definition: wakeup_fd_posix.h:65
errno.h
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:52