Skip to content

Commit

Permalink
Added FreeBSD support for "listen ... reuseport".
Browse files Browse the repository at this point in the history
  • Loading branch information
mdocguard committed Jul 2, 2018
1 parent 4bf4650 commit 468e377
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/core/ngx_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
reuseport = 0;
olen = sizeof(int);

#ifdef SO_REUSEPORT_LB

if (getsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT_LB,
(void *) &reuseport, &olen)
== -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
"getsockopt(SO_REUSEPORT_LB) %V failed, ignored",
&ls[i].addr_text);

} else {
ls[i].reuseport = reuseport ? 1 : 0;
}

#else

if (getsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT,
(void *) &reuseport, &olen)
== -1)
Expand All @@ -292,6 +308,7 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
} else {
ls[i].reuseport = reuseport ? 1 : 0;
}
#endif

#endif

Expand Down Expand Up @@ -430,6 +447,20 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)

int reuseport = 1;

#ifdef SO_REUSEPORT_LB

if (setsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT_LB,
(const void *) &reuseport, sizeof(int))
== -1)
{
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_socket_errno,
"setsockopt(SO_REUSEPORT_LB) %V failed, "
"ignored",
&ls[i].addr_text);
}

#else

if (setsockopt(ls[i].fd, SOL_SOCKET, SO_REUSEPORT,
(const void *) &reuseport, sizeof(int))
== -1)
Expand All @@ -438,6 +469,7 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)
"setsockopt(SO_REUSEPORT) %V failed, ignored",
&ls[i].addr_text);
}
#endif

ls[i].add_reuseport = 0;
}
Expand Down Expand Up @@ -488,6 +520,27 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)

reuseport = 1;

#ifdef SO_REUSEPORT_LB

if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT_LB,
(const void *) &reuseport, sizeof(int))
== -1)
{
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
"setsockopt(SO_REUSEPORT_LB) %V failed",
&ls[i].addr_text);

if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
ngx_close_socket_n " %V failed",
&ls[i].addr_text);
}

return NGX_ERROR;
}

#else

if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT,
(const void *) &reuseport, sizeof(int))
== -1)
Expand All @@ -504,6 +557,7 @@ ngx_open_listening_sockets(ngx_cycle_t *cycle)

return NGX_ERROR;
}
#endif
}
#endif

Expand Down

0 comments on commit 468e377

Please sign in to comment.