github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/sentry/syscalls/linux/sys_syslog.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/MerlinKodo/gvisor/pkg/errors/linuxerr"
    19  	"github.com/MerlinKodo/gvisor/pkg/sentry/arch"
    20  	"github.com/MerlinKodo/gvisor/pkg/sentry/kernel"
    21  )
    22  
    23  const (
    24  	_SYSLOG_ACTION_READ_ALL    = 3
    25  	_SYSLOG_ACTION_SIZE_BUFFER = 10
    26  )
    27  
    28  // logBufLen is the default syslog buffer size on Linux.
    29  const logBufLen = 1 << 17
    30  
    31  // Syslog implements part of Linux syscall syslog.
    32  //
    33  // Only the unpriviledged commands are implemented, allowing applications to
    34  // read a fun dmesg.
    35  func Syslog(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    36  	command := args[0].Int()
    37  	buf := args[1].Pointer()
    38  	size := int(args[2].Int())
    39  
    40  	switch command {
    41  	case _SYSLOG_ACTION_READ_ALL:
    42  		if size < 0 {
    43  			return 0, nil, linuxerr.EINVAL
    44  		}
    45  		if size > logBufLen {
    46  			size = logBufLen
    47  		}
    48  
    49  		log := t.Kernel().Syslog().Log()
    50  		if len(log) > size {
    51  			log = log[:size]
    52  		}
    53  
    54  		n, err := t.CopyOutBytes(buf, log)
    55  		return uintptr(n), nil, err
    56  	case _SYSLOG_ACTION_SIZE_BUFFER:
    57  		return logBufLen, nil, nil
    58  	default:
    59  		return 0, nil, linuxerr.ENOSYS
    60  	}
    61  }