diff --git a/caddy/caddy.go b/caddy/caddy.go index da7fd14b..a8e12bc3 100644 --- a/caddy/caddy.go +++ b/caddy/caddy.go @@ -23,6 +23,7 @@ import ( "errors" "fmt" "io/ioutil" + "log" "net" "os" "path" @@ -44,6 +45,9 @@ var ( // HTTP2 indicates whether HTTP2 is enabled or not HTTP2 bool // TODO: temporary flag until http2 is standard + + // PidFile is the path to the pidfile to create + PidFile string ) var ( @@ -72,8 +76,8 @@ var ( // variable is not safe for concurrent access. loadedGob caddyfileGob - // startedBefore should be set to true if caddy has been - // started at least once. + // startedBefore should be set to true if caddy has been started + // at least once (does not indicate whether currently running). startedBefore bool ) @@ -99,9 +103,18 @@ const ( // In any case, an error is returned if Caddy could not be // started. func Start(cdyfile Input) (err error) { - defer func() { signalParent(err == nil) }() - - // TODO: What if already started -- is that an error? + // When we return, tell the parent whether we started + // successfully, and if so, write the pidfile (if enabled) + defer func() { + success := err == nil + signalParent(success) + if success && PidFile != "" { + err := writePidFile() + if err != nil { + log.Printf("[ERROR] Could not write pidfile: %v", err) + } + } + }() // Input must never be nil; try to load something if cdyfile == nil { diff --git a/caddy/helpers.go b/caddy/helpers.go index 8189c760..01a35fc4 100644 --- a/caddy/helpers.go +++ b/caddy/helpers.go @@ -3,6 +3,7 @@ package caddy import ( "bytes" "fmt" + "io/ioutil" "os" "os/exec" "runtime" @@ -67,6 +68,12 @@ func IsRestart() bool { return os.Getenv("CADDY_RESTART") == "true" } +// writePidFile writes the process ID to the file at PidFile, if specified. +func writePidFile() error { + pid := []byte(strconv.Itoa(os.Getpid()) + "\n") + return ioutil.WriteFile(PidFile, pid, 0644) +} + // CaddyfileInput represents a Caddyfile as input // and is simply a convenient way to implement // the Input interface. diff --git a/main.go b/main.go index aa9a2fbc..a6c37ff6 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,6 @@ var ( conf string cpu string logfile string - pidfile string revoke string version bool ) @@ -38,7 +37,7 @@ func init() { flag.StringVar(&caddy.Host, "host", caddy.DefaultHost, "Default host") flag.BoolVar(&caddy.HTTP2, "http2", true, "HTTP/2 support") // TODO: temporary flag until http2 merged into std lib flag.StringVar(&logfile, "log", "", "Process log file") - flag.StringVar(&pidfile, "pidfile", "", "Path to write pid file") + flag.StringVar(&caddy.PidFile, "pidfile", "", "Path to write pid file") flag.StringVar(&caddy.Port, "port", caddy.DefaultPort, "Default port") flag.BoolVar(&caddy.Quiet, "quiet", false, "Quiet mode (no initialization output)") flag.StringVar(&revoke, "revoke", "", "Hostname for which to revoke the certificate") @@ -68,13 +67,6 @@ func main() { log.SetOutput(file) } - if pidfile != "" { - pid := []byte(strconv.Itoa(os.Getpid()) + "\n") - err := ioutil.WriteFile(pidfile, pid, 0644) - if err != nil { - log.Fatal(err) - } - } if revoke != "" { err := letsencrypt.Revoke(revoke) if err != nil {