Skip to content

Commit b02c7a1

Browse files
printer: make sure output and errfile are always truncated (#2160)
Output and error files should be always truncated. It is imperative that either the output file is truncated OR that the cursor positioning (to EOF) is done before any write to the file happens. If not, then we might have situations such as: $ cat ebpf_events.json | wc -l 58010 $ cat ebpf_events.json | wc -l 58010 $ cat ebpf_events.json | wc -l 58011 $ cat ebpf_events.json | wc -l 58002 $ cat ebpf_events.json | wc -l 57989 $ cat ebpf_events.json | wc -l 57978 when the output file is truncated while being written to.
1 parent 4b94f4b commit b02c7a1

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

cmd/tracee-ebpf/flags/output.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,14 @@ func PrepareOutput(outputSlice []string) (tracee.OutputConfig, printer.Config, e
110110
} else {
111111
printcfg.OutPath = outPath
112112
fileInfo, err := os.Stat(outPath)
113-
if err == nil {
114-
if fileInfo.IsDir() {
115-
return outcfg, printcfg, fmt.Errorf("cannot use a path of existing directory %s", outPath)
116-
}
117-
} else {
118-
dir := filepath.Dir(outPath)
119-
os.MkdirAll(dir, 0755)
120-
printcfg.OutFile, err = os.Create(outPath)
121-
if err != nil {
122-
return outcfg, printcfg, fmt.Errorf("failed to create output path: %v", err)
123-
}
113+
if err == nil && fileInfo.IsDir() {
114+
return outcfg, printcfg, fmt.Errorf("cannot use a path of existing directory %s", outPath)
115+
}
116+
dir := filepath.Dir(outPath)
117+
os.MkdirAll(dir, 0755)
118+
printcfg.OutFile, err = os.Create(outPath)
119+
if err != nil {
120+
return outcfg, printcfg, fmt.Errorf("failed to create output path: %v", err)
124121
}
125122
}
126123

@@ -129,17 +126,14 @@ func PrepareOutput(outputSlice []string) (tracee.OutputConfig, printer.Config, e
129126
} else {
130127
printcfg.ErrPath = errPath
131128
fileInfo, err := os.Stat(errPath)
132-
if err == nil {
133-
if fileInfo.IsDir() {
134-
return outcfg, printcfg, fmt.Errorf("cannot use a path of existing directory %s", errPath)
135-
}
136-
} else {
137-
dir := filepath.Dir(errPath)
138-
os.MkdirAll(dir, 0755)
139-
printcfg.ErrFile, err = os.Create(errPath)
140-
if err != nil {
141-
return outcfg, printcfg, fmt.Errorf("failed to create output path: %v", err)
142-
}
129+
if err == nil && fileInfo.IsDir() {
130+
return outcfg, printcfg, fmt.Errorf("cannot use a path of existing directory %s", errPath)
131+
}
132+
dir := filepath.Dir(errPath)
133+
os.MkdirAll(dir, 0755)
134+
printcfg.ErrFile, err = os.Create(errPath)
135+
if err != nil {
136+
return outcfg, printcfg, fmt.Errorf("failed to create output path: %v", err)
143137
}
144138
}
145139

cmd/tracee-ebpf/internal/printer/printer.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ type Config struct {
4242
func New(config Config) (EventPrinter, error) {
4343
var res EventPrinter
4444
kind := config.Kind
45+
46+
if config.OutFile == nil {
47+
return res, fmt.Errorf("out file is not set")
48+
}
49+
if config.ErrFile == nil {
50+
return res, fmt.Errorf("err file is not set")
51+
}
52+
4553
switch {
4654
case kind == "ignore":
4755
res = &ignoreEventPrinter{

cmd/tracee-ebpf/main.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,6 @@ func main() {
218218

219219
}
220220

221-
if printerConfig.OutFile == nil {
222-
printerConfig.OutFile, err = os.OpenFile(printerConfig.OutPath, os.O_WRONLY, 0755)
223-
if err != nil {
224-
return err
225-
}
226-
}
227-
if printerConfig.ErrFile == nil {
228-
printerConfig.ErrFile, err = os.OpenFile(printerConfig.ErrPath, os.O_WRONLY, 0755)
229-
if err != nil {
230-
return err
231-
}
232-
}
233-
234221
printer, err := printer.New(printerConfig)
235222
if err != nil {
236223
return err

0 commit comments

Comments
 (0)