grpc_ares_ev_driver_posix.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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  */
19 
20 #include <memory>
21 #include <string>
22 
23 #include "absl/base/thread_annotations.h"
24 #include "absl/memory/memory.h"
25 
31 #if GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER)
32 
33 #include <string.h>
34 #include <sys/ioctl.h>
35 
36 #include <ares.h>
37 
38 #include "absl/strings/str_cat.h"
39 
43 
44 namespace grpc_core {
45 
46 class GrpcPolledFdPosix : public GrpcPolledFd {
47  public:
48  GrpcPolledFdPosix(ares_socket_t as, grpc_pollset_set* driver_pollset_set)
49  : name_(absl::StrCat("c-ares fd: ", static_cast<int>(as))), as_(as) {
50  fd_ = grpc_fd_create(static_cast<int>(as), name_.c_str(), false);
51  driver_pollset_set_ = driver_pollset_set;
52  grpc_pollset_set_add_fd(driver_pollset_set_, fd_);
53  }
54 
55  ~GrpcPolledFdPosix() override {
56  grpc_pollset_set_del_fd(driver_pollset_set_, fd_);
57  /* c-ares library will close the fd inside grpc_fd. This fd may be picked up
58  immediately by another thread, and should not be closed by the following
59  grpc_fd_orphan. */
60  int phony_release_fd;
61  grpc_fd_orphan(fd_, nullptr, &phony_release_fd, "c-ares query finished");
62  }
63 
64  void RegisterForOnReadableLocked(grpc_closure* read_closure)
66  grpc_fd_notify_on_read(fd_, read_closure);
67  }
68 
69  void RegisterForOnWriteableLocked(grpc_closure* write_closure)
71  grpc_fd_notify_on_write(fd_, write_closure);
72  }
73 
74  bool IsFdStillReadableLocked()
76  size_t bytes_available = 0;
77  return ioctl(grpc_fd_wrapped_fd(fd_), FIONREAD, &bytes_available) == 0 &&
78  bytes_available > 0;
79  }
80 
81  void ShutdownLocked(grpc_error_handle error)
83  grpc_fd_shutdown(fd_, error);
84  }
85 
86  ares_socket_t GetWrappedAresSocketLocked()
88  return as_;
89  }
90 
91  const char* GetName() const override { return name_.c_str(); }
92 
93  private:
94  const std::string name_;
95  const ares_socket_t as_;
98 };
99 
100 class GrpcPolledFdFactoryPosix : public GrpcPolledFdFactory {
101  public:
102  GrpcPolledFd* NewGrpcPolledFdLocked(
103  ares_socket_t as, grpc_pollset_set* driver_pollset_set) override {
104  return new GrpcPolledFdPosix(as, driver_pollset_set);
105  }
106 
107  void ConfigureAresChannelLocked(ares_channel /*channel*/) override {}
108 };
109 
110 std::unique_ptr<GrpcPolledFdFactory> NewGrpcPolledFdFactory(Mutex* /* mu */) {
111  return absl::make_unique<GrpcPolledFdFactoryPosix>();
112 }
113 
114 } // namespace grpc_core
115 
116 #endif /* GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER) */
grpc_pollset_set_del_fd
void grpc_pollset_set_del_fd(grpc_pollset_set *pollset_set, grpc_fd *fd)
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
ares.h
grpc_core
Definition: call_metric_recorder.h:31
grpc_fd_shutdown
void grpc_fd_shutdown(grpc_fd *fd, grpc_error_handle why)
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
closure.h
ABSL_GUARDED_BY
#define ABSL_GUARDED_BY(x)
Definition: abseil-cpp/absl/base/thread_annotations.h:62
name_
const std::string name_
Definition: priority.cc:233
grpc_ares_ev_driver.h
xds_interop_client.int
int
Definition: xds_interop_client.py:113
ev_posix.h
grpc_fd
struct grpc_fd grpc_fd
Definition: ev_posix.h:44
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
grpc_ares_wrapper.h
ABSL_EXCLUSIVE_LOCKS_REQUIRED
#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: abseil-cpp/absl/base/thread_annotations.h:145
grpc_fd_create
grpc_fd * grpc_fd_create(int fd, const char *name, bool track_err)
grpc_ares_request::mu
grpc_core::Mutex mu
Definition: grpc_ares_wrapper.h:59
grpc_fd_notify_on_write
void grpc_fd_notify_on_write(grpc_fd *fd, grpc_closure *closure)
error.h
grpc_ares_request
Definition: grpc_ares_wrapper.h:56
grpc_fd_notify_on_read
void grpc_fd_notify_on_read(grpc_fd *fd, grpc_closure *closure)
ares_channeldata
Definition: ares_private.h:266
port.h
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
ares_socket_t
int ares_socket_t
Definition: ares.h:229
grpc_pollset_set_add_fd
void grpc_pollset_set_add_fd(grpc_pollset_set *pollset_set, grpc_fd *fd)
google::protobuf::python::message_descriptor::GetName
static PyObject * GetName(PyBaseDescriptor *self, void *closure)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor.cc:471
grpc_fd_orphan
void grpc_fd_orphan(grpc_fd *fd, grpc_closure *on_done, int *release_fd, const char *reason)
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
iomgr_fwd.h
grpc_error
Definition: error_internal.h:42
grpc_core::NewGrpcPolledFdFactory
std::unique_ptr< GrpcPolledFdFactory > NewGrpcPolledFdFactory(Mutex *mu)
grpc_closure
Definition: closure.h:56
sync.h
grpc_fd_wrapped_fd
int grpc_fd_wrapped_fd(grpc_fd *fd)
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:44