github.com/sagernet/gvisor@v0.0.0-20240428053021-e691de28565f/pkg/syserr/host_linux.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  //go:build linux
    16  // +build linux
    17  
    18  package syserr
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"golang.org/x/sys/unix"
    24  	"github.com/sagernet/gvisor/pkg/abi/linux/errno"
    25  )
    26  
    27  const maxErrno = 134
    28  
    29  var linuxHostTranslations [maxErrno]*Error
    30  
    31  // FromHost translates a unix.Errno to a corresponding Error value.
    32  func FromHost(err unix.Errno) *Error {
    33  	if int(err) >= len(linuxHostTranslations) || linuxHostTranslations[err] == nil {
    34  		panic(fmt.Sprintf("unknown host errno %q (%d)", err.Error(), err))
    35  	}
    36  	return linuxHostTranslations[err]
    37  }
    38  
    39  func addHostTranslation(host unix.Errno, trans *Error) {
    40  	if linuxHostTranslations[host] != nil {
    41  		panic(fmt.Sprintf("duplicate translation for host errno %q (%d)", host.Error(), host))
    42  	}
    43  	linuxHostTranslations[host] = trans
    44  }
    45  
    46  // TODO(b/34162363): Remove or replace most of these errors.
    47  //
    48  // Some of the errors should be replaced with package specific errors and
    49  // others should be removed entirely.
    50  var (
    51  	ErrDeadlock                  = newWithHost("resource deadlock would occur", errno.EDEADLOCK, unix.EDEADLOCK)
    52  	ErrChannelOutOfRange         = newWithHost("channel number out of range", errno.ECHRNG, unix.ECHRNG)
    53  	ErrLevelTwoNotSynced         = newWithHost("level 2 not synchronized", errno.EL2NSYNC, unix.EL2NSYNC)
    54  	ErrLevelThreeHalted          = newWithHost("level 3 halted", errno.EL3HLT, unix.EL3HLT)
    55  	ErrLevelThreeReset           = newWithHost("level 3 reset", errno.EL3RST, unix.EL3RST)
    56  	ErrLinkNumberOutOfRange      = newWithHost("link number out of range", errno.ELNRNG, unix.ELNRNG)
    57  	ErrProtocolDriverNotAttached = newWithHost("protocol driver not attached", errno.EUNATCH, unix.EUNATCH)
    58  	ErrNoCSIAvailable            = newWithHost("no CSI structure available", errno.ENOCSI, unix.ENOCSI)
    59  	ErrLevelTwoHalted            = newWithHost("level 2 halted", errno.EL2HLT, unix.EL2HLT)
    60  	ErrInvalidExchange           = newWithHost("invalid exchange", errno.EBADE, unix.EBADE)
    61  	ErrInvalidRequestDescriptor  = newWithHost("invalid request descriptor", errno.EBADR, unix.EBADR)
    62  	ErrExchangeFull              = newWithHost("exchange full", errno.EXFULL, unix.EXFULL)
    63  	ErrNoAnode                   = newWithHost("no anode", errno.ENOANO, unix.ENOANO)
    64  	ErrInvalidRequestCode        = newWithHost("invalid request code", errno.EBADRQC, unix.EBADRQC)
    65  	ErrInvalidSlot               = newWithHost("invalid slot", errno.EBADSLT, unix.EBADSLT)
    66  	ErrBadFontFile               = newWithHost("bad font file format", errno.EBFONT, unix.EBFONT)
    67  	ErrMachineNotOnNetwork       = newWithHost("machine is not on the network", errno.ENONET, unix.ENONET)
    68  	ErrPackageNotInstalled       = newWithHost("package not installed", errno.ENOPKG, unix.ENOPKG)
    69  	ErrAdvertise                 = newWithHost("advertise error", errno.EADV, unix.EADV)
    70  	ErrSRMount                   = newWithHost("srmount error", errno.ESRMNT, unix.ESRMNT)
    71  	ErrSendCommunication         = newWithHost("communication error on send", errno.ECOMM, unix.ECOMM)
    72  	ErrRFS                       = newWithHost("RFS specific error", errno.EDOTDOT, unix.EDOTDOT)
    73  	ErrNetworkNameNotUnique      = newWithHost("name not unique on network", errno.ENOTUNIQ, unix.ENOTUNIQ)
    74  	ErrFDInBadState              = newWithHost("file descriptor in bad state", errno.EBADFD, unix.EBADFD)
    75  	ErrRemoteAddressChanged      = newWithHost("remote address changed", errno.EREMCHG, unix.EREMCHG)
    76  	ErrSharedLibraryInaccessible = newWithHost("can not access a needed shared library", errno.ELIBACC, unix.ELIBACC)
    77  	ErrCorruptedSharedLibrary    = newWithHost("accessing a corrupted shared library", errno.ELIBBAD, unix.ELIBBAD)
    78  	ErrLibSectionCorrupted       = newWithHost(".lib section in a.out corrupted", errno.ELIBSCN, unix.ELIBSCN)
    79  	ErrTooManySharedLibraries    = newWithHost("attempting to link in too many shared libraries", errno.ELIBMAX, unix.ELIBMAX)
    80  	ErrSharedLibraryExeced       = newWithHost("cannot exec a shared library directly", errno.ELIBEXEC, unix.ELIBEXEC)
    81  	ErrShouldRestart             = newWithHost("interrupted system call should be restarted", errno.ERESTART, unix.ERESTART)
    82  	ErrStreamPipe                = newWithHost("streams pipe error", errno.ESTRPIPE, unix.ESTRPIPE)
    83  	ErrStructureNeedsCleaning    = newWithHost("structure needs cleaning", errno.EUCLEAN, unix.EUCLEAN)
    84  	ErrIsNamedFile               = newWithHost("is a named type file", errno.ENOTNAM, unix.ENOTNAM)
    85  	ErrRemoteIO                  = newWithHost("remote I/O error", errno.EREMOTEIO, unix.EREMOTEIO)
    86  	ErrNoMedium                  = newWithHost("no medium found", errno.ENOMEDIUM, unix.ENOMEDIUM)
    87  	ErrWrongMediumType           = newWithHost("wrong medium type", errno.EMEDIUMTYPE, unix.EMEDIUMTYPE)
    88  	ErrNoKey                     = newWithHost("required key not available", errno.ENOKEY, unix.ENOKEY)
    89  	ErrKeyExpired                = newWithHost("key has expired", errno.EKEYEXPIRED, unix.EKEYEXPIRED)
    90  	ErrKeyRevoked                = newWithHost("key has been revoked", errno.EKEYREVOKED, unix.EKEYREVOKED)
    91  	ErrKeyRejected               = newWithHost("key was rejected by service", errno.EKEYREJECTED, unix.EKEYREJECTED)
    92  )