diff options
author | Jose <jose@zeroc.com> | 2018-10-29 18:30:50 +0100 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2018-10-29 18:30:50 +0100 |
commit | 5ce35f80fb561b58372fdf46ad52c2fd2949a630 (patch) | |
tree | 8da6db0bfc4a2a587e1266589a08259869856a9c /cpp | |
parent | Fix for PHP 5.3 (Amzn 2018.03) build failures (diff) | |
download | ice-5ce35f80fb561b58372fdf46ad52c2fd2949a630.tar.bz2 ice-5ce35f80fb561b58372fdf46ad52c2fd2949a630.tar.xz ice-5ce35f80fb561b58372fdf46ad52c2fd2949a630.zip |
Add support for systemd Type=notify to Ice::Service
Close #75
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/src/Ice/Instance.cpp | 8 | ||||
-rw-r--r-- | cpp/src/Ice/Makefile.mk | 7 | ||||
-rw-r--r-- | cpp/src/Ice/PropertyNames.cpp | 3 | ||||
-rw-r--r-- | cpp/src/Ice/PropertyNames.h | 2 | ||||
-rw-r--r-- | cpp/src/Ice/Service.cpp | 29 | ||||
-rw-r--r-- | cpp/src/Ice/SystemdJournalI.cpp | 71 | ||||
-rw-r--r-- | cpp/src/Ice/SystemdJournalI.h | 44 | ||||
-rw-r--r-- | cpp/src/IceBox/ServiceManagerI.cpp | 1 |
8 files changed, 163 insertions, 2 deletions
diff --git a/cpp/src/Ice/Instance.cpp b/cpp/src/Ice/Instance.cpp index 45dad54ca6e..d1d1c7c9fda 100644 --- a/cpp/src/Ice/Instance.cpp +++ b/cpp/src/Ice/Instance.cpp @@ -58,6 +58,7 @@ #ifndef _WIN32 # include <Ice/SysLoggerI.h> +# include <Ice/SystemdJournalI.h> # include <signal.h> # include <syslog.h> @@ -1100,6 +1101,13 @@ IceInternal::Instance::Instance(const CommunicatorPtr& communicator, const Initi _initData.properties->getProperty("Ice.ProgramName"), _initData.properties->getPropertyWithDefault("Ice.SyslogFacility", "LOG_USER")); } +# ifdef ICE_USE_SYSTEMD + else if(_initData.properties->getPropertyAsInt("Ice.UseSystemdJournal") > 0) + { + _initData.logger = ICE_MAKE_SHARED(SystemdJournalI, + _initData.properties->getProperty("Ice.ProgramName")); + } +# endif else #endif if(!logfile.empty()) diff --git a/cpp/src/Ice/Makefile.mk b/cpp/src/Ice/Makefile.mk index 7e4187cbc41..321188df0bc 100644 --- a/cpp/src/Ice/Makefile.mk +++ b/cpp/src/Ice/Makefile.mk @@ -26,6 +26,13 @@ ifeq ($(os),Darwin) Ice_excludes += src/IceUtil/ConvertUTF.cpp src/IceUtil/Unicode.cpp endif +ifeq ($(os),Linux) +ifeq ($(shell pkg-config --exists libsystemd 2> /dev/null && echo yes),yes) +Ice_cppflags += -DICE_USE_SYSTEMD $(shell pkg-config --cflags libsystemd) +Ice_ldflags += $(shell pkg-config --libs libsystemd) +endif +endif + Ice[iphoneos]_excludes := $(wildcard $(addprefix $(currentdir)/,Tcp*.cpp)) Ice[iphoneos]_extra_sources := $(wildcard $(addprefix $(currentdir)/ios/,*.cpp *.mm)) Ice[iphonesimulator]_excludes = $(Ice[iphoneos]_excludes) diff --git a/cpp/src/Ice/PropertyNames.cpp b/cpp/src/Ice/PropertyNames.cpp index 5edd1c14013..982a087045f 100644 --- a/cpp/src/Ice/PropertyNames.cpp +++ b/cpp/src/Ice/PropertyNames.cpp @@ -6,7 +6,7 @@ // ICE_LICENSE file included in this distribution. // // ********************************************************************** -// Generated by makeprops.py from file ./config/PropertyNames.xml, Wed Sep 12 19:11:29 2018 +// Generated by makeprops.py from file ../config/PropertyNames.xml, Thu Oct 25 22:10:11 2018 // IMPORTANT: Do not edit this file -- any edits made here will be lost! @@ -181,6 +181,7 @@ const IceInternal::Property IcePropsData[] = IceInternal::Property("Ice.TCP.SndSize", false, 0), IceInternal::Property("Ice.UseApplicationClassLoader", false, 0), IceInternal::Property("Ice.UseSyslog", false, 0), + IceInternal::Property("Ice.UseSystemdJournal", false, 0), IceInternal::Property("Ice.Warn.AMICallback", false, 0), IceInternal::Property("Ice.Warn.Connections", false, 0), IceInternal::Property("Ice.Warn.Datagrams", false, 0), diff --git a/cpp/src/Ice/PropertyNames.h b/cpp/src/Ice/PropertyNames.h index dac29ada243..16afba38aca 100644 --- a/cpp/src/Ice/PropertyNames.h +++ b/cpp/src/Ice/PropertyNames.h @@ -6,7 +6,7 @@ // ICE_LICENSE file included in this distribution. // // ********************************************************************** -// Generated by makeprops.py from file ./config/PropertyNames.xml, Wed Sep 12 19:11:29 2018 +// Generated by makeprops.py from file ../config/PropertyNames.xml, Thu Oct 25 22:10:11 2018 // IMPORTANT: Do not edit this file -- any edits made here will be lost! diff --git a/cpp/src/Ice/Service.cpp b/cpp/src/Ice/Service.cpp index b1243532bfc..a6b6d70493a 100644 --- a/cpp/src/Ice/Service.cpp +++ b/cpp/src/Ice/Service.cpp @@ -34,6 +34,9 @@ # include <sys/types.h> # include <sys/stat.h> # include <csignal> +# ifdef ICE_USE_SYSTEMD +# include <systemd/sd-daemon.h> +# endif #endif using namespace std; @@ -822,11 +825,20 @@ Ice::Service::run(int argc, const char* const argv[], const InitializationData& // if(start(av.argc, av.argv, status)) { +#ifdef ICE_USE_SYSTEMD + sd_notify(0, "READY=1"); +#endif // // Wait for service shutdown. // waitForShutdown(); +#ifdef ICE_USE_SYSTEMD + // + // Inform the service manager that the service is beginning its shutdown. + // + sd_notify(0, "STOPPING=1"); +#endif // // Stop the service. // @@ -840,25 +852,42 @@ Ice::Service::run(int argc, const char* const argv[], const InitializationData& { ServiceError err(this); err << "service terminating after catching exception:\n" << ex; +#ifdef ICE_USE_SYSTEMD + const string msg = err.str(); + sd_notifyf(0, "STATUS=Failed service terminating after catching exception: %s", msg.c_str()); +#endif } catch(const std::exception& ex) { ServiceError err(this); err << "service terminating after catching exception:\n" << ex; +#ifdef ICE_USE_SYSTEMD + const string msg = err.str(); + sd_notifyf(0, "STATUS=Failed service terminating after catching exception: %s", msg.c_str()); +#endif } catch(const std::string& msg) { ServiceError err(this); err << "service terminating after catching exception:\n" << msg; +#ifdef ICE_USE_SYSTEMD + sd_notifyf(0, "STATUS=Failed service terminating after catching exception: %s", msg.c_str()); +#endif } catch(const char* msg) { ServiceError err(this); err << "service terminating after catching exception:\n" << msg; +#ifdef ICE_USE_SYSTEMD + sd_notifyf(0, "STATUS=Failed service terminating after catching exception: %s", msg); +#endif } catch(...) { error("service terminating after catching unknown exception"); +#ifdef ICE_USE_SYSTEMD + sd_notify(0, "STATUS=Failed service terminating after catching unknown exception"); +#endif } if(_communicator) diff --git a/cpp/src/Ice/SystemdJournalI.cpp b/cpp/src/Ice/SystemdJournalI.cpp new file mode 100644 index 00000000000..aaede9991e3 --- /dev/null +++ b/cpp/src/Ice/SystemdJournalI.cpp @@ -0,0 +1,71 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2018 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#ifdef ICE_USE_SYSTEMD + +#include <Ice/SystemdJournalI.h> +#include <Ice/LocalException.h> +#include <syslog.h> +#include <systemd/sd-journal.h> + +using namespace std; +using namespace Ice; +using namespace IceInternal; + +Ice::SystemdJournalI::SystemdJournalI(const string& prefix) : + _prefix(prefix) +{ +} + +void +Ice::SystemdJournalI::print(const string& message) +{ + write(LOG_INFO, message); +} + +void +Ice::SystemdJournalI::trace(const string& category, const string& message) +{ + write(LOG_INFO, category + ": " + message); +} + +void +Ice::SystemdJournalI::warning(const string& message) +{ + write(LOG_WARNING, message); +} + +void +Ice::SystemdJournalI::error(const string& message) +{ + write(LOG_ERR, message); +} + +string +Ice::SystemdJournalI::getPrefix() +{ + return _prefix; +} + +Ice::LoggerPtr +Ice::SystemdJournalI::cloneWithPrefix(const string& prefix) +{ + return ICE_MAKE_SHARED(SystemdJournalI, prefix); +} + +void +Ice::SystemdJournalI::write(int priority, const string& message) const +{ + sd_journal_send("MESSAGE=%s", message.c_str(), + "PRIORITY=%i", priority, + "SYSLOG_IDENTIFIER=%s", _prefix.c_str(), + 0); +} + +#endif diff --git a/cpp/src/Ice/SystemdJournalI.h b/cpp/src/Ice/SystemdJournalI.h new file mode 100644 index 00000000000..c21a68e9015 --- /dev/null +++ b/cpp/src/Ice/SystemdJournalI.h @@ -0,0 +1,44 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2018 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#ifndef ICE_SYSTEMD_JOURNAL_I_H +#define ICE_SYSTEMD_JOURNAL_I_H + +#ifdef ICE_USE_SYSTEMD + +#include <Ice/Logger.h> + +namespace Ice +{ + +class SystemdJournalI : public Logger +{ +public: + + SystemdJournalI(const std::string&); + + virtual void print(const std::string&); + virtual void trace(const std::string&, const std::string&); + virtual void warning(const std::string&); + virtual void error(const std::string&); + virtual std::string getPrefix(); + virtual LoggerPtr cloneWithPrefix(const std::string&); + +private: + + void write(int, const std::string&) const; + + const std::string _prefix; +}; + +} + +#endif + +#endif diff --git a/cpp/src/IceBox/ServiceManagerI.cpp b/cpp/src/IceBox/ServiceManagerI.cpp index d0c82984777..1bfdb899f5d 100644 --- a/cpp/src/IceBox/ServiceManagerI.cpp +++ b/cpp/src/IceBox/ServiceManagerI.cpp @@ -622,6 +622,7 @@ IceBox::ServiceManagerI::start(const string& service, const string& entryPoint, if(initData.properties->getProperty("Ice.LogFile").empty() #ifndef _WIN32 && initData.properties->getPropertyAsInt("Ice.UseSyslog") <= 0 + && initData.properties->getPropertyAsInt("Ice.UseSystemdJournal") <= 0 #endif ) { |