cmd: Eliminate unintended use of cgo

This means the stop command can only use the API to stop the instance;
no more signaling, unless we find a cgo-free way of doing it.
This commit is contained in:
Matthew Holt 2019-12-23 12:41:05 -07:00
parent 724c728678
commit cbb405f6aa
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
4 changed files with 5 additions and 36 deletions

View file

@ -33,7 +33,6 @@ import (
"github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig" "github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/keybase/go-ps"
"github.com/mholt/certmagic" "github.com/mholt/certmagic"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -211,34 +210,11 @@ func cmdStop(fl Flags) (int, error) {
err = apiRequest(req) err = apiRequest(req)
if err != nil { if err != nil {
// if the caddy instance doesn't have an API listener set up, caddy.Log().Warn("failed using API to stop instance",
// or we are unable to reach it for some reason, try signaling it
caddy.Log().Warn("unable to use API to stop instance; will try to signal the process",
zap.String("endpoint", stopEndpoint), zap.String("endpoint", stopEndpoint),
zap.Error(err), zap.Error(err),
) )
return caddy.ExitCodeFailedStartup, err
processList, err := ps.Processes()
if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("listing processes: %v", err)
}
thisProcName := getProcessName()
var found bool
for _, p := range processList {
// the process we're looking for should have the same name as us but different PID
if p.Executable() == thisProcName && p.Pid() != os.Getpid() {
found = true
fmt.Printf("pid=%d\n", p.Pid())
if err := gracefullyStopProcess(p.Pid()); err != nil {
return caddy.ExitCodeFailedStartup, err
}
}
}
if !found {
return caddy.ExitCodeFailedStartup, fmt.Errorf("Caddy is not running")
}
} }
return caddy.ExitCodeSuccess, nil return caddy.ExitCodeSuccess, nil

View file

@ -134,13 +134,9 @@ not quit after printing, and can be useful for troubleshooting.`,
Long: ` Long: `
Stops the background Caddy process as gracefully as possible. Stops the background Caddy process as gracefully as possible.
It will first try to use the admin API's /stop endpoint; the address of It requires that the admin API is enabled and accessible, since it will
this request can be customized using the --address flag if it is not the use the API's /stop endpoint. The address of this request can be
default. customized using the --address flag if it is not the default.`,
If that fails for any reason, it will attempt to signal the first process
it can find named the same as this one (os.Args[0]). On Windows, such
a stop is forceful because Windows does not have signals.`,
Flags: func() *flag.FlagSet { Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("stop", flag.ExitOnError) fs := flag.NewFlagSet("stop", flag.ExitOnError)
fs.String("address", "", "The address to use to reach the admin API endpoint, if not the default") fs.String("address", "", "The address to use to reach the admin API endpoint, if not the default")

1
go.mod
View file

@ -12,7 +12,6 @@ require (
github.com/ilibs/json5 v1.0.1 github.com/ilibs/json5 v1.0.1
github.com/imdario/mergo v0.3.8 // indirect github.com/imdario/mergo v0.3.8 // indirect
github.com/jsternberg/zap-logfmt v1.2.0 github.com/jsternberg/zap-logfmt v1.2.0
github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19
github.com/klauspost/compress v1.8.6 github.com/klauspost/compress v1.8.6
github.com/klauspost/cpuid v1.2.2 github.com/klauspost/cpuid v1.2.2
github.com/lucas-clemente/quic-go v0.14.1 github.com/lucas-clemente/quic-go v0.14.1

2
go.sum
View file

@ -143,8 +143,6 @@ github.com/jsternberg/zap-logfmt v1.2.0 h1:1v+PK4/B48cy8cfQbxL4FmmNZrjnIMr2BsnyE
github.com/jsternberg/zap-logfmt v1.2.0/go.mod h1:kz+1CUmCutPWABnNkOu9hOHKdT2q3TDYCcsFy9hpqb0= github.com/jsternberg/zap-logfmt v1.2.0/go.mod h1:kz+1CUmCutPWABnNkOu9hOHKdT2q3TDYCcsFy9hpqb0=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19 h1:WjT3fLi9n8YWh/Ih8Q1LHAPsTqGddPcHqscN+PJ3i68=
github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19/go.mod h1:hY+WOq6m2FpbvyrI93sMaypsttvaIL5nhVR92dTMUcQ=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.8.6 h1:970MQcQdxX7hfgc/aqmB4a3grW0ivUVV6i1TLkP8CiE= github.com/klauspost/compress v1.8.6 h1:970MQcQdxX7hfgc/aqmB4a3grW0ivUVV6i1TLkP8CiE=
github.com/klauspost/compress v1.8.6/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/compress v1.8.6/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=