/usr/include/Poco/Util/ServerApplication.h is in libpoco-dev 1.8.0.1-1ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | //
// ServerApplication.h
//
// Library: Util
// Package: Application
// Module: ServerApplication
//
// Definition of the ServerApplication class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Util_ServerApplication_INCLUDED
#define Util_ServerApplication_INCLUDED
#include "Poco/Util/Util.h"
#include "Poco/Util/Application.h"
#include "Poco/Event.h"
#if defined(POCO_OS_FAMILY_WINDOWS)
#include "Poco/NamedEvent.h"
#endif
namespace Poco {
namespace Util {
class Util_API ServerApplication: public Application
/// A subclass of the Application class that is used
/// for implementing server applications.
///
/// A ServerApplication allows for the application
/// to run as a Windows service or as a Unix daemon
/// without the need to add extra code.
///
/// For a ServerApplication to work both from the command line
/// and as a daemon or service, a few rules must be met:
/// - Subsystems must be registered in the constructor.
/// - All non-trivial initializations must be made in the
/// initialize() method.
/// - At the end of the main() method, waitForTerminationRequest()
/// should be called.
/// - New threads must only be created in initialize() or main() or
/// methods called from there, but not in the application class'
/// constructor or in the constructor of instance variables.
/// The reason for this is that fork() will be called in order to
/// create the daemon process, and threads created prior to calling
/// fork() won't be taken over to the daemon process.
/// - The main(argc, argv) function must look as follows:
///
/// int main(int argc, char** argv)
/// {
/// MyServerApplication app;
/// return app.run(argc, argv);
/// }
///
/// The POCO_SERVER_MAIN macro can be used to implement main(argc, argv).
/// If POCO has been built with POCO_WIN32_UTF8, POCO_SERVER_MAIN supports
/// Unicode command line arguments.
///
/// On Windows platforms, an application built on top of the
/// ServerApplication class can be run both from the command line
/// or as a service.
///
/// To run an application as a Windows service, it must be registered
/// with the Windows Service Control Manager (SCM). To do this, the application
/// can be started from the command line, with the /registerService option
/// specified. This causes the application to register itself with the
/// SCM, and then exit. Similarly, an application registered as a service can
/// be unregistered, by specifying the /unregisterService option.
/// The file name of the application executable (excluding the .exe suffix)
/// is used as the service name. Additionally, a more user-friendly name can be
/// specified, using the /displayName option (e.g., /displayName="Demo Service")
/// and a service description can be added with the /description option.
/// The startup mode (automatic or manual) for the service can be specified
/// with the /startup option.
///
/// An application can determine whether it is running as a service by checking
/// for the "application.runAsService" configuration property.
///
/// if (config().getBool("application.runAsService", false))
/// {
/// // do service specific things
/// }
///
/// Note that the working directory for an application running as a service
/// is the Windows system directory (e.g., C:\Windows\system32). Take this
/// into account when working with relative filesystem paths. Also, services
/// run under a different user account, so an application that works when
/// started from the command line may fail to run as a service if it depends
/// on a certain environment (e.g., the PATH environment variable).
///
/// An application registered as a Windows service can be started
/// with the NET START <name> command and stopped with the NET STOP <name>
/// command. Alternatively, the Services MMC applet can be used.
///
/// On Unix platforms, an application built on top of the ServerApplication
/// class can be optionally run as a daemon by giving the --daemon
/// command line option. A daemon, when launched, immediately
/// forks off a background process that does the actual work. After launching
/// the background process, the foreground process exits.
///
/// After the initialization is complete, but before entering the main() method,
/// the current working directory for the daemon process is changed to the root
/// directory ("/"), as it is common practice for daemon processes. Therefore, be
/// careful when working with files, as relative paths may not point to where
/// you expect them point to.
///
/// An application can determine whether it is running as a daemon by checking
/// for the "application.runAsDaemon" configuration property.
///
/// if (config().getBool("application.runAsDaemon", false))
/// {
/// // do daemon specific things
/// }
///
/// When running as a daemon, specifying the --pidfile option (e.g.,
/// --pidfile=/var/run/sample.pid) may be useful to record the process ID of
/// the daemon in a file. The PID file will be removed when the daemon process
/// terminates (but not, if it crashes).
{
public:
ServerApplication();
/// Creates the ServerApplication.
~ServerApplication();
/// Destroys the ServerApplication.
bool isInteractive() const;
/// Returns true if the application runs from the command line.
/// Returns false if the application runs as a Unix daemon
/// or Windows service.
int run(int argc, char** argv);
/// Runs the application by performing additional initializations
/// and calling the main() method.
int run(const std::vector<std::string>& args);
/// Runs the application by performing additional initializations
/// and calling the main() method.
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
int run(int argc, wchar_t** argv);
/// Runs the application by performing additional initializations
/// and calling the main() method.
///
/// This Windows-specific version of init is used for passing
/// Unicode command line arguments from wmain().
#endif
static void terminate();
/// Sends a friendly termination request to the application.
/// If the application's main thread is waiting in
/// waitForTerminationRequest(), this method will return
/// and the application can shut down.
protected:
int run();
void waitForTerminationRequest();
#if !defined(_WIN32_WCE)
void defineOptions(OptionSet& options);
#endif
private:
#if defined(POCO_VXWORKS)
static Poco::Event _terminate;
#elif defined(POCO_OS_FAMILY_UNIX)
void handleDaemon(const std::string& name, const std::string& value);
void handleUMask(const std::string& name, const std::string& value);
void handlePidFile(const std::string& name, const std::string& value);
bool isDaemon(int argc, char** argv);
void beDaemon();
#if defined(POCO_ANDROID)
static Poco::Event _terminate;
#endif
#elif defined(POCO_OS_FAMILY_WINDOWS)
#if !defined(_WIN32_WCE)
enum Action
{
SRV_RUN,
SRV_REGISTER,
SRV_UNREGISTER
};
static BOOL __stdcall ConsoleCtrlHandler(DWORD ctrlType);
static void __stdcall ServiceControlHandler(DWORD control);
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
static void __stdcall ServiceMain(DWORD argc, LPWSTR* argv);
#else
static void __stdcall ServiceMain(DWORD argc, LPTSTR* argv);
#endif
bool hasConsole();
bool isService();
void beService();
void registerService();
void unregisterService();
void handleRegisterService(const std::string& name, const std::string& value);
void handleUnregisterService(const std::string& name, const std::string& value);
void handleDisplayName(const std::string& name, const std::string& value);
void handleDescription(const std::string& name, const std::string& value);
void handleStartup(const std::string& name, const std::string& value);
Action _action;
std::string _displayName;
std::string _description;
std::string _startup;
static Poco::Event _terminated;
static SERVICE_STATUS _serviceStatus;
static SERVICE_STATUS_HANDLE _serviceStatusHandle;
#endif // _WIN32_WCE
static Poco::NamedEvent _terminate;
#endif
};
} } // namespace Poco::Util
//
// Macro to implement main()
//
#if defined(_WIN32) && defined(POCO_WIN32_UTF8)
#define POCO_SERVER_MAIN(App) \
int wmain(int argc, wchar_t** argv) \
{ \
try \
{ \
App app; \
return app.run(argc, argv); \
} \
catch (Poco::Exception& exc) \
{ \
std::cerr << exc.displayText() << std::endl; \
return Poco::Util::Application::EXIT_SOFTWARE; \
} \
}
#elif defined(POCO_VXWORKS)
#define POCO_SERVER_MAIN(App) \
int pocoSrvMain(const char* appName, ...) \
{ \
std::vector<std::string> args; \
args.push_back(std::string(appName)); \
va_list vargs; \
va_start(vargs, appName); \
const char* arg = va_arg(vargs, const char*); \
while (arg) \
{ \
args.push_back(std::string(arg)); \
arg = va_arg(vargs, const char*); \
} \
va_end(vargs); \
try \
{ \
App app; \
return app.run(args); \
} \
catch (Poco::Exception& exc) \
{ \
std::cerr << exc.displayText() << std::endl; \
return Poco::Util::Application::EXIT_SOFTWARE; \
} \
}
#else
#define POCO_SERVER_MAIN(App) \
int main(int argc, char** argv) \
{ \
try \
{ \
App app; \
return app.run(argc, argv); \
} \
catch (Poco::Exception& exc) \
{ \
std::cerr << exc.displayText() << std::endl; \
return Poco::Util::Application::EXIT_SOFTWARE; \
} \
}
#endif
#endif // Util_ServerApplication_INCLUDED
|