Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / iomgr / endpoint_pair_posix.cc
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  */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/iomgr/port.h"
22
23 #ifdef GRPC_POSIX_SOCKET
24
25 #include "src/core/lib/iomgr/endpoint_pair.h"
26 #include "src/core/lib/iomgr/socket_utils_posix.h"
27 #include "src/core/lib/iomgr/unix_sockets_posix.h"
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <sys/types.h>
34
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
37 #include <grpc/support/string_util.h>
38 #include "src/core/lib/gpr/string.h"
39 #include "src/core/lib/iomgr/tcp_posix.h"
40
41 static void create_sockets(int sv[2]) {
42   int flags;
43   grpc_create_socketpair_if_unix(sv);
44   flags = fcntl(sv[0], F_GETFL, 0);
45   GPR_ASSERT(fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) == 0);
46   flags = fcntl(sv[1], F_GETFL, 0);
47   GPR_ASSERT(fcntl(sv[1], F_SETFL, flags | O_NONBLOCK) == 0);
48   GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[0]) == GRPC_ERROR_NONE);
49   GPR_ASSERT(grpc_set_socket_no_sigpipe_if_possible(sv[1]) == GRPC_ERROR_NONE);
50 }
51
52 grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char* name,
53                                                    grpc_channel_args* args) {
54   int sv[2];
55   grpc_endpoint_pair p;
56   char* final_name;
57   create_sockets(sv);
58
59   grpc_core::ExecCtx exec_ctx;
60
61   gpr_asprintf(&final_name, "%s:client", name);
62   p.client = grpc_tcp_create(grpc_fd_create(sv[1], final_name, false), args,
63                              "socketpair-server");
64   gpr_free(final_name);
65   gpr_asprintf(&final_name, "%s:server", name);
66   p.server = grpc_tcp_create(grpc_fd_create(sv[0], final_name, false), args,
67                              "socketpair-client");
68   gpr_free(final_name);
69
70   return p;
71 }
72
73 #endif