From 1df417ad484e7fd84488f72e7b555771fa5cb328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Thu, 9 Feb 2023 04:52:26 +0100 Subject: [PATCH] So now we also have to retry on EADDRINUSE --- supla-MEW-01-VA.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/supla-MEW-01-VA.c b/supla-MEW-01-VA.c index db6ff0f..372dba2 100644 --- a/supla-MEW-01-VA.c +++ b/supla-MEW-01-VA.c @@ -62,9 +62,12 @@ int main(int argc, char ** argv) { return fprintf(stderr, "getaddrinfo(%s:%s): %s\n", argv[optind], svc ? svc + 1 : NULL, gai_strerror(e)), 2; if(svc) *svc = ':'; - if((listener = socket(listen_on->ai_family, listen_on->ai_socktype | SOCK_CLOEXEC, listen_on->ai_protocol)) == -1 || - bind(listener, listen_on->ai_addr, listen_on->ai_addrlen) == -1) + if((listener = socket(listen_on->ai_family, listen_on->ai_socktype | SOCK_CLOEXEC, listen_on->ai_protocol)) == -1) return fprintf(stderr, "%s\n", strerror(errno)), 3; + while((e = bind(listener, listen_on->ai_addr, listen_on->ai_addrlen)) == -1 && errno == EADDRINUSE) + sleep(1); + if(e == -1) + return fprintf(stderr, "%s\n", strerror(errno)), 4; freeaddrinfo(listen_on); ++optind; } @@ -87,7 +90,7 @@ int main(int argc, char ** argv) { { int pipe[2]; if(pipe2(pipe, O_CLOEXEC) == -1 || !(child = fdopen(pipe[0], "r"))) - return fprintf(stderr, "%s\n", strerror(errno)), 4; + return fprintf(stderr, "%s\n", strerror(errno)), 5; if(!(child_pid = fork())) { char ** sccargv = alloca(sizeof(char *) * (argc + 1)); memcpy(sccargv, argv+optind, sizeof(char *) * (argc - 2)); @@ -111,7 +114,7 @@ int main(int argc, char ** argv) { timerfd_settime(death_timer, 0, &(struct itimerspec){.it_interval = {.tv_sec = 30}, .it_value = {.tv_sec = 60}}, NULL); if(listen(listener, 128)) - return fprintf(stderr, "%s\n", strerror(errno)), 5; + return fprintf(stderr, "%s\n", strerror(errno)), 6; struct pollfd fds[3 + 5] = {{.fd = fileno(child), .events = POLLIN}, // {.fd = listener, .events = POLLIN}, // -- 2.45.2