2016-07-03 17:51:40 +02:00
|
|
|
package syscallcompat
|
2016-01-07 21:39:41 +01:00
|
|
|
|
2016-07-03 19:14:12 +02:00
|
|
|
import (
|
2017-06-18 15:40:38 +02:00
|
|
|
"log"
|
2019-01-13 18:12:00 +01:00
|
|
|
"runtime"
|
2016-07-03 19:14:12 +02:00
|
|
|
"syscall"
|
2017-12-03 17:53:14 +01:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
2016-07-03 19:14:12 +02:00
|
|
|
)
|
|
|
|
|
2018-09-08 18:06:33 +02:00
|
|
|
const (
|
|
|
|
// O_DIRECT means oncached I/O on Linux. No direct equivalent on MacOS and defined
|
|
|
|
// to zero there.
|
|
|
|
O_DIRECT = 0
|
|
|
|
|
|
|
|
// O_PATH is only defined on Linux
|
|
|
|
O_PATH = 0
|
2019-01-13 18:12:00 +01:00
|
|
|
|
|
|
|
// KAUTH_UID_NONE and KAUTH_GID_NONE are special values to
|
|
|
|
// revert permissions to the process credentials.
|
|
|
|
KAUTH_UID_NONE = ^uint32(0) - 100
|
|
|
|
KAUTH_GID_NONE = ^uint32(0) - 100
|
2018-09-08 18:06:33 +02:00
|
|
|
)
|
2018-07-04 09:04:00 +02:00
|
|
|
|
2019-01-13 18:12:00 +01:00
|
|
|
// Unfortunately pthread_setugid_np does not have a syscall wrapper yet.
|
|
|
|
func pthread_setugid_np(uid uint32, gid uint32) (err error) {
|
|
|
|
_, _, e1 := syscall.RawSyscall(syscall.SYS_SETTID, uintptr(uid), uintptr(gid), 0)
|
|
|
|
if e1 != 0 {
|
|
|
|
err = e1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:05:32 +02:00
|
|
|
// Sorry, fallocate is not available on OSX at all and
|
|
|
|
// fcntl F_PREALLOCATE is not accessible from Go.
|
|
|
|
// See https://github.com/rfjakob/gocryptfs/issues/18 if you want to help.
|
2017-02-16 19:23:17 +01:00
|
|
|
func EnospcPrealloc(fd int, off int64, len int64) error {
|
2016-01-09 13:04:48 +01:00
|
|
|
return nil
|
2016-01-07 21:39:41 +01:00
|
|
|
}
|
2016-07-03 19:14:12 +02:00
|
|
|
|
2016-07-03 20:05:32 +02:00
|
|
|
// See above.
|
2017-02-16 19:23:17 +01:00
|
|
|
func Fallocate(fd int, mode uint32, off int64, len int64) error {
|
2016-07-03 20:05:32 +02:00
|
|
|
return syscall.EOPNOTSUPP
|
|
|
|
}
|
2016-07-03 19:14:12 +02:00
|
|
|
|
2017-11-30 17:07:55 +01:00
|
|
|
// Dup3 is not available on Darwin, so we use Dup2 instead.
|
|
|
|
func Dup3(oldfd int, newfd int, flags int) (err error) {
|
|
|
|
if flags != 0 {
|
|
|
|
log.Panic("darwin does not support dup3 flags")
|
2016-07-03 20:05:32 +02:00
|
|
|
}
|
2017-11-30 17:07:55 +01:00
|
|
|
return syscall.Dup2(oldfd, newfd)
|
2016-07-03 20:05:32 +02:00
|
|
|
}
|
2016-07-03 19:14:12 +02:00
|
|
|
|
2017-11-30 17:07:55 +01:00
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//// Emulated Syscalls (see emulate.go) ////////////////
|
|
|
|
////////////////////////////////////////////////////////
|
2016-07-03 19:14:12 +02:00
|
|
|
|
2019-01-12 20:42:05 +01:00
|
|
|
func OpenatUser(dirfd int, path string, flags int, mode uint32, context *fuse.Context) (fd int, err error) {
|
2019-01-13 18:12:00 +01:00
|
|
|
if context != nil {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
|
|
|
}
|
2019-01-13 14:05:03 +01:00
|
|
|
|
2019-01-12 20:42:05 +01:00
|
|
|
return Openat(dirfd, path, flags, mode)
|
|
|
|
}
|
|
|
|
|
2017-11-30 17:07:55 +01:00
|
|
|
func Mknodat(dirfd int, path string, mode uint32, dev int) (err error) {
|
|
|
|
return emulateMknodat(dirfd, path, mode, dev)
|
2017-06-18 15:40:38 +02:00
|
|
|
}
|
2017-11-26 21:59:24 +01:00
|
|
|
|
2019-01-12 21:19:23 +01:00
|
|
|
func MknodatUser(dirfd int, path string, mode uint32, dev int, context *fuse.Context) (err error) {
|
2019-01-13 18:12:00 +01:00
|
|
|
if context != nil {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
|
|
|
}
|
2019-01-13 14:05:03 +01:00
|
|
|
|
2019-01-12 21:19:23 +01:00
|
|
|
return Mknodat(dirfd, path, mode, dev)
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:54:16 +01:00
|
|
|
func FchmodatNofollow(dirfd int, path string, mode uint32) (err error) {
|
|
|
|
return unix.Fchmodat(dirfd, path, mode, unix.AT_SYMLINK_NOFOLLOW)
|
2017-11-29 12:54:34 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 21:22:52 +01:00
|
|
|
func SymlinkatUser(oldpath string, newdirfd int, newpath string, context *fuse.Context) (err error) {
|
2019-01-13 18:12:00 +01:00
|
|
|
if context != nil {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
|
|
|
}
|
|
|
|
|
2019-01-12 21:22:52 +01:00
|
|
|
return Symlinkat(oldpath, newdirfd, newpath)
|
|
|
|
}
|
|
|
|
|
2019-01-12 20:57:31 +01:00
|
|
|
func MkdiratUser(dirfd int, path string, mode uint32, context *fuse.Context) (err error) {
|
2019-01-13 18:12:00 +01:00
|
|
|
if context != nil {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
|
|
|
}
|
2019-01-13 14:05:03 +01:00
|
|
|
|
2019-01-12 20:57:31 +01:00
|
|
|
return Mkdirat(dirfd, path, mode)
|
|
|
|
}
|
|
|
|
|
2017-12-03 17:53:14 +01:00
|
|
|
func Getdents(fd int) ([]fuse.DirEntry, error) {
|
|
|
|
return emulateGetdents(fd)
|
|
|
|
}
|