namespace

package module
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2023 License: MIT Imports: 13 Imported by: 0

README

Namespace

A namespace executor help library, used to execute task in specified linux namespaces

Example

package main

import (
	"bytes"
	"log"
	"os/exec"
	"context"
	"github.com/derekhjray/namespace"
)

func ipLink(_ ...interface{}) error {
	var (
		stdout bytes.Buffer
	)

	cmd := exec.CommandContext(context.TODO(), "ip", "link")
	cmd.Stdout = &stdout
	if err := cmd.Run(); err != nil {
		return nil
	}

	log.Println(stdout.String())
	return nil
}

func main() {
	ns, err := namespace.New(namespace.Types(NET), namespace.Pid(2398))
	if err != nil {
		log.Println(err)
		return
	}

	err = ns.Execute(ipLink)
	if err != nil {
		log.Println(err)
		return
	}

	if err = ipLink(); err != nil {
		log.Println(err)
	}
}

Documentation

Index

Constants

Variables

View Source
var (
	ErrNotImplemented = errors.New("not implemented")
)
View Source
var SYS_SETNS = map[string]uintptr{
	"386":     346,
	"amd64":   308,
	"arm64":   268,
	"arm":     375,
	"ppc64":   350,
	"ppc64le": 350,
	"s390x":   339,
}[runtime.GOARCH]

SYS_SETNS syscall allows changing the namespace of the current process.

Functions

func ReadFile added in v0.1.7

func ReadFile(filename, ns string) (*bytes.Buffer, error)

func Stat added in v0.1.7

func Stat(filename, ns string) (*types.FileInfo, error)

Types

type Namespace

type Namespace struct {
	// contains filtered or unexported fields
}

func New

func New(options ...Option) (*Namespace, error)

New creates a new Namespace instance

func (*Namespace) Execute

func (ns *Namespace) Execute(command interface{}, args ...interface{}) (err error)

Execute executes a func in specified namespaces, args specify arguments used by fn

type Option

type Option func(*Namespace)

func Pid

func Pid(pid int) Option

Pid specify which namespaces of process to use

func Prefix

func Prefix(prefix string) Option

Prefix specify prefix of directory /proc, which is useful while using this package in container that has bind host /proc into container

func Types

func Types(types ...int) Option

Types specify namespace types while creating Namespace instance, supports NET, UTS, IPC, USER, PID and NET.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL