Skip to content

Commit 2bc95ec

Browse files
committed
ForwardSignals option, signal cleanup fixes
Signed-off-by: Michael Dwan <[email protected]>
1 parent 6b73a26 commit 2bc95ec

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

machine.go

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ type Config struct {
117117
// NetNS represents the path to a network namespace handle. If present, the
118118
// application will use this to join the associated network namespace
119119
NetNS string
120+
121+
// ForwardSignals is an optional list of signals to catch and forward to
122+
// firecracker. If not provided, the default signals will be used.
123+
ForwardSignals []os.Signal
120124
}
121125

122126
// Validate will ensure that the required fields are set and that
@@ -481,20 +485,7 @@ func (m *Machine) startVMM(ctx context.Context) error {
481485
close(errCh)
482486
}()
483487

484-
// Set up a signal handler and pass INT, QUIT, and TERM through to firecracker
485-
sigchan := make(chan os.Signal)
486-
signal.Notify(sigchan, os.Interrupt,
487-
syscall.SIGQUIT,
488-
syscall.SIGTERM,
489-
syscall.SIGHUP,
490-
syscall.SIGABRT)
491-
m.logger.Debugf("Setting up signal handler")
492-
go func() {
493-
if sig, ok := <-sigchan; ok {
494-
m.logger.Printf("Caught signal %s", sig)
495-
m.cmd.Process.Signal(sig)
496-
}
497-
}()
488+
m.setupSignals()
498489

499490
// Wait for firecracker to initialize:
500491
err = m.waitForSocket(time.Duration(m.client.firecrackerInitTimeout)*time.Second, errCh)
@@ -513,8 +504,6 @@ func (m *Machine) startVMM(ctx context.Context) error {
513504
m.fatalErr = err
514505
}
515506

516-
signal.Stop(sigchan)
517-
close(sigchan)
518507
close(m.exitCh)
519508
}()
520509

@@ -885,3 +874,38 @@ func (m *Machine) waitForSocket(timeout time.Duration, exitchan chan error) erro
885874
}
886875
}
887876
}
877+
878+
// Set up a signal handler to pass through to firecracker
879+
func (m *Machine) setupSignals() {
880+
signals := m.Cfg.ForwardSignals
881+
882+
if signals == nil {
883+
signals = []os.Signal{
884+
os.Interrupt,
885+
syscall.SIGQUIT,
886+
syscall.SIGTERM,
887+
syscall.SIGHUP,
888+
syscall.SIGABRT,
889+
}
890+
}
891+
892+
if len(signals) == 0 {
893+
return
894+
}
895+
896+
m.logger.Debugf("Setting up signal handler: %v", signals)
897+
sigchan := make(chan os.Signal)
898+
signal.Notify(sigchan, signals...)
899+
900+
go func() {
901+
select {
902+
case sig := <-sigchan:
903+
m.logger.Printf("Caught signal %s", sig)
904+
m.cmd.Process.Signal(sig)
905+
case <-m.exitCh:
906+
}
907+
908+
signal.Stop(sigchan)
909+
close(sigchan)
910+
}()
911+
}

0 commit comments

Comments
 (0)