You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2014/06/19 05:41:04 UTC

[2/2] git commit: Added a SIGUSR1 handler to cleanly shut down the slave.

Added a SIGUSR1 handler to cleanly shut down the slave.

Review: https://reviews.apache.org/r/21379/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/671b4c4b
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/671b4c4b
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/671b4c4b

Branch: refs/heads/master
Commit: 671b4c4b852dab10645af5e94fcdacde48c54adf
Parents: 6c385a9
Author: Alexandra Sava <al...@gmail.com>
Authored: Wed Jun 18 20:14:39 2014 -0700
Committer: Benjamin Mahler <bm...@twitter.com>
Committed: Wed Jun 18 20:40:57 2014 -0700

----------------------------------------------------------------------
 src/slave/slave.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
 src/slave/slave.hpp |  3 +++
 2 files changed, 49 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/671b4c4b/src/slave/slave.cpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.cpp b/src/slave/slave.cpp
index bc976b7..ed3483f 100644
--- a/src/slave/slave.cpp
+++ b/src/slave/slave.cpp
@@ -138,6 +138,28 @@ Slave::~Slave()
 }
 
 
+lambda::function<void(int, int)> signaledWrapper;
+
+
+static void signalHandler(int sig, siginfo_t* siginfo, void* context)
+{
+  signaledWrapper(sig, siginfo->si_uid);
+}
+
+
+void Slave::signaled(int signal, int uid)
+{
+  if (signal == SIGUSR1) {
+    Result<string> user = os::user(uid);
+
+    shutdown(
+        UPID(),
+        "Received SIGUSR1 signal" +
+        (user.isSome() ? " from user " + user.get() : ""));
+  }
+}
+
+
 void Slave::initialize()
 {
   LOG(INFO) << "Slave started on " << string(self()).substr(6);
@@ -413,6 +435,24 @@ void Slave::initialize()
             << ". Please run the slave with '--help' to see the valid options";
   }
 
+  struct sigaction action;
+  memset(&action, 0, sizeof(struct sigaction));
+
+  // Do not block additional signals while in the handler.
+  sigemptyset(&action.sa_mask);
+
+  // The SA_SIGINFO flag tells sigaction() to use
+  // the sa_sigaction field, not sa_handler.
+  action.sa_flags = SA_SIGINFO;
+
+  signaledWrapper = defer(self(), &Slave::signaled, lambda::_1, lambda::_2);
+
+  action.sa_sigaction = signalHandler;
+
+  if (sigaction(SIGUSR1, &action, NULL) < 0) {
+    EXIT(1) << "Failed to set sigaction: " << strerror(errno);
+  }
+
   // Do recovery.
   async(&state::recover, metaDir, flags.strict)
     .then(defer(self(), &Slave::recover, lambda::_1))
@@ -467,8 +507,12 @@ void Slave::shutdown(const UPID& from, const string& message)
     return;
   }
 
-  LOG(INFO) << "Slave asked to shut down by " << from
-            << (message.empty() ? "" : " because '" + message + "'");
+  if (from) {
+    LOG(INFO) << "Slave asked to shut down by " << from
+              << (message.empty() ? "" : " because '" + message + "'");
+  } else {
+    LOG(INFO) << message << "; shutting down";
+  }
 
   state = TERMINATING;
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/671b4c4b/src/slave/slave.hpp
----------------------------------------------------------------------
diff --git a/src/slave/slave.hpp b/src/slave/slave.hpp
index 3227e73..605ee4a 100644
--- a/src/slave/slave.hpp
+++ b/src/slave/slave.hpp
@@ -309,6 +309,9 @@ public:
   // Schedules a 'path' for gc based on its modification time.
   Future<Nothing> garbageCollect(const std::string& path);
 
+  // Called when the slave was signaled from the specified user.
+  void signaled(int signal, int uid);
+
 private:
   void _authenticate();
   void authenticationTimeout(process::Future<bool> future);