Skip to content

Commit adb72fe

Browse files
gianmfjy
authored andcommitted
Improve verify-default-ports to check both INADDR_ANY and 127.0.0.1. (#8942)
1 parent 50f7cf6 commit adb72fe

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

examples/bin/verify-default-ports

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,12 @@ use strict;
2121
use warnings;
2222
use Socket;
2323

24-
my $skip_var = $ENV{'DRUID_SKIP_PORT_CHECK'};
25-
if ($skip_var && $skip_var ne "0" && $skip_var ne "false" && $skip_var ne "f") {
26-
exit 0;
27-
}
24+
sub try_bind {
25+
my ($port, $addr) = @_;
2826

29-
my @ports = (1527, 2181, 8081, 8082, 8083, 8090, 8091, 8200, 8888);
30-
31-
my $tcp = getprotobyname("tcp");
32-
for my $port (@ports) {
33-
socket(my $sock, PF_INET, SOCK_STREAM, $tcp) or die "socket: $!";
27+
socket(my $sock, PF_INET, SOCK_STREAM, Socket::IPPROTO_TCP) or die "socket: $!";
3428
setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) or die "setsockopt: $!";
35-
if (!bind($sock, sockaddr_in($port, INADDR_ANY))) {
29+
if (!bind($sock, sockaddr_in($port, $addr))) {
3630
print STDERR <<"EOT";
3731
Cannot start up because port $port is already in use.
3832
@@ -42,11 +36,27 @@ configuration documentation:
4236
https://druid.apache.org/docs/latest/configuration/index.html
4337
4438
If you believe this check is in error, or if you have changed your ports away
45-
from the defaults, you can skip the check using an environment variable:
39+
from the defaults, you can skip this check using an environment variable:
4640
4741
export DRUID_SKIP_PORT_CHECK=1
4842
4943
EOT
5044
exit 1;
5145
}
46+
shutdown($sock, 2);
47+
}
48+
49+
my $skip_var = $ENV{'DRUID_SKIP_PORT_CHECK'};
50+
if ($skip_var && $skip_var ne "0" && $skip_var ne "false" && $skip_var ne "f") {
51+
exit 0;
52+
}
53+
54+
my @ports = @ARGV;
55+
if (!@ports) {
56+
@ports = (1527, 2181, 8081, 8082, 8083, 8090, 8091, 8100, 8200, 8888);
57+
}
58+
59+
for my $port (@ports) {
60+
try_bind($port, INADDR_ANY);
61+
try_bind($port, inet_aton("127.0.0.1"));
5262
}

0 commit comments

Comments
 (0)