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 "src/core/lib/iomgr/port.h"
20
21 #include <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/sync.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include <utility>
28
29 #include "absl/log/check.h"
30 #include "src/core/util/sync.h"
31 #include "test/core/test_util/port.h"
32 #include "test/core/test_util/port_server_client.h"
33
34 static int* chosen_ports = nullptr;
35 static size_t num_chosen_ports = 0;
36 static grpc_core::Mutex* g_default_port_picker_mu;
37 static gpr_once g_default_port_picker_init = GPR_ONCE_INIT;
38
init_default_port_picker()39 static void init_default_port_picker() {
40 g_default_port_picker_mu = new grpc_core::Mutex();
41 }
42
free_chosen_port_locked(int port)43 static int free_chosen_port_locked(int port) {
44 size_t i;
45 int found = 0;
46 size_t found_at = 0;
47 // Find the port and erase it from the list, then tell the server it can be
48 // freed.
49 for (i = 0; i < num_chosen_ports; i++) {
50 if (chosen_ports[i] == port) {
51 CHECK_EQ(found, 0);
52 found = 1;
53 found_at = i;
54 }
55 }
56 if (found) {
57 chosen_ports[found_at] = chosen_ports[num_chosen_ports - 1];
58 num_chosen_ports--;
59 grpc_free_port_using_server(port);
60 }
61 return found;
62 }
63
free_chosen_ports(void)64 static void free_chosen_ports(void) {
65 grpc_core::MutexLock lock(g_default_port_picker_mu);
66 size_t i;
67 grpc_init();
68 for (i = 0; i < num_chosen_ports; i++) {
69 grpc_free_port_using_server(chosen_ports[i]);
70 }
71 grpc_shutdown();
72 gpr_free(chosen_ports);
73 }
74
chose_port_locked(int port)75 static void chose_port_locked(int port) {
76 if (chosen_ports == nullptr) {
77 atexit(free_chosen_ports);
78 }
79 num_chosen_ports++;
80 chosen_ports = static_cast<int*>(
81 gpr_realloc(chosen_ports, sizeof(int) * num_chosen_ports));
82 chosen_ports[num_chosen_ports - 1] = port;
83 }
84
grpc_pick_unused_port_impl(void)85 static int grpc_pick_unused_port_impl(void) {
86 gpr_once_init(&g_default_port_picker_init, init_default_port_picker);
87 grpc_core::MutexLock lock(g_default_port_picker_mu);
88 int port = grpc_pick_port_using_server();
89 if (port != 0) {
90 chose_port_locked(port);
91 }
92
93 return port;
94 }
95
grpc_pick_unused_port_or_die_impl(void)96 static int grpc_pick_unused_port_or_die_impl(void) {
97 int port = grpc_pick_unused_port_impl();
98 if (port == 0) {
99 fprintf(stderr,
100 "gRPC tests require a helper port server to allocate ports used \n"
101 "during the test.\n\n"
102 "This server is not currently running.\n\n"
103 "To start it, run tools/run_tests/start_port_server.py\n\n");
104 exit(1);
105 }
106 return port;
107 }
108
grpc_recycle_unused_port_impl(int port)109 static void grpc_recycle_unused_port_impl(int port) {
110 gpr_once_init(&g_default_port_picker_init, init_default_port_picker);
111 grpc_core::MutexLock lock(g_default_port_picker_mu);
112 CHECK(free_chosen_port_locked(port));
113 }
114
115 namespace {
functions()116 grpc_pick_port_functions& functions() {
117 static grpc_pick_port_functions* functions = new grpc_pick_port_functions{
118 grpc_pick_unused_port_or_die_impl, grpc_recycle_unused_port_impl};
119 return *functions;
120 }
121 } // namespace
122
grpc_pick_unused_port_or_die(void)123 int grpc_pick_unused_port_or_die(void) {
124 return functions().pick_unused_port_or_die_fn();
125 }
126
grpc_recycle_unused_port(int port)127 void grpc_recycle_unused_port(int port) {
128 functions().recycle_unused_port_fn(port);
129 }
130
grpc_set_pick_port_functions(grpc_pick_port_functions new_functions)131 grpc_pick_port_functions grpc_set_pick_port_functions(
132 grpc_pick_port_functions new_functions) {
133 CHECK_NE(new_functions.pick_unused_port_or_die_fn, nullptr);
134 CHECK_NE(new_functions.recycle_unused_port_fn, nullptr);
135 return std::exchange(functions(), new_functions);
136 }
137