@@ -60,8 +60,8 @@ constexpr uint32_t daemon_absent = ENOENT;
6060
6161#if defined(HAVE_WINDOWS)
6262
63- // Node drain exceeds the default shutdown window, so preshutdown is required.
64- // This has no effect until the service accepts SERVICE_ACCEPT_PRESHUTDOWN.
63+ // Node drain exceeds default shutdown window, so preshutdown is required. No
64+ // effect until service accepts SERVICE_ACCEPT_PRESHUTDOWN.
6565constexpr DWORD preshutdown_milliseconds = 600'000 ;
6666
6767// The manager terminates a pending start that does not progress within this.
@@ -77,10 +77,10 @@ parser* executor::service_metadata_{};
7777// Service runtime.
7878// ----------------------------------------------------------------------------
7979
80- // A null status handle implies console mode, in which this is a noop. Pending
81- // states must advance the checkpoint, as the manager otherwise assumes a hang.
82- // Reported from the manager, network, and poller threads, so holds no state
83- // apart from the atomics that the reported status is composed from.
80+ // Null status handle implies console mode, in which this is a noop. Pending
81+ // states must advance checkpoint, as manager otherwise assumes hang. Reported
82+ // from manager, network, and poller threads, so holds no state apart from
83+ // atomics reported status is composed from.
8484void executor::report_status (DWORD state) NOEXCEPT
8585{
8686 if (is_null (status_handle_))
@@ -93,19 +93,22 @@ void executor::report_status(DWORD state) NOEXCEPT
9393 const auto failed = failed_.load ();
9494 const DWORD code = failed ? ERROR_SERVICE_SPECIFIC_ERROR : NO_ERROR ;
9595 const DWORD specific = failed ? 1u : 0u ;
96+ const auto running = (state == SERVICE_RUNNING );
97+ const DWORD controls = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN ;
98+ const DWORD accepted = running ? controls : 0u ;
99+ const DWORD stop_hint = stopping ? preshutdown_milliseconds : 0u ;
100+ const DWORD hint = starting ? pending_milliseconds : stop_hint;
96101 state_.store (state);
97102
98103 SERVICE_STATUS status
99104 {
100105 .dwServiceType = SERVICE_WIN32_OWN_PROCESS ,
101106 .dwCurrentState = state,
102- .dwControlsAccepted = (state == SERVICE_RUNNING ) ?
103- (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN ) : 0u ,
107+ .dwControlsAccepted = accepted,
104108 .dwWin32ExitCode = code,
105109 .dwServiceSpecificExitCode = specific,
106110 .dwCheckPoint = (starting || stopping) ? ++checkpoint : 0u ,
107- .dwWaitHint = starting ? pending_milliseconds :
108- (stopping ? preshutdown_milliseconds : 0u )
111+ .dwWaitHint = hint
109112 };
110113
111114 ::SetServiceStatus (status_handle_, &status);
@@ -146,13 +149,13 @@ void WINAPI executor::service_main(DWORD, LPWSTR*) NOEXCEPT
146149 if (is_null (status_handle_))
147150 return ;
148151
149- // Suppresses the hidden window and console capture, which have no meaning
150- // in session zero, and are otherwise created by the executor constructor.
152+ // Suppresses hidden window and console capture, which have no meaning in
153+ // session zero, otherwise created by executor constructor.
151154 service_ = true ;
152155 report_status (SERVICE_START_PENDING );
153156
154- // The service has no console, so console output is discarded. This must
155- // outlive the executor, which retains a reference to it.
157+ // Service has no console, so console output is discarded. Must outlive
158+ // executor, which retains reference to it.
156159 static std::ostream discard{ nullptr };
157160 auto & host = executor::factory (*service_metadata_, *service_input_,
158161 discard, *service_error_);
@@ -166,9 +169,9 @@ void WINAPI executor::service_main(DWORD, LPWSTR*) NOEXCEPT
166169 report_status (SERVICE_STOPPED );
167170}
168171
169- // Grant the account the right to log on as a service, without which the
170- // service control manager cannot start the service. This is idempotent, and
171- // is not revoked on removal, as other services may rely upon it.
172+ // Grant account the right to log on as a service, without which service control
173+ // manager cannot start service. Idempotent, and not revoked on removal, as
174+ // other services may rely upon it.
172175DWORD executor::grant_logon_right (const std::string& account) NOEXCEPT
173176{
174177 using namespace system ;
@@ -265,8 +268,8 @@ uint32_t executor::create_service(const std::filesystem::path& config,
265268 return result;
266269 }
267270
268- // Delayed start defers the node until boot completes, so that its startup
269- // does not contend with that of the operating system.
271+ // Delayed start defers node until boot completes, so its startup does not
272+ // contend with that of operating system.
270273 constexpr auto text = SERVICE_CONFIG_DESCRIPTION ;
271274 constexpr auto start = SERVICE_CONFIG_DELAYED_AUTO_START_INFO ;
272275 constexpr auto info = SERVICE_CONFIG_PRESHUTDOWN_INFO ;
@@ -329,8 +332,8 @@ uint32_t executor::delete_service() NOEXCEPT
329332#if defined(HAVE_POSIX)
330333
331334#if defined(HAVE_LINUX) || defined(HAVE_APPLE)
332- // The drain exceeds the default stop timeout on both platforms, and can run
333- // to minutes, depending on dirty page volume and disk speed.
335+ // Drain exceeds default stop timeout on both platforms, and can run to minutes,
336+ // depending on dirty page volume and disk speed.
334337constexpr auto stop_timeout_seconds = 600 ;
335338#endif
336339
@@ -347,10 +350,10 @@ static std::filesystem::path enabled_path(const std::string& name) NOEXCEPT
347350 return { " /etc/systemd/system/multi-user.target.wants/" + name + " .service" };
348351}
349352
350- // Type=notify defers dependent units until the node reports ready, which is
351- // the posix counterpart to reporting SERVICE_RUNNING to the manager. Restart
352- // is not configured, as a store that fails to open does so deterministically,
353- // and retrying only obscures the fault (as does the manager on windows).
353+ // Type=notify defers dependent units until node reports ready, posix
354+ // counterpart to reporting SERVICE_RUNNING to manager. Restart not configured,
355+ // as a store that fails to open does so deterministically, and retrying only
356+ // obscures fault (as does manager on windows).
354357static std::string unit_text (const std::string&, const std::string& command,
355358 const std::string&, const std::string& account) NOEXCEPT
356359{
@@ -373,21 +376,21 @@ static std::string unit_text(const std::string&, const std::string& command,
373376
374377#elif defined(HAVE_APPLE)
375378
376- // launchd clamps a positive ExitTimeOut to 60 seconds (600 reports as 60), so
377- // this is the effective drain ceiling on osx. Do not "fix" this with zero,
378- // which launchd documents as infinite but which kills without draining.
379+ // launchd clamps positive ExitTimeOut to 60 seconds (600 reports as 60), so
380+ // this is effective drain ceiling on osx. Do not "fix" this with zero, which
381+ // launchd documents as infinite but which kills without draining.
379382constexpr auto exit_timeout_seconds = stop_timeout_seconds;
380383
381384static std::filesystem::path unit_path (const std::string&) NOEXCEPT
382385{
383386 return { " /Library/LaunchDaemons/" BS_SERVICE_LABEL " .plist" };
384387}
385388
386- // launchd has no readiness protocol, so the job is simply run at load.
387- // KeepAlive is not configured, as it restarts upon any exit, and a store that
388- // fails to open does so deterministically (as with the other two managers).
389- // launchd discards console output, and the log file is buffered, so redirect
390- // it to obtain the live view that the journal provides on linux.
389+ // launchd has no readiness protocol, so job runs at load. KeepAlive
390+ // not configured, as it restarts on any exit, and a store that fails
391+ // to open does so deterministically (as with other two managers).
392+ // launchd discards console output and log file is buffered, so
393+ // redirect it to obtain live view journal provides on linux.
391394static std::string unit_text (const std::string&, const std::string& command,
392395 const std::string& console, const std::string& account) NOEXCEPT
393396{
@@ -430,11 +433,11 @@ static std::filesystem::path enabled_path(const std::string& name) NOEXCEPT
430433// Set by the rc.d script, as rc.subr exports nothing by which to detect it.
431434constexpr auto service_variable = " BS_SERVICE" ;
432435
433- // rc.subr has no readiness protocol, so daemon(8) detaches the node, records
434- // its pid for rc.subr, and redirects console output (otherwise discarded).
435- // Restart is not configured (as with the other managers). rc.subr stops with
436- // SIGTERM and waits without limit, but rc.shutdown terminates the system after
437- // rcshutdown_timeout (90 seconds by default), which may not cover the drain.
436+ // rc.subr has no readiness protocol, so daemon(8) detaches node, records its
437+ // pid for rc.subr, and redirects console output (otherwise discarded). Restart
438+ // not configured (as with other managers). rc.subr stops with SIGTERM and waits
439+ // without limit, but rc.shutdown terminates system after rcshutdown_timeout (90
440+ // seconds by default), which may not cover drain.
438441static std::string unit_text (const std::string& name,
439442 const std::string& command, const std::string& console,
440443 const std::string& account) NOEXCEPT
@@ -470,8 +473,8 @@ static std::string unit_text(const std::string& name,
470473
471474#endif // HAVE_LINUX
472475
473- // Console redirection file, within the configured log directory. Distinct
474- // from the rotated log files, which the node writes and buffers itself.
476+ // Console redirection file, within configured log directory. Distinct from
477+ // rotated log files, which node writes and buffers itself.
475478constexpr auto console_file = " console.log" ;
476479
477480// Returns an errno value, where zero is success.
@@ -506,8 +509,8 @@ uint32_t executor::create_service(const std::filesystem::path& config,
506509 return EIO ;
507510
508511#if defined(HAVE_LINUX)
509- // This is what enabling a unit does, avoiding a systemctl invocation.
510- // A reload is required before the unit can be started without a reboot.
512+ // What enabling a unit does, avoiding a systemctl invocation. Reload is
513+ // required before unit can be started without reboot.
511514 file.close ();
512515 const auto enabled = enabled_path (name_);
513516 std::filesystem::create_directories (enabled.parent_path (), ec);
@@ -518,8 +521,8 @@ uint32_t executor::create_service(const std::filesystem::path& config,
518521 if (ec)
519522 return EACCES ;
520523#elif defined(HAVE_FREEBSD)
521- // rc.d requires an executable script, and rc.subr sources the rc.conf.d
522- // file of the same name, in which the script is enabled.
524+ // rc.d requires an executable script, and rc.subr sources rc.conf.d file of
525+ // same name, in which script is enabled.
523526 file.close ();
524527 using perms = std::filesystem::perms;
525528 std::filesystem::permissions (unit, perms::owner_all | perms::group_read |
@@ -605,9 +608,9 @@ std::string executor::command_line(const std::filesystem::path& config) NOEXCEPT
605608
606609#if defined(HAVE_LINUX)
607610
608- // Notify the service manager, as defined by sd_notify(3). The socket is unset
609- // unless the unit is Type=notify, in which case this is a noop. Implemented
610- // here to avoid a libsystemd dependency for two datagrams.
611+ // Notify service manager, as defined by sd_notify(3). Socket is unset unless
612+ // unit is Type=notify, in which case this is a noop. Implemented here to avoid
613+ // libsystemd dependency for two datagrams.
611614static void notify_manager (const std::string& state) NOEXCEPT
612615{
613616 const auto path = std::getenv (" NOTIFY_SOCKET" );
@@ -686,8 +689,8 @@ bool executor::service(parser& metadata, std::istream& input,
686689 { NULL , NULL }
687690 };
688691
689- // Blocks until the service stops, and fails immediately (without any
690- // connection attempt) when the process was not started by the manager.
692+ // Blocks until service stops, fails immediately (without any connection
693+ // attempt) when process was not started by manager.
691694 if (!is_zero (::StartServiceCtrlDispatcherW (table)))
692695 return true ;
693696
@@ -696,8 +699,8 @@ bool executor::service(parser& metadata, std::istream& input,
696699
697700#else
698701
699- // A posix daemon runs in the foreground under its manager, so there is no
700- // dispatcher to connect. This only detects that a manager is supervising.
702+ // Posix daemon runs in foreground under its manager, so there is no dispatcher
703+ // to connect. Only detects that a manager is supervising.
701704bool executor::service (parser&, std::istream&, std::ostream&)
702705{
703706#if defined(HAVE_LINUX)
0 commit comments