[lxc-devel] [distrobuilder/master] *: Handle SIGINT and SIGKILL

monstermunchkin on Github lxc-bot at linuxcontainers.org
Fri Aug 16 18:27:25 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190816/1e442777/attachment.bin>
-------------- next part --------------
From 71f1ec4808dc255d9c5dcafcaf8df5179d9747c0 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Fri, 16 Aug 2019 20:24:33 +0200
Subject: [PATCH] *: Handle SIGINT and SIGKILL

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 distrobuilder/main.go | 21 +++++++++++++++++++++
 shared/chroot.go      | 12 ++++++++++--
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/distrobuilder/main.go b/distrobuilder/main.go
index b278e02..6141bf0 100644
--- a/distrobuilder/main.go
+++ b/distrobuilder/main.go
@@ -59,6 +59,7 @@ import (
 	"io"
 	"io/ioutil"
 	"os"
+	"os/signal"
 	"path/filepath"
 	"strings"
 	"time"
@@ -79,6 +80,7 @@ type cmdGlobal struct {
 	definition *shared.Definition
 	sourceDir  string
 	targetDir  string
+	interrupt  chan os.Signal
 }
 
 func main() {
@@ -144,9 +146,28 @@ func main() {
 		os.Exit(1)
 	}()
 
+	go func() {
+		<-globalCmd.interrupt
+
+		// exit all chroots otherwise we cannot remove the cache directory
+		for _, exit := range shared.ActiveChroots {
+			if exit != nil {
+				exit()
+			}
+		}
+
+		globalCmd.postRun(nil, nil)
+		fmt.Println("Interrupted")
+		os.Exit(1)
+	}()
+
+	globalCmd.interrupt = make(chan os.Signal, 1)
+	signal.Notify(globalCmd.interrupt, os.Interrupt, os.Kill)
+
 	// Run the main command and handle errors
 	err := app.Execute()
 	if err != nil {
+		globalCmd.postRun(nil, nil)
 		os.Exit(1)
 	}
 }
diff --git a/shared/chroot.go b/shared/chroot.go
index 3f112b9..df26e57 100644
--- a/shared/chroot.go
+++ b/shared/chroot.go
@@ -21,6 +21,8 @@ type chrootMount struct {
 	isDir  bool
 }
 
+var ActiveChroots = make(map[string]func() error)
+
 func setupMounts(rootfs string, mounts []chrootMount) error {
 	// Create a temporary mount path
 	err := os.MkdirAll(filepath.Join(rootfs, ".distrobuilder"), 0700)
@@ -256,7 +258,7 @@ exit 101
 		policyCleanup = true
 	}
 
-	return func() error {
+	exitFunc := func() error {
 		defer root.Close()
 
 		// Cleanup policy-rc.d
@@ -301,6 +303,12 @@ exit 101
 			return err
 		}
 
+		ActiveChroots[rootfs] = nil
+
 		return os.MkdirAll(devPath, 0755)
-	}, nil
+	}
+
+	ActiveChroots[rootfs] = exitFunc
+
+	return exitFunc, nil
 }


More information about the lxc-devel mailing list