(fl Flags)
| 43 | ) |
| 44 | |
| 45 | func cmdStart(fl Flags) (int, error) { |
| 46 | configFlag := fl.String("config") |
| 47 | configAdapterFlag := fl.String("adapter") |
| 48 | pidfileFlag := fl.String("pidfile") |
| 49 | watchFlag := fl.Bool("watch") |
| 50 | |
| 51 | var err error |
| 52 | var envfileFlag []string |
| 53 | envfileFlag, err = fl.GetStringSlice("envfile") |
| 54 | if err != nil { |
| 55 | return caddy.ExitCodeFailedStartup, |
| 56 | fmt.Errorf("reading envfile flag: %v", err) |
| 57 | } |
| 58 | |
| 59 | // open a listener to which the child process will connect when |
| 60 | // it is ready to confirm that it has successfully started |
| 61 | ln, err := listenTCPForPingback(net.Listen) |
| 62 | if err != nil { |
| 63 | return caddy.ExitCodeFailedStartup, |
| 64 | fmt.Errorf("opening listener for success confirmation: %v", err) |
| 65 | } |
| 66 | defer ln.Close() |
| 67 | |
| 68 | // craft the command with a pingback address and with a |
| 69 | // pipe for its stdin, so we can tell it our confirmation |
| 70 | // code that we expect so that some random port scan at |
| 71 | // the most unfortunate time won't fool us into thinking |
| 72 | // the child succeeded (i.e. the alternative is to just |
| 73 | // wait for any connection on our listener, but better to |
| 74 | // ensure it's the process we're expecting - we can be |
| 75 | // sure by giving it some random bytes and having it echo |
| 76 | // them back to us) |
| 77 | cmd := exec.Command(os.Args[0], "run", "--pingback", ln.Addr().String()) //nolint:gosec // no command injection that I can determine... |
| 78 | // we should be able to run caddy in relative paths |
| 79 | if errors.Is(cmd.Err, exec.ErrDot) { |
| 80 | cmd.Err = nil |
| 81 | } |
| 82 | if configFlag != "" { |
| 83 | cmd.Args = append(cmd.Args, "--config", configFlag) |
| 84 | } |
| 85 | |
| 86 | for _, envfile := range envfileFlag { |
| 87 | cmd.Args = append(cmd.Args, "--envfile", envfile) |
| 88 | } |
| 89 | if configAdapterFlag != "" { |
| 90 | cmd.Args = append(cmd.Args, "--adapter", configAdapterFlag) |
| 91 | } |
| 92 | if watchFlag { |
| 93 | cmd.Args = append(cmd.Args, "--watch") |
| 94 | } |
| 95 | if pidfileFlag != "" { |
| 96 | cmd.Args = append(cmd.Args, "--pidfile", pidfileFlag) |
| 97 | } |
| 98 | stdinPipe, err := cmd.StdinPipe() |
| 99 | if err != nil { |
| 100 | return caddy.ExitCodeFailedStartup, |
| 101 | fmt.Errorf("creating stdin pipe: %v", err) |
| 102 | } |
nothing calls this directly
no test coverage detected