feat: if running as a service set workdir to /var/lib/AdGuardHome

Fixes issue where the service would write the data dir into /usr/bin if the binary was within /usr/bin
This commit is contained in:
go-compile 2024-02-18 17:09:16 +00:00
parent c0a5e389d9
commit 60177cebf2
No known key found for this signature in database
GPG Key ID: 53F4922E9D5497B8
2 changed files with 19 additions and 3 deletions

View File

@ -748,7 +748,6 @@ func writePIDFile(fn string) bool {
// initConfigFilename sets up context config file path. This file path can be
// overridden by command-line arguments, or is set to default.
func initConfigFilename(opts options) {
// TODO: if running as service the config location should be /etc/AdGuardHome/AdGuardHome.yaml
Context.configFilename = stringutil.Coalesce(opts.confFilename, "AdGuardHome.yaml")
}
@ -761,11 +760,18 @@ func initWorkingDir(opts options) (err error) {
return err
}
// TODO: if running as a service use /var/lib/AdGuardHome
if opts.workDir != "" {
// If there is a custom config file, use it's directory as our working dir
Context.workDir = opts.workDir
} else if !execDirAvaliable() {
// If running as a service and from /usr/bin/ use /var/lib for working dir instead of
// /usr/bin/data
Context.workDir = "/var/lib/AdGuardHome"
// Create dir if it does not already exist
if err := os.MkdirAll(Context.workDir, 0755); err != nil {
return err
}
} else {
Context.workDir = filepath.Dir(execPath)
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
@ -729,3 +730,12 @@ func secureBinaryUnix() error {
return nil
}
// execDirAvaliable returns true if the executable's current folder is avaliable to be
// used as a workDir.
// If AdGuardHome is running as a service, it should not use the binary's location as a
// workDir, thus this function will return false.
func execDirAvaliable() bool {
// If installed in /usr/bin do not use /usr/bin/data to store files
return filepath.Dir(os.Args[0]) != "/usr/bin"
}