-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinuxUEKernelToolsSender.h
More file actions
159 lines (138 loc) · 4.1 KB
/
LinuxUEKernelToolsSender.h
File metadata and controls
159 lines (138 loc) · 4.1 KB
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
// Copyright Epic Games, Inc. All Rights Reserved.
// SPDX-License-Identifier: GPL-2.0-only
#pragma once
#include <unordered_map>
#include <unordered_set>
#include <sys/stat.h>
#include "LinuxUEKernelToolsShared.h"
struct FPendingProcess
{
sockaddr_un Address;
FILE* TempFile;
};
struct LinuxUEKernelToolsIPCSender : public LinuxUEKernelToolsIPC
{
const char* BasePath;
std::unordered_map<pid_t, sockaddr_un> ConnectedProcesses;
std::unordered_map<pid_t, FPendingProcess> PendingProcesses;
LinuxUEKernelToolsIPCSender(const char* InBasePath)
: LinuxUEKernelToolsIPC()
, BasePath(InBasePath)
, ConnectedProcesses()
, PendingProcesses()
{
}
bool Send(const BPFEvent* Event)
{
if (Fd == INVALID_FD || Event == nullptr)
{
return false;
}
// first try already connected processes
{
const auto Iter = ConnectedProcesses.find(Event->Pid);
if (Iter != ConnectedProcesses.end())
{
const sockaddr_un& Address = Iter->second;
return sendto(Fd, Event, sizeof(BPFEvent), 0, (const struct sockaddr*)&Address, sizeof(Address)) == sizeof(BPFEvent);
}
}
// next try already pending processes
{
auto Iter = PendingProcesses.find(Event->Pid);
if (Iter != PendingProcesses.end())
{
const FPendingProcess& PendingProcess = Iter->second;
return fwrite(Event, 1, sizeof(BPFEvent), PendingProcess.TempFile) == sizeof(BPFEvent);
}
}
sockaddr_un Address;
FillSockAddrUn(&Address, BasePath, Event->Pid);
// if socket has been created we can start sending events already
if (access(Address.sun_path, F_OK) == 0)
{
ConnectedProcesses.emplace(Event->Pid, Address);
return sendto(Fd, Event, sizeof(BPFEvent), 0, (const struct sockaddr*)&Address, sizeof(Address)) == sizeof(BPFEvent);
}
// socket doesn't exist yet so buffer to a temp file
FILE* TmpFile = tmpfile64();
if (TmpFile)
{
PendingProcesses.emplace(Event->Pid, FPendingProcess{.Address = Address, .TempFile = TmpFile});
return fwrite(Event, 1, sizeof(BPFEvent), TmpFile) == sizeof(BPFEvent);
}
else
{
UE_LOGF(LogLinuxUEKernelTools, Error, "failed to create temp file, errno=%d:%s", errno, strerror(errno));
return false;
}
}
void Update()
{
// I tried using inotify for this but /proc doesn't generate inotify events :(
std::unordered_set<pid_t> ToRemove;
for (auto& Iter : ConnectedProcesses)
{
const pid_t Pid = Iter.first;
char Path[32];
snprintf(Path, sizeof(Path), "/proc/%d", Pid);
if (access(Path, F_OK) != 0)
{
UE_LOGF(LogLinuxUEKernelTools, Log, "Connected process %d dead", Pid);
ToRemove.emplace(Pid);
}
}
for (auto Pid : ToRemove)
{
ConnectedProcesses.erase(Pid);
}
ToRemove.clear();
for (auto& Iter : PendingProcesses)
{
const pid_t Pid = Iter.first;
FPendingProcess& PendingProcess = Iter.second;
// see if socket file now exists
if (access(PendingProcess.Address.sun_path, F_OK) == 0)
{
char Buffer[MAX_EVENTS_PER_PACKET * sizeof(BPFEvent)];
fseek(PendingProcess.TempFile, 0, SEEK_SET);
while (true)
{
const size_t EventCount = fread(Buffer, sizeof(BPFEvent), MAX_EVENTS_PER_PACKET, PendingProcess.TempFile);
if (EventCount > 0)
{
UE_LOG_S("flushing %lu events", EventCount);
const size_t EventsSize = EventCount * sizeof(BPFEvent);
if (sendto(Fd, Buffer, EventsSize, 0, (const struct sockaddr*)&PendingProcess.Address, sizeof(PendingProcess.Address)) != EventsSize)
{
UE_LOG_S("sendto failed while sending buffered events, errno=%d:%s", errno, strerror(errno));
}
}
if (EventCount < MAX_EVENTS_PER_PACKET)
{
// eof
break;
}
}
fclose(PendingProcess.TempFile);
ToRemove.emplace(Pid);
ConnectedProcesses.emplace(Pid, PendingProcess.Address);
}
// if no socket file exists, check the /proc still exists
else
{
char Path[32];
snprintf(Path, sizeof(Path), "/proc/%d", Pid);
if (access(Path, F_OK) != 0)
{
UE_LOGF(LogLinuxUEKernelTools, Log, "Pending connection pid %d dead", Pid);
ToRemove.emplace(Pid);
}
}
}
for (auto Pid : ToRemove)
{
PendingProcesses.erase(Pid);
}
}
};