diff options
author | We-unite <3205135446@qq.com> | 2024-07-23 19:32:09 +0800 |
---|---|---|
committer | We-unite <3205135446@qq.com> | 2024-07-25 17:09:45 +0800 |
commit | fc61a4a525846fa31ee2288df4e82f745bb39c95 (patch) | |
tree | e97c7b942c7e843782efbcc48882e6c0854df473 /src/mongo.go | |
parent | cf5618ff2e2a183c5bdf6444787dccdcbf26ce76 (diff) | |
download | godo-fc61a4a525846fa31ee2288df4e82f745bb39c95.tar.gz godo-fc61a4a525846fa31ee2288df4e82f745bb39c95.zip |
Try ot fix the out-of-order bug, add EXECVE to itthings_left
The Most important work during this time is to find out solution
to the out-of-order bug. Discribe it here in detail: info from
audit may be out of order, which means fork may comes after execve,
even after exit. What an absurd penomenon to see a process not yet
created to work or exit!
To deal with this problem, I've tried several ways:
- in the 2nd coroutine, when EOE msg comes, if it's a fork/clone
event, send it immediately, otherwise wait for some time(such as
100 ms). But after all it delays longer, and has other problems.
- the 2nd coroutine doesn't send directly, but record all the finished
event id in a slice, and another thread checks once every one second,
if there are sth in slice, send corresponding events in the order of
event id. But: event that happens first doesn't always has lower id
or time, for example, 1 forks 2, then 2 execve, the audit in kernel
it self may gets execve before fork(maybe fork makes other settings),
which means execve has earlier timestamp and lower event id. The out-
of-order problem is not completely resolved. If we then add delays
to non-clone event, a more serious problem happens: we must use mutex
to lock the slice recording finished event id to prevent crush between
send thread and wait thread, but the wait thread can't get the mutex
again, because there are to much clone event and frequent send!
- So I use no delay but mongodb, when an execve comes, if pid is not
recorded, just insert it and wait for the fork. It does works, but
some other works is still left to do:
- what should i do if 2 forks 3 comes before 1 forks 2? Now I
suggest it doesn't happen, but what if?
- when execve comes before fork, i recorded it, but if this process
has a parent i don't care, delete, or stays there?
Also, as mentioned above, I've add EXECVE field in process into db,
records all the execve(time, and args) from the same process. Besides,
exit_timestamp and exit_code can be caught now, but too many process
has no exit info. This is also to be fixed.
Now, let's listen to the file changed by process. Don't forget the
to-do works listed above!
Diffstat (limited to 'src/mongo.go')
-rw-r--r-- | src/mongo.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/mongo.go b/src/mongo.go new file mode 100644 index 0000000..d00abd2 --- /dev/null +++ b/src/mongo.go | |||
@@ -0,0 +1,79 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "context" | ||
5 | "time" | ||
6 | |||
7 | "go.mongodb.org/mongo-driver/bson" | ||
8 | "go.mongodb.org/mongo-driver/mongo" | ||
9 | "go.mongodb.org/mongo-driver/mongo/options" | ||
10 | ) | ||
11 | |||
12 | type mongoClient struct { | ||
13 | dbName, colName string | ||
14 | client *mongo.Client | ||
15 | col *mongo.Collection | ||
16 | } | ||
17 | |||
18 | func (mc *mongoClient) Connect(dbName, colName string) error { | ||
19 | var err error | ||
20 | mc.client, err = mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017")) | ||
21 | |||
22 | if err != nil { | ||
23 | return err | ||
24 | } | ||
25 | |||
26 | ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) | ||
27 | err = mc.client.Connect(ctx) | ||
28 | if err != nil { | ||
29 | return err | ||
30 | } | ||
31 | |||
32 | mc.col = mc.client.Database(dbName).Collection(colName) | ||
33 | mc.dbName = dbName | ||
34 | mc.colName = colName | ||
35 | return nil | ||
36 | } | ||
37 | |||
38 | func (mc *mongoClient) InsertOne(document interface{}) error { | ||
39 | _, err := mc.col.InsertOne(context.Background(), document) | ||
40 | return err | ||
41 | } | ||
42 | |||
43 | func (mc *mongoClient) UpdateOne(filter, update interface{}) error { | ||
44 | _, err := mc.col.UpdateOne(context.Background(), filter, update) | ||
45 | return err | ||
46 | } | ||
47 | |||
48 | func (mc *mongoClient) UpdateMany(filter, update interface{}) error { | ||
49 | _, err := mc.col.UpdateMany(context.Background(), filter, update) | ||
50 | return err | ||
51 | } | ||
52 | |||
53 | func (mc *mongoClient) Finddoc(filter bson.M) ([]bson.M, error) { | ||
54 | cur, err := mc.col.Find(context.Background(), filter) | ||
55 | if err != nil { | ||
56 | return nil, err | ||
57 | } | ||
58 | |||
59 | var results []bson.M | ||
60 | err = cur.All(context.Background(), &results) | ||
61 | |||
62 | return results, err | ||
63 | } | ||
64 | |||
65 | func (mc *mongoClient) Drop() error { | ||
66 | return mc.col.Drop(context.Background()) | ||
67 | } | ||
68 | |||
69 | func (mc *mongoClient) Disconnect() error { | ||
70 | err := mc.client.Disconnect(context.Background()) | ||
71 | if err != nil { | ||
72 | return err | ||
73 | } | ||
74 | mc.col = nil | ||
75 | mc.client = nil | ||
76 | mc.dbName = "" | ||
77 | mc.colName = "" | ||
78 | return nil | ||
79 | } | ||