Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
7b064535
Commit
7b064535
authored
Nov 14, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed SIGINT and added support for HUP, QUIT, and TERM
parent
b42334eb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
37 deletions
+78
-37
caddy/caddy.go
caddy/caddy.go
+2
-3
caddy/sigtrap.go
caddy/sigtrap.go
+28
-8
caddy/sigtrap_posix.go
caddy/sigtrap_posix.go
+48
-26
No files found.
caddy/caddy.go
View file @
7b064535
...
...
@@ -12,8 +12,7 @@
// You should use caddy.Wait() to wait for all Caddy servers
// to quit before your process exits.
//
// Importing this package has the side-effect of trapping
// SIGINT on all platforms and SIGUSR1 on not-Windows systems.
// Importing this package has the side-effect of trapping signals.
// It has to do this in order to perform shutdowns or reloads.
package
caddy
...
...
@@ -275,7 +274,7 @@ func startServers(groupings bindingGroup) error {
// Stop stops all servers. It blocks until they are all stopped.
// It does NOT execute shutdown callbacks that may have been
// configured by middleware (they
are executed on SIGINT
).
// configured by middleware (they
must be executed separately
).
func
Stop
()
error
{
letsencrypt
.
Deactivate
()
...
...
caddy/sigtrap.go
View file @
7b064535
...
...
@@ -4,30 +4,50 @@ import (
"log"
"os"
"os/signal"
"sync"
"github.com/mholt/caddy/server"
)
func
init
()
{
// Trap quit signals (cross-platform)
// Trap interrupt signal (cross-platform); triggers forceful shutdown
// that executes shutdown callbacks first. A second interrupt signal
// will exit the process immediately.
go
func
()
{
shutdown
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
shutdown
,
os
.
Interrupt
,
os
.
Kill
)
<-
shutdown
signal
.
Notify
(
shutdown
,
os
.
Interrupt
)
var
exitCode
int
for
i
:=
0
;
true
;
i
++
{
<-
shutdown
if
i
>
0
{
log
.
Println
(
"[INFO] SIGINT: Force quit"
)
os
.
Exit
(
1
)
}
log
.
Println
(
"[INFO] SIGINT: Shutting down"
)
go
os
.
Exit
(
executeShutdownCallbacks
(
"SIGINT"
))
}
}()
}
// executeShutdownCallbacks executes the shutdown callbacks as initiated
// by signame. It logs any errors and returns the recommended exit status.
// This function is idempotent; subsequent invocations always return 0.
func
executeShutdownCallbacks
(
signame
string
)
(
exitCode
int
)
{
shutdownCallbacksOnce
.
Do
(
func
()
{
serversMu
.
Lock
()
errs
:=
server
.
ShutdownCallbacks
(
servers
)
serversMu
.
Unlock
()
if
len
(
errs
)
>
0
{
for
_
,
err
:=
range
errs
{
log
.
Printf
(
"[ERROR]
Shutting down: %v"
,
err
)
log
.
Printf
(
"[ERROR]
%s shutdown: %v"
,
signame
,
err
)
}
exitCode
=
1
}
os
.
Exit
(
exitCode
)
}()
})
return
}
var
shutdownCallbacksOnce
sync
.
Once
caddy/sigtrap_posix.go
View file @
7b064535
...
...
@@ -11,40 +11,62 @@ import (
)
func
init
()
{
// Trap POSIX-only signals
// Trap
all
POSIX-only signals
go
func
()
{
reload
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
reload
,
syscall
.
SIGUSR1
)
// reload configuration
sigchan
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
sigchan
,
syscall
.
SIGTERM
,
syscall
.
SIGHUP
,
syscall
.
SIGQUIT
,
syscall
.
SIGUSR1
)
for
{
<-
reload
for
sig
:=
range
sigchan
{
switch
sig
{
case
syscall
.
SIGTERM
:
log
.
Println
(
"[INFO] SIGTERM: Terminating process"
)
os
.
Exit
(
0
)
log
.
Println
(
"[INFO] SIGUSR1: Reloading"
)
case
syscall
.
SIGQUIT
:
log
.
Println
(
"[INFO] SIGQUIT: Shutting down"
)
exitCode
:=
executeShutdownCallbacks
(
"SIGQUIT"
)
err
:=
Stop
()
if
err
!=
nil
{
log
.
Printf
(
"[ERROR] SIGQUIT stop: %v"
,
err
)
exitCode
=
1
}
os
.
Exit
(
exitCode
)
var
updatedCaddyfile
Input
case
syscall
.
SIGHUP
:
log
.
Println
(
"[INFO] SIGHUP: Hanging up"
)
err
:=
Stop
()
if
err
!=
nil
{
log
.
Printf
(
"[ERROR] SIGHUP stop: %v"
,
err
)
}
caddyfileMu
.
Lock
()
if
caddyfile
==
nil
{
// Hmm, did spawing process forget to close stdin? Anyhow, this is unusual.
log
.
Println
(
"[ERROR] SIGUSR1: no Caddyfile to reload (was stdin left open?)"
)
caddyfileMu
.
Unlock
()
continue
}
if
caddyfile
.
IsFile
()
{
body
,
err
:=
ioutil
.
ReadFile
(
caddyfile
.
Path
())
if
err
==
nil
{
updatedCaddyfile
=
CaddyfileInput
{
Filepath
:
caddyfile
.
Path
(),
Contents
:
body
,
RealFile
:
true
,
case
syscall
.
SIGUSR1
:
log
.
Println
(
"[INFO] SIGUSR1: Reloading"
)
var
updatedCaddyfile
Input
caddyfileMu
.
Lock
()
if
caddyfile
==
nil
{
// Hmm, did spawing process forget to close stdin? Anyhow, this is unusual.
log
.
Println
(
"[ERROR] SIGUSR1: no Caddyfile to reload (was stdin left open?)"
)
caddyfileMu
.
Unlock
()
continue
}
if
caddyfile
.
IsFile
()
{
body
,
err
:=
ioutil
.
ReadFile
(
caddyfile
.
Path
())
if
err
==
nil
{
updatedCaddyfile
=
CaddyfileInput
{
Filepath
:
caddyfile
.
Path
(),
Contents
:
body
,
RealFile
:
true
,
}
}
}
}
caddyfileMu
.
Unlock
()
caddyfileMu
.
Unlock
()
err
:=
Restart
(
updatedCaddyfile
)
if
err
!=
nil
{
log
.
Printf
(
"[ERROR] SIGUSR1: %v"
,
err
)
err
:=
Restart
(
updatedCaddyfile
)
if
err
!=
nil
{
log
.
Printf
(
"[ERROR] SIGUSR1: %v"
,
err
)
}
}
}
}()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment