Skip to content

Commit

Permalink
fix: race condition in EventsTrace ctx init
Browse files Browse the repository at this point in the history
  • Loading branch information
mmat11 committed Jan 11, 2024
1 parent a0d0a8f commit c550ab7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
10 changes: 9 additions & 1 deletion non-GPL/Events/EventsTrace/EventsTrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>

#include <arpa/inet.h>
#include <netinet/in.h>
Expand Down Expand Up @@ -109,6 +110,7 @@ uint64_t g_events_env = 0;
uint64_t g_features_env = 0;

bool g_print_features_init = 0;
bool g_features_printed = 0;
bool g_unbuffer_stdout = 0;
bool g_libbpf_verbose = 0;

Expand Down Expand Up @@ -859,6 +861,10 @@ static void out_network_connection_closed_event(struct ebpf_net_event *evt)

static int event_ctx_callback(struct ebpf_event_header *evt_hdr)
{
if (g_print_features_init)
while (!g_features_printed)
usleep(100000);

switch (evt_hdr->type) {
case EBPF_EVENT_PROCESS_FORK:
out_process_fork((struct ebpf_process_fork_event *)evt_hdr);
Expand Down Expand Up @@ -945,8 +951,10 @@ int main(int argc, char **argv)
goto out;
}

if (g_print_features_init)
if (g_print_features_init) {
print_init_msg(ebpf_event_ctx__get_features(ctx));
g_features_printed = 1;
}

while (!exiting) {
err = ebpf_event_ctx__next(ctx, 10);
Expand Down
26 changes: 24 additions & 2 deletions testing/test_bins/create_rename_delete_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

#include "common.h"
Expand All @@ -25,13 +26,34 @@ int main()
filename_orig, filename_new);

FILE *f;
// create
CHECK(f = fopen(filename_orig, "w"), NULL);

// rename
CHECK(rename(filename_orig, filename_new), -1);

// modify(permissions)
CHECK(chmod(filename_new, S_IRWXU | S_IRWXG | S_IRWXO), -1);
CHECK(fprintf(f, "test"), -1);
CHECK(ftruncate(fileno(f), 0), -1);

int fd = fileno(f);

// modify(content)
CHECK(write(fd, "test", 4), -1);

// modify(content)
struct iovec iov[2];
iov[0].iov_base = "test2";
iov[0].iov_len = 5;
iov[1].iov_base = "test3";
iov[1].iov_len = 5;
CHECK(writev(fd, iov, 2), -1);

// modify(content)
CHECK(ftruncate(fd, 0), -1);

CHECK(fclose(f), EOF);

// delete
CHECK(unlink(filename_new), -1);

return 0;
Expand Down
13 changes: 9 additions & 4 deletions testing/testrunner/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestFileDelete(et *EventsTraceInstance) {
AssertStringsEqual(fileDeleteEvent.Finfo.Type, "FILE")
AssertUint64NotEqual(fileDeleteEvent.Finfo.Inode, 0)
AssertUint64Equal(fileDeleteEvent.Finfo.Mode, 100777)
AssertUint64Equal(fileDeleteEvent.Finfo.Size, 4)
AssertUint64Equal(fileDeleteEvent.Finfo.Size, 0)
AssertUint64Equal(fileDeleteEvent.Finfo.Uid, 0)
AssertUint64Equal(fileDeleteEvent.Finfo.Gid, 0)
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func TestFileModify(et *EventsTraceInstance) {
TestFail("failed to unmarshal json", err)
}

eventsCount := 3 // chmod, write, truncate
eventsCount := 4 // chmod, write, writev, truncate
events := make([]FileModifyEvent, 0, eventsCount)
for {
var event FileModifyEvent
Expand All @@ -427,12 +427,17 @@ func TestFileModify(et *EventsTraceInstance) {
// write
AssertStringsEqual(events[1].Path, binOutput.FileNameNew)
AssertStringsEqual(events[1].ChangeType, "CONTENT")
AssertUint64Equal(events[1].Finfo.Size, 4)

// truncate
// writev
AssertStringsEqual(events[2].Path, binOutput.FileNameNew)
AssertStringsEqual(events[2].ChangeType, "CONTENT")
AssertUint64Equal(events[2].Finfo.Size, 4+5+5)

AssertTrue(events[1].Finfo.Size != events[2].Finfo.Size)
// truncate
AssertStringsEqual(events[3].Path, binOutput.FileNameNew)
AssertStringsEqual(events[3].ChangeType, "CONTENT")
AssertUint64Equal(events[3].Finfo.Size, 0)
}

func TestTtyWrite(et *EventsTraceInstance) {
Expand Down

0 comments on commit c550ab7

Please sign in to comment.