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