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