v1.14.3
ADDED: * Convenience function to return a namespace FD and its type from a namespace ID (Linux only)
This commit is contained in:
parent
d7db23d58c
commit
35c56d3f98
9
errs/errs_linux.go
Normal file
9
errs/errs_linux.go
Normal file
@ -0,0 +1,9 @@
|
||||
package errs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidNs error = errors.New("invalid namespace identifier")
|
||||
)
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user