|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package ossvc |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/AdguardTeam/AdGuardHome/internal/aghos" |
| 7 | + "github.com/kardianos/service" |
| 8 | +) |
| 9 | + |
| 10 | +// configureServiceOptions defines additional settings of the service |
| 11 | +// configuration on Linux. conf must not be nil. |
| 12 | +func configureOSOptions(conf *service.Config) { |
| 13 | + conf.Option["LogOutput"] = true |
| 14 | + conf.Option["Restart"] = "always" |
| 15 | + |
| 16 | + conf.Dependencies = []string{ |
| 17 | + "After=syslog.target network-online.target", |
| 18 | + } |
| 19 | + |
| 20 | + conf.Option["SystemdScript"] = systemdScript |
| 21 | + |
| 22 | + // Use different scripts on OpenWrt. |
| 23 | + if aghos.IsOpenWrt() { |
| 24 | + conf.Option["SysvScript"] = openWrtScript |
| 25 | + } else { |
| 26 | + conf.Option["SysvScript"] = sysvScript |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// systemdScript is an improved version of the systemd script originally from |
| 31 | +// the systemdScript constant in file service_systemd_linux.go in module |
| 32 | +// github.com/kardianos/service. The following changes have been made: |
| 33 | +// |
| 34 | +// 1. The RestartSec setting is set to a lower value of 10 to make sure we |
| 35 | +// always restart quickly. |
| 36 | +// |
| 37 | +// 2. The StandardOutput and StandardError settings are set to redirect the |
| 38 | +// output to the systemd journal, see |
| 39 | +// https://man7.org/linux/man-pages/man5/systemd.exec.5.html#LOGGING_AND_STANDARD_INPUT/OUTPUT. |
| 40 | +// |
| 41 | +//lint:ignore U1000 TODO(e.burkov): Use. |
| 42 | +const systemdScript = `[Unit] |
| 43 | +Description={{.Description}} |
| 44 | +ConditionFileIsExecutable={{.Path|cmdEscape}} |
| 45 | +{{range $i, $dep := .Dependencies}} |
| 46 | +{{$dep}} {{end}} |
| 47 | +
|
| 48 | +[Service] |
| 49 | +StartLimitInterval=5 |
| 50 | +StartLimitBurst=10 |
| 51 | +ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmd}}{{end}} |
| 52 | +{{if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{end}} |
| 53 | +{{if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{end}} |
| 54 | +{{if .UserName}}User={{.UserName}}{{end}} |
| 55 | +{{if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{end}} |
| 56 | +{{if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{end}} |
| 57 | +{{if and .LogOutput .HasOutputFileSupport -}} |
| 58 | +StandardOutput=journal |
| 59 | +StandardError=journal |
| 60 | +{{- end}} |
| 61 | +{{if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{end}} |
| 62 | +{{if .Restart}}Restart={{.Restart}}{{end}} |
| 63 | +{{if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{end}} |
| 64 | +RestartSec=10 |
| 65 | +EnvironmentFile=-/etc/sysconfig/{{.Name}} |
| 66 | +
|
| 67 | +[Install] |
| 68 | +WantedBy=multi-user.target |
| 69 | +` |
| 70 | + |
| 71 | +// sysvScript is the source of the daemon script for SysV-based Linux systems. |
| 72 | +// Keep as close as possible to the https://github.com/kardianos/service/blob/29f8c79c511bc18422bb99992779f96e6bc33921/service_sysv_linux.go#L187. |
| 73 | +// |
| 74 | +// Use ps command instead of reading the procfs since it's a more |
| 75 | +// implementation-independent approach. |
| 76 | +// |
| 77 | +//lint:ignore U1000 TODO(e.burkov): Use. |
| 78 | +const sysvScript = `#!/bin/sh |
| 79 | +# For RedHat and cousins: |
| 80 | +# chkconfig: - 99 01 |
| 81 | +# description: {{.Description}} |
| 82 | +# processname: {{.Path}} |
| 83 | +
|
| 84 | +### BEGIN INIT INFO |
| 85 | +# Provides: {{.Path}} |
| 86 | +# Required-Start: |
| 87 | +# Required-Stop: |
| 88 | +# Default-Start: 2 3 4 5 |
| 89 | +# Default-Stop: 0 1 6 |
| 90 | +# Short-Description: {{.DisplayName}} |
| 91 | +# Description: {{.Description}} |
| 92 | +### END INIT INFO |
| 93 | +
|
| 94 | +cmd="{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}" |
| 95 | +
|
| 96 | +name=$(basename $(readlink -f $0)) |
| 97 | +pid_file="/var/run/$name.pid" |
| 98 | +stdout_log="/var/log/$name.log" |
| 99 | +stderr_log="/var/log/$name.err" |
| 100 | +
|
| 101 | +[ -e /etc/sysconfig/$name ] && . /etc/sysconfig/$name |
| 102 | +
|
| 103 | +get_pid() { |
| 104 | + cat "$pid_file" |
| 105 | +} |
| 106 | +
|
| 107 | +is_running() { |
| 108 | + [ -f "$pid_file" ] && ps -p "$(get_pid)" > /dev/null 2>&1 |
| 109 | +} |
| 110 | +
|
| 111 | +case "$1" in |
| 112 | + start) |
| 113 | + if is_running; then |
| 114 | + echo "Already started" |
| 115 | + else |
| 116 | + echo "Starting $name" |
| 117 | + {{if .WorkingDirectory}}cd '{{.WorkingDirectory}}'{{end}} |
| 118 | + $cmd >> "$stdout_log" 2>> "$stderr_log" & |
| 119 | + echo $! > "$pid_file" |
| 120 | + if ! is_running; then |
| 121 | + echo "Unable to start, see $stdout_log and $stderr_log" |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | + fi |
| 125 | + ;; |
| 126 | + stop) |
| 127 | + if is_running; then |
| 128 | + echo -n "Stopping $name.." |
| 129 | + kill $(get_pid) |
| 130 | + for i in $(seq 1 10) |
| 131 | + do |
| 132 | + if ! is_running; then |
| 133 | + break |
| 134 | + fi |
| 135 | + echo -n "." |
| 136 | + sleep 1 |
| 137 | + done |
| 138 | + echo |
| 139 | + if is_running; then |
| 140 | + echo "Not stopped; may still be shutting down or shutdown may have failed" |
| 141 | + exit 1 |
| 142 | + else |
| 143 | + echo "Stopped" |
| 144 | + if [ -f "$pid_file" ]; then |
| 145 | + rm "$pid_file" |
| 146 | + fi |
| 147 | + fi |
| 148 | + else |
| 149 | + echo "Not running" |
| 150 | + fi |
| 151 | + ;; |
| 152 | + restart) |
| 153 | + $0 stop |
| 154 | + if is_running; then |
| 155 | + echo "Unable to stop, will not attempt to start" |
| 156 | + exit 1 |
| 157 | + fi |
| 158 | + $0 start |
| 159 | + ;; |
| 160 | + status) |
| 161 | + if is_running; then |
| 162 | + echo "Running" |
| 163 | + else |
| 164 | + echo "Stopped" |
| 165 | + exit 1 |
| 166 | + fi |
| 167 | + ;; |
| 168 | + *) |
| 169 | + echo "Usage: $0 {start|stop|restart|status}" |
| 170 | + exit 1 |
| 171 | + ;; |
| 172 | +esac |
| 173 | +exit 0 |
| 174 | +` |
| 175 | + |
| 176 | +// OpenWrt procd init script |
| 177 | +// https://github.com/AdguardTeam/AdGuardHome/internal/issues/1386 |
| 178 | +// |
| 179 | +//lint:ignore U1000 TODO(e.burkov): Use. |
| 180 | +const openWrtScript = `#!/bin/sh /etc/rc.common |
| 181 | +
|
| 182 | +USE_PROCD=1 |
| 183 | +
|
| 184 | +START=95 |
| 185 | +STOP=01 |
| 186 | +
|
| 187 | +cmd="{{.Path}}{{range .Arguments}} {{.|cmd}}{{end}}" |
| 188 | +name="{{.Name}}" |
| 189 | +pid_file="/var/run/${name}.pid" |
| 190 | +
|
| 191 | +start_service() { |
| 192 | + echo "Starting ${name}" |
| 193 | +
|
| 194 | + procd_open_instance |
| 195 | + procd_set_param command ${cmd} |
| 196 | + procd_set_param respawn # respawn automatically if something died |
| 197 | + procd_set_param stdout 1 # forward stdout of the command to logd |
| 198 | + procd_set_param stderr 1 # same for stderr |
| 199 | + procd_set_param pidfile ${pid_file} # write a pid file on instance start and remove it on stop |
| 200 | +
|
| 201 | + procd_close_instance |
| 202 | + echo "${name} has been started" |
| 203 | +} |
| 204 | +
|
| 205 | +stop_service() { |
| 206 | + echo "Stopping ${name}" |
| 207 | +} |
| 208 | +
|
| 209 | +EXTRA_COMMANDS="status" |
| 210 | +EXTRA_HELP=" status Print the service status" |
| 211 | +
|
| 212 | +get_pid() { |
| 213 | + cat "${pid_file}" |
| 214 | +} |
| 215 | +
|
| 216 | +is_running() { |
| 217 | + [ -f "${pid_file}" ] && ps | grep -v grep | grep $(get_pid) >/dev/null 2>&1 |
| 218 | +} |
| 219 | +
|
| 220 | +status() { |
| 221 | + if is_running; then |
| 222 | + echo "Running" |
| 223 | + else |
| 224 | + echo "Stopped" |
| 225 | + exit 1 |
| 226 | + fi |
| 227 | +} |
| 228 | +` |
0 commit comments