Skip to content

Commit

Permalink
isProcHandle: simplify usage, improve diagnostics
Browse files Browse the repository at this point in the history
Simplify isProcHandle() to only return an error, making it easier to use.

Improve errors from isProcHandle (and so from readCon/writeCon) to
provide the file name in case of fstatfs() error.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Feb 19, 2020
1 parent a843350 commit 0d4b6a2
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions go-selinux/selinux_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,17 @@ func getSELinuxPolicyRoot() string {
return filepath.Join(selinuxDir, readConfig(selinuxTypeTag))
}

func isProcHandle(fh *os.File) (bool, error) {
func isProcHandle(fh *os.File) error {
var buf unix.Statfs_t
err := unix.Fstatfs(int(fh.Fd()), &buf)
return buf.Type == unix.PROC_SUPER_MAGIC, err
if err != nil {
return fmt.Errorf("statfs(%q) failed: %v", fh.Name(), err)
}
if buf.Type != unix.PROC_SUPER_MAGIC {
return fmt.Errorf("file %q is not on procfs", fh.Name())
}

return nil
}

func readCon(fpath string) (string, error) {
Expand All @@ -272,10 +279,8 @@ func readCon(fpath string) (string, error) {
}
defer in.Close()

if ok, err := isProcHandle(in); err != nil {
if err := isProcHandle(in); err != nil {
return "", err
} else if !ok {
return "", fmt.Errorf("%s not on procfs", fpath)
}

var retval string
Expand Down Expand Up @@ -347,7 +352,7 @@ func ExecLabel() (string, error) {
return readCon(fmt.Sprintf("/proc/self/task/%d/attr/exec", syscall.Gettid()))
}

func writeCon(fpath string, val string) error {
func writeCon(fpath, val string) error {
if fpath == "" {
return ErrEmptyPath
}
Expand All @@ -363,10 +368,8 @@ func writeCon(fpath string, val string) error {
}
defer out.Close()

if ok, err := isProcHandle(out); err != nil {
if err := isProcHandle(out); err != nil {
return err
} else if !ok {
return fmt.Errorf("%s not on procfs", fpath)
}

if val != "" {
Expand Down

0 comments on commit 0d4b6a2

Please sign in to comment.