Pull request: 3457 fix service reload

Merge in DNS/adguard-home from 3457-darwin-svc-reload to master

Closes #3457.

Squashed commit of the following:

commit e3d6fbccf8373194360b6480e2d702ccd0ec7107
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Aug 18 00:52:39 2021 +0300

    aghos: imp docs

commit 220d37ebc1e0c2e9ba37a34650bff1480bd2fcf6
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Tue Aug 17 15:45:52 2021 +0300

    all: fix ps
This commit is contained in:
Eugene Burkov
2021-08-18 13:20:56 +03:00
parent d2c9052dde
commit 550b1798a1
9 changed files with 220 additions and 53 deletions

View File

@@ -97,41 +97,48 @@ func sendSigReload() {
}
pidfile := fmt.Sprintf("/var/run/%s.pid", serviceName)
var pid int
data, err := os.ReadFile(pidfile)
if errors.Is(err, os.ErrNotExist) {
var code int
var psdata string
code, psdata, err = aghos.RunCommand("ps", "-C", serviceName, "-o", "pid=")
if err != nil || code != 0 {
log.Error("finding AdGuardHome process: code: %d, error: %s", code, err)
if pid, err = aghos.PIDByCommand(serviceName, os.Getpid()); err != nil {
log.Error("finding AdGuardHome process: %s", err)
return
}
data = []byte(psdata)
} else if err != nil {
log.Error("reading pid file %s: %s", pidfile, err)
return
} else {
parts := strings.SplitN(string(data), "\n", 2)
if len(parts) == 0 {
log.Error("can't read pid file %s: bad value", pidfile)
return
}
if pid, err = strconv.Atoi(strings.TrimSpace(parts[0])); err != nil {
log.Error("can't read pid file %s: %s", pidfile, err)
return
}
}
parts := strings.SplitN(string(data), "\n", 2)
if len(parts) == 0 {
log.Error("Can't read PID file %s: bad value", pidfile)
var proc *os.Process
if proc, err = os.FindProcess(pid); err != nil {
log.Error("can't send signal to pid %d: %s", pid, err)
return
}
pid, err := strconv.Atoi(strings.TrimSpace(parts[0]))
if err != nil {
log.Error("Can't read PID file %s: %s", pidfile, err)
if err = proc.Signal(syscall.SIGHUP); err != nil {
log.Error("Can't send signal to pid %d: %s", pid, err)
return
}
err = aghos.SendProcessSignal(pid, syscall.SIGHUP)
if err != nil {
log.Error("Can't send signal to PID %d: %s", pid, err)
return
}
log.Debug("Sent signal to PID %d", pid)
log.Debug("sent signal to PID %d", pid)
}
// handleServiceControlAction one of the possible control actions:
@@ -541,6 +548,6 @@ name="{{.Name}}"
{{.Name}}_user="root"
pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
command_args="-P ${pidfile} -f -r {{.WorkingDirectory}}/{{.Name}}"
command_args="-p ${pidfile} -f -r {{.WorkingDirectory}}/{{.Name}}"
run_rc_command "$1"
`