summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/syscalls/linux/sys_stat_amd64.go
blob: 75a567bd4442c70446c3f2ef5d822506522f6a63 (plain)
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
// Copyright 2020 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.

//+build amd64

package linux

import (
	"gvisor.dev/gvisor/pkg/abi/linux"
	"gvisor.dev/gvisor/pkg/binary"
	"gvisor.dev/gvisor/pkg/sentry/fs"
	"gvisor.dev/gvisor/pkg/sentry/kernel"
	"gvisor.dev/gvisor/pkg/usermem"
)

// copyOutStat copies the attributes (sattr, uattr) to the struct stat at
// address dst in t's address space. It encodes the stat struct to bytes
// manually, as stat() is a very common syscall for many applications, and
// t.CopyObjectOut has noticeable performance impact due to its many slice
// allocations and use of reflection.
func copyOutStat(t *kernel.Task, dst usermem.Addr, sattr fs.StableAttr, uattr fs.UnstableAttr) error {
	b := t.CopyScratchBuffer(int(linux.SizeOfStat))[:0]

	// Dev (uint64)
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(sattr.DeviceID))
	// Ino (uint64)
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(sattr.InodeID))
	// Nlink (uint64)
	b = binary.AppendUint64(b, usermem.ByteOrder, uattr.Links)
	// Mode (uint32)
	b = binary.AppendUint32(b, usermem.ByteOrder, sattr.Type.LinuxType()|uint32(uattr.Perms.LinuxMode()))
	// UID (uint32)
	b = binary.AppendUint32(b, usermem.ByteOrder, uint32(uattr.Owner.UID.In(t.UserNamespace()).OrOverflow()))
	// GID (uint32)
	b = binary.AppendUint32(b, usermem.ByteOrder, uint32(uattr.Owner.GID.In(t.UserNamespace()).OrOverflow()))
	// Padding (uint32)
	b = binary.AppendUint32(b, usermem.ByteOrder, 0)
	// Rdev (uint64)
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(linux.MakeDeviceID(sattr.DeviceFileMajor, sattr.DeviceFileMinor)))
	// Size (uint64)
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(uattr.Size))
	// Blksize (uint64)
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(sattr.BlockSize))
	// Blocks (uint64)
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(uattr.Usage/512))

	// ATime
	atime := uattr.AccessTime.Timespec()
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(atime.Sec))
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(atime.Nsec))

	// MTime
	mtime := uattr.ModificationTime.Timespec()
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(mtime.Sec))
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(mtime.Nsec))

	// CTime
	ctime := uattr.StatusChangeTime.Timespec()
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(ctime.Sec))
	b = binary.AppendUint64(b, usermem.ByteOrder, uint64(ctime.Nsec))

	_, err := t.CopyOutBytes(dst, b)
	return err
}