From a93e53aa54cd1c66b326ee58138184abf02d3aec Mon Sep 17 00:00:00 2001 From: Lev Gromov Date: Wed, 16 Oct 2024 18:40:05 +0200 Subject: [PATCH] Fix checking if port is free --- hpc/Makefile | 5 ++++- hpc/hq_scripts/job.sh | 2 +- hpc/is_port_free.cpp | 43 ++++++++++++++++++++++++++++++++++++++++ hpc/slurm_scripts/job.sh | 2 +- 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 hpc/is_port_free.cpp diff --git a/hpc/Makefile b/hpc/Makefile index 5757bea..3a420d4 100644 --- a/hpc/Makefile +++ b/hpc/Makefile @@ -1,7 +1,10 @@ -all: build-load-balancer build-testmodel +all: build-load-balancer build-is-port-free build-testmodel build-load-balancer: - g++ -O3 -Wno-unused-result -std=c++17 -I../lib/ LoadBalancer.cpp -o load-balancer -pthread +build-is-port-free: + - g++ -O3 -std=c++17 is_port_free.cpp -o is_port_free + build-testmodel: - g++ -O3 -Wno-unused-result -std=c++17 -I../lib/ ../models/testmodel/minimal-server.cpp -o testmodel -pthread diff --git a/hpc/hq_scripts/job.sh b/hpc/hq_scripts/job.sh index fcbb91a..8c4a38f 100755 --- a/hpc/hq_scripts/job.sh +++ b/hpc/hq_scripts/job.sh @@ -18,7 +18,7 @@ function get_available_port { port=$(shuf -i $MIN_PORT-$MAX_PORT -n 1) # Check if the port is in use - while lsof -Pi :$port -sTCP:LISTEN -t >/dev/null; do + until ./is_port_free $port; do # If the port is in use, generate a new random port number port=$(shuf -i $MIN_PORT-$MAX_PORT -n 1) done diff --git a/hpc/is_port_free.cpp b/hpc/is_port_free.cpp new file mode 100644 index 0000000..2ac6c48 --- /dev/null +++ b/hpc/is_port_free.cpp @@ -0,0 +1,43 @@ +#include "../lib/httplib.h" + +#include + +#include +#include +#include + +// Takes one integer argument - a port. +// If the port is free, returns EXIT_SUCCESS; otherwise returns EXIT_FAILURE. +// Note: Uses the Linux IPv4 protocol implementation! +int main(int argc, char* argv[]) +{ + // Get port to check from command line args + if (argc < 2) { + std::cerr << "Missing required positional argument: port" << std::endl; + return EXIT_FAILURE; + } + int port = std::atoi(argv[1]); + + // Create socket + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) { + std::cerr << "Failed to create socket: " << std::strerror(errno) << std::endl; + return EXIT_FAILURE; + } + + // Set address to 0.0.0.0:port + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + + // Attempt to assign address to socket + int success = bind(sockfd, (struct sockaddr*) &addr, sizeof(addr)); + + if (success == -1) { + return EXIT_FAILURE; + } else { + return EXIT_SUCCESS; + } +} \ No newline at end of file diff --git a/hpc/slurm_scripts/job.sh b/hpc/slurm_scripts/job.sh index a843db5..23a5719 100755 --- a/hpc/slurm_scripts/job.sh +++ b/hpc/slurm_scripts/job.sh @@ -16,7 +16,7 @@ function get_available_port { port=$(shuf -i $MIN_PORT-$MAX_PORT -n 1) # Check if the port is in use - while lsof -Pi :$port -sTCP:LISTEN -t >/dev/null; do + until ./is_port_free $port; do # If the port is in use, generate a new random port number port=$(shuf -i $MIN_PORT-$MAX_PORT -n 1) done