Browse code

Make sure we mark the libdevmapper /dev/mapper/control fd CLOEXEC

We do a hack to mark it such, because otherwise lxc-start will not
work.

Alexander Larsson authored on 2013/10/04 04:00:16
Showing 1 changed files
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"os/exec"
11 11
 	"path"
12 12
 	"path/filepath"
13
+	"strconv"
13 14
 	"strings"
14 15
 	"syscall"
15 16
 )
... ...
@@ -564,6 +565,21 @@ func (devices *DeviceSetDM) setupBaseImage() error {
564 564
 	return nil
565 565
 }
566 566
 
567
+func setCloseOnExec(name string) {
568
+	fileInfos, _ := ioutil.ReadDir("/proc/self/fd")
569
+	if fileInfos != nil {
570
+		for _, i := range fileInfos {
571
+			link, _ := os.Readlink(filepath.Join("/proc/self/fd", i.Name()))
572
+			if link ==  name {
573
+				fd, err := strconv.Atoi(i.Name())
574
+				if err == nil {
575
+					syscall.CloseOnExec(fd)
576
+				}
577
+			}
578
+		}
579
+	}
580
+}
581
+
567 582
 func (devices *DeviceSetDM) initDevmapper() error {
568 583
 	info, err := devices.getInfo(devices.getPoolName())
569 584
 	if info == nil {
... ...
@@ -572,6 +588,11 @@ func (devices *DeviceSetDM) initDevmapper() error {
572 572
 	}
573 573
 	utils.Debugf("initDevmapper(). Pool exists: %v", info.Exists)
574 574
 
575
+	// It seems libdevmapper opens this without O_CLOEXEC, and go exec will not close files
576
+	// that are not Close-on-exec, and lxc-start will die if it inherits any unexpected files,
577
+	// so we add this badhack to make sure it closes itself
578
+	setCloseOnExec("/dev/mapper/control")
579
+
575 580
 	if info.Exists != 0 {
576 581
 		/* Pool exists, assume everything is up */
577 582
 		if err := devices.loadMetaData(); err != nil {