ADDED:
* Convenience function to return a namespace FD and its type from a
  namespace ID (Linux only)
This commit is contained in:
brent saner 2025-10-07 17:11:37 -04:00
parent d7db23d58c
commit 35c56d3f98
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
2 changed files with 40 additions and 4 deletions

9
errs/errs_linux.go Normal file
View File

@ -0,0 +1,9 @@
package errs
import (
"errors"
)
var (
ErrInvalidNs error = errors.New("invalid namespace identifier")
)

View File

@ -1,11 +1,14 @@
package sysutils
import (
`fmt`
`os`
"fmt"
"os"
"strconv"
"strings"
`golang.org/x/sys/unix`
`r00t2.io/sysutils/envs`
"golang.org/x/sys/unix"
"r00t2.io/sysutils/envs"
"r00t2.io/sysutils/errs"
)
// GetIDState returns current ID/elevation information. An IDState should *not* be explicitly created/defined.
@ -48,3 +51,27 @@ func GetIDState() (ids IDState) {
return
}
// NsToFD splits a namespace identifier (e.g. `net:[12345]`) to its type (e.g. `net`) and FD (e.g. `12345`).
func NsToFD(ns string) (typ string, fd uint64, err error) {
var fields []string
fields = strings.SplitN(ns, ":", 2)
if len(fields) != 2 {
err = errs.ErrInvalidNs
return
}
fields[1] = strings.TrimPrefix(fields[1], "[")
fields[1] = strings.TrimSuffix(fields[1], "]")
if fd, err = strconv.ParseUint(fields[1], 10, 64); err != nil {
return
}
typ = fields[0]
return
}