2014-02-19 00:56:11 +00:00
|
|
|
package namespaces
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TIOCGPTN = 0x80045430
|
|
|
|
TIOCSPTLCK = 0x40045431
|
|
|
|
)
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Chroot(dir string) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Chroot(dir)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Chdir(dir string) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Chdir(dir)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Exec(cmd string, args []string, env []string) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Exec(cmd, args, env)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Fork() (int, error) {
|
2014-02-19 00:56:11 +00:00
|
|
|
syscall.ForkLock.Lock()
|
|
|
|
pid, _, err := syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
|
|
|
|
syscall.ForkLock.Unlock()
|
|
|
|
if err != 0 {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return int(pid), nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Mount(source, target, fstype string, flags uintptr, data string) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Mount(source, target, fstype, flags, data)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Unmount(target string, flags int) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Unmount(target, flags)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Pivotroot(newroot, putold string) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.PivotRoot(newroot, putold)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Unshare(flags int) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Unshare(flags)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Clone(flags uintptr) (int, error) {
|
2014-02-19 00:56:11 +00:00
|
|
|
syscall.ForkLock.Lock()
|
|
|
|
pid, _, err := syscall.RawSyscall(syscall.SYS_CLONE, flags, 0, 0)
|
|
|
|
syscall.ForkLock.Unlock()
|
|
|
|
if err != 0 {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return int(pid), nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Setns(fd uintptr, flags uintptr) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
_, _, err := syscall.RawSyscall(SYS_SETNS, fd, flags, 0)
|
|
|
|
if err != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func UsetCloseOnExec(fd uintptr) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
if _, _, err := syscall.Syscall(syscall.SYS_FCNTL, fd, syscall.F_SETFD, 0); err != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Setgroups(gids []int) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Setgroups(gids)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Setresgid(rgid, egid, sgid int) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Setresgid(rgid, egid, sgid)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Setresuid(ruid, euid, suid int) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Setresuid(ruid, euid, suid)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Sethostname(name string) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Sethostname([]byte(name))
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Setsid() (int, error) {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Setsid()
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Unlockpt(f *os.File) error {
|
|
|
|
var u int
|
|
|
|
return Ioctl(f.Fd(), TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Ioctl(fd uintptr, flag, data uintptr) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, flag, data); err != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Ptsname(f *os.File) (string, error) {
|
2014-02-19 00:56:11 +00:00
|
|
|
var n int
|
2014-02-19 01:52:06 +00:00
|
|
|
if err := Ioctl(f.Fd(), TIOCGPTN, uintptr(unsafe.Pointer(&n))); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("/dev/pts/%d", n), nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Openpmtx() (*os.File, error) {
|
|
|
|
return os.OpenFile("/dev/ptmx", syscall.O_RDONLY|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Closefd(fd uintptr) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Close(int(fd))
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Dup2(fd1, fd2 uintptr) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Dup2(int(fd1), int(fd2))
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Mknod(path string, mode uint32, dev int) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Mknod(path, mode, dev)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func ParentDeathSignal() error {
|
2014-02-19 00:56:11 +00:00
|
|
|
if _, _, err := syscall.RawSyscall6(syscall.SYS_PRCTL, syscall.PR_SET_PDEATHSIG, uintptr(syscall.SIGKILL), 0, 0, 0, 0); err != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Setctty() error {
|
2014-02-19 00:56:11 +00:00
|
|
|
if _, _, err := syscall.RawSyscall(syscall.SYS_IOCTL, 0, uintptr(syscall.TIOCSCTTY), 0); err != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Mkfifo(name string, mode uint32) error {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Mkfifo(name, mode)
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
func Umask(mask int) int {
|
2014-02-19 00:56:11 +00:00
|
|
|
return syscall.Umask(mask)
|
|
|
|
}
|