-
Notifications
You must be signed in to change notification settings - Fork 27
/
trace.go
173 lines (157 loc) · 3.21 KB
/
trace.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"errors"
"encoding/json"
"fmt"
"os"
"net/http"
"runtime"
"sort"
"strings"
"syscall"
"time"
)
const (
T_WEBDAV = 1 << iota
T_HTTP_REQUEST
T_HTTP_HEADERS
T_FUSE
T_LOCK
)
var traceOptions = uint32(0)
var timeFmt = "2006-01-02 15:04:05"
var traceFile *os.File
var traceChan = make(chan string, 8)
func startLogger (file *os.File, fileName string) {
go func() {
for {
line := <-traceChan
if file == traceFile {
// first check if file was unlinked-
// if so, stop logging.
var fi, fi2 syscall.Stat_t
err := syscall.Fstat(int(file.Fd()), &fi)
if fi.Nlink == 0 {
traceOptions = 0
syscall.Ftruncate(int(file.Fd()), 0)
file, _, _ = openDevNull()
} else {
// or, see if file was renamed.
// if so, reopen.
err = syscall.Stat(fileName, &fi2)
if err != nil || fi.Ino != fi2.Ino {
fh, err := unprivOpenFile(fileName,
os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err == nil {
file = fh
}
}
}
}
if file != nil {
file.Write([]byte(line))
}
}
}()
}
func trace(flags uint32) bool {
return (traceOptions & flags) > 0
}
func tPrintf(format string, args ...interface{}) {
t := time.Now().Format(timeFmt) + " "
s := fmt.Sprintf(format, args...)
if strings.Index(s, "\n") < 0 {
s = t + s + "\n"
} else {
lines := strings.Split(s, "\n")
l2 := []string{}
for _, l := range lines {
l2 = append(l2, t + l + "\n")
}
s = strings.Join(l2, "")
}
traceChan <- s
}
func tJson(obj interface{}) string {
r, err := json.Marshal(obj)
if err == nil {
return string(r)
}
return fmt.Sprintf("%+v", obj)
}
func tHeaders(hdrs http.Header, prefix string) string {
h := []string{}
r := []string{}
for n := range hdrs {
h = append(h, n)
}
sort.Strings(h)
for _, m := range h {
r = append(r, prefix + m + ": " + strings.Join(hdrs[m], "\n") + "\n")
}
return strings.Join(r, "")
}
func unprivOpenFile(name string, flag int, perm os.FileMode) (fh *os.File, err error) {
ruid := syscall.Getuid()
euid := syscall.Geteuid()
if ruid != euid {
defer runtime.UnlockOSThread()
runtime.LockOSThread()
err = syscall.Setreuid(euid, ruid)
if err != nil {
return
}
}
fh, err = os.OpenFile(name, flag, perm)
if ruid != euid {
err2 := syscall.Setreuid(ruid, euid)
if err2 != nil {
if err == nil {
fh.Close()
}
err = err2
}
}
return
}
func traceredirectStdoutErr() {
if traceFile == nil {
return
}
Dup2(int(traceFile.Fd()), 1)
Dup2(int(traceFile.Fd()), 2)
}
func traceOpts(opt string, fn string) (err error) {
if opt == "" {
return
}
opts := strings.Split(opt, ",")
for _, o := range(opts) {
switch o {
case "webdav":
traceOptions |= T_WEBDAV
case "httpreq":
traceOptions |= T_HTTP_REQUEST
case "httphdr":
traceOptions |= T_HTTP_HEADERS
case "fuse":
traceOptions |= T_FUSE
case "locking":
traceOptions |= T_LOCK
default:
err = errors.New("unknown trace option: " + o)
return
}
}
if traceOptions == 0 || fn == "" {
startLogger(os.Stdout, "stdout")
return
}
traceFile, err = unprivOpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err == nil {
startLogger(traceFile, fn)
} else {
startLogger(os.Stdout, "stdout")
}
return
}