Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
Service.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_SERVICE_H
4#define ICE_SERVICE_H
5
6#include "Ice.h"
7
8#ifdef _WIN32
9# ifndef NOMINMAX
10# define NOMINMAX
11# endif
12# include <windows.h>
13# include <winsvc.h>
14#endif
15
16namespace Ice
17{
18 /// A singleton class that helps you write Windows services and Unix daemons using Ice.
19 /// @headerfile Ice/Ice.h
20 class ICE_API Service
21 {
22 public:
23 /// Default constructor.
25
26 virtual ~Service();
27
28 /// Shuts down the service. The default implementation calls Communicator::shutdown.
29 virtual bool shutdown();
30
31 /// Notifies the service that the program received a signal. The default implementation calls #shutdown.
32 /// @see Ice::CtrlCHandler
33 virtual void interrupt();
34
35 /// The primary entry point for services. This function examines @p argv for reserved options and takes the
36 /// appropriate action. The reserved options are shown below.
37 ///
38 /// Win32:
39 ///
40 /// --service NAME
41 ///
42 /// Unix:
43 ///
44 /// --daemon [--nochdir] [--noclose]
45 ///
46 /// If --service or --daemon are specified, the program runs as a service, otherwise the program runs as a
47 /// regular foreground process. Any service-specific (and Ice-specific) options are stripped from @p argv (just
48 /// as for ::Ice::initialize).
49 /// @param argc The number of arguments in @p argv.
50 /// @param argv The command-line arguments.
51 /// @param initData Configuration data for the new communicator.
52 /// @return The application's exit status: EXIT_FAILURE or EXIT_SUCCESS.
53 int main(int argc, const char* const argv[], InitializationData initData = {});
54
55#if defined(_WIN32) || defined(ICE_DOXYGEN)
56 /// @copydoc main(int, const char* const[], InitializationData)
57 /// @remarks Windows only.
58 int main(int argc, const wchar_t* const argv[], InitializationData initData = {});
59#endif
60
61 /// The primary entry point for services. This function examines @p args for reserved options and takes the
62 /// appropriate action. The reserved options are shown below.
63 ///
64 /// Win32:
65 ///
66 /// --service NAME
67 ///
68 /// Unix:
69 ///
70 /// --daemon [--nochdir] [--noclose]
71 ///
72 /// If --service or --daemon are specified, the program runs as a service, otherwise the program runs as a
73 /// regular foreground process. Any service-specific (and Ice-specific) options are stripped from @p args (just
74 /// as for ::Ice::initialize).
75 /// @param args The command-line arguments.
76 /// @param initData Configuration data for the new communicator.
77 /// @return The application's exit status: EXIT_FAILURE or EXIT_SUCCESS.
78 int main(const StringSeq& args, InitializationData initData = {});
79
80 /// Gets the communicator created by the service.
81 /// @return The service's communicator.
82 [[nodiscard]] Ice::CommunicatorPtr communicator() const;
83
84 /// Gets the Service singleton.
85 /// @return A pointer to this service.
86 static Service* instance();
87
88 /// Indicates whether the program is running as a Win32 service or Unix daemon.
89 /// @return `true` if the program is running as a service, `false` otherwise.
90 [[nodiscard]] bool service() const;
91
92 /// Gets the program name. If the program is running as a Win32 service, the return value is the service name.
93 /// Otherwise the return value is the executable name (i.e., argv[0]).
94 /// @return The service name.
95 [[nodiscard]] std::string name() const;
96
97 /// Handles a signal.
98 /// @remarks The default implementation calls #interrupt unless the signal is SIGHUP or CTRL_LOGOFF_EVENT and
99 /// the property `Ice.Nohup` is greater than 0 (or not set at all).
100 /// @param sig The signal that was caught.
101 virtual void handleInterrupt(int sig);
102
103#ifdef _WIN32
104 /// @private
105 static void setModuleHandle(HMODULE);
106
107 /// @private
108 void serviceMain(int, const wchar_t* const[]);
109
110 /// @private
111 void control(int);
112#endif
113
114 protected:
115 /// Prepares a service for execution, including the creation and activation of object adapters and servants.
116 /// @param argc The number of arguments in @p argv.
117 /// @param argv The command-line arguments.
118 /// @param[out] status The exit status, which is returned by #main when `start` or `stop` returns `false`.
119 /// @return `true` if startup was successful, `false` otherwise.
120 virtual bool start(int argc, char* argv[], int& status) = 0;
121
122 /// Blocks until the service shuts down. The default implementation calls Communicator::waitForShutdown.
123 virtual void waitForShutdown();
124
125 /// Cleans up resources after shutting down.
126 virtual bool stop();
127
128 /// Initializes a communicator.
129 /// @param argc The number of arguments in @p argv.
130 /// @param argv The command-line arguments.
131 /// @param initData Configuration data for the new communicator.
132 /// @return The new communicator instance.
133 virtual Ice::CommunicatorPtr initializeCommunicator(int& argc, char* argv[], InitializationData initData);
134
135 /// Logs a system error, which includes a description of the current system error code.
136 /// @param msg The log message.
137 virtual void syserror(const std::string& msg);
138
139 /// Logs an error.
140 /// @param msg The log message.
141 virtual void error(const std::string& msg);
142
143 /// Logs a warning.
144 /// @param msg The log message.
145 virtual void warning(const std::string& msg);
146
147 /// Logs trace information.
148 /// @param msg The log message.
149 virtual void trace(const std::string& msg);
150
151 /// Logs a literal message.
152 /// @param msg The log message.
153 virtual void print(const std::string& msg);
154
155 /// Makes this service call #handleInterrupt when it receives a signal.
157
158 /// Ignores signals.
160
161 /// Logger utility class for a system error.
163
164 /// Logger utility class for an error.
166
167 /// Logger utility class for a warning.
169
170 /// Logger utility class for a trace message.
172
173 /// Logger utility class for a literal message.
175
176 private:
177 Ice::LoggerPtr _logger;
178 Ice::CommunicatorPtr _communicator;
179 bool _nohup = true;
180 bool _service = false;
181 std::string _name;
182
183 static Service* _instance;
184
185 int run(int argc, const char* const argv[], InitializationData initData = {});
186
187#ifdef _WIN32
188 /// Configures the program to run as a Windows service.
189 void configureService(const std::string& name);
190
191 int runService(int, const char* const[], InitializationData);
192 void terminateService(DWORD);
193 bool waitForServiceState(SC_HANDLE, DWORD, SERVICE_STATUS&);
194 void showServiceStatus(const std::string&, SERVICE_STATUS&);
195
196 SERVICE_STATUS_HANDLE _statusHandle;
197 std::vector<std::string> _serviceArgs;
198 InitializationData _initData;
199#else
200 /// Configures the program to run as a Unix daemon.
201 /// @param changeDirectory `true` if the daemon should change its working directory to the root directory,
202 /// `false` otherwise.
203 /// @param closeFiles `true` if the daemon should close unnecessary file descriptors (i.e., stdin, stdout,
204 /// etc.), `false` otherwise.
205 /// @param pidFile If a non-empty string is provided, the daemon writes its process ID to the given file.
206 /// @remarks Linux and macOS only.
207 void configureDaemon(bool changeDirectory, bool closeFiles, const std::string& pidFile);
208
209 int runDaemon(int, char*[], InitializationData);
210
211 bool _changeDirectory = true;
212 bool _closeFiles = true;
213 std::string _pidFile;
214#endif
215 };
216
217}
218
219#endif
Collects output and flushes it via a logger method.
Definition LoggerUtil.h:89
LoggerOutput< Service, Service *, &Service::trace > ServiceTrace
Logger utility class for a trace message.
Definition Service.h:171
int main(int argc, const wchar_t *const argv[], InitializationData initData={})
The primary entry point for services.
int main(const StringSeq &args, InitializationData initData={})
The primary entry point for services.
void enableInterrupt()
Makes this service call handleInterrupt when it receives a signal.
virtual bool start(int argc, char *argv[], int &status)=0
Prepares a service for execution, including the creation and activation of object adapters and servan...
void disableInterrupt()
Ignores signals.
int main(int argc, const char *const argv[], InitializationData initData={})
The primary entry point for services.
LoggerOutput< Service, Service *, &Service::error > ServiceError
Logger utility class for an error.
Definition Service.h:165
virtual void interrupt()
Notifies the service that the program received a signal.
virtual void warning(const std::string &msg)
Logs a warning.
LoggerOutput< Service, Service *, &Service::warning > ServiceWarning
Logger utility class for a warning.
Definition Service.h:168
virtual Ice::CommunicatorPtr initializeCommunicator(int &argc, char *argv[], InitializationData initData)
Initializes a communicator.
virtual void trace(const std::string &msg)
Logs trace information.
virtual bool shutdown()
Shuts down the service. The default implementation calls Communicator::shutdown.
virtual void waitForShutdown()
Blocks until the service shuts down. The default implementation calls Communicator::waitForShutdown.
virtual void print(const std::string &msg)
Logs a literal message.
Ice::CommunicatorPtr communicator() const
Gets the communicator created by the service.
virtual void syserror(const std::string &msg)
Logs a system error, which includes a description of the current system error code.
virtual bool stop()
Cleans up resources after shutting down.
bool service() const
Indicates whether the program is running as a Win32 service or Unix daemon.
virtual void error(const std::string &msg)
Logs an error.
std::string name() const
Gets the program name.
virtual void handleInterrupt(int sig)
Handles a signal.
LoggerOutput< Service, Service *, &Service::syserror > ServiceSysError
Logger utility class for a system error.
Definition Service.h:162
Service()
Default constructor.
static Service * instance()
Gets the Service singleton.
LoggerOutput< Service, Service *, &Service::print > ServicePrint
Logger utility class for a literal message.
Definition Service.h:174
std::shared_ptr< Communicator > CommunicatorPtr
A shared pointer to a Communicator.
std::shared_ptr< Logger > LoggerPtr
A shared pointer to a Logger.
Definition Logger.h:16
std::vector< std::string > StringSeq
A sequence of strings.
The Ice RPC framework.
Definition SampleEvent.h:59
Represents a set of options that you can specify when initializing a communicator.
Definition Initialize.h:65