summaryrefslogtreecommitdiffhomepage
path: root/admin.go
diff options
context:
space:
mode:
authorMatthew Holt <[email protected]>2020-05-13 11:28:15 -0600
committerMatthew Holt <[email protected]>2020-05-13 11:28:15 -0600
commit4df56c77e380cdfec5e23862f7faa8bd843022e6 (patch)
tree09fdf02b4ffc915b520a5249e91e839b0ea997e2 /admin.go
parentcee5589b9856de299ea6960d60541d851498b7c3 (diff)
downloadcaddy-4df56c77e380cdfec5e23862f7faa8bd843022e6.tar.gz
caddy-4df56c77e380cdfec5e23862f7faa8bd843022e6.zip
cmd: Add pidfile support (closes #3235)
Diffstat (limited to 'admin.go')
-rw-r--r--admin.go23
1 files changed, 16 insertions, 7 deletions
diff --git a/admin.go b/admin.go
index 1d1906673..e584a3bbd 100644
--- a/admin.go
+++ b/admin.go
@@ -21,6 +21,7 @@ import (
"expvar"
"fmt"
"io"
+ "io/ioutil"
"net"
"net/http"
"net/http/pprof"
@@ -546,13 +547,6 @@ func handleUnload(w http.ResponseWriter, r *http.Request) error {
Err: fmt.Errorf("method not allowed"),
}
}
- currentCfgMu.RLock()
- hasCfg := currentCfg != nil
- currentCfgMu.RUnlock()
- if !hasCfg {
- Log().Named("admin.api").Info("nothing to unload")
- return nil
- }
Log().Named("admin.api").Info("unloading")
if err := stopAndCleanup(); err != nil {
Log().Named("admin.api").Error("error unloading", zap.Error(err))
@@ -801,12 +795,27 @@ var (
}
)
+// PIDFile writes a pidfile to the file at filename. It
+// will get deleted before the process gracefully exits.
+func PIDFile(filename string) error {
+ pid := []byte(strconv.Itoa(os.Getpid()) + "\n")
+ err := ioutil.WriteFile(filename, pid, 0644)
+ if err != nil {
+ return err
+ }
+ pidfile = filename
+ return nil
+}
+
// idRegexp is used to match ID fields and their associated values
// in the config. It also matches adjacent commas so that syntax
// can be preserved no matter where in the object the field appears.
// It supports string and most numeric values.
var idRegexp = regexp.MustCompile(`(?m),?\s*"` + idKey + `"\s*:\s*(-?[0-9]+(\.[0-9]+)?|(?U)".*")\s*,?`)
+// pidfile is the name of the pidfile, if any.
+var pidfile string
+
const (
rawConfigKey = "config"
idKey = "@id"