wait-for-connection 923 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. $timeout = 60;
  8. switch ($argc) {
  9. case 4:
  10. $timeout = (float)$argv[3];
  11. case 3:
  12. $host = $argv[1];
  13. $port = (int)$argv[2];
  14. break;
  15. default:
  16. fwrite(STDERR, 'Usage: '.$argv[0].' host port [timeout]'."\n");
  17. exit(2);
  18. }
  19. if ($timeout < 0) {
  20. fwrite(STDERR, 'Timeout must be greater than zero'."\n");
  21. exit(2);
  22. }
  23. if ($port < 1) {
  24. fwrite(STDERR, 'Port must be an integer greater than zero'."\n");
  25. exit(2);
  26. }
  27. $socketTimeout = (float)ini_get('default_socket_timeout');
  28. if ($socketTimeout > $timeout) {
  29. $socketTimeout = $timeout;
  30. }
  31. $stopTime = time() + $timeout;
  32. do {
  33. $sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout);
  34. if ($sock !== false) {
  35. fclose($sock);
  36. fwrite(STDOUT, "\n");
  37. exit(0);
  38. }
  39. sleep(1);
  40. fwrite(STDOUT, '.');
  41. } while (time() < $stopTime);
  42. fwrite(STDOUT, "\n");
  43. exit(1);