// TODO: install functions should be defined on an interface. Composed implementations // would persist to various stores. This implementation will use mounted tmpfs, but others // might include some vault. func installKey(key *rsa.PrivateKey, location string) error { pkDirPrepTimeStart := time.Now() dir := path.Dir(location) // Create destination directory if err := syscall.Mkdir(dir, 0600); err != nil { if err != syscall.EEXIST { return err } // The directory already exists log.Printf("The key destination directory already exists.") } // with CAP_SYS_ADMIN we could create a tmpfs mount if err := syscall.Mount("tmpfs", dir, "tmpfs", 0600, "size=1M"); err != nil { log.Printf("Unable to create tmpfs mount. Do you have CAP_SYS_ADMIN? Error: %s", err) } log.Printf("[TIMER] [%s] Prepared PK storage\n", time.Since(pkDirPrepTimeStart)) keyOut, err := os.OpenFile(location, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) defer keyOut.Close() if err != nil { log.Print("failed to open key.pem for writing:", err) return nil } pkFileWriteTimeStart := time.Now() pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}) log.Printf("[TIMER] [%s] Wrote PK\n", time.Since(pkFileWriteTimeStart)) return nil }
func initLog() { log_level := l4g.INFO log_path := config.LOGGING.PATH syscall.Mkdir(filepath.Dir(log_path), 0755) log = make(l4g.Logger) log.AddFilter("file", log_level, l4g.NewFileLogWriter(log_path, true)) }
func NewRun() *Run { var r Run r.state = STOPPED // r.updatePositionMode = ACROSS_SQUARE_BORDER r.updatePositionMode = WITHIN_SQUARE_BORDER r.gridFieldNb = 10 bodies := make([]quadtree.Body, 0) // create output directory and cwd to it r.OutputDir = time.Now().Local().Format(time.RFC3339) Info.Printf("Output dir %s", r.OutputDir) syscall.Mkdir(r.OutputDir, 0777) // init the file storing the gini distribution over time filename := fmt.Sprintf(r.OutputDir + "/gini_out.csv") file, err := os.Create(filename) if err != nil { log.Fatal(err) return nil } r.giniFileLog = file // init the file storing status of the run at all steps filename = fmt.Sprintf(r.OutputDir + "/status_out.csv") file, err = os.Create(filename) if err != nil { log.Fatal(err) return nil } r.StatusFileLog = file r.Init(&bodies) return &r }
// Mkdir creates a new directory with the specified name and permission bits. // If there is an error, it will be of type *PathError. func Mkdir(name string, perm FileMode) error { e := syscall.Mkdir(name, syscallMode(perm)) if e != nil { return &PathError{"mkdir", name, e} } return nil }
// Mkdir creates a new directory with the specified name and permission bits. // It returns an error, if any. func Mkdir(name string, perm uint32) Error { e := syscall.Mkdir(name, perm) if iserror(e) { return &PathError{"mkdir", name, Errno(e)} } return nil }
// Mkdir creates a new directory with the specified name and permission bits. // It returns an error, if any. func Mkdir(name string, perm int) Error { e := syscall.Mkdir(name, perm) if e != 0 { return &PathError{"mkdir", name, Errno(e)} } return nil }
func (constor *Constor) Mkdir(input *fuse.MkdirIn, name string, out *fuse.EntryOut) (code fuse.Status) { inode := constor.inodemap.findInodePtr(input.NodeId) if inode == nil { constor.error("inode == nil") return fuse.ENOENT } constor.log("%s %s", inode.id, name) err := constor.copyup(inode) if err != nil { constor.error("copyup failed on %s : ", inode.id, err) return fuse.ToStatus(err) } dirpath := constor.getPath(0, inode.id) entrypath := Path.Join(dirpath, name) syscall.Unlink(entrypath) // remove a deleted entry err = syscall.Mkdir(entrypath, input.Mode) if err != nil { constor.error("Failed on %s : %s", entrypath, err) return fuse.ToStatus(err) } id := constor.setid(entrypath, "") if id == "" { constor.error("setid failed on %s", entrypath) return fuse.ENOENT } if err := constor.createPath(id); err != nil { constor.error("createPath failed on %s : %s", id, err) return fuse.ToStatus(err) } path := constor.getPath(0, id) err = syscall.Mkdir(path, input.Mode) if err != nil { constor.error("Mkdir failed on %s : %s", path, err) return fuse.ToStatus(err) } err = syscall.Chown(path, int(input.Uid), int(input.Gid)) if err != nil { constor.error("Chown failed on %s : %s", path, err) return fuse.ToStatus(err) } return constor.Lookup((*fuse.InHeader)(unsafe.Pointer(input)), name, out) }
func cmd_mkdir(cmd *interpreter.Interpreter) (interpreter.NextT, error) { if len(cmd.Args) <= 1 { fmt.Println("Usage: mkdir [/s] DIRECTORIES...") return interpreter.CONTINUE, nil } for _, arg1 := range cmd.Args[1:] { err := syscall.Mkdir(arg1, 0777) if err != nil { fmt.Fprintf(cmd.Stderr, "%s: %s\n", arg1, err) } } return interpreter.CONTINUE, nil }
// Mkdir creates a new directory with the specified name and permission bits. // If there is an error, it will be of type *PathError. func Mkdir(name string, perm FileMode) error { e := syscall.Mkdir(name, syscallMode(perm)) if e != nil { return &PathError{"mkdir", name, e} } // mkdir(2) itself won't handle the sticky bit on *BSD and Solaris if !supportsCreateWithStickyBit && perm&ModeSticky != 0 { Chmod(name, perm) } return nil }
func cmd_mkdir(cmd *Interpreter) (ErrorLevel, error) { if len(cmd.Args) <= 1 { fmt.Println("Usage: mkdir [/s] DIRECTORIES...") return NOERROR, nil } var errorcount ErrorLevel = 0 for _, arg1 := range cmd.Args[1:] { err := syscall.Mkdir(arg1, 0777) if err != nil { fmt.Fprintf(cmd.Stderr, "%s: %s\n", arg1, err) errorcount++ } } return errorcount, nil }
func (constor *Constor) createPath(dirpath string) error { dirs := strings.Split(dirpath, "/") if len(dirs) == 0 { return syscall.EIO } subdir := "" for _, dir := range dirs { if dir == "" { continue } subdir = Path.Join(subdir, "/", dir) li := constor.getLayer(subdir) if li == 0 { continue } if li == -1 { return syscall.EIO } stat := syscall.Stat_t{} if err := constor.Lstat(subdir, &stat); err != nil { return err } subdirl := Path.Join(constor.layers[0], subdir) if err := syscall.Mkdir(subdirl, stat.Mode); err != nil { return err } if err := syscall.Chown(subdirl, int(stat.Uid), int(stat.Gid)); err != nil { return err } if err := syscall.UtimesNano(subdirl, []syscall.Timespec{stat.Atim, stat.Mtim}); err != nil { return err } inoitoa := strconv.Itoa(int(stat.Ino)) inobyte := []byte(inoitoa) if err := syscall.Setxattr(subdirl, INOXATTR, inobyte, 0); err != nil { return err } inode, err := constor.inodemap.findInode(stat.Ino) if err != nil { return err } inode.Lock() inode.layer = 0 inode.Unlock() } return nil }
func log_init(log l4g.Logger, conf *proxy.ProxyConfig) { log_level := l4g.INFO switch conf.LOGGING.LEVEL { case "debug": log_level = l4g.DEBUG case "info": log_level = l4g.INFO case "warning": log_level = l4g.WARNING case "error": log_level = l4g.ERROR case "critical": log_level = l4g.CRITICAL } log_path := conf.LOGGING.PATH syscall.Mkdir(filepath.Dir(log_path), 0755) log.AddFilter("file", log_level, l4g.NewFileLogWriter(log_path, true)) }
func (file *File) remove( fs *Fs, path string) error { file.RWMutex.Lock() defer file.RWMutex.Unlock() // Unlink what's there. err := file.unlink() if err != nil { return err } // Make sure the parent exists. err = file.makeTree(fs, path) if err != nil { file.RWMutex.Unlock() return err } // We need to have something we can record // on. Even for files we record a directory, // this later on packs may choose to make this // into a tree and we need to be ready for that. mode := (syscall.S_IFDIR | syscall.S_IRUSR | syscall.S_IWUSR | syscall.S_IXUSR) err = syscall.Mkdir(file.write_path, uint32(mode)) if err != nil { return err } // Mark this file as deleted. err = setdelattr(file.write_path) if err != nil { return err } // We're deleted. file.write_exists = true file.write_deleted = true return nil }
func (constor *Constor) Mkdir(input *fuse.MkdirIn, name string, out *fuse.EntryOut) (code fuse.Status) { constor.log("%d %s", input.NodeId, name) path, err := constor.dentrymap.getPath(input.NodeId) if err != nil { return fuse.ToStatus(err) } if err := constor.createPath(path); err != nil { return fuse.ToStatus(err) } pathl := Path.Join(constor.layers[0], path, name) syscall.Unlink(pathl) // remove a deleted entry constor.log("mkdir(%s)", pathl) err = syscall.Mkdir(pathl, input.Mode) if err != nil { return fuse.ToStatus(err) } err = syscall.Chown(pathl, int(input.Uid), int(input.Gid)) if err != nil { return fuse.ToStatus(err) } return constor.Lookup((*fuse.InHeader)(unsafe.Pointer(input)), name, out) }
func (k *Key) SavePrivate(out *string) error { // Create destination directory dir := path.Dir(*out) if err := syscall.Mkdir(dir, 0600); err != nil { if err != syscall.EEXIST { return err } // The key destination directory already exists. } keyOut, err := os.OpenFile(*out, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) defer keyOut.Close() if err != nil { return fmt.Errorf("failed to open key.pem for writing:", err) } privateKeyPem, err := k.ExportPrivate() if _, err := keyOut.Write(privateKeyPem); err != nil { return fmt.Errorf("failed to write key.pem:", err) } return nil }
func TestInitVariables(t *testing.T) { logToFile := os.Getenv("WOODSMAN_LOGTOFILE") os.Setenv("WOODSMAN_LOGTOFILE", "TRUE") tmpdir := os.Getenv("TMPDIR") os.Setenv("TMPDIR", "/tmp/woodsman") syscall.Mkdir("/tmp/woodsman", 0777) logMsg := "THIS SHOULD BE WRITTEN TO A FILE" Info(logMsg) // read whole the file b, err := ioutil.ReadFile("/tmp/woodsman/main.test.INFO") if err != nil { t.Errorf("Expected no errors when opening log file - got %v", err) } if !strings.Contains(string(b), logMsg) { t.Errorf("Expected file to contain %v - got %v.\n", logMsg, string(b)) } os.Setenv("WOODSMAN_LOGTOFILE", logToFile) os.Setenv("TMPDIR", tmpdir) }
func main() { globalFlagset.Parse(os.Args[1:]) args := globalFlagset.Args() if len(args) > 0 { fmt.Fprintln(os.Stderr, "Wrong parameters") os.Exit(1) } if globalFlags.PrintNoNewPrivs { r1, _, err := syscall.Syscall( syscall.SYS_PRCTL, uintptr(unix.PR_GET_NO_NEW_PRIVS), uintptr(0), uintptr(0), ) fmt.Printf("no_new_privs: %v err: %v\n", r1, err) } if globalFlags.CheckMknod != "" { /* format: c:5:2:name */ dev := strings.SplitN(globalFlags.CheckMknod, ":", 4) if len(dev) < 4 { fmt.Fprintln(os.Stderr, "Not enough parameters for mknod") os.Exit(1) } typ := dev[0] major, err := strconv.Atoi(dev[1]) if err != nil { fmt.Fprintln(os.Stderr, "Wrong major") os.Exit(1) } minor, err := strconv.Atoi(dev[2]) if err != nil { fmt.Fprintln(os.Stderr, "Wrong minor") os.Exit(1) } nodeName := dev[3] majorMinor := device.Makedev(uint(major), uint(minor)) mode := uint32(0777) switch typ { case "c": mode |= syscall.S_IFCHR case "b": mode |= syscall.S_IFBLK default: fmt.Fprintln(os.Stderr, "Wrong device node type") os.Exit(1) } if err := syscall.Mknod(nodeName, mode, int(majorMinor)); err != nil { fmt.Fprintf(os.Stderr, "mknod %s: fail: %v\n", nodeName, err) os.Exit(1) } else { fmt.Printf("mknod %s: succeed\n", nodeName) os.Exit(0) } } if globalFlags.SilentSigterm { terminateCh := make(chan os.Signal, 1) signal.Notify(terminateCh, syscall.SIGTERM) go func() { <-terminateCh os.Exit(0) }() } if globalFlags.PreSleep >= 0 { time.Sleep(time.Duration(globalFlags.PreSleep) * time.Second) } if globalFlags.ReadStdin { reader := bufio.NewReader(os.Stdin) fmt.Printf("Enter text:\n") text, _ := reader.ReadString('\n') fmt.Printf("Received text: %s\n", text) } if globalFlags.CheckTty { fd := int(os.Stdin.Fd()) var termios syscall.Termios _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), syscall.TCGETS, uintptr(unsafe.Pointer(&termios)), 0, 0, 0) if err == 0 { fmt.Printf("stdin is a terminal\n") } else { fmt.Printf("stdin is not a terminal\n") } } if globalFlags.CheckPath { envBytes, err := ioutil.ReadFile("/proc/self/environ") if err != nil { fmt.Fprintf(os.Stderr, "Error reading environment from \"/proc/self/environ\": %v\n", err) os.Exit(1) } for _, v := range bytes.Split(envBytes, []byte{0}) { if len(v) == 0 { continue } if strings.HasPrefix(string(v), "PATH=") { if strings.Contains(string(v), "\n") { fmt.Fprintf(os.Stderr, "Malformed PATH: found new line") os.Exit(1) } else { fmt.Printf("PATH is good\n") os.Exit(0) } } else { continue } } fmt.Fprintf(os.Stderr, "PATH not found") os.Exit(1) } if globalFlags.PrintExec { fmt.Fprintf(os.Stdout, "inspect execed as: %s\n", os.Args[0]) } if globalFlags.PrintMsg != "" { fmt.Fprintf(os.Stdout, "%s\n", globalFlags.PrintMsg) messageLoopStr := os.Getenv("MESSAGE_LOOP") messageLoop, err := strconv.Atoi(messageLoopStr) if err == nil { for i := 0; i < messageLoop; i++ { time.Sleep(time.Second) fmt.Fprintf(os.Stdout, "%s\n", globalFlags.PrintMsg) } } } if globalFlags.PrintEnv != "" { fmt.Fprintf(os.Stdout, "%s=%s\n", globalFlags.PrintEnv, os.Getenv(globalFlags.PrintEnv)) } if globalFlags.PrintCapsPid >= 0 { caps, err := capability.NewPid(globalFlags.PrintCapsPid) if err != nil { fmt.Fprintf(os.Stderr, "Cannot get caps: %v\n", err) os.Exit(1) } fmt.Printf("Capability set: effective: %s (%s)\n", caps.StringCap(capability.EFFECTIVE), globalFlags.SuffixMsg) fmt.Printf("Capability set: permitted: %s (%s)\n", caps.StringCap(capability.PERMITTED), globalFlags.SuffixMsg) fmt.Printf("Capability set: inheritable: %s (%s)\n", caps.StringCap(capability.INHERITABLE), globalFlags.SuffixMsg) fmt.Printf("Capability set: bounding: %s (%s)\n", caps.StringCap(capability.BOUNDING), globalFlags.SuffixMsg) if capStr := os.Getenv("CAPABILITY"); capStr != "" { capInt, err := strconv.Atoi(capStr) if err != nil { fmt.Fprintf(os.Stderr, "Environment variable $CAPABILITY is not a valid capability number: %v\n", err) os.Exit(1) } c := capability.Cap(capInt) if caps.Get(capability.BOUNDING, c) { fmt.Printf("%v=enabled (%s)\n", c.String(), globalFlags.SuffixMsg) } else { fmt.Printf("%v=disabled (%s)\n", c.String(), globalFlags.SuffixMsg) } } } if globalFlags.PrintUser { fmt.Printf("User: uid=%d euid=%d gid=%d egid=%d\n", os.Getuid(), os.Geteuid(), os.Getgid(), os.Getegid()) } if globalFlags.PrintGroups { gids, err := os.Getgroups() if err != nil { fmt.Fprintf(os.Stderr, "Error getting groups: %v\n", err) os.Exit(1) } // getgroups(2): It is unspecified whether the effective group ID of // the calling process is included in the returned list. (Thus, an // application should also call getegid(2) and add or remove the // resulting value.) egid := os.Getegid() if !in(gids, egid) { gids = append(gids, egid) sort.Ints(gids) } var b bytes.Buffer for _, gid := range gids { b.WriteString(fmt.Sprintf("%d ", gid)) } fmt.Printf("Groups: %s\n", b.String()) } if globalFlags.WriteFile { fileName := os.Getenv("FILE") if globalFlags.FileName != "" { fileName = globalFlags.FileName } content := os.Getenv("CONTENT") if globalFlags.Content != "" { content = globalFlags.Content } err := ioutil.WriteFile(fileName, []byte(content), 0600) if err != nil { fmt.Fprintf(os.Stderr, "Cannot write to file %q: %v\n", fileName, err) os.Exit(1) } } if globalFlags.ReadFile { fileName := os.Getenv("FILE") if globalFlags.FileName != "" { fileName = globalFlags.FileName } dat, err := ioutil.ReadFile(fileName) if err != nil { fmt.Fprintf(os.Stderr, "Cannot read file %q: %v\n", fileName, err) os.Exit(1) } fmt.Print("<<<") fmt.Print(string(dat)) fmt.Print(">>>\n") } if globalFlags.StatFile { fileName := os.Getenv("FILE") if globalFlags.FileName != "" { fileName = globalFlags.FileName } fi, err := os.Stat(fileName) if err != nil { fmt.Fprintf(os.Stderr, "Cannot stat file %q: %v\n", fileName, err) os.Exit(1) } fmt.Printf("%s: mode: %s\n", fileName, fi.Mode().String()) fmt.Printf("%s: user: %v\n", fileName, fi.Sys().(*syscall.Stat_t).Uid) fmt.Printf("%s: group: %v\n", fileName, fi.Sys().(*syscall.Stat_t).Gid) } if globalFlags.PrintCwd { wd, err := os.Getwd() if err != nil { fmt.Fprintf(os.Stderr, "Cannot get working directory: %v\n", err) os.Exit(1) } fmt.Printf("cwd: %s\n", wd) } if globalFlags.Sleep >= 0 { time.Sleep(time.Duration(globalFlags.Sleep) * time.Second) } if globalFlags.PrintMemoryLimit { memCgroupPath, err := cgroup.GetOwnCgroupPath("memory") if err != nil { fmt.Fprintf(os.Stderr, "Error getting own memory cgroup path: %v\n", err) os.Exit(1) } // we use /proc/1/root to escape the chroot we're in and read our // memory limit limitPath := filepath.Join("/proc/1/root/sys/fs/cgroup/memory", memCgroupPath, "memory.limit_in_bytes") limit, err := ioutil.ReadFile(limitPath) if err != nil { fmt.Fprintf(os.Stderr, "Can't read memory.limit_in_bytes\n") os.Exit(1) } fmt.Printf("Memory Limit: %s\n", string(limit)) } if globalFlags.PrintCPUQuota { cpuCgroupPath, err := cgroup.GetOwnCgroupPath("cpu") if err != nil { fmt.Fprintf(os.Stderr, "Error getting own cpu cgroup path: %v\n", err) os.Exit(1) } // we use /proc/1/root to escape the chroot we're in and read our // cpu quota periodPath := filepath.Join("/proc/1/root/sys/fs/cgroup/cpu", cpuCgroupPath, "cpu.cfs_period_us") periodBytes, err := ioutil.ReadFile(periodPath) if err != nil { fmt.Fprintf(os.Stderr, "Can't read cpu.cpu_period_us\n") os.Exit(1) } quotaPath := filepath.Join("/proc/1/root/sys/fs/cgroup/cpu", cpuCgroupPath, "cpu.cfs_quota_us") quotaBytes, err := ioutil.ReadFile(quotaPath) if err != nil { fmt.Fprintf(os.Stderr, "Can't read cpu.cpu_quota_us\n") os.Exit(1) } period, err := strconv.Atoi(strings.Trim(string(periodBytes), "\n")) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } quota, err := strconv.Atoi(strings.Trim(string(quotaBytes), "\n")) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } quotaMilliCores := quota * 1000 / period fmt.Printf("CPU Quota: %s\n", strconv.Itoa(quotaMilliCores)) } if globalFlags.CheckCgroupMounts { rootCgroupPath := "/proc/1/root/sys/fs/cgroup" testPaths := []string{rootCgroupPath} // test a couple of controllers if they're available if _, err := os.Stat(filepath.Join(rootCgroupPath, "memory")); err == nil { testPaths = append(testPaths, filepath.Join(rootCgroupPath, "memory")) } if _, err := os.Stat(filepath.Join(rootCgroupPath, "cpu")); err == nil { testPaths = append(testPaths, filepath.Join(rootCgroupPath, "cpu")) } for _, p := range testPaths { if err := syscall.Mkdir(filepath.Join(p, "test"), 0600); err == nil || err != syscall.EROFS { fmt.Fprintf(os.Stderr, "check-cgroups: FAIL (%v)", err) os.Exit(1) } } fmt.Println("check-cgroups: SUCCESS") } if globalFlags.PrintNetNS { ns, err := os.Readlink("/proc/self/ns/net") if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("NetNS: %s\n", ns) } if globalFlags.PrintIPv4 != "" { iface := globalFlags.PrintIPv4 ips, err := testutils.GetIPsv4(iface) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } if len(ips) == 0 { fmt.Fprintf(os.Stderr, "No IPv4 found for interface %+v:\n", iface) os.Exit(1) } fmt.Printf("%v IPv4: %s\n", iface, ips[0]) } if globalFlags.PrintDefaultGWv4 { gw, err := testutils.GetDefaultGWv4() if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("DefaultGWv4: %s\n", gw) } if globalFlags.PrintDefaultGWv6 { gw, err := testutils.GetDefaultGWv6() if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("DefaultGWv6: %s\n", gw) } if globalFlags.PrintGWv4 != "" { // TODO: GetGW not implemented yet iface := globalFlags.PrintGWv4 gw, err := testutils.GetGWv4(iface) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("%v GWv4: %s\n", iface, gw) } if globalFlags.PrintIPv6 != "" { // TODO } if globalFlags.PrintGWv6 != "" { // TODO } if globalFlags.PrintHostname { hostname, err := os.Hostname() if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("Hostname: %s\n", hostname) } if globalFlags.ServeHTTP != "" { err := testutils.HTTPServe(globalFlags.ServeHTTP, globalFlags.ServeHTTPTimeout) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } } if globalFlags.GetHTTP != "" { body, err := testutils.HTTPGet(globalFlags.GetHTTP) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("HTTP-Get received: %s\n", body) } if globalFlags.PrintIfaceCount { ifaceCount, err := testutils.GetIfaceCount() if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("Interface count: %d\n", ifaceCount) } if globalFlags.PrintAppAnnotation != "" { mdsUrl, appName := os.Getenv("AC_METADATA_URL"), os.Getenv("AC_APP_NAME") body, err := testutils.HTTPGet(fmt.Sprintf("%s/acMetadata/v1/apps/%s/annotations/%s", mdsUrl, appName, globalFlags.PrintAppAnnotation)) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("Annotation %s=%s\n", globalFlags.PrintAppAnnotation, body) } if globalFlags.CheckMountNS { appMountNS, err := os.Readlink("/proc/self/ns/mnt") if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } s1MountNS, err := os.Readlink("/proc/1/ns/mnt") if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } if appMountNS != s1MountNS { fmt.Println("check-mountns: DIFFERENT") } else { fmt.Println("check-mountns: IDENTICAL") os.Exit(1) } } os.Exit(globalFlags.ExitCode) }
func main() { globalFlagset.Parse(os.Args[1:]) args := globalFlagset.Args() if len(args) > 0 { fmt.Fprintln(os.Stderr, "Wrong parameters") os.Exit(1) } if globalFlags.PreSleep >= 0 { time.Sleep(time.Duration(globalFlags.PreSleep) * time.Second) } if globalFlags.ReadStdin { reader := bufio.NewReader(os.Stdin) fmt.Printf("Enter text:\n") text, _ := reader.ReadString('\n') fmt.Printf("Received text: %s\n", text) } if globalFlags.CheckTty { fd := int(os.Stdin.Fd()) var termios syscall.Termios _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), syscall.TCGETS, uintptr(unsafe.Pointer(&termios)), 0, 0, 0) if err == 0 { fmt.Printf("stdin is a terminal\n") } else { fmt.Printf("stdin is not a terminal\n") } } if globalFlags.PrintMsg != "" { fmt.Fprintf(os.Stdout, "%s\n", globalFlags.PrintMsg) messageLoopStr := os.Getenv("MESSAGE_LOOP") messageLoop, err := strconv.Atoi(messageLoopStr) if err == nil { for i := 0; i < messageLoop; i++ { time.Sleep(time.Second) fmt.Fprintf(os.Stdout, "%s\n", globalFlags.PrintMsg) } } } if globalFlags.PrintEnv != "" { fmt.Fprintf(os.Stdout, "%s=%s\n", globalFlags.PrintEnv, os.Getenv(globalFlags.PrintEnv)) } if globalFlags.PrintCapsPid >= 0 { caps, err := capability.NewPid(globalFlags.PrintCapsPid) if err != nil { fmt.Fprintf(os.Stderr, "Cannot get caps: %v\n", err) os.Exit(1) return } fmt.Printf("Capability set: effective: %s\n", caps.StringCap(capability.EFFECTIVE)) fmt.Printf("Capability set: permitted: %s\n", caps.StringCap(capability.PERMITTED)) fmt.Printf("Capability set: inheritable: %s\n", caps.StringCap(capability.INHERITABLE)) fmt.Printf("Capability set: bounding: %s\n", caps.StringCap(capability.BOUNDING)) if capStr := os.Getenv("CAPABILITY"); capStr != "" { capInt, err := strconv.Atoi(capStr) if err != nil { fmt.Fprintf(os.Stderr, "Environment variable $CAPABILITY is not a valid capability number: %v\n", err) os.Exit(1) return } c := capability.Cap(capInt) if caps.Get(capability.BOUNDING, c) { fmt.Printf("%v=enabled\n", c.String()) } else { fmt.Printf("%v=disabled\n", c.String()) } } } if globalFlags.PrintUser { fmt.Printf("User: uid=%d euid=%d gid=%d egid=%d\n", os.Getuid(), os.Geteuid(), os.Getgid(), os.Getegid()) } if globalFlags.WriteFile { fileName := os.Getenv("FILE") if globalFlags.FileName != "" { fileName = globalFlags.FileName } content := os.Getenv("CONTENT") if globalFlags.Content != "" { content = globalFlags.Content } err := ioutil.WriteFile(fileName, []byte(content), 0600) if err != nil { fmt.Fprintf(os.Stderr, "Cannot write to file %q: %v\n", fileName, err) os.Exit(1) return } } if globalFlags.ReadFile { fileName := os.Getenv("FILE") if globalFlags.FileName != "" { fileName = globalFlags.FileName } dat, err := ioutil.ReadFile(fileName) if err != nil { fmt.Fprintf(os.Stderr, "Cannot read file %q: %v\n", fileName, err) os.Exit(1) return } fmt.Print("<<<") fmt.Print(string(dat)) fmt.Print(">>>\n") } if globalFlags.CheckCwd != "" { wd, err := os.Getwd() if err != nil { fmt.Fprintf(os.Stderr, "Cannot get working directory: %v\n", err) os.Exit(1) } if wd != globalFlags.CheckCwd { fmt.Fprintf(os.Stderr, "Working directory: %q. Expected: %q.\n", wd, globalFlags.CheckCwd) os.Exit(1) } } if globalFlags.Sleep >= 0 { time.Sleep(time.Duration(globalFlags.Sleep) * time.Second) } if globalFlags.PrintMemoryLimit { memCgroupPath, err := cgroup.GetOwnCgroupPath("memory") if err != nil { fmt.Fprintf(os.Stderr, "Error getting own memory cgroup path: %v\n", err) os.Exit(1) } // we use /proc/1/root to escape the chroot we're in and read our // memory limit limitPath := filepath.Join("/proc/1/root/sys/fs/cgroup/memory", memCgroupPath, "memory.limit_in_bytes") limit, err := ioutil.ReadFile(limitPath) if err != nil { fmt.Fprintf(os.Stderr, "Can't read memory.limit_in_bytes\n") os.Exit(1) } fmt.Printf("Memory Limit: %s\n", string(limit)) } if globalFlags.PrintCPUQuota { cpuCgroupPath, err := cgroup.GetOwnCgroupPath("cpu") if err != nil { fmt.Fprintf(os.Stderr, "Error getting own cpu cgroup path: %v\n", err) os.Exit(1) } // we use /proc/1/root to escape the chroot we're in and read our // cpu quota periodPath := filepath.Join("/proc/1/root/sys/fs/cgroup/cpu", cpuCgroupPath, "cpu.cfs_period_us") periodBytes, err := ioutil.ReadFile(periodPath) if err != nil { fmt.Fprintf(os.Stderr, "Can't read cpu.cpu_period_us\n") os.Exit(1) } quotaPath := filepath.Join("/proc/1/root/sys/fs/cgroup/cpu", cpuCgroupPath, "cpu.cfs_quota_us") quotaBytes, err := ioutil.ReadFile(quotaPath) if err != nil { fmt.Fprintf(os.Stderr, "Can't read cpu.cpu_quota_us\n") os.Exit(1) } period, err := strconv.Atoi(strings.Trim(string(periodBytes), "\n")) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } quota, err := strconv.Atoi(strings.Trim(string(quotaBytes), "\n")) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } quotaMilliCores := quota * 1000 / period fmt.Printf("CPU Quota: %s\n", strconv.Itoa(quotaMilliCores)) } if globalFlags.CheckCgroupMounts { rootCgroupPath := "/proc/1/root/sys/fs/cgroup" testPaths := []string{rootCgroupPath} // test a couple of controllers if they're available if cgroup.IsIsolatorSupported("memory") { testPaths = append(testPaths, filepath.Join(rootCgroupPath, "memory")) } if cgroup.IsIsolatorSupported("cpu") { testPaths = append(testPaths, filepath.Join(rootCgroupPath, "cpu")) } for _, p := range testPaths { if err := syscall.Mkdir(filepath.Join(p, "test"), 0600); err == nil || err != syscall.EROFS { fmt.Println("check-cgroups: FAIL") os.Exit(1) } } fmt.Println("check-cgroups: SUCCESS") } os.Exit(globalFlags.ExitCode) }
func (file *File) create( fs *Fs, path string, mode uint32) error { file.RWMutex.Lock() did_exist := file.exists() if file.write_exists && !file.write_deleted { file.RWMutex.Unlock() return Eexist } // Save our mode. file.mode = mode // Is it a directory? if file.mode&syscall.S_IFDIR != 0 { if file.write_exists && file.write_deleted { // Is it just marked deleted? err := file.unlink() if err != nil { file.RWMutex.Unlock() return err } } // Make sure the parent exists. err := file.makeTree(fs, path) if err != nil { file.RWMutex.Unlock() return err } // Make this directory. err = syscall.Mkdir(file.write_path, mode) if err != nil { file.RWMutex.Unlock() return err } // Fill out type. err = file.fillType(file.write_path) if err != nil { file.RWMutex.Unlock() return err } // We now exist. file.write_exists = true file.RWMutex.Unlock() } else { // Make sure the parent exists. err := file.makeTree(fs, path) if err != nil { file.RWMutex.Unlock() return err } file.RWMutex.Unlock() err = file.lockWrite(fs) if err != nil { return err } err = file.fillType(file.write_path) if err != nil { file.unlock() return err } file.unlock() } if did_exist { return Eexist } return nil }
func (inode *DirectoryInode) make(name string) error { return syscall.Mkdir(name, uint32(inode.Mode)) }
func main() { globalFlagset.Parse(os.Args[1:]) args := globalFlagset.Args() if len(args) > 0 { fmt.Fprintln(os.Stderr, "Wrong parameters") os.Exit(1) } if globalFlags.PreSleep >= 0 { time.Sleep(time.Duration(globalFlags.PreSleep) * time.Second) } if globalFlags.ReadStdin { reader := bufio.NewReader(os.Stdin) fmt.Printf("Enter text:\n") text, _ := reader.ReadString('\n') fmt.Printf("Received text: %s\n", text) } if globalFlags.CheckTty { fd := int(os.Stdin.Fd()) var termios syscall.Termios _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), syscall.TCGETS, uintptr(unsafe.Pointer(&termios)), 0, 0, 0) if err == 0 { fmt.Printf("stdin is a terminal\n") } else { fmt.Printf("stdin is not a terminal\n") } } if globalFlags.PrintExec { fmt.Fprintf(os.Stdout, "inspect execed as: %s\n", os.Args[0]) } if globalFlags.PrintMsg != "" { fmt.Fprintf(os.Stdout, "%s\n", globalFlags.PrintMsg) messageLoopStr := os.Getenv("MESSAGE_LOOP") messageLoop, err := strconv.Atoi(messageLoopStr) if err == nil { for i := 0; i < messageLoop; i++ { time.Sleep(time.Second) fmt.Fprintf(os.Stdout, "%s\n", globalFlags.PrintMsg) } } } if globalFlags.PrintEnv != "" { fmt.Fprintf(os.Stdout, "%s=%s\n", globalFlags.PrintEnv, os.Getenv(globalFlags.PrintEnv)) } if globalFlags.PrintCapsPid >= 0 { caps, err := capability.NewPid(globalFlags.PrintCapsPid) if err != nil { fmt.Fprintf(os.Stderr, "Cannot get caps: %v\n", err) os.Exit(1) } fmt.Printf("Capability set: effective: %s\n", caps.StringCap(capability.EFFECTIVE)) fmt.Printf("Capability set: permitted: %s\n", caps.StringCap(capability.PERMITTED)) fmt.Printf("Capability set: inheritable: %s\n", caps.StringCap(capability.INHERITABLE)) fmt.Printf("Capability set: bounding: %s\n", caps.StringCap(capability.BOUNDING)) if capStr := os.Getenv("CAPABILITY"); capStr != "" { capInt, err := strconv.Atoi(capStr) if err != nil { fmt.Fprintf(os.Stderr, "Environment variable $CAPABILITY is not a valid capability number: %v\n", err) os.Exit(1) } c := capability.Cap(capInt) if caps.Get(capability.BOUNDING, c) { fmt.Printf("%v=enabled\n", c.String()) } else { fmt.Printf("%v=disabled\n", c.String()) } } } if globalFlags.PrintUser { fmt.Printf("User: uid=%d euid=%d gid=%d egid=%d\n", os.Getuid(), os.Geteuid(), os.Getgid(), os.Getegid()) } if globalFlags.PrintGroups { gids, err := os.Getgroups() if err != nil { fmt.Fprintf(os.Stderr, "Error getting groups: %v\n", err) os.Exit(1) } // getgroups(2): It is unspecified whether the effective group ID of // the calling process is included in the returned list. (Thus, an // application should also call getegid(2) and add or remove the // resulting value.) egid := os.Getegid() if !in(gids, egid) { gids = append(gids, egid) sort.Ints(gids) } var b bytes.Buffer for _, gid := range gids { b.WriteString(fmt.Sprintf("%d ", gid)) } fmt.Printf("Groups: %s\n", b.String()) } if globalFlags.WriteFile { fileName := os.Getenv("FILE") if globalFlags.FileName != "" { fileName = globalFlags.FileName } content := os.Getenv("CONTENT") if globalFlags.Content != "" { content = globalFlags.Content } err := ioutil.WriteFile(fileName, []byte(content), 0600) if err != nil { fmt.Fprintf(os.Stderr, "Cannot write to file %q: %v\n", fileName, err) os.Exit(1) } } if globalFlags.ReadFile { fileName := os.Getenv("FILE") if globalFlags.FileName != "" { fileName = globalFlags.FileName } dat, err := ioutil.ReadFile(fileName) if err != nil { fmt.Fprintf(os.Stderr, "Cannot read file %q: %v\n", fileName, err) os.Exit(1) } fmt.Print("<<<") fmt.Print(string(dat)) fmt.Print(">>>\n") } if globalFlags.CheckCwd != "" { wd, err := os.Getwd() if err != nil { fmt.Fprintf(os.Stderr, "Cannot get working directory: %v\n", err) os.Exit(1) } if wd != globalFlags.CheckCwd { fmt.Fprintf(os.Stderr, "Working directory: %q. Expected: %q.\n", wd, globalFlags.CheckCwd) os.Exit(1) } } if globalFlags.Sleep >= 0 { time.Sleep(time.Duration(globalFlags.Sleep) * time.Second) } if globalFlags.PrintMemoryLimit { memCgroupPath, err := cgroup.GetOwnCgroupPath("memory") if err != nil { fmt.Fprintf(os.Stderr, "Error getting own memory cgroup path: %v\n", err) os.Exit(1) } // we use /proc/1/root to escape the chroot we're in and read our // memory limit limitPath := filepath.Join("/proc/1/root/sys/fs/cgroup/memory", memCgroupPath, "memory.limit_in_bytes") limit, err := ioutil.ReadFile(limitPath) if err != nil { fmt.Fprintf(os.Stderr, "Can't read memory.limit_in_bytes\n") os.Exit(1) } fmt.Printf("Memory Limit: %s\n", string(limit)) } if globalFlags.PrintCPUQuota { cpuCgroupPath, err := cgroup.GetOwnCgroupPath("cpu") if err != nil { fmt.Fprintf(os.Stderr, "Error getting own cpu cgroup path: %v\n", err) os.Exit(1) } // we use /proc/1/root to escape the chroot we're in and read our // cpu quota periodPath := filepath.Join("/proc/1/root/sys/fs/cgroup/cpu", cpuCgroupPath, "cpu.cfs_period_us") periodBytes, err := ioutil.ReadFile(periodPath) if err != nil { fmt.Fprintf(os.Stderr, "Can't read cpu.cpu_period_us\n") os.Exit(1) } quotaPath := filepath.Join("/proc/1/root/sys/fs/cgroup/cpu", cpuCgroupPath, "cpu.cfs_quota_us") quotaBytes, err := ioutil.ReadFile(quotaPath) if err != nil { fmt.Fprintf(os.Stderr, "Can't read cpu.cpu_quota_us\n") os.Exit(1) } period, err := strconv.Atoi(strings.Trim(string(periodBytes), "\n")) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } quota, err := strconv.Atoi(strings.Trim(string(quotaBytes), "\n")) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } quotaMilliCores := quota * 1000 / period fmt.Printf("CPU Quota: %s\n", strconv.Itoa(quotaMilliCores)) } if globalFlags.CheckCgroupMounts { rootCgroupPath := "/proc/1/root/sys/fs/cgroup" testPaths := []string{rootCgroupPath} // test a couple of controllers if they're available if cgroup.IsIsolatorSupported("memory") { testPaths = append(testPaths, filepath.Join(rootCgroupPath, "memory")) } if cgroup.IsIsolatorSupported("cpu") { testPaths = append(testPaths, filepath.Join(rootCgroupPath, "cpu")) } for _, p := range testPaths { if err := syscall.Mkdir(filepath.Join(p, "test"), 0600); err == nil || err != syscall.EROFS { fmt.Println("check-cgroups: FAIL") os.Exit(1) } } fmt.Println("check-cgroups: SUCCESS") } if globalFlags.PrintNetNS { ns, err := os.Readlink("/proc/self/ns/net") if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("NetNS: %s\n", ns) } if globalFlags.PrintIPv4 != "" { iface := globalFlags.PrintIPv4 ips, err := testutils.GetIPsv4(iface) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("%v IPv4: %s\n", iface, ips[0]) } if globalFlags.PrintDefaultGWv4 { gw, err := testutils.GetDefaultGWv4() if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("DefaultGWv4: %s\n", gw) } if globalFlags.PrintDefaultGWv6 { gw, err := testutils.GetDefaultGWv6() if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("DefaultGWv6: %s\n", gw) } if globalFlags.PrintGWv4 != "" { // TODO: GetGW not implemented yet iface := globalFlags.PrintGWv4 gw, err := testutils.GetGWv4(iface) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("%v GWv4: %s\n", iface, gw) } if globalFlags.PrintIPv6 != "" { // TODO } if globalFlags.PrintGWv6 != "" { // TODO } if globalFlags.ServeHttp != "" { err := testutils.HttpServe(globalFlags.ServeHttp, globalFlags.ServeHttpTimeout) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } } if globalFlags.GetHttp != "" { body, err := testutils.HttpGet(globalFlags.GetHttp) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Printf("HTTP-Get received: %s\n", body) } os.Exit(globalFlags.ExitCode) }