diff --git a/cmd/ctrld/cli.go b/cmd/ctrld/cli.go index 6549f39..b84cc06 100644 --- a/cmd/ctrld/cli.go +++ b/cmd/ctrld/cli.go @@ -144,6 +144,8 @@ func initCLI() { runCmd.Flags().StringVarP(&logPath, "log", "", "", "path to log file") runCmd.Flags().IntVarP(&cacheSize, "cache_size", "", 0, "Enable cache with size items") runCmd.Flags().StringVarP(&cdUID, "cd", "", "", "Control D resolver uid") + runCmd.Flags().StringVarP(&homedir, "homedir", "", "", "") + _ = runCmd.Flags().MarkHidden("homedir") rootCmd.AddCommand(runCmd) @@ -169,14 +171,18 @@ func initCLI() { defaultConfigFile = filepath.Join(dir, defaultConfigFile) readConfigFile(true) } + sc.Arguments = append(sc.Arguments, "--homedir="+dir) + } - // On Windows, the service will be run as SYSTEM, so if ctrld start as Admin, - // the written config won't be writable by SYSTEM account, we have to update - // the config here when "--cd" is supplied. - if runtime.GOOS == "windows" && cdUID != "" { - processCDFlags() + // On Windows, the service will be run as SYSTEM, so if ctrld start as Admin, + // the user home dir is different, so pass specific arguments that relevant here. + if runtime.GOOS == "windows" { + processCDFlags() + if configPath == "" { + sc.Arguments = append(sc.Arguments, "--config="+defaultConfigFile) } } + s, err := service.New(&prog{}, sc) if err != nil { stderrMsg(err.Error()) diff --git a/cmd/ctrld/main.go b/cmd/ctrld/main.go index f82ee2f..5185e28 100644 --- a/cmd/ctrld/main.go +++ b/cmd/ctrld/main.go @@ -22,6 +22,7 @@ var ( secondaryUpstream string domains []string logPath string + homedir string cacheSize int cfg ctrld.Config verbose int @@ -44,6 +45,9 @@ func normalizeLogFilePath(logFilePath string) string { if logFilePath == "" || filepath.IsAbs(logFilePath) || service.Interactive() { return logFilePath } + if homedir != "" { + return filepath.Join(homedir, logFilePath) + } dir, _ := os.UserHomeDir() if dir == "" { return logFilePath