Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When receiving a SIGTERM wait() for children #1

Closed
wants to merge 3 commits into from
Closed
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
76 changes: 50 additions & 26 deletions cop/TrafficCop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#if defined(linux) || defined (solaris)
#include "sys/utsname.h"
#include "ink_killall.h"
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
Expand All @@ -45,7 +44,7 @@ union semun
#endif // linux check

// For debugging, turn this on.
// #define TRACE_LOG_COP 1
#define TRACE_LOG_COP 1

#define OPTIONS_MAX 32
#define OPTIONS_LEN_MAX 1024
Expand Down Expand Up @@ -212,6 +211,7 @@ chown_file_to_admin_user(const char *file) {
}
}
}

static void
sig_child(int signum)
{
Expand All @@ -238,6 +238,43 @@ sig_child(int signum)
cop_log_trace("Leaving sig_child(%d)\n", signum);
}

static void
sig_term(int signum)
{
pid_t pid = 0;
int status = 0;
int err;
pid_t holding_pid;

//killsig = SIGTERM;

cop_log_trace("Entering sig_term(%d)\n", signum);

// safely^W commit suicide.
cop_log_trace("Sending signal %d to entire group\n", signum);
killpg(0, signum);

cop_log_trace("Waiting for children to exit.");

for (;;) {
pid = waitpid(WAIT_ANY, &status, WNOHANG);

if (pid <= 0) {
break;
}
// TSqa03086 - We can not log the child status signal from
// the signal handler since syslog can deadlock. Record
// the pid and the status in a global for logging
// next time through the event loop. We will occasionally
// lose some information if we get two sig childs in rapid
// succession
child_pid = pid;
child_status = status;
}
cop_log_trace("Leaving sig_term(%d), exiting traffic_cop\n", signum);
exit(0);
}

static void
#if defined(solaris)
sig_fatal(int signum, siginfo_t * t, void *c)
Expand Down Expand Up @@ -1381,18 +1418,7 @@ check_programs()
Lockfile manager_lf(manager_lockfile);
err = manager_lf.Open(&holding_pid);
chown_file_to_admin_user(manager_lockfile);
#if defined(linux)
// if lockfile held, but process doesn't exist, killall and try again
if (err == 0) {
if (kill(holding_pid, 0) == -1) {
cop_log(COP_WARNING, "%s's lockfile is held, but its pid (%d) is missing;"
" killing all processes named '%s' and retrying\n", manager_binary, holding_pid, manager_binary);
ink_killall(manager_binary, killsig);
sleep(1); // give signals a chance to be received
err = manager_lf.Open(&holding_pid);
}
}
#endif

if (err > 0) {
// 'lockfile_open' returns the file descriptor of the opened
// lockfile. We need to close this before spawning the
Expand Down Expand Up @@ -1473,18 +1499,7 @@ check_programs()

Lockfile server_lf(server_lockfile);
err = server_lf.Open(&holding_pid);
#if defined(linux)
// if lockfile held, but process doesn't exist, killall and try again
if (err == 0) {
if (kill(holding_pid, 0) == -1) {
cop_log(COP_WARNING, "%s's lockfile is held, but its pid (%d) is missing;"
" killing all processes named '%s' and retrying\n", server_binary, holding_pid, server_binary);
ink_killall(server_binary, killsig);
sleep(1); // give signals a chance to be received
err = server_lf.Open(&holding_pid);
}
}
#endif

if (err > 0) {
server_lf.Close();

Expand Down Expand Up @@ -1673,6 +1688,15 @@ init_signals()
struct sigaction action;

cop_log_trace("Entering init_signals()\n");
// Handle the SIGTERM and SIGINT signal:
// We kill the process group and wait() for all children
action.sa_handler = sig_term;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;

sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);

// Handle the SIGCHLD signal. We simply reap all children that
// die (which should only be spawned traffic_manager's).
action.sa_handler = sig_child;
Expand Down
2 changes: 0 additions & 2 deletions lib/ts/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ libtsutil_la_SOURCES = \
ink_inet.cc \
ink_inet.h \
ink_inout.h \
ink_killall.cc \
ink_killall.h \
ink_llqueue.h \
ink_lockfile.h \
INK_MD5.h \
Expand Down
149 changes: 0 additions & 149 deletions lib/ts/ink_killall.cc

This file was deleted.

63 changes: 0 additions & 63 deletions lib/ts/ink_killall.h

This file was deleted.

1 change: 0 additions & 1 deletion lib/ts/libts.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
#include "ink_hash_table.h"
#include "ink_hrtime.h"
#include "ink_inout.h"
#include "ink_killall.h"
#include "ink_llqueue.h"
#include "ink_lockfile.h"
#include "ink_memory.h"
Expand Down
Loading