2019-09-16 12:47:49 +00:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?php
|
2020-02-09 15:34:23 +00:00
|
|
|
/**
|
2024-08-24 13:32:32 +00:00
|
|
|
* Copyright (C) 2010-2024, the Friendica project
|
|
|
|
* SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
2020-02-09 15:34:23 +00:00
|
|
|
*
|
2024-08-24 13:32:32 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-02-02 21:27:05 +00:00
|
|
|
*
|
|
|
|
* Usage: php bin/wait-for-connection {HOST} {PORT} [{TIMEOUT}]
|
|
|
|
*/
|
|
|
|
|
2020-09-07 09:51:26 +00:00
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
|
|
header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden');
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2019-09-16 12:47:49 +00:00
|
|
|
$timeout = 60;
|
|
|
|
switch ($argc) {
|
|
|
|
case 4:
|
|
|
|
$timeout = (float)$argv[3];
|
|
|
|
case 3:
|
|
|
|
$host = $argv[1];
|
|
|
|
$port = (int)$argv[2];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fwrite(STDERR, 'Usage: '.$argv[0].' host port [timeout]'."\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if ($timeout < 0) {
|
|
|
|
fwrite(STDERR, 'Timeout must be greater than zero'."\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if ($port < 1) {
|
|
|
|
fwrite(STDERR, 'Port must be an integer greater than zero'."\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
$socketTimeout = (float)ini_get('default_socket_timeout');
|
|
|
|
if ($socketTimeout > $timeout) {
|
|
|
|
$socketTimeout = $timeout;
|
|
|
|
}
|
|
|
|
$stopTime = time() + $timeout;
|
|
|
|
do {
|
|
|
|
$sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout);
|
|
|
|
if ($sock !== false) {
|
|
|
|
fclose($sock);
|
|
|
|
fwrite(STDOUT, "\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
fwrite(STDOUT, '.');
|
|
|
|
} while (time() < $stopTime);
|
|
|
|
fwrite(STDOUT, "\n");
|
2019-09-23 13:36:16 +00:00
|
|
|
exit(1);
|