Skip to content

Commit 03f335f

Browse files
authored
Enable SO_REUSE{ADDR,PORT} for TCP/IP server sockets (openstreetmap#460)
Otherwise `renderd` processes configured to use TCP/IP server sockets may need to wait until the address and/or port are released before restarting under certain circumstances (especially an ungraceful termination).
1 parent aa53e60 commit 03f335f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/renderd.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ int server_socket_init(renderd_config *sConfig)
494494
int fd;
495495

496496
if (sConfig->ipport > 0) {
497+
const int enable = 1;
498+
497499
g_logger(G_LOG_LEVEL_INFO, "Initialising TCP/IP server socket on %s:%i",
498500
sConfig->iphostname, sConfig->ipport);
499501
fd = socket(PF_INET6, SOCK_STREAM, 0);
@@ -503,6 +505,18 @@ int server_socket_init(renderd_config *sConfig)
503505
exit(2);
504506
}
505507

508+
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0) {
509+
g_logger(G_LOG_LEVEL_CRITICAL, "setsockopt SO_REUSEADDR failed for: %s:%i",
510+
sConfig->iphostname, sConfig->ipport);
511+
exit(3);
512+
}
513+
514+
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(enable)) < 0) {
515+
g_logger(G_LOG_LEVEL_CRITICAL, "setsockopt SO_REUSEPORT failed for: %s:%i",
516+
sConfig->iphostname, sConfig->ipport);
517+
exit(3);
518+
}
519+
506520
bzero(&addrI, sizeof(addrI));
507521
addrI.sin6_family = AF_INET6;
508522
addrI.sin6_addr = in6addr_any;

0 commit comments

Comments
 (0)