Blob Blame History Raw
From 760028cca19c07dd56162453a4eb3d3b0de7f3af Mon Sep 17 00:00:00 2001
From: Tomas Jelinek <tojeline@redhat.com>
Date: Tue, 19 Jul 2016 13:11:04 +0200
Subject: [PATCH] fix traceback when stopping pcsd shortly after start

- properly notify systemd that pcsd finished starting up
- gracefully exit on SIGINT and SIGTERM
---
 pcsd/pcsd.service        |  1 +
 pcsd/pcsd.service-runner | 25 ++++++++++++++++++-------
 pcsd/ssl.rb              | 18 ++++++++++++++++++
 3 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/pcsd/pcsd.service b/pcsd/pcsd.service
index e506f1b..20bc9ab 100644
--- a/pcsd/pcsd.service
+++ b/pcsd/pcsd.service
@@ -5,6 +5,7 @@ Description=PCS GUI and remote configuration interface
 EnvironmentFile=/etc/sysconfig/pcsd
 Environment=GEM_HOME=/usr/lib/pcsd/vendor/bundle/ruby
 ExecStart=/usr/lib/pcsd/pcsd > /dev/null &
+Type=notify
 
 [Install]
 WantedBy=multi-user.target
diff --git a/pcsd/pcsd.service-runner b/pcsd/pcsd.service-runner
index 1949a68..883d290 100644
--- a/pcsd/pcsd.service-runner
+++ b/pcsd/pcsd.service-runner
@@ -2,12 +2,23 @@
 # this file is a pcsd runner callable from a systemd unit
 # it also serves as a holder of a selinux context
 
-# add pcsd to the load path (ruby -I)
-libdir = File.dirname(__FILE__)
-$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
+begin
+  # add pcsd to the load path (ruby -I)
+  libdir = File.dirname(__FILE__)
+  $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
 
-# change current directory (ruby -C)
-Dir.chdir('/var/lib/pcsd')
+  # change current directory (ruby -C)
+  Dir.chdir('/var/lib/pcsd')
 
-# import and run pcsd
-require 'ssl'
+  # import and run pcsd
+  require 'ssl'
+rescue SignalException => e
+  if [Signal.list['INT'], Signal.list['TERM']].include?(e.signo)
+    # gracefully exit on SIGINT and SIGTERM
+    # pcsd sets up signal handlers later, this catches exceptions which occur
+    # by recieving signals before the handlers have been set up.
+    exit
+  else
+    raise
+  end
+end
diff --git a/pcsd/ssl.rb b/pcsd/ssl.rb
index f56c947..c00d8b3 100644
--- a/pcsd/ssl.rb
+++ b/pcsd/ssl.rb
@@ -3,6 +3,7 @@ require 'webrick'
 require 'webrick/https'
 require 'openssl'
 require 'rack'
+require 'socket'
 
 require 'bootstrap.rb'
 require 'pcs.rb'
@@ -66,11 +67,28 @@ def run_server(server, webrick_options, secondary_addrs)
 
   $logger.info("Listening on #{primary_addr} port #{port}")
   server.run(Sinatra::Application, webrick_options) { |server_instance|
+    # configure ssl options
     server_instance.ssl_context.ciphers = ciphers
+    # set listening addresses
     secondary_addrs.each { |addr|
       $logger.info("Adding listener on #{addr} port #{port}")
       server_instance.listen(addr, port)
     }
+    # notify systemd we are running
+    if ISSYSTEMCTL
+      socket_name = ENV['NOTIFY_SOCKET']
+      if socket_name
+        if socket_name.start_with?('@')
+          # abstract namespace socket
+          socket_name[0] = "\0"
+        end
+        $logger.info("Notifying systemd we are running (socket #{socket_name})")
+        sd_socket = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM)
+        sd_socket.connect(Socket.pack_sockaddr_un(socket_name))
+        sd_socket.send('READY=1', 0)
+        sd_socket.close()
+      end
+    end
   }
 end
 
-- 
1.8.3.1