Skip to content

Commit

Permalink
Ignore SIGHUP when running as a daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
Trond Norbye authored and Trond Norbye committed Apr 13, 2009
1 parent acb84f0 commit ee0c3d5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ fi
AC_CHECK_FUNCS(mlockall)
AC_CHECK_FUNCS(getpagesizes)
AC_CHECK_FUNCS(memcntl)
AC_CHECK_FUNCS(sigignore)

AC_DEFUN([AC_C_ALIGNMENT],
[AC_CACHE_CHECK(for alignment, ac_cv_c_alignment,
Expand Down Expand Up @@ -353,9 +354,11 @@ if test "$ICC" = "yes"
then
dnl ICC trying to be gcc.
CFLAGS="$CFLAGS -diag-disable 187 -Wall -Werror"
AC_DEFINE([_GNU_SOURCE],[1],[find sigignore on Linux])
elif test "$GCC" = "yes"
then
CFLAGS="$CFLAGS -Wall -Werror -pedantic -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls"
AC_DEFINE([_GNU_SOURCE],[1],[find sigignore on Linux])
elif test "$SUNCC" = "yes"
then
CFLAGS="$CFLAGS -errfmt=error -errwarn -errshort=tags"
Expand Down
25 changes: 24 additions & 1 deletion internal_tests.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */

#undef NDEBUG
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include "memcached.h"

Expand Down Expand Up @@ -104,11 +105,33 @@ static void test_safe_strtol() {
assert(val == 123);
}

static void test_issue_44(void) {
char pidfile[80];
char buffer[256];
sprintf(pidfile, "/tmp/memcached.%d", getpid());
sprintf(buffer, "./memcached-debug -p 0 -P %s -d", pidfile);
assert(system(buffer) == 0);
sleep(1);
FILE *fp = fopen(pidfile, "r");
assert(fp);
assert(fgets(buffer, sizeof(buffer), fp));
fclose(fp);
pid_t pid = atol(buffer);
assert(kill(pid, 0) == 0);
assert(kill(pid, SIGHUP) == 0);
sleep(1);
assert(kill(pid, 0) == 0);
assert(kill(pid, SIGTERM) == 0);
assert(remove(pidfile) == 0);
}


int main(int argc, char **argv) {
test_safe_strtoull();
test_safe_strtoll();
test_safe_strtoul();
test_safe_strtol();
test_issue_44();
printf("OK.\n");
return 0;
}
25 changes: 17 additions & 8 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -3895,6 +3895,18 @@ static void sig_handler(const int sig) {
exit(EXIT_SUCCESS);
}

#ifndef HAVE_SIGIGNORE
static int sigignore(int sig) {
struct sigaction sa = { .sa_handler = SIG_IGN, .sa_flags = 0 };

if (sigemptyset(&sa.sa_mask) == -1 || sigaction(sig, &sa, 0) == -1) {
return -1;
}
return 0;
}
#endif


#if defined(HAVE_GETPAGESIZES) && defined(HAVE_MEMCNTL)
/*
* On systems that supports multiple page sizes we may reduce the
Expand Down Expand Up @@ -3945,7 +3957,6 @@ int main (int argc, char **argv) {
char *username = NULL;
char *pid_file = NULL;
struct passwd *pw;
struct sigaction sa;
struct rlimit rlim;
/* listening sockets */
static int *l_socket = NULL;
Expand Down Expand Up @@ -4159,9 +4170,10 @@ int main (int argc, char **argv) {
/* daemonize if requested */
/* if we want to ensure our ability to dump core, don't chdir to / */
if (do_daemonize) {
int res;
res = daemonize(maxcore, settings.verbose);
if (res == -1) {
if (sigignore(SIGHUP) == -1) {
perror("Failed to ignore SIGHUP");
}
if (daemonize(maxcore, settings.verbose) == -1) {
fprintf(stderr, "failed to daemon() in order to daemonize\n");
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -4196,10 +4208,7 @@ int main (int argc, char **argv) {
* ignore SIGPIPE signals; we can use errno == EPIPE if we
* need that information
*/
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
if (sigemptyset(&sa.sa_mask) == -1 ||
sigaction(SIGPIPE, &sa, 0) == -1) {
if (sigignore(SIGPIPE) == -1) {
perror("failed to ignore SIGPIPE; sigaction");
exit(EX_OSERR);
}
Expand Down

0 comments on commit ee0c3d5

Please sign in to comment.