Skip to content

Commit

Permalink
fixes nanomsg#576 IPC listen unlinks UNIX socket on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Jul 5, 2018
1 parent 68d117e commit d2fc63f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/platform/posix/posix_epdesc.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct nni_posix_epdesc {
nni_list acceptq;
bool closed;
bool started;
bool ipcbound; // if true unlink socket on exit
struct sockaddr_storage locaddr;
struct sockaddr_storage remaddr;
socklen_t loclen;
Expand Down Expand Up @@ -176,8 +177,7 @@ nni_epdesc_doclose(nni_posix_epdesc *ed)
}

// clean up stale UNIX socket when closing the server.
if ((ed->mode == NNI_EP_MODE_LISTEN) && (ed->loclen != 0) &&
(ed->locaddr.ss_family == AF_UNIX)) {
if (ed->ipcbound) {
struct sockaddr_un *sun = (void *) &ed->locaddr;
(void) unlink(sun->sun_path);
}
Expand Down Expand Up @@ -271,14 +271,17 @@ nni_posix_epdesc_listen(nni_posix_epdesc *ed)
// it is done *before* the listen() operation, whereas fchmod seems to
// have no impact. This behavior was observed on both macOS and Linux.
// YMMV on other platforms.
if ((ss->ss_family == AF_UNIX) && (ed->perms != 0)) {
struct sockaddr_un *sun = (void *) ss;
mode_t perms = ed->perms & ~(S_IFMT);
if ((rv = chmod(sun->sun_path, perms)) != 0) {
rv = nni_plat_errno(errno);
nni_mtx_unlock(&ed->mtx);
nni_posix_pfd_fini(pfd);
return (rv);
if (ss->ss_family == AF_UNIX) {
ed->ipcbound = true;
if (ed->perms != 0) {
struct sockaddr_un *sun = (void *) ss;
mode_t perms = ed->perms & ~(S_IFMT);
if ((rv = chmod(sun->sun_path, perms)) != 0) {
rv = nni_plat_errno(errno);
nni_mtx_unlock(&ed->mtx);
nni_posix_pfd_fini(pfd);
return (rv);
}
}
}

Expand Down

0 comments on commit d2fc63f

Please sign in to comment.