github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/syscalls/linux/sys_inotify.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package linux
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/abi/linux"
    19  	"github.com/SagerNet/gvisor/pkg/errors/linuxerr"
    20  	"github.com/SagerNet/gvisor/pkg/sentry/arch"
    21  	"github.com/SagerNet/gvisor/pkg/sentry/fs"
    22  	"github.com/SagerNet/gvisor/pkg/sentry/fs/anon"
    23  	"github.com/SagerNet/gvisor/pkg/sentry/kernel"
    24  	"github.com/SagerNet/gvisor/pkg/syserror"
    25  )
    26  
    27  const allFlags = int(linux.IN_NONBLOCK | linux.IN_CLOEXEC)
    28  
    29  // InotifyInit1 implements the inotify_init1() syscalls.
    30  func InotifyInit1(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    31  	flags := int(args[0].Int())
    32  
    33  	if flags&^allFlags != 0 {
    34  		return 0, nil, linuxerr.EINVAL
    35  	}
    36  
    37  	dirent := fs.NewDirent(t, anon.NewInode(t), "inotify")
    38  	fileFlags := fs.FileFlags{
    39  		Read:        true,
    40  		Write:       true,
    41  		NonBlocking: flags&linux.IN_NONBLOCK != 0,
    42  	}
    43  	n := fs.NewFile(t, dirent, fileFlags, fs.NewInotify(t))
    44  	defer n.DecRef(t)
    45  
    46  	fd, err := t.NewFDFrom(0, n, kernel.FDFlags{
    47  		CloseOnExec: flags&linux.IN_CLOEXEC != 0,
    48  	})
    49  
    50  	if err != nil {
    51  		return 0, nil, err
    52  	}
    53  
    54  	return uintptr(fd), nil, nil
    55  }
    56  
    57  // InotifyInit implements the inotify_init() syscalls.
    58  func InotifyInit(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    59  	args[0].Value = 0
    60  	return InotifyInit1(t, args)
    61  }
    62  
    63  // fdToInotify resolves an fd to an inotify object. If successful, the file will
    64  // have an extra ref and the caller is responsible for releasing the ref.
    65  func fdToInotify(t *kernel.Task, fd int32) (*fs.Inotify, *fs.File, error) {
    66  	file := t.GetFile(fd)
    67  	if file == nil {
    68  		// Invalid fd.
    69  		return nil, nil, linuxerr.EBADF
    70  	}
    71  
    72  	ino, ok := file.FileOperations.(*fs.Inotify)
    73  	if !ok {
    74  		// Not an inotify fd.
    75  		file.DecRef(t)
    76  		return nil, nil, linuxerr.EINVAL
    77  	}
    78  
    79  	return ino, file, nil
    80  }
    81  
    82  // InotifyAddWatch implements the inotify_add_watch() syscall.
    83  func InotifyAddWatch(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    84  	fd := args[0].Int()
    85  	addr := args[1].Pointer()
    86  	mask := args[2].Uint()
    87  
    88  	// "IN_DONT_FOLLOW: Don't dereference pathname if it is a symbolic link."
    89  	//  -- inotify(7)
    90  	resolve := mask&linux.IN_DONT_FOLLOW == 0
    91  
    92  	// "EINVAL: The given event mask contains no valid events."
    93  	// -- inotify_add_watch(2)
    94  	if validBits := mask & linux.ALL_INOTIFY_BITS; validBits == 0 {
    95  		return 0, nil, linuxerr.EINVAL
    96  	}
    97  
    98  	ino, file, err := fdToInotify(t, fd)
    99  	if err != nil {
   100  		return 0, nil, err
   101  	}
   102  	defer file.DecRef(t)
   103  
   104  	path, _, err := copyInPath(t, addr, false /* allowEmpty */)
   105  	if err != nil {
   106  		return 0, nil, err
   107  	}
   108  
   109  	err = fileOpOn(t, linux.AT_FDCWD, path, resolve, func(root *fs.Dirent, dirent *fs.Dirent, _ uint) error {
   110  		// "IN_ONLYDIR: Only watch pathname if it is a directory." -- inotify(7)
   111  		if onlyDir := mask&linux.IN_ONLYDIR != 0; onlyDir && !fs.IsDir(dirent.Inode.StableAttr) {
   112  			return syserror.ENOTDIR
   113  		}
   114  
   115  		// Copy out to the return frame.
   116  		fd = ino.AddWatch(dirent, mask)
   117  
   118  		return nil
   119  	})
   120  	return uintptr(fd), nil, err // Return from the existing value.
   121  }
   122  
   123  // InotifyRmWatch implements the inotify_rm_watch() syscall.
   124  func InotifyRmWatch(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
   125  	fd := args[0].Int()
   126  	wd := args[1].Int()
   127  
   128  	ino, file, err := fdToInotify(t, fd)
   129  	if err != nil {
   130  		return 0, nil, err
   131  	}
   132  	defer file.DecRef(t)
   133  	return 0, nil, ino.RmWatch(t, wd)
   134  }