Selinux: reduce redundant parsing of mountinfo

Avoid parsing the whole lines of mountinfo after the mountpoint
is found.

Signed-off-by: Tatsushi Inagaki <e29253@jp.ibm.com>
This commit is contained in:
Tatsushi Inagaki 2016-02-29 16:51:01 +09:00
parent 2a1a6cdf44
commit 78e1a4fc2e
1 changed files with 20 additions and 6 deletions

View File

@ -16,7 +16,6 @@ import (
"sync" "sync"
"syscall" "syscall"
"github.com/docker/docker/pkg/mount"
"github.com/opencontainers/runc/libcontainer/system" "github.com/opencontainers/runc/libcontainer/system"
) )
@ -60,16 +59,31 @@ func getSelinuxMountPoint() string {
} }
selinuxfs = "" selinuxfs = ""
mounts, err := mount.GetMounts() f, err := os.Open("/proc/self/mountinfo")
if err != nil { if err != nil {
return selinuxfs return selinuxfs
} }
for _, mount := range mounts { defer f.Close()
if mount.Fstype == "selinuxfs" {
selinuxfs = mount.Mountpoint scanner := bufio.NewScanner(f)
break for scanner.Scan() {
txt := scanner.Text()
// Safe as mountinfo encodes mountpoints with spaces as \040.
sepIdx := strings.Index(txt, " - ")
if sepIdx == -1 {
continue
} }
if !strings.Contains(txt[sepIdx:], "selinuxfs") {
continue
}
fields := strings.Split(txt, " ")
if len(fields) < 5 {
continue
}
selinuxfs = fields[4]
break
} }
if selinuxfs != "" { if selinuxfs != "" {
var buf syscall.Statfs_t var buf syscall.Statfs_t
syscall.Statfs(selinuxfs, &buf) syscall.Statfs(selinuxfs, &buf)