Skip to content

Commit

Permalink
Fix FIFO handling on Solaris
Browse files Browse the repository at this point in the history
On Solaris, files need to be opened to obtain or change their
attributes. But opening a FIFO file causes the calling thread to block
until the write end is opened. Files are now opened using O_NONBLOCK to
prevent this from happening.

Should fix restic/restic#4003.

Also fix comment on stringsFromByteSlice.
  • Loading branch information
greatroar committed Nov 5, 2022
1 parent 87ba458 commit 606b554
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
44 changes: 28 additions & 16 deletions xattr_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ const (
)

func getxattr(path string, name string, data []byte) (int, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0)
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_NONBLOCK, 0)
if err != nil {
return 0, err
}
defer func() {
_ = f.Close()
_ = unix.Close(fd)
}()
return fgetxattr(f, name, data)
return fdgetxattr(fd, name, data)
}

func lgetxattr(path string, name string, data []byte) (int, error) {
return 0, unix.ENOTSUP
}

func fgetxattr(f *os.File, name string, data []byte) (int, error) {
fd, err := unix.Openat(int(f.Fd()), name, unix.O_RDONLY|unix.O_XATTR, 0)
return fdgetxattr(int(f.Fd()), name, data)
}

func fdgetxattr(fd int, name string, data []byte) (int, error) {
fd, err := unix.Openat(fd, name, unix.O_RDONLY|unix.O_XATTR, 0)
if err != nil {
return 0, err
}
Expand All @@ -50,23 +54,27 @@ func fgetxattr(f *os.File, name string, data []byte) (int, error) {
}

func setxattr(path string, name string, data []byte, flags int) error {
f, err := os.OpenFile(path, os.O_RDONLY, 0)
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_NONBLOCK, 0)
if err != nil {
return err
}
err = fsetxattr(f, name, data, flags)
err = fdsetxattr(fd, name, data, flags)
if err != nil {
_ = f.Close()
_ = unix.Close(fd)
return err
}
return f.Close()
return unix.Close(fd)
}

func lsetxattr(path string, name string, data []byte, flags int) error {
return unix.ENOTSUP
}

func fsetxattr(f *os.File, name string, data []byte, flags int) error {
return fdsetxattr(int(f.Fd()), name, data, flags)
}

func fdsetxattr(fd int, name string, data []byte, flags int) error {
mode := unix.O_WRONLY | unix.O_XATTR
if flags&XATTR_REPLACE != 0 {
mode |= unix.O_TRUNC
Expand All @@ -75,7 +83,7 @@ func fsetxattr(f *os.File, name string, data []byte, flags int) error {
} else {
mode |= unix.O_CREAT | unix.O_TRUNC
}
fd, err := unix.Openat(int(f.Fd()), name, mode, 0666)
fd, err := unix.Openat(fd, name, mode, 0666)
if err != nil {
return err
}
Expand All @@ -87,23 +95,26 @@ func fsetxattr(f *os.File, name string, data []byte, flags int) error {
}

func removexattr(path string, name string) error {
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_XATTR, 0)
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_NONBLOCK|unix.O_XATTR, 0)
if err != nil {
return err
}
f := os.NewFile(uintptr(fd), path)
defer func() {
_ = f.Close()
_ = unix.Close(fd)
}()
return fremovexattr(f, name)
return fdremovexattr(fd, name)
}

func lremovexattr(path string, name string) error {
return unix.ENOTSUP
}

func fremovexattr(f *os.File, name string) error {
fd, err := unix.Openat(int(f.Fd()), ".", unix.O_XATTR, 0)
return fdremovexattr(int(f.Fd()), name)
}

func fdremovexattr(fd int, name string) error {
fd, err := unix.Openat(fd, ".", unix.O_XATTR, 0)
if err != nil {
return err
}
Expand All @@ -114,10 +125,11 @@ func fremovexattr(f *os.File, name string) error {
}

func listxattr(path string, data []byte) (int, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0)
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_NONBLOCK, 0)
if err != nil {
return 0, err
}
f := os.NewFile(uintptr(fd), path)
defer func() {
_ = f.Close()
}()
Expand Down Expand Up @@ -152,7 +164,7 @@ func flistxattr(f *os.File, data []byte) (int, error) {
}

// stringsFromByteSlice converts a sequence of attributes to a []string.
// On Darwin and Linux, each entry is a NULL-terminated string.
// We simulate Linux/Darwin, where each entry is a NULL-terminated string.
func stringsFromByteSlice(buf []byte) (result []string) {
offset := 0
for index, b := range buf {
Expand Down
27 changes: 27 additions & 0 deletions xattr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"syscall"
"testing"

"golang.org/x/sys/unix"
)

const UserPrefix = "user."
Expand Down Expand Up @@ -260,6 +263,30 @@ func TestLargeVal(t *testing.T) {
}
}

// Get should work on a FIFO that is not opened by any other process.
// This is mainly relevant on Solaris, where getting the attributes
// requires opening the file and doing that the wrong way blocks the caller.
func TestFIFO(t *testing.T) {
d, err := ioutil.TempDir("", "pkg-xattr-testfifo-*")
if err != nil {
t.Fatal(err)
}
t.Log("tempdir:", d)
defer os.RemoveAll(d)

fifo := filepath.Join(d, "fifo")
err = unix.Mkfifo(fifo, 0777)
if err != nil {
t.Fatal(err)
}

// We only care about this not blocking.
_ = Set(fifo, "foo")
_, _ = Get(fifo, "foo")
_, _ = List(fifo)
_ = Remove(fifo, "foo")
}

// checkIfError calls t.Skip() if the underlying syscall.Errno is
// ENOTSUP or EOPNOTSUPP. It calls t.Fatal() on any other non-zero error.
func checkIfError(t *testing.T, err error) {
Expand Down

0 comments on commit 606b554

Please sign in to comment.