diff --git a/internal/home/home.go b/internal/home/home.go index adaeb635..aa781f7b 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -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) } diff --git a/internal/home/service.go b/internal/home/service.go index 13248373..2f3f2a02 100644 --- a/internal/home/service.go +++ b/internal/home/service.go @@ -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" +}