| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package libpathrs |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"syscall" |
| 20 |
"unsafe" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
import "C" |
| 35 |
|
| 36 |
func fetchError(errID C.int) error { |
| 37 |
if errID >= C.__PATHRS_MAX_ERR_VALUE { |
| 38 |
return nil |
| 39 |
} |
| 40 |
cErr := C.pathrs_errorinfo(errID) |
| 41 |
defer C.pathrs_errorinfo_free(cErr) |
| 42 |
|
| 43 |
var err error |
| 44 |
if cErr != nil { |
| 45 |
err = &Error{ |
| 46 |
errno: syscall.Errno(cErr.saved_errno), |
| 47 |
description: C.GoString(cErr.description), |
| 48 |
} |
| 49 |
} |
| 50 |
return err |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func OpenRoot(path string) (uintptr, error) { |
| 55 |
cPath := C.CString(path) |
| 56 |
defer C.free(unsafe.Pointer(cPath)) |
| 57 |
|
| 58 |
fd := C.pathrs_open_root(cPath) |
| 59 |
return uintptr(fd), fetchError(fd) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func Reopen(fd uintptr, flags uint64) (uintptr, error) { |
| 64 |
newFd := C.pathrs_reopen(C.int(fd), C.uint64_t(flags)) |
| 65 |
return uintptr(newFd), fetchError(newFd) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func InRootResolve(rootFd uintptr, path string) (uintptr, error) { |
| 70 |
cPath := C.CString(path) |
| 71 |
defer C.free(unsafe.Pointer(cPath)) |
| 72 |
|
| 73 |
fd := C.pathrs_inroot_resolve(C.int(rootFd), cPath) |
| 74 |
return uintptr(fd), fetchError(fd) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func InRootResolveNoFollow(rootFd uintptr, path string) (uintptr, error) { |
| 79 |
cPath := C.CString(path) |
| 80 |
defer C.free(unsafe.Pointer(cPath)) |
| 81 |
|
| 82 |
fd := C.pathrs_inroot_resolve_nofollow(C.int(rootFd), cPath) |
| 83 |
return uintptr(fd), fetchError(fd) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func InRootOpen(rootFd uintptr, path string, flags uint64) (uintptr, error) { |
| 88 |
cPath := C.CString(path) |
| 89 |
defer C.free(unsafe.Pointer(cPath)) |
| 90 |
|
| 91 |
fd := C.pathrs_inroot_open(C.int(rootFd), cPath, C.uint64_t(flags)) |
| 92 |
return uintptr(fd), fetchError(fd) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func InRootReadlink(rootFd uintptr, path string) (string, error) { |
| 97 |
cPath := C.CString(path) |
| 98 |
defer C.free(unsafe.Pointer(cPath)) |
| 99 |
|
| 100 |
size := 128 |
| 101 |
for { |
| 102 |
linkBuf := make([]byte, size) |
| 103 |
n := C.pathrs_inroot_readlink(C.int(rootFd), cPath, C.cast_ptr(unsafe.Pointer(&linkBuf[0])), C.size_t(len(linkBuf))) |
| 104 |
switch { |
| 105 |
case int(n) < C.__PATHRS_MAX_ERR_VALUE: |
| 106 |
return "", fetchError(n) |
| 107 |
case int(n) <= len(linkBuf): |
| 108 |
return string(linkBuf[:int(n)]), nil |
| 109 |
default: |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
size += int(n) |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func InRootRmdir(rootFd uintptr, path string) error { |
| 122 |
cPath := C.CString(path) |
| 123 |
defer C.free(unsafe.Pointer(cPath)) |
| 124 |
|
| 125 |
err := C.pathrs_inroot_rmdir(C.int(rootFd), cPath) |
| 126 |
return fetchError(err) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
func InRootUnlink(rootFd uintptr, path string) error { |
| 131 |
cPath := C.CString(path) |
| 132 |
defer C.free(unsafe.Pointer(cPath)) |
| 133 |
|
| 134 |
err := C.pathrs_inroot_unlink(C.int(rootFd), cPath) |
| 135 |
return fetchError(err) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func InRootRemoveAll(rootFd uintptr, path string) error { |
| 140 |
cPath := C.CString(path) |
| 141 |
defer C.free(unsafe.Pointer(cPath)) |
| 142 |
|
| 143 |
err := C.pathrs_inroot_remove_all(C.int(rootFd), cPath) |
| 144 |
return fetchError(err) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
func InRootCreat(rootFd uintptr, path string, flags uint64, mode uint32) (uintptr, error) { |
| 149 |
cPath := C.CString(path) |
| 150 |
defer C.free(unsafe.Pointer(cPath)) |
| 151 |
|
| 152 |
fd := C.pathrs_inroot_creat(C.int(rootFd), cPath, C.uint64_t(flags), C.uint(mode)) |
| 153 |
return uintptr(fd), fetchError(fd) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func InRootRename(oldRootFd uintptr, oldPath string, newRootFd uintptr, newPath string, flags uint64) error { |
| 158 |
cOldPath := C.CString(oldPath) |
| 159 |
defer C.free(unsafe.Pointer(cOldPath)) |
| 160 |
|
| 161 |
cNewPath := C.CString(newPath) |
| 162 |
defer C.free(unsafe.Pointer(cNewPath)) |
| 163 |
|
| 164 |
err := C.pathrs_inroot_rename(C.int(oldRootFd), cOldPath, C.int(newRootFd), cNewPath, C.uint64_t(flags)) |
| 165 |
return fetchError(err) |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
func InRootMkdir(rootFd uintptr, path string, mode uint32) error { |
| 170 |
cPath := C.CString(path) |
| 171 |
defer C.free(unsafe.Pointer(cPath)) |
| 172 |
|
| 173 |
err := C.pathrs_inroot_mkdir(C.int(rootFd), cPath, C.uint(mode)) |
| 174 |
return fetchError(err) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
func InRootMkdirAll(rootFd uintptr, path string, mode uint32) (uintptr, error) { |
| 179 |
cPath := C.CString(path) |
| 180 |
defer C.free(unsafe.Pointer(cPath)) |
| 181 |
|
| 182 |
fd := C.pathrs_inroot_mkdir_all(C.int(rootFd), cPath, C.uint(mode)) |
| 183 |
return uintptr(fd), fetchError(fd) |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
func InRootMknod(rootFd uintptr, path string, mode uint32, dev uint64) error { |
| 188 |
cPath := C.CString(path) |
| 189 |
defer C.free(unsafe.Pointer(cPath)) |
| 190 |
|
| 191 |
err := C.pathrs_inroot_mknod(C.int(rootFd), cPath, C.uint(mode), C.dev_t(dev)) |
| 192 |
return fetchError(err) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
func InRootSymlink(target string, rootFd uintptr, linkpath string) error { |
| 197 |
cLinkpath := C.CString(linkpath) |
| 198 |
defer C.free(unsafe.Pointer(cLinkpath)) |
| 199 |
|
| 200 |
cTarget := C.CString(target) |
| 201 |
defer C.free(unsafe.Pointer(cTarget)) |
| 202 |
|
| 203 |
err := C.pathrs_inroot_symlink(cTarget, C.int(rootFd), cLinkpath) |
| 204 |
return fetchError(err) |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
func InRootHardlink(oldRootFd uintptr, oldPath string, newRootFd uintptr, newPath string, flags uint64) error { |
| 209 |
cNewPath := C.CString(newPath) |
| 210 |
defer C.free(unsafe.Pointer(cNewPath)) |
| 211 |
|
| 212 |
cOldPath := C.CString(oldPath) |
| 213 |
defer C.free(unsafe.Pointer(cOldPath)) |
| 214 |
|
| 215 |
err := C.pathrs_inroot_hardlink(C.int(oldRootFd), cOldPath, C.int(newRootFd), cNewPath, C.uint64_t(flags)) |
| 216 |
return fetchError(err) |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
type ProcBase C.pathrs_proc_base_t |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
const ( |
| 227 |
|
| 228 |
ProcRoot ProcBase = 0xFFFF_FFFE_7072_6F63 |
| 229 |
|
| 230 |
ProcSelf ProcBase = 0xFFFF_FFFE_091D_5E1F |
| 231 |
|
| 232 |
ProcThreadSelf ProcBase = 0xFFFF_FFFE_3EAD_5E1F |
| 233 |
|
| 234 |
|
| 235 |
ProcBaseTypeMask ProcBase = 0xFFFF_FFFF_0000_0000 |
| 236 |
|
| 237 |
ProcBaseTypePid ProcBase = 0x8000_0000_0000_0000 |
| 238 |
|
| 239 |
|
| 240 |
ProcDefaultRootFd = -int(syscall.EBADF) |
| 241 |
) |
| 242 |
|
| 243 |
func assertEqual[T comparable](a, b T, msg string) { |
| 244 |
if a != b { |
| 245 |
panic(fmt.Sprintf("%s ((%T) %#v != (%T) %#v)", msg, a, a, b, b)) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
func init() { |
| 255 |
var ( |
| 256 |
actualProcRoot int64 = C.PATHRS_PROC_ROOT |
| 257 |
actualProcSelf int64 = C.PATHRS_PROC_SELF |
| 258 |
actualProcThreadSelf int64 = C.PATHRS_PROC_THREAD_SELF |
| 259 |
) |
| 260 |
|
| 261 |
assertEqual(ProcRoot, ProcBase(actualProcRoot), "PATHRS_PROC_ROOT") |
| 262 |
assertEqual(ProcSelf, ProcBase(actualProcSelf), "PATHRS_PROC_SELF") |
| 263 |
assertEqual(ProcThreadSelf, ProcBase(actualProcThreadSelf), "PATHRS_PROC_THREAD_SELF") |
| 264 |
|
| 265 |
var ( |
| 266 |
actualProcBaseTypeMask uint64 = C.__PATHRS_PROC_TYPE_MASK |
| 267 |
actualProcBaseTypePid uint64 = C.__PATHRS_PROC_TYPE_PID |
| 268 |
) |
| 269 |
|
| 270 |
assertEqual(ProcBaseTypeMask, ProcBase(actualProcBaseTypeMask), "__PATHRS_PROC_TYPE_MASK") |
| 271 |
assertEqual(ProcBaseTypePid, ProcBase(actualProcBaseTypePid), "__PATHRS_PROC_TYPE_PID") |
| 272 |
|
| 273 |
assertEqual(ProcDefaultRootFd, int(C.PATHRS_PROC_DEFAULT_ROOTFD), "PATHRS_PROC_DEFAULT_ROOTFD") |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
func ProcPid(pid uint32) ProcBase { return ProcBaseTypePid | ProcBase(pid) } |
| 278 |
|
| 279 |
|
| 280 |
func ProcOpenat(procRootFd int, base ProcBase, path string, flags uint64) (uintptr, error) { |
| 281 |
cBase := C.pathrs_proc_base_t(base) |
| 282 |
|
| 283 |
cPath := C.CString(path) |
| 284 |
defer C.free(unsafe.Pointer(cPath)) |
| 285 |
|
| 286 |
fd := C.pathrs_proc_openat(C.int(procRootFd), cBase, cPath, C.uint64_t(flags)) |
| 287 |
return uintptr(fd), fetchError(fd) |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
func ProcReadlinkat(procRootFd int, base ProcBase, path string) (string, error) { |
| 292 |
|
| 293 |
|
| 294 |
cBase := C.pathrs_proc_base_t(base) |
| 295 |
|
| 296 |
cPath := C.CString(path) |
| 297 |
defer C.free(unsafe.Pointer(cPath)) |
| 298 |
|
| 299 |
size := 128 |
| 300 |
for { |
| 301 |
linkBuf := make([]byte, size) |
| 302 |
n := C.pathrs_proc_readlinkat( |
| 303 |
C.int(procRootFd), cBase, cPath, |
| 304 |
C.cast_ptr(unsafe.Pointer(&linkBuf[0])), C.size_t(len(linkBuf))) |
| 305 |
switch { |
| 306 |
case int(n) < C.__PATHRS_MAX_ERR_VALUE: |
| 307 |
return "", fetchError(n) |
| 308 |
case int(n) <= len(linkBuf): |
| 309 |
return string(linkBuf[:int(n)]), nil |
| 310 |
default: |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
size += int(n) |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
type ProcfsOpenHow C.pathrs_procfs_open_how |
| 323 |
|
| 324 |
const ( |
| 325 |
|
| 326 |
ProcfsNewUnmasked = C.PATHRS_PROCFS_NEW_UNMASKED |
| 327 |
) |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
func (how *ProcfsOpenHow) Flags() *C.uint64_t { return &how.flags } |
| 332 |
|
| 333 |
|
| 334 |
func ProcfsOpen(how *ProcfsOpenHow) (uintptr, error) { |
| 335 |
fd := C.pathrs_procfs_open((*C.pathrs_procfs_open_how)(how), C.size_t(unsafe.Sizeof(*how))) |
| 336 |
return uintptr(fd), fetchError(fd) |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
type VersionInfo struct { |
| 341 |
VersionString string |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
type versionInfo C.pathrs_version_info_t |
| 346 |
|
| 347 |
|
| 348 |
func Version() (*VersionInfo, error) { |
| 349 |
var rawVersion versionInfo |
| 350 |
size := C.pathrs_version((*C.pathrs_version_info_t)(&rawVersion), C.size_t(unsafe.Sizeof(rawVersion))) |
| 351 |
switch { |
| 352 |
case size < 0: |
| 353 |
return nil, fetchError(size) |
| 354 |
case size > 0: |
| 355 |
|
| 356 |
fallthrough |
| 357 |
default: |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
return &VersionInfo{ |
| 362 |
VersionString: C.GoString(rawVersion.version_string), |
| 363 |
}, nil |
| 364 |
} |
| 365 |
} |