MCPcopy
hub / github.com/caddyserver/caddy / cmdStart

Function cmdStart

cmd/commandfuncs.go:45–170  ·  view source on GitHub ↗
(fl Flags)

Source from the content-addressed store, hash-verified

43)
44
45func 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 }

Callers

nothing calls this directly

Calls 11

listenTCPForPingbackFunction · 0.85
handlePingbackConnFunction · 0.85
AddrMethod · 0.80
PrintlnMethod · 0.80
StartMethod · 0.65
StringMethod · 0.45
BoolMethod · 0.45
CloseMethod · 0.45
ReadMethod · 0.45
WriteMethod · 0.45
AcceptMethod · 0.45

Tested by

no test coverage detected