-
Notifications
You must be signed in to change notification settings - Fork 10
/
osutil_unix.go
146 lines (136 loc) · 3.89 KB
/
osutil_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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.
// +build freebsd,!appengine netbsd,!appengine openbsd,!appengine linux,!appengine darwin,!appengine
package osutil
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
)
// 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")
}
// HandleInterrupts closes shutdown chan on first SIGINT
// (expecting that the program will gracefully shutdown and exit)
// and terminates the process on third SIGINT.
func HandleInterrupts(shutdown chan struct{}) {
go func() {
c := make(chan os.Signal, 3)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
<-c
close(shutdown)
fmt.Fprint(os.Stderr, "SIGINT: shutting down...\n")
<-c
fmt.Fprint(os.Stderr, "SIGINT: shutting down harder...\n")
<-c
fmt.Fprint(os.Stderr, "SIGINT: terminating\n")
os.Exit(int(syscall.SIGINT))
}()
}
func LongPipe() (io.ReadCloser, io.WriteCloser, error) {
r, w, err := os.Pipe()
if err != nil {
return nil, nil, fmt.Errorf("failed to create pipe: %v", err)
}
prolongPipe(r, w)
return r, w, err
}
// CreateMemMappedFile creates a temp file with the requested size and maps it into memory.
func CreateMemMappedFile(size int) (f *os.File, mem []byte, err error) {
f, err = ioutil.TempFile("./", "syzkaller-shm")
if err != nil {
err = fmt.Errorf("failed to create temp file: %v", err)
return
}
if err = f.Truncate(int64(size)); err != nil {
err = fmt.Errorf("failed to truncate shm file: %v", err)
f.Close()
os.Remove(f.Name())
return
}
f.Close()
fname := f.Name()
f, err = os.OpenFile(f.Name(), os.O_RDWR, DefaultFilePerm)
if err != nil {
err = fmt.Errorf("failed to open shm file: %v", err)
os.Remove(fname)
return
}
mem, err = syscall.Mmap(int(f.Fd()), 0, size, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
err = fmt.Errorf("failed to mmap shm file: %v", err)
f.Close()
os.Remove(f.Name())
return
}
return
}
// CloseMemMappedFile destroys memory mapping created by CreateMemMappedFile.
func CloseMemMappedFile(f *os.File, mem []byte) error {
err1 := syscall.Munmap(mem)
err2 := f.Close()
err3 := os.Remove(f.Name())
switch {
case err1 != nil:
return err1
case err2 != nil:
return err2
case err3 != nil:
return err3
default:
return nil
}
}
// ProcessExitStatus returns process exit status.
// This is here only because of fuchsia that does not implement WaitStatus.
func ProcessExitStatus(ps *os.ProcessState) int {
return ps.Sys().(syscall.WaitStatus).ExitStatus()
}