1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright 2021 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mqfs
import (
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/errors/linuxerr"
"gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs"
"gvisor.dev/gvisor/pkg/sentry/kernel/auth"
"gvisor.dev/gvisor/pkg/sentry/kernel/mq"
"gvisor.dev/gvisor/pkg/sentry/vfs"
"gvisor.dev/gvisor/pkg/usermem"
"gvisor.dev/gvisor/pkg/waiter"
)
// queueInode represents an inode for a message queue (/dev/mqueue/[name]).
//
// +stateify savable
type queueInode struct {
kernfs.DynamicBytesFile
// queue is the message queue backing this inode.
queue *mq.Queue
}
var _ kernfs.Inode = (*queueInode)(nil)
// newQueueInode returns a new, initialized queueInode.
func (fs *filesystem) newQueueInode(ctx context.Context, creds *auth.Credentials, q *mq.Queue, perm linux.FileMode) kernfs.Inode {
inode := &queueInode{queue: q}
inode.Init(ctx, creds, linux.UNNAMED_MAJOR, fs.devMinor, fs.NextIno(), q, perm)
return inode
}
// Keep implements kernfs.Inode.Keep.
func (q *queueInode) Keep() bool {
// Return true so that the fs keeps newly created dentries. This is done
// because inodes returned by root.Lookup are not temporary, they exist
// in the fs, and refer to message queues.
return true
}
// queueFD implements vfs.FileDescriptionImpl for FD backed by a POSIX message
// queue. It's mostly similar to DynamicBytesFD, but implements more operations.
//
// +stateify savable
type queueFD struct {
vfs.FileDescriptionDefaultImpl
vfs.DynamicBytesFileDescriptionImpl
vfs.LockFD
vfsfd vfs.FileDescription
inode kernfs.Inode
// queue is a view into the queue backing this fd.
queue mq.View
}
// Init initializes a queueFD. Mostly copied from DynamicBytesFD.Init, but uses
// the queueFD as FileDescriptionImpl.
func (fd *queueFD) Init(m *vfs.Mount, d *kernfs.Dentry, data vfs.DynamicBytesSource, locks *vfs.FileLocks, flags uint32) error {
fd.LockFD.Init(locks)
if err := fd.vfsfd.Init(fd, flags, m, d.VFSDentry(), &vfs.FileDescriptionOptions{}); err != nil {
return err
}
fd.inode = d.Inode()
fd.DynamicBytesFileDescriptionImpl.Init(&fd.vfsfd, data)
return nil
}
// Seek implements vfs.FileDescriptionImpl.Seek.
func (fd *queueFD) Seek(ctx context.Context, offset int64, whence int32) (int64, error) {
return fd.DynamicBytesFileDescriptionImpl.Seek(ctx, offset, whence)
}
// Read implements vfs.FileDescriptionImpl.Read.
func (fd *queueFD) Read(ctx context.Context, dst usermem.IOSequence, opts vfs.ReadOptions) (int64, error) {
return fd.DynamicBytesFileDescriptionImpl.Read(ctx, dst, opts)
}
// PRead implements vfs.FileDescriptionImpl.PRead.
func (fd *queueFD) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts vfs.ReadOptions) (int64, error) {
return fd.DynamicBytesFileDescriptionImpl.PRead(ctx, dst, offset, opts)
}
// Write implements vfs.FileDescriptionImpl.Write.
func (fd *queueFD) Write(ctx context.Context, src usermem.IOSequence, opts vfs.WriteOptions) (int64, error) {
return fd.DynamicBytesFileDescriptionImpl.Write(ctx, src, opts)
}
// PWrite implements vfs.FileDescriptionImpl.PWrite.
func (fd *queueFD) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts vfs.WriteOptions) (int64, error) {
return fd.DynamicBytesFileDescriptionImpl.PWrite(ctx, src, offset, opts)
}
// Release implements vfs.FileDescriptionImpl.Release.
func (fd *queueFD) Release(context.Context) {}
// Stat implements vfs.FileDescriptionImpl.Stat.
func (fd *queueFD) Stat(ctx context.Context, opts vfs.StatOptions) (linux.Statx, error) {
fs := fd.vfsfd.VirtualDentry().Mount().Filesystem()
return fd.inode.Stat(ctx, fs, opts)
}
// SetStat implements vfs.FileDescriptionImpl.SetStat.
func (fd *queueFD) SetStat(context.Context, vfs.SetStatOptions) error {
// DynamicBytesFiles are immutable.
return linuxerr.EPERM
}
// OnClose implements FileDescriptionImpl.OnClose similar to
// ipc/mqueue.c::mqueue_flush_file.
func (fd *queueFD) OnClose(ctx context.Context) error {
fd.queue.Flush(ctx)
return nil
}
// Readiness implements waiter.Waitable.Readiness similar to
// ipc/mqueue.c::mqueue_poll_file.
func (fd *queueFD) Readiness(mask waiter.EventMask) waiter.EventMask {
return fd.queue.Readiness(mask)
}
// EventRegister implements Waitable.EventRegister.
func (fd *queueFD) EventRegister(e *waiter.Entry) error {
return fd.queue.EventRegister(e)
}
// EventUnregister implements Waitable.EventUnregister.
func (fd *queueFD) EventUnregister(e *waiter.Entry) {
fd.queue.EventUnregister(e)
}
// Epollable implements FileDescriptionImpl.Epollable.
func (fd *queueFD) Epollable() bool {
return true
}
|