Skip to content

Commit

Permalink
Merge pull request #248 from oleg-umnik/hangfix
Browse files Browse the repository at this point in the history
Use FD_CLOEXEC fcntl for WEB-server socket
  • Loading branch information
mhaas committed May 22, 2016
2 parents 628b7ea + d7039a5 commit 8a2e54e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/safe.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,22 @@
static void cleanup_fds(void);

/** List of fd's to close on fork. */
typedef struct _fd_list {
int fd; /**< @brief file descriptor */
struct _fd_list *next; /**< @brief linked list pointer */
} fd_list_t;

static fd_list_t *fd_list = NULL;
typedef int fd_list_t;
static fd_list_t fd_list[MAX_FD_CLEANUP];

/** Clean up all the registered fds. Frees the list as it goes.
* XXX This should only be run by CHILD processes.
*/
static void
cleanup_fds(void)
{
fd_list_t *entry;
unsigned int i;

while (NULL != (entry = fd_list)) {
close(entry->fd);
fd_list = entry->next;
free(entry);
for (i = 0; i < sizeof(fd_list) / sizeof(int); i++) {
if (fd_list[i]) {
close(fd_list[i]);
fd_list[i] = 0;
}
}
}

Expand All @@ -70,11 +67,18 @@ cleanup_fds(void)
void
register_fd_cleanup_on_fork(const int fd)
{
fd_list_t *entry = safe_malloc(sizeof(fd_list_t));

entry->fd = fd;
entry->next = fd_list;
fd_list = entry;
unsigned int i;
for (i = 0; i < sizeof(fd_list) / sizeof(int); i++) {
if (!fd_list[i]) {
break;
}
}
if (MAX_FD_CLEANUP == i) {
debug(LOG_CRIT, "Trying to register more than %d fds for cleanup on fork",
MAX_FD_CLEANUP);
exit(1);
}
fd_list[i] = fd;
}

/** Allocate zero-filled ram or die.
Expand Down
2 changes: 2 additions & 0 deletions src/safe.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <sys/types.h> /* For fork */
#include <unistd.h> /* For fork */

#define MAX_FD_CLEANUP 16

/** Register an fd for auto-cleanup on fork() */
void register_fd_cleanup_on_fork(const int);

Expand Down

0 comments on commit 8a2e54e

Please sign in to comment.