open-source-search-engine/Msg1f.cpp

115 lines
2.5 KiB
C++
Raw Normal View History

2013-08-03 00:12:24 +04:00
#include "gb-include.h"
#include "Msg1f.h"
2014-11-11 01:45:11 +03:00
static void handleRequest ( UdpSlot *slot , int32_t netnice );
2013-08-03 00:12:24 +04:00
#define LOG_WINDOW 2048
bool Msg1f::init() {
if ( ! g_udpServer.registerHandler ( 0x1f, handleRequest )) {
log(LOG_WARN, "db: logview initialization failed");
return false;
}
return true;
}
2014-11-11 01:45:11 +03:00
bool Msg1f::getLog(int32_t hostId,
int32_t numBytes,
2013-08-03 00:12:24 +04:00
void *callbackState,
void ( *callback) (void *state, UdpSlot* slot)) {
2014-11-11 01:45:11 +03:00
char* sendBuf = (char*)mmalloc(sizeof(int32_t), "Msg1fA");
2013-08-03 00:12:24 +04:00
char* p = sendBuf;
2014-11-11 01:45:11 +03:00
*(int32_t*)p = numBytes;
p += sizeof(int32_t);
2013-08-03 00:12:24 +04:00
UdpSlot *slot;
g_udpServer.sendRequest(sendBuf,
p - sendBuf,
0x1f,
g_hostdb.m_hostPtrs[hostId]->m_ip,
g_hostdb.m_hostPtrs[hostId]->m_port,
g_hostdb.m_hostPtrs[hostId]->m_hostId,
&slot,
callbackState,
callback,
5);
return false;
}
2014-11-11 01:45:11 +03:00
void handleRequest ( UdpSlot *slot , int32_t netnice ) {
2013-08-03 00:12:24 +04:00
char *p = slot->m_readBuf;
2014-11-11 01:45:11 +03:00
int32_t numBytes = *(int32_t*)p;
p += sizeof(int32_t);
2013-08-03 00:12:24 +04:00
char *filename = g_hostdb.m_logFilename;
// running just ./gb will log to stderr...
if ( strcmp(filename ,"/dev/stderr") == 0 ) {
g_errno = EBADFILE;
g_udpServer.sendErrorReply ( slot, g_errno );
return;
}
2014-11-11 01:45:11 +03:00
int32_t fd = open ( filename , O_RDONLY,
2013-08-03 00:12:24 +04:00
S_IRUSR |S_IWUSR |S_IRGRP |S_IWGRP| S_IROTH );
if ( ! fd ) {
log(LOG_DEBUG, "logviewer: Failed to open %s for reading: ",
filename);
g_errno = EBADFILE;
g_udpServer.sendErrorReply ( slot, g_errno );
return;
}
char stackSpace[LOG_WINDOW];
char *buf = stackSpace;
char *allocBuf = NULL;
2014-11-11 01:45:11 +03:00
int32_t allocBufSize = 0;
2013-08-03 00:12:24 +04:00
if(numBytes > LOG_WINDOW) {
buf = (char*)mmalloc(numBytes, "Msg1fA");
if(!buf) {
log(LOG_INFO,
2014-11-11 01:45:11 +03:00
"admin: malloc of %"INT32" bytes failed "
2013-08-03 00:12:24 +04:00
"for logview,"
" falling back on stack buffer.",
numBytes);
buf = stackSpace;
numBytes = LOG_WINDOW;
}
else {
allocBuf = buf;
allocBufSize = numBytes;
}
}
lseek(fd, -1 * numBytes, SEEK_END);
if(errno == EINVAL) {
//oops! we seeked to before the begining of the file
//log(LOG_WARN, "bad seek!");
lseek(fd, 0, SEEK_SET);
}
2014-11-11 01:45:11 +03:00
int32_t numRead = read(fd, buf, numBytes-1);
2013-08-03 00:12:24 +04:00
close(fd);
if(numRead > 0) buf[numRead-1] = '\0';
else {
buf[0] = '\0';
numRead = 0;
if(allocBuf) mfree(allocBuf, allocBufSize, "Msg1fA");
allocBufSize = 0;
allocBuf = NULL;
g_udpServer.sendErrorReply ( slot, EBADFILE );
return;
}
2014-11-11 01:45:11 +03:00
//log(LOG_DEBUG, "bytes read! %"INT32" ", numRead);
2013-08-03 00:12:24 +04:00
g_udpServer.sendReply_ass (buf, numRead, allocBuf,allocBufSize, slot); //send
}