| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
//go:build linux |
| 4 |
|
| 5 |
// Copyright (C) 2024-2025 Aleksa Sarai <[email protected]> |
| 6 |
// Copyright (C) 2024-2025 SUSE LLC |
| 7 |
// |
| 8 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 9 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 10 |
// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 11 |
|
| 12 |
package procfs |
| 13 |
|
| 14 |
import ( |
| 15 |
"io" |
| 16 |
) |
| 17 |
|
| 18 |
type forceGetProcRootLevel int |
| 19 |
|
| 20 |
const ( |
| 21 |
forceGetProcRootDefault forceGetProcRootLevel = iota |
| 22 |
forceGetProcRootOpenTree // force open_tree() |
| 23 |
forceGetProcRootOpenTreeAtRecursive // force open_tree(AT_RECURSIVE) |
| 24 |
forceGetProcRootUnsafe // force open() |
| 25 |
) |
| 26 |
|
| 27 |
var testingForceGetProcRoot *forceGetProcRootLevel |
| 28 |
|
| 29 |
func testingCheckClose(check bool, f io.Closer) bool { |
| 30 |
if check { |
| 31 |
if f != nil { |
| 32 |
_ = f.Close() |
| 33 |
} |
| 34 |
return true |
| 35 |
} |
| 36 |
return false |
| 37 |
} |
| 38 |
|
| 39 |
func testingForcePrivateProcRootOpenTree(f io.Closer) bool { |
| 40 |
return testingForceGetProcRoot != nil && |
| 41 |
testingCheckClose(*testingForceGetProcRoot >= forceGetProcRootOpenTree, f) |
| 42 |
} |
| 43 |
|
| 44 |
func testingForcePrivateProcRootOpenTreeAtRecursive(f io.Closer) bool { |
| 45 |
return testingForceGetProcRoot != nil && |
| 46 |
testingCheckClose(*testingForceGetProcRoot >= forceGetProcRootOpenTreeAtRecursive, f) |
| 47 |
} |
| 48 |
|
| 49 |
func testingForceGetProcRootUnsafe() bool { |
| 50 |
return testingForceGetProcRoot != nil && |
| 51 |
*testingForceGetProcRoot >= forceGetProcRootUnsafe |
| 52 |
} |
| 53 |
|
| 54 |
type forceProcThreadSelfLevel int |
| 55 |
|
| 56 |
const ( |
| 57 |
forceProcThreadSelfDefault forceProcThreadSelfLevel = iota |
| 58 |
forceProcSelfTask |
| 59 |
forceProcSelf |
| 60 |
) |
| 61 |
|
| 62 |
var testingForceProcThreadSelf *forceProcThreadSelfLevel |
| 63 |
|
| 64 |
func testingForceProcSelfTask() bool { |
| 65 |
return testingForceProcThreadSelf != nil && |
| 66 |
*testingForceProcThreadSelf >= forceProcSelfTask |
| 67 |
} |
| 68 |
|
| 69 |
func testingForceProcSelf() bool { |
| 70 |
return testingForceProcThreadSelf != nil && |
| 71 |
*testingForceProcThreadSelf >= forceProcSelf |
| 72 |
} |
| 73 |
|
| 74 |
func init() { |
| 75 |
hookForceGetProcRootUnsafe = testingForceGetProcRootUnsafe |
| 76 |
hookForcePrivateProcRootOpenTree = testingForcePrivateProcRootOpenTree |
| 77 |
hookForcePrivateProcRootOpenTreeAtRecursive = testingForcePrivateProcRootOpenTreeAtRecursive |
| 78 |
|
| 79 |
hookForceProcSelf = testingForceProcSelf |
| 80 |
hookForceProcSelfTask = testingForceProcSelfTask |
| 81 |
} |