github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/sentry/syscalls/linux/sys_sysinfo.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/ttpreport/gvisor-ligolo/pkg/abi/linux"
    19  	"github.com/ttpreport/gvisor-ligolo/pkg/sentry/arch"
    20  	"github.com/ttpreport/gvisor-ligolo/pkg/sentry/kernel"
    21  	"github.com/ttpreport/gvisor-ligolo/pkg/sentry/usage"
    22  )
    23  
    24  // Sysinfo implements Linux syscall sysinfo(2).
    25  func Sysinfo(t *kernel.Task, sysno uintptr, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) {
    26  	addr := args[0].Pointer()
    27  
    28  	mf := t.Kernel().MemoryFile()
    29  	mfUsage, err := mf.TotalUsage()
    30  	if err != nil {
    31  		return 0, nil, err
    32  	}
    33  	memStats, _ := usage.MemoryAccounting.Copy()
    34  	totalUsage := mfUsage + memStats.Mapped
    35  	totalSize := usage.TotalMemory(mf.TotalSize(), totalUsage)
    36  	memFree := totalSize - totalUsage
    37  	if memFree > totalSize {
    38  		// Underflow.
    39  		memFree = 0
    40  	}
    41  
    42  	// Only a subset of the fields in sysinfo_t make sense to return.
    43  	si := linux.Sysinfo{
    44  		Procs:    uint16(t.Kernel().TaskSet().Root.NumTasks()),
    45  		Uptime:   t.Kernel().MonotonicClock().Now().Seconds(),
    46  		TotalRAM: totalSize,
    47  		FreeRAM:  memFree,
    48  		Unit:     1,
    49  	}
    50  	_, err = si.CopyOut(t, addr)
    51  	return 0, nil, err
    52  }