func createDevices(rootDir string, uid, gid int) error { nullDir := fp.Join(rootDir, os.DevNull) if err := osutil.Mknod(nullDir, unix.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil { return err } if err := os.Lchown(nullDir, uid, gid); err != nil { log.Debugf("Failed to lchown %s: %s\n", nullDir, err) return err } zeroDir := fp.Join(rootDir, "/dev/zero") if err := osutil.Mknod(zeroDir, unix.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil { return err } if err := os.Lchown(zeroDir, uid, gid); err != nil { log.Debugf("Failed to lchown %s: %s\n", zeroDir, err) return err } for _, f := range []string{"/dev/random", "/dev/urandom"} { randomDir := fp.Join(rootDir, f) if err := osutil.Mknod(randomDir, unix.S_IFCHR|uint32(os.FileMode(0666)), 1*256+9); err != nil { return err } if err := os.Lchown(randomDir, uid, gid); err != nil { log.Debugf("Failed to lchown %s: %s\n", randomDir, err) return err } } return nil }
// Mknod unless path does not exists. func Mknod(path string, mode uint32, dev int) error { if ExistsFile(path) { return nil } log.Debugf("mknod %s %d %d", path, mode, dev) if err := unix.Mknod(path, mode, dev); err != nil { return err } return nil }
// Symlink, but ignore already exists file. func Symlink(oldname, newname string) error { log.Debug("symlink", oldname, newname) if err := os.Symlink(oldname, newname); err != nil { // Ignore already created symlink if _, ok := err.(*os.LinkError); !ok { log.Debugf("Failed to symlink %s %s: %s", oldname, newname, err) return err } } return nil }
func RunCmd(name string, arg ...string) error { log.Debug("runcmd: ", name, arg) out, err := exec.Command(name, arg...).CombinedOutput() if len(out) > 0 { log.Debug(string(out)) } if err != nil { log.Debugf("Failed to exec %s %s: %s", name, arg, err) return err } return nil }
func IsDirEmpty(dir string) bool { f, err := os.Open(dir) if err != nil { log.Debugf("Failed to open %s: %s\n", dir, err) return false } defer f.Close() _, err = f.Readdirnames(1) if err == io.EOF { return true } return false }
func Setuid(id int) error { log.Debugf("setuid %d\n", id) return system.Setuid(id) }