# frozen_string_literal: truerequire"socket"modulePuma# The MIT License## Copyright (c) 2017-2022 Agis Anastasopoulos## Permission is hereby granted, free of charge, to any person obtaining a copy of# this software and associated documentation files (the "Software"), to deal in# the Software without restriction, including without limitation the rights to# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of# the Software, and to permit persons to whom the Software is furnished to do so,# subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.## This is a copy of https://github.com/agis/ruby-sdnotify as of commit cca575c# The only changes made was "rehoming" it within the Puma module to avoid# namespace collisions and applying standard's code formatting style.## SdNotify is a pure-Ruby implementation of sd_notify(3). It can be used to# notify systemd about state changes. Methods of this package are no-op on# non-systemd systems (eg. Darwin).## The API maps closely to the original implementation of sd_notify(3),# therefore be sure to check the official man pages prior to using SdNotify.## @see https://www.freedesktop.org/software/systemd/man/sd_notify.htmlmoduleSdNotify# Exception raised when there's an error writing to the notification socketclassNotifyError<RuntimeError;endREADY="READY=1"RELOADING="RELOADING=1"STOPPING="STOPPING=1"STATUS="STATUS="ERRNO="ERRNO="MAINPID="MAINPID="WATCHDOG="WATCHDOG=1"FDSTORE="FDSTORE=1"defself.ready(unset_env=false)notify(READY,unset_env)enddefself.reloading(unset_env=false)notify(RELOADING,unset_env)enddefself.stopping(unset_env=false)notify(STOPPING,unset_env)end# @param status [String] a custom status string that describes the current# state of the servicedefself.status(status,unset_env=false)notify("#{STATUS}#{status}",unset_env)end# @param errno [Integer]defself.errno(errno,unset_env=false)notify("#{ERRNO}#{errno}",unset_env)end# @param pid [Integer]defself.mainpid(pid,unset_env=false)notify("#{MAINPID}#{pid}",unset_env)enddefself.watchdog(unset_env=false)notify(WATCHDOG,unset_env)enddefself.fdstore(unset_env=false)notify(FDSTORE,unset_env)end# @param [Boolean] true if the service manager expects watchdog keep-alive# notification messages to be sent from this process.## If the $WATCHDOG_USEC environment variable is set,# and the $WATCHDOG_PID variable is unset or set to the PID of the current# process## @note Unlike sd_watchdog_enabled(3), this method does not mutate the# environment.defself.watchdog?wd_usec=ENV["WATCHDOG_USEC"]wd_pid=ENV["WATCHDOG_PID"]returnfalseif!wd_usecbeginwd_usec=Integer(wd_usec)rescuereturnfalseendreturnfalseifwd_usec<=0returntrueif!wd_pid||wd_pid==$$.to_sfalseend# Notify systemd with the provided state, via the notification socket, if# any.## Generally this method will be used indirectly through the other methods# of the library.## @param state [String]# @param unset_env [Boolean]## @return [Fixnum, nil] the number of bytes written to the notification# socket or nil if there was no socket to report to (eg. the program wasn't# started by systemd)## @raise [NotifyError] if there was an error communicating with the systemd# socket## @see https://www.freedesktop.org/software/systemd/man/sd_notify.htmldefself.notify(state,unset_env=false)sock=ENV["NOTIFY_SOCKET"]returnnilif!sockENV.delete("NOTIFY_SOCKET")ifunset_envbeginAddrinfo.unix(sock,:DGRAM).connectdo|s|s.close_on_exec=trues.write(state)endrescueStandardError=>eraiseNotifyError,"#{e.class}: #{e.message}",e.backtraceendendendend