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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
#include <sys/socket.h>
#include <sys/un.h>
#include <iostream>
#include <sstream>
#include "abrtlib.h"
#include "CommLayerInner.h"
#include "ABRTException.h"
#include "CrashTypesSocket.h"
#include "CrashWatcher.h"
#include "CommLayerServerSocket.h"
void CCommLayerServerSocket::Send(const std::string& pData, GIOChannel *pDestination)
{
ssize_t ret = -1;
gsize len = pData.length();
int offset = 0;
GError *err = NULL;
gchar* message = new gchar[len + 3];
memcpy(message, pData.c_str(), len);
message[len] = MESSAGE_END_MARKER;
message[len + 1] = '\n';
message[len + 2] = '\0';
len = 0;
while (len != strlen(message + offset))
{
offset += len;
ret = g_io_channel_write_chars(pDestination, message + offset, strlen(message + offset), &len, &err);
if (ret == G_IO_STATUS_ERROR)
{
warn_client("Error during sending data.");
}
}
g_io_channel_flush(pDestination, &err);
delete[] message;
}
std::string CCommLayerServerSocket::GetSenderUID(int pSenderSocket)
{
struct ucred creds;
socklen_t len = sizeof(creds);
if (getsockopt(pSenderSocket, SOL_SOCKET, SO_PEERCRED, &creds, &len) == -1)
{
throw CABRTException(EXCEP_ERROR, "CCommLayerServerSocket::GetSenderUID(): Error can get sender uid.");
}
std::stringstream ss;
ss << creds.uid;
return ss.str();
}
gboolean CCommLayerServerSocket::client_socket_cb(GIOChannel *source, GIOCondition condition, gpointer data)
{
CCommLayerServerSocket* serverSocket = static_cast<CCommLayerServerSocket*>(data);
std::string senderUID = serverSocket->GetSenderUID(g_io_channel_unix_get_fd(source));
gchar buff[1];
gsize len;
GIOStatus ret;
GError *err = NULL;
bool receivingMessage = true;
std::string message = "";
if (condition & G_IO_HUP ||
condition & G_IO_ERR ||
condition & G_IO_NVAL)
{
log("Socket client disconnected");
g_io_channel_unref(serverSocket->m_mapClientChannels[g_io_channel_unix_get_fd(source)]);
serverSocket->m_mapClientChannels.erase(g_io_channel_unix_get_fd(source));
return FALSE;
}
// TODO: rewrite this
while (receivingMessage)
{
ret = g_io_channel_read_chars(source, buff, 1, &len, &err);
if (ret == G_IO_STATUS_ERROR)
{
warn_client(std::string("Error while reading data from client socket: ") + err->message);
return FALSE;
}
message += buff[0];
if (message.length() > 2 &&
message[message.length() - 2] == MESSAGE_END_MARKER &&
message[message.length() - 1] == '\n')
{
receivingMessage = false;
message = message.substr(0, message.length() - 2);
}
}
serverSocket->ProcessMessage(message, source);
return TRUE;
}
gboolean CCommLayerServerSocket::server_socket_cb(GIOChannel *source, GIOCondition condition, gpointer data)
{
CCommLayerServerSocket* serverSocket = static_cast<CCommLayerServerSocket*>(data);
int socket;
struct sockaddr_un remote;
socklen_t len = sizeof(remote);
if (condition & G_IO_HUP ||
condition & G_IO_ERR ||
condition & G_IO_NVAL)
{
warn_client("Server socket error.");
return FALSE;
}
if ((socket = accept(serverSocket->m_nSocket, (struct sockaddr *)&remote, &len)) == -1)
{
warn_client("Server can not accept client.");
return TRUE;
}
log("New socket client connected");
GIOChannel* gSocket = g_io_channel_unix_new(socket);
if (!g_io_add_watch(gSocket,
static_cast<GIOCondition>(G_IO_IN |G_IO_PRI| G_IO_ERR | G_IO_HUP | G_IO_NVAL),
static_cast<GIOFunc>(client_socket_cb),
data))
{
warn_client("Can not init g_io_channel.");
return TRUE;
}
serverSocket->m_mapClientChannels[socket] = gSocket;
return TRUE;
}
void CCommLayerServerSocket::ProcessMessage(const std::string& pMessage, GIOChannel *pSource)
{
std::string UID = GetSenderUID(g_io_channel_unix_get_fd(pSource));
if (!strncmp(pMessage.c_str(), MESSAGE_GET_CRASH_INFOS, sizeof(MESSAGE_GET_CRASH_INFOS) - 1))
{
vector_crash_infos_t crashInfos = GetCrashInfos(UID);
std::string message = MESSAGE_GET_CRASH_INFOS + crash_infos_to_string(crashInfos);
Send(message, pSource);
}
else if (!strncmp(pMessage.c_str(), MESSAGE_REPORT, sizeof(MESSAGE_REPORT) - 1))
{
std::string message = pMessage.substr(sizeof(MESSAGE_REPORT) - 1);
map_crash_report_t report = string_to_crash_report(message);
Report(report, UID);
}
else if (!strncmp(pMessage.c_str(), MESSAGE_CREATE_REPORT, sizeof(MESSAGE_CREATE_REPORT) - 1))
{
// std::string UUID = pMessage.substr(sizeof(MESSAGE_CREATE_REPORT) - 1);
// map_crash_report_t crashReport = CreateReport(UUID, UID);
//use CreateReportThread instead of CreateReport?
// std::string message = MESSAGE_CREATE_REPORT + crash_report_to_string(crashReport);
// Send(message, pSource);
}
else if (!strncmp(pMessage.c_str(), MESSAGE_DELETE_DEBUG_DUMP, sizeof(MESSAGE_DELETE_DEBUG_DUMP) - 1))
{
std::string UUID = pMessage.substr(sizeof(MESSAGE_DELETE_DEBUG_DUMP) - 1);
DeleteDebugDump(UUID, UID);
}
else
{
warn_client("Received unknown message type.");
}
}
CCommLayerServerSocket::CCommLayerServerSocket()
: CCommLayerServer()
{
struct sockaddr_un local;
unlink(SOCKET_FILE);
m_nSocket = socket(AF_UNIX, SOCK_STREAM, 0);
if (m_nSocket == -1)
{
m_init_error = 1;
perror_msg("CCommLayerServerSocket: can't create AF_UNIX socket");
return;
}
fcntl(m_nSocket, F_SETFD, FD_CLOEXEC);
memset(&local, 0, sizeof(local));
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCKET_FILE);
if (bind(m_nSocket, (struct sockaddr *)&local, sizeof(local)) == -1)
{
m_init_error = 1;
perror_msg("CCommLayerServerSocket: can't bind AF_UNIX socket to '%s'", SOCKET_FILE);
return;
}
if (listen(m_nSocket, 5) == -1)
{
m_init_error = 1;
perror_msg("CCommLayerServerSocket: can't listen on AF_UNIX socket");
return;
}
chmod(SOCKET_FILE, SOCKET_PERMISSION);
m_pGSocket = g_io_channel_unix_new(m_nSocket);
if (!g_io_add_watch(m_pGSocket,
static_cast<GIOCondition>(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
static_cast<GIOFunc>(server_socket_cb),
this))
{
m_init_error = 1;
perror_msg("CCommLayerServerSocket: can't hook AF_UNIX socket to glb main loop");
return;
}
}
CCommLayerServerSocket::~CCommLayerServerSocket()
{
g_io_channel_unref(m_pGSocket);
close(m_nSocket);
}
vector_crash_infos_t CCommLayerServerSocket::GetCrashInfos(const std::string &pSender)
{
vector_crash_infos_t crashInfos;
crashInfos = ::GetCrashInfos(pSender);
return crashInfos;
}
//reimplement as CreateReportThread(...)?
//map_crash_report_t CCommLayerServerSocket::CreateReport(const std::string &pUUID,const std::string &pSender)
//{
// map_crash_report_t crashReport;
// crashReport = ::CreateReport(pUUID, pSender);
// return crashReport;
//}
report_status_t CCommLayerServerSocket::Report(const map_crash_report_t& pReport, const std::string& pSender)
{
report_status_t rs;
rs = ::Report(pReport, pSender);
return rs;
}
bool CCommLayerServerSocket::DeleteDebugDump(const std::string& pUUID, const std::string& pSender)
{
::DeleteDebugDump(pUUID, pSender);
return true;
}
void CCommLayerServerSocket::Crash(const std::string& arg1)
{
//Send("(CRASH)New Crash Detected: " + arg1);
}
void CCommLayerServerSocket::Error(const std::string& arg1)
{
//Send("(ERROR)Error: " + arg1);
}
|