cmd/ctrld: always pass config file on windows start mode

On windows, the SYSTEM user is used to run ctrld service. This user has
different environment with the user that run the `ctrld` binary via CLI.
That causes the mismatch issue in config file path, log path, or more
generally, everything that involve with home directory.

To circumvent this pain, just always passing the config path and the
original home dir in start mode. So `ctrld run` command can setup things
correctly.
This commit is contained in:
Cuong Manh Le
2023-01-20 21:37:50 +07:00
committed by Cuong Manh Le
parent 53306235dc
commit 8e164185b9
2 changed files with 15 additions and 5 deletions
+11 -5
View File
@@ -144,6 +144,8 @@ func initCLI() {
runCmd.Flags().StringVarP(&logPath, "log", "", "", "path to log file") runCmd.Flags().StringVarP(&logPath, "log", "", "", "path to log file")
runCmd.Flags().IntVarP(&cacheSize, "cache_size", "", 0, "Enable cache with size items") runCmd.Flags().IntVarP(&cacheSize, "cache_size", "", 0, "Enable cache with size items")
runCmd.Flags().StringVarP(&cdUID, "cd", "", "", "Control D resolver uid") runCmd.Flags().StringVarP(&cdUID, "cd", "", "", "Control D resolver uid")
runCmd.Flags().StringVarP(&homedir, "homedir", "", "", "")
_ = runCmd.Flags().MarkHidden("homedir")
rootCmd.AddCommand(runCmd) rootCmd.AddCommand(runCmd)
@@ -169,14 +171,18 @@ func initCLI() {
defaultConfigFile = filepath.Join(dir, defaultConfigFile) defaultConfigFile = filepath.Join(dir, defaultConfigFile)
readConfigFile(true) readConfigFile(true)
} }
sc.Arguments = append(sc.Arguments, "--homedir="+dir)
}
// On Windows, the service will be run as SYSTEM, so if ctrld start as Admin, // 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 user home dir is different, so pass specific arguments that relevant here.
// the config here when "--cd" is supplied. if runtime.GOOS == "windows" {
if runtime.GOOS == "windows" && cdUID != "" { processCDFlags()
processCDFlags() if configPath == "" {
sc.Arguments = append(sc.Arguments, "--config="+defaultConfigFile)
} }
} }
s, err := service.New(&prog{}, sc) s, err := service.New(&prog{}, sc)
if err != nil { if err != nil {
stderrMsg(err.Error()) stderrMsg(err.Error())
+4
View File
@@ -22,6 +22,7 @@ var (
secondaryUpstream string secondaryUpstream string
domains []string domains []string
logPath string logPath string
homedir string
cacheSize int cacheSize int
cfg ctrld.Config cfg ctrld.Config
verbose int verbose int
@@ -44,6 +45,9 @@ func normalizeLogFilePath(logFilePath string) string {
if logFilePath == "" || filepath.IsAbs(logFilePath) || service.Interactive() { if logFilePath == "" || filepath.IsAbs(logFilePath) || service.Interactive() {
return logFilePath return logFilePath
} }
if homedir != "" {
return filepath.Join(homedir, logFilePath)
}
dir, _ := os.UserHomeDir() dir, _ := os.UserHomeDir()
if dir == "" { if dir == "" {
return logFilePath return logFilePath