Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

do listen() after fork() when SO_REUSEPORT enabled #151

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ CFLAGS = -O2 -g -std=c99 -fno-strict-aliasing -Wall -W -D_GNU_SOURCE -I/usr/loc
LDFLAGS = -lssl -lcrypto -lev -L/usr/local/lib
OBJS = stud.o ringbuffer.o configuration.o

#Some security enhancement
CFLAGS += -fstack-protector --param=ssp-buffer-size=4 -Wp,-D_FORTIFY_SOURCE=2

all: realall

# Shared cache feature
Expand Down
30 changes: 21 additions & 9 deletions stud.c
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,20 @@ static int create_main_socket() {
return s;
}


void do_listen(){
listener_socket = create_main_socket();

#ifdef USE_SHARED_CACHE
if (CONFIG->SHCUPD_PORT) {
/* create socket to send(children) and
receive(parent) cache updates */
shcupd_socket = create_shcupd_socket();
}
#endif /* USE_SHARED_CACHE */
}


/* Initiate a clear-text nonblocking connect() to the backend IP on behalf
* of a newly connected upstream (encrypted) client*/
static int create_back_socket() {
Expand Down Expand Up @@ -1520,6 +1534,10 @@ static void handle_connections() {
/* child cannot create new children... */
create_workers = 0;

#ifdef SO_REUSEPORT
do_listen();
#endif

#if defined(CPU_ZERO) && defined(CPU_SET)
cpu_set_t cpus;

Expand Down Expand Up @@ -1819,15 +1837,9 @@ int main(int argc, char **argv) {

init_globals();

listener_socket = create_main_socket();

#ifdef USE_SHARED_CACHE
if (CONFIG->SHCUPD_PORT) {
/* create socket to send(children) and
receive(parent) cache updates */
shcupd_socket = create_shcupd_socket();
}
#endif /* USE_SHARED_CACHE */
#ifndef SO_REUSEPORT
do_listen();
#endif

/* load certificates, pass to handle_connections */
init_openssl();
Expand Down