Skip to content

Commit

Permalink
sstp: unlink stray unix socket on init/bind error
Browse files Browse the repository at this point in the history
  • Loading branch information
themiron committed Feb 27, 2018
1 parent c2fe175 commit 6f93514
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions accel-pppd/ctrl/sstp/sstp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2412,34 +2412,33 @@ static void sstp_init(void)
value = fcntl(serv.hnd.fd, F_GETFD);
if (value < 0 || fcntl(serv.hnd.fd, F_SETFD, value | FD_CLOEXEC) < 0) {
log_emerg("sstp: failed to set socket flags: %s\n", strerror(errno));
close(serv.hnd.fd);
return;
goto error_close;
}

if (addr->u.sa.sa_family == AF_UNIX) {
if (addr->u.sun.sun_path[0] &&
stat(addr->u.sun.sun_path, &st) == 0 && S_ISSOCK(st.st_mode)) {
unlink(addr->u.sun.sun_path);
}
} else
setsockopt(serv.hnd.fd, SOL_SOCKET, SO_REUSEADDR, &serv.hnd.fd, 4);
} else {
value = 1;
setsockopt(serv.hnd.fd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
}

if (bind(serv.hnd.fd, &addr->u.sa, addr->len) < 0) {
log_emerg("sstp: failed to bind socket: %s\n", strerror(errno));
close(serv.hnd.fd);
return;
goto error_close;
}

if (listen(serv.hnd.fd, 10) < 0) {
log_emerg("sstp: failed to listen socket: %s\n", strerror(errno));
close(serv.hnd.fd);
return;
goto error_unlink;
}

if (fcntl(serv.hnd.fd, F_SETFL, O_NONBLOCK)) {
value = fcntl(serv.hnd.fd, F_GETFL);
if (fcntl(serv.hnd.fd, F_SETFL, value | O_NONBLOCK)) {
log_emerg("sstp: failed to set nonblocking mode: %s\n", strerror(errno));
close(serv.hnd.fd);
return;
goto error_unlink;
}

conn_pool = mempool_create(sizeof(struct sstp_conn_t));
Expand All @@ -2452,6 +2451,13 @@ static void sstp_init(void)
triton_context_wakeup(&serv.ctx);

triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
return;

error_unlink:
if (addr->u.sa.sa_family == AF_UNIX && addr->u.sun.sun_path[0])
unlink(addr->u.sun.sun_path);
error_close:
close(serv.hnd.fd);
}

DEFINE_INIT(20, sstp_init);

0 comments on commit 6f93514

Please sign in to comment.