diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2017-07-03 14:00:47 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2017-07-03 14:00:47 +0200 |
| commit | a7b199253f7a517fd62f36a5d632efee3bac737e (patch) | |
| tree | 9f981ff2814ed7389e973c02376f8b2618224be2 /pkg/osutil | |
| parent | 1438a6de8130467a2fcde187ba80b8a616f8daa0 (diff) | |
all: use consistent file permissions
Currently we have unix permissions for new files/dirs
hardcoded throughout the code base. Some places use 0644,
some - 0640, some - 0600 and a variety of other constants.
Introduce osutil.MkdirAll/WriteFile that use the default
permissions and use them throughout the code base.
This makes permissions consistent and also allows to easily
change the permissions later if we change our minds.
Also merge pkg/fileutil into pkg/osutil as they become
dependent on each other. The line between them was poorly
defined anyway as both operate on files.
Diffstat (limited to 'pkg/osutil')
| -rw-r--r-- | pkg/osutil/fileutil.go | 107 | ||||
| -rw-r--r-- | pkg/osutil/fileutil_linux.go | 24 | ||||
| -rw-r--r-- | pkg/osutil/fileutil_test.go | 65 | ||||
| -rw-r--r-- | pkg/osutil/osutil.go | 26 |
4 files changed, 215 insertions, 7 deletions
diff --git a/pkg/osutil/fileutil.go b/pkg/osutil/fileutil.go new file mode 100644 index 000000000..801453326 --- /dev/null +++ b/pkg/osutil/fileutil.go @@ -0,0 +1,107 @@ +// Copyright 2015 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package osutil + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "path/filepath" + "strconv" + "syscall" +) + +// CopyFile atomically copies oldFile to newFile preserving permissions and modification time. +func CopyFile(oldFile, newFile string) error { + oldf, err := os.Open(oldFile) + if err != nil { + return err + } + defer oldf.Close() + stat, err := oldf.Stat() + if err != nil { + return err + } + tmpFile := newFile + ".tmp" + newf, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, stat.Mode()&os.ModePerm) + if err != nil { + return err + } + defer newf.Close() + _, err = io.Copy(newf, oldf) + if err != nil { + return err + } + if err := newf.Close(); err != nil { + return err + } + if err := os.Chtimes(tmpFile, stat.ModTime(), stat.ModTime()); err != nil { + return err + } + return os.Rename(tmpFile, newFile) +} + +// WriteTempFile writes data to a temp file and returns its name. +func WriteTempFile(data []byte) (string, error) { + f, err := ioutil.TempFile("", "syzkaller") + if err != nil { + return "", fmt.Errorf("failed to create a temp file: %v", err) + } + if _, err := f.Write(data); err != nil { + f.Close() + os.Remove(f.Name()) + return "", fmt.Errorf("failed to write a temp file: %v", err) + } + f.Close() + return f.Name(), nil +} + +// ProcessTempDir creates a new temp dir in where and returns its path and an unique index. +// It also cleans up old, unused temp dirs after dead processes. +func ProcessTempDir(where string) (string, error) { + lk := filepath.Join(where, "instance-lock") + lkf, err := syscall.Open(lk, syscall.O_RDWR|syscall.O_CREAT, DefaultFilePerm) + if err != nil { + return "", err + } + defer syscall.Close(lkf) + if err := syscall.Flock(lkf, syscall.LOCK_EX); err != nil { + return "", err + } + defer syscall.Flock(lkf, syscall.LOCK_UN) + + for i := 0; i < 1e3; i++ { + path := filepath.Join(where, fmt.Sprintf("instance-%v", i)) + pidfile := filepath.Join(path, ".pid") + err := os.Mkdir(path, DefaultDirPerm) + if os.IsExist(err) { + // Try to clean up. + data, err := ioutil.ReadFile(pidfile) + if err == nil && len(data) > 0 { + pid, err := strconv.Atoi(string(data)) + if err == nil && pid > 1 { + if err := syscall.Kill(pid, 0); err == syscall.ESRCH { + if os.Remove(pidfile) == nil { + if os.RemoveAll(path) == nil { + i-- + continue + } + } + } + } + } + // If err != nil, assume that the pid file is not created yet. + continue + } + if err != nil { + return "", err + } + if err := WriteFile(pidfile, []byte(strconv.Itoa(syscall.Getpid()))); err != nil { + return "", err + } + return path, nil + } + return "", fmt.Errorf("too many live instances") +} diff --git a/pkg/osutil/fileutil_linux.go b/pkg/osutil/fileutil_linux.go new file mode 100644 index 000000000..415c00353 --- /dev/null +++ b/pkg/osutil/fileutil_linux.go @@ -0,0 +1,24 @@ +// Copyright 2017 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package osutil + +import ( + "io/ioutil" + "path/filepath" + "syscall" + "unsafe" +) + +// UmountAll recurusively unmounts all mounts in dir. +func UmountAll(dir string) { + files, _ := ioutil.ReadDir(dir) + for _, f := range files { + name := filepath.Join(dir, f.Name()) + if f.IsDir() { + UmountAll(name) + } + fn := []byte(name + "\x00") + syscall.Syscall(syscall.SYS_UMOUNT2, uintptr(unsafe.Pointer(&fn[0])), syscall.MNT_FORCE, 0) + } +} diff --git a/pkg/osutil/fileutil_test.go b/pkg/osutil/fileutil_test.go new file mode 100644 index 000000000..4db5c0d83 --- /dev/null +++ b/pkg/osutil/fileutil_test.go @@ -0,0 +1,65 @@ +// Copyright 2015 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package osutil + +import ( + "io/ioutil" + "os" + "path/filepath" + "strconv" + "sync" + "testing" +) + +func TestProcessTempDir(t *testing.T) { + for try := 0; try < 10; try++ { + func() { + tmp, err := ioutil.TempDir("", "syz") + if err != nil { + t.Fatalf("failed to create a temp dir: %v", err) + } + defer os.RemoveAll(tmp) + const P = 16 + // Pre-create half of the instances with stale pid. + var dirs []string + for i := 0; i < P/2; i++ { + dir, err := ProcessTempDir(tmp) + if err != nil { + t.Fatalf("failed to create process temp dir") + } + dirs = append(dirs, dir) + } + for _, dir := range dirs { + if err := WriteFile(filepath.Join(dir, ".pid"), []byte(strconv.Itoa(999999999))); err != nil { + t.Fatalf("failed to write pid file: %v", err) + } + } + // Now request a bunch of instances concurrently. + done := make(chan bool) + allDirs := make(map[string]bool) + var mu sync.Mutex + for p := 0; p < P; p++ { + go func() { + defer func() { + done <- true + }() + dir, err := ProcessTempDir(tmp) + if err != nil { + t.Fatalf("failed to create process temp dir") + } + mu.Lock() + present := allDirs[dir] + allDirs[dir] = true + mu.Unlock() + if present { + t.Fatalf("duplicate dir %v", dir) + } + }() + } + for p := 0; p < P; p++ { + <-done + } + }() + } +} diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index b63fffc1e..4a411c38e 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -7,19 +7,19 @@ import ( "bytes" "fmt" "io" + "io/ioutil" "os" "os/exec" "os/signal" "path/filepath" "syscall" "time" - - "github.com/google/syzkaller/pkg/fileutil" ) const ( DefaultDirPerm = 0755 DefaultFilePerm = 0644 + DefaultExecPerm = 0755 ) // RunCmd runs "bin args..." in dir with timeout and returns its output. @@ -121,16 +121,16 @@ func CopyFiles(srcDir, dstDir string, files []string) error { if err := os.RemoveAll(tmpDir); err != nil { return err } - if err := os.MkdirAll(tmpDir, DefaultDirPerm); err != nil { + if err := MkdirAll(tmpDir); err != nil { return err } for _, f := range files { src := filepath.Join(srcDir, filepath.FromSlash(f)) dst := filepath.Join(tmpDir, filepath.FromSlash(f)) - if err := os.MkdirAll(filepath.Dir(dst), DefaultDirPerm); err != nil { + if err := MkdirAll(filepath.Dir(dst)); err != nil { return err } - if err := fileutil.CopyFile(src, dst); err != nil { + if err := CopyFile(src, dst); err != nil { return err } } @@ -147,13 +147,13 @@ func LinkFiles(srcDir, dstDir string, files []string) error { if err := os.RemoveAll(dstDir); err != nil { return err } - if err := os.MkdirAll(dstDir, DefaultDirPerm); err != nil { + if err := MkdirAll(dstDir); err != nil { return err } for _, f := range files { src := filepath.Join(srcDir, filepath.FromSlash(f)) dst := filepath.Join(dstDir, filepath.FromSlash(f)) - if err := os.MkdirAll(filepath.Dir(dst), DefaultDirPerm); err != nil { + if err := MkdirAll(filepath.Dir(dst)); err != nil { return err } if err := os.Link(src, dst); err != nil { @@ -162,3 +162,15 @@ func LinkFiles(srcDir, dstDir string, files []string) error { } return nil } + +func MkdirAll(dir string) error { + return os.MkdirAll(dir, DefaultDirPerm) +} + +func WriteFile(filename string, data []byte) error { + return ioutil.WriteFile(filename, data, DefaultFilePerm) +} + +func WriteExecFile(filename string, data []byte) error { + return ioutil.WriteFile(filename, data, DefaultExecPerm) +} |
