Files

109 lines
2.8 KiB
C++
Raw Permalink Normal View History

2013-08-02 13:12:24 -07:00
#include "Msg1f.h"
2016-07-27 01:15:57 +02:00
#include "UdpServer.h"
#include "Conf.h"
2013-08-02 13:12:24 -07:00
2014-11-10 14:45:11 -08:00
static void handleRequest ( UdpSlot *slot , int32_t netnice );
2013-08-02 13:12:24 -07:00
#define LOG_WINDOW 2048
bool Msg1f::init() {
2016-07-25 16:00:20 +02:00
if ( ! g_udpServer.registerHandler ( msg_type_1f, handleRequest )) {
2013-08-02 13:12:24 -07:00
log(LOG_WARN, "db: logview initialization failed");
return false;
}
return true;
}
2014-11-10 14:45:11 -08:00
bool Msg1f::getLog(int32_t hostId,
int32_t numBytes,
2013-08-02 13:12:24 -07:00
void *callbackState,
void ( *callback) (void *state, UdpSlot* slot)) {
2014-11-10 14:45:11 -08:00
char* sendBuf = (char*)mmalloc(sizeof(int32_t), "Msg1fA");
2013-08-02 13:12:24 -07:00
char* p = sendBuf;
2014-11-10 14:45:11 -08:00
*(int32_t*)p = numBytes;
p += sizeof(int32_t);
2016-08-10 13:38:05 +02:00
g_udpServer.sendRequest(sendBuf, p - sendBuf, msg_type_1f, g_hostdb.m_hostPtrs[hostId]->m_ip, g_hostdb.m_hostPtrs[hostId]->m_port, g_hostdb.m_hostPtrs[hostId]->m_hostId, NULL, callbackState, callback, 5000);
2013-08-02 13:12:24 -07:00
return false;
}
2014-11-10 14:45:11 -08:00
void handleRequest ( UdpSlot *slot , int32_t netnice ) {
2013-08-02 13:12:24 -07:00
char *p = slot->m_readBuf;
2014-11-10 14:45:11 -08:00
int32_t numBytes = *(int32_t*)p;
p += sizeof(int32_t);
2013-08-02 13:12:24 -07:00
char *filename = g_hostdb.m_logFilename;
// running just ./gb will log to stderr...
if ( strcmp(filename ,"/dev/stderr") == 0 ) {
g_errno = EBADFILE;
log(LOG_ERROR,"%s:%s:%d: call sendErrorReply.", __FILE__, __func__, __LINE__);
g_udpServer.sendErrorReply ( slot, g_errno );
return;
}
2015-09-21 12:44:41 -06:00
int32_t fd = open ( filename , O_RDONLY ,
getFileCreationFlags() );
// S_IRUSR |S_IWUSR |S_IRGRP |S_IWGRP| S_IROTH );
2013-08-02 13:12:24 -07:00
if ( ! fd ) {
log(LOG_DEBUG, "logviewer: Failed to open %s for reading: ",
filename);
g_errno = EBADFILE;
log(LOG_ERROR,"%s:%s:%d: call sendErrorReply.", __FILE__, __func__, __LINE__);
2013-08-02 13:12:24 -07:00
g_udpServer.sendErrorReply ( slot, g_errno );
return;
}
char stackSpace[LOG_WINDOW];
char *buf = stackSpace;
char *allocBuf = NULL;
2014-11-10 14:45:11 -08:00
int32_t allocBufSize = 0;
2013-08-02 13:12:24 -07:00
if(numBytes > LOG_WINDOW) {
buf = (char*)mmalloc(numBytes, "Msg1fA");
if(!buf) {
log(LOG_INFO,
"admin: malloc of %" PRId32" bytes failed "
2013-08-02 13:12:24 -07: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-10 14:45:11 -08:00
int32_t numRead = read(fd, buf, numBytes-1);
2013-08-02 13:12:24 -07: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;
log(LOG_ERROR,"%s:%s:%d: call sendErrorReply.", __FILE__, __func__, __LINE__);
2013-08-02 13:12:24 -07:00
g_udpServer.sendErrorReply ( slot, EBADFILE );
return;
}
//log(LOG_DEBUG, "bytes read! %" PRId32" ", numRead);
2013-08-02 13:12:24 -07:00
2016-08-11 17:22:00 +02:00
g_udpServer.sendReply(buf, numRead, allocBuf,allocBufSize, slot); //send
2013-08-02 13:12:24 -07:00
}