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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
// Copyright 2019 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 signalfd provides basic signalfd file implementations.
package signalfd
import (
"gvisor.dev/gvisor/pkg/abi/linux"
"gvisor.dev/gvisor/pkg/context"
"gvisor.dev/gvisor/pkg/errors/linuxerr"
"gvisor.dev/gvisor/pkg/sentry/kernel"
"gvisor.dev/gvisor/pkg/sentry/vfs"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/usermem"
"gvisor.dev/gvisor/pkg/waiter"
)
// SignalFileDescription implements vfs.FileDescriptionImpl for signal fds.
//
// +stateify savable
type SignalFileDescription struct {
vfsfd vfs.FileDescription
vfs.FileDescriptionDefaultImpl
vfs.DentryMetadataFileDescriptionImpl
vfs.NoLockFD
vfs.NoAsyncEventFD
// target is the original signal target task.
//
// The semantics here are a bit broken. Linux will always use current
// for all reads, regardless of where the signalfd originated. We can't
// do exactly that because we need to plumb the context through
// EventRegister in order to support proper blocking behavior. This
// will undoubtedly become very complicated quickly.
target *kernel.Task
// queue is the queue for listeners.
queue waiter.Queue
// mu protects entry.
mu sync.Mutex `state:"nosave"`
// entry is the entry in the task signal queue.
entry waiter.Entry
}
var _ vfs.FileDescriptionImpl = (*SignalFileDescription)(nil)
// New creates a new signal fd.
func New(vfsObj *vfs.VirtualFilesystem, target *kernel.Task, mask linux.SignalSet, flags uint32) (*vfs.FileDescription, error) {
vd := vfsObj.NewAnonVirtualDentry("[signalfd]")
defer vd.DecRef(target)
sfd := &SignalFileDescription{
target: target,
}
sfd.entry.Init(sfd, waiter.EventMask(mask))
sfd.target.SignalRegister(&sfd.entry)
if err := sfd.vfsfd.Init(sfd, flags, vd.Mount(), vd.Dentry(), &vfs.FileDescriptionOptions{
UseDentryMetadata: true,
DenyPRead: true,
DenyPWrite: true,
}); err != nil {
sfd.target.SignalUnregister(&sfd.entry)
return nil, err
}
return &sfd.vfsfd, nil
}
// Mask returns the signal mask.
func (sfd *SignalFileDescription) Mask() linux.SignalSet {
sfd.mu.Lock()
defer sfd.mu.Unlock()
return linux.SignalSet(sfd.entry.Mask())
}
// SetMask sets the signal mask.
func (sfd *SignalFileDescription) SetMask(mask linux.SignalSet) {
sfd.mu.Lock()
defer sfd.mu.Unlock()
sfd.target.SignalUnregister(&sfd.entry)
sfd.entry.Init(sfd, waiter.EventMask(mask))
sfd.target.SignalRegister(&sfd.entry)
}
// Read implements vfs.FileDescriptionImpl.Read.
func (sfd *SignalFileDescription) Read(ctx context.Context, dst usermem.IOSequence, _ vfs.ReadOptions) (int64, error) {
// Attempt to dequeue relevant signals.
info, err := sfd.target.Sigtimedwait(sfd.Mask(), 0)
if err != nil {
// There must be no signal available.
return 0, linuxerr.ErrWouldBlock
}
// Copy out the signal info using the specified format.
infoNative := linux.SignalfdSiginfo{
Signo: uint32(info.Signo),
Errno: info.Errno,
Code: info.Code,
PID: uint32(info.PID()),
UID: uint32(info.UID()),
Status: info.Status(),
Overrun: uint32(info.Overrun()),
Addr: info.Addr(),
}
n, err := infoNative.WriteTo(dst.Writer(ctx))
if err == usermem.ErrEndOfIOSequence {
// Partial copy-out ok.
err = nil
}
return n, err
}
// Readiness implements waiter.Waitable.Readiness.
func (sfd *SignalFileDescription) Readiness(mask waiter.EventMask) waiter.EventMask {
sfd.mu.Lock()
defer sfd.mu.Unlock()
if mask&waiter.ReadableEvents != 0 && sfd.target.PendingSignals()&linux.SignalSet(sfd.entry.Mask()) != 0 {
return waiter.ReadableEvents // Pending signals.
}
return 0
}
// EventRegister implements waiter.Waitable.EventRegister.
func (sfd *SignalFileDescription) EventRegister(e *waiter.Entry) error {
sfd.queue.EventRegister(e)
return nil
}
// EventUnregister implements waiter.Waitable.EventUnregister.
func (sfd *SignalFileDescription) EventUnregister(e *waiter.Entry) {
sfd.queue.EventUnregister(e)
}
// NotifyEvent implements waiter.EventListener.NotifyEvent.
func (sfd *SignalFileDescription) NotifyEvent(mask waiter.EventMask) {
sfd.queue.Notify(waiter.EventIn) // Always notify data available.
}
// Epollable implements FileDescriptionImpl.Epollable.
func (sfd *SignalFileDescription) Epollable() bool {
return true
}
// Release implements vfs.FileDescriptionImpl.Release.
func (sfd *SignalFileDescription) Release(context.Context) {
sfd.target.SignalUnregister(&sfd.entry)
}
// RegisterFileAsyncHandler implements vfs.FileDescriptionImpl.RegisterFileAsyncHandler.
func (sfd *SignalFileDescription) RegisterFileAsyncHandler(fd *vfs.FileDescription) error {
return sfd.NoAsyncEventFD.RegisterFileAsyncHandler(fd)
}
// UnregisterFileAsyncHandler implements vfs.FileDescriptionImpl.UnregisterFileAsyncHandler.
func (sfd *SignalFileDescription) UnregisterFileAsyncHandler(fd *vfs.FileDescription) {
sfd.NoAsyncEventFD.UnregisterFileAsyncHandler(fd)
}
|