* "<TRUNC>" was added one byte too late, causing the stack to be overwritten.
* now makes sure the message string received is null terminated. * made buffer size depending on the syslog defines. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@16072 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright 2003, Axel Dörfler, [email protected]. All rights reserved.
|
* Copyright 2003-2006, Axel Dörfler, [email protected]. All rights reserved.
|
||||||
** Distributed under the terms of the OpenBeOS License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "SyslogDaemon.h"
|
#include "SyslogDaemon.h"
|
||||||
@@ -28,22 +28,23 @@ SyslogDaemon::SyslogDaemon()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SyslogDaemon::ReadyToRun()
|
SyslogDaemon::ReadyToRun()
|
||||||
{
|
{
|
||||||
fPort = create_port(256, SYSLOG_PORT_NAME);
|
fPort = create_port(256, SYSLOG_PORT_NAME);
|
||||||
fDaemon = spawn_thread(daemon_thread, "daemon", B_NORMAL_PRIORITY, this);
|
fDaemon = spawn_thread(daemon_thread, "daemon", B_NORMAL_PRIORITY, this);
|
||||||
resume_thread(fDaemon);
|
|
||||||
|
|
||||||
if (fPort >= B_OK && fDaemon >= B_OK) {
|
if (fPort >= B_OK && fDaemon >= B_OK) {
|
||||||
init_syslog_output(this);
|
init_syslog_output(this);
|
||||||
init_listener_output(this);
|
init_listener_output(this);
|
||||||
|
|
||||||
|
resume_thread(fDaemon);
|
||||||
} else
|
} else
|
||||||
Quit();
|
Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SyslogDaemon::AboutRequested()
|
SyslogDaemon::AboutRequested()
|
||||||
{
|
{
|
||||||
BPath path;
|
BPath path;
|
||||||
@@ -72,7 +73,7 @@ SyslogDaemon::AboutRequested()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
SyslogDaemon::QuitRequested()
|
SyslogDaemon::QuitRequested()
|
||||||
{
|
{
|
||||||
delete_port(fPort);
|
delete_port(fPort);
|
||||||
@@ -84,7 +85,7 @@ SyslogDaemon::QuitRequested()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SyslogDaemon::MessageReceived(BMessage *msg)
|
SyslogDaemon::MessageReceived(BMessage *msg)
|
||||||
{
|
{
|
||||||
switch (msg->what) {
|
switch (msg->what) {
|
||||||
@@ -109,17 +110,17 @@ SyslogDaemon::MessageReceived(BMessage *msg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SyslogDaemon::AddHandler(handler_func function)
|
SyslogDaemon::AddHandler(handler_func function)
|
||||||
{
|
{
|
||||||
fHandlers.AddItem((void *)function);
|
fHandlers.AddItem((void *)function);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SyslogDaemon::Daemon()
|
SyslogDaemon::Daemon()
|
||||||
{
|
{
|
||||||
char buffer[4096];
|
char buffer[SYSLOG_MESSAGE_BUFFER_SIZE + 1];
|
||||||
syslog_message &message = *(syslog_message *)buffer;
|
syslog_message &message = *(syslog_message *)buffer;
|
||||||
int32 code;
|
int32 code;
|
||||||
|
|
||||||
@@ -134,6 +135,9 @@ SyslogDaemon::Daemon()
|
|||||||
if (bytesRead < (ssize_t)sizeof(syslog_message) || code != SYSLOG_MESSAGE)
|
if (bytesRead < (ssize_t)sizeof(syslog_message) || code != SYSLOG_MESSAGE)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
// add terminating null byte
|
||||||
|
message.message[bytesRead - sizeof(syslog_message)] = '\0';
|
||||||
|
|
||||||
fHandlerLock.Lock();
|
fHandlerLock.Lock();
|
||||||
|
|
||||||
for (int32 i = fHandlers.CountItems(); i-- > 0;) {
|
for (int32 i = fHandlers.CountItems(); i-- > 0;) {
|
||||||
@@ -147,7 +151,7 @@ SyslogDaemon::Daemon()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int32
|
int32
|
||||||
SyslogDaemon::daemon_thread(void *data)
|
SyslogDaemon::daemon_thread(void *data)
|
||||||
{
|
{
|
||||||
((SyslogDaemon *)data)->Daemon();
|
((SyslogDaemon *)data)->Daemon();
|
||||||
@@ -155,7 +159,7 @@ SyslogDaemon::daemon_thread(void *data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
SyslogDaemon daemon;
|
SyslogDaemon daemon;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright 2003, Axel Dörfler, [email protected]. All rights reserved.
|
* Copyright 2003-2006, Axel Dörfler, [email protected]. All rights reserved.
|
||||||
** Distributed under the terms of the OpenBeOS License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "syslog_output.h"
|
#include "syslog_output.h"
|
||||||
@@ -35,6 +35,10 @@ thread_id sLastThread;
|
|||||||
int32 sRepeatCount;
|
int32 sRepeatCount;
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Creates the log file if not yet existing, or renames the old
|
||||||
|
log file, if it's too big already.
|
||||||
|
*/
|
||||||
static status_t
|
static status_t
|
||||||
prepare_output()
|
prepare_output()
|
||||||
{
|
{
|
||||||
@@ -119,7 +123,7 @@ syslog_output(syslog_message &message)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[4096];
|
char buffer[SYSLOG_MESSAGE_BUFFER_SIZE + 64];
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
// parse & nicely print the time stamp from the message
|
// parse & nicely print the time stamp from the message
|
||||||
@@ -146,11 +150,16 @@ syslog_output(syslog_message &message)
|
|||||||
pos += snprintf(buffer + pos, sizeof(buffer) - pos, "[%ld]", message.from);
|
pos += snprintf(buffer + pos, sizeof(buffer) - pos, "[%ld]", message.from);
|
||||||
|
|
||||||
// add message itself
|
// add message itself
|
||||||
int32 length = pos + snprintf(buffer + pos, sizeof(buffer) - pos, ": %s\n", message.message);
|
int32 length = pos + snprintf(buffer + pos, sizeof(buffer) - pos, ": %s\n",
|
||||||
|
message.message);
|
||||||
|
|
||||||
|
// TODO: insert header too every single line of the message
|
||||||
|
|
||||||
// ToDo: be less lazy about it - there is virtually no reason to truncate the message
|
// ToDo: be less lazy about it - there is virtually no reason to truncate the message
|
||||||
if (strlen(message.message) > sizeof(buffer) - pos - 1)
|
if (strlen(message.message) > sizeof(buffer) - pos - 1) {
|
||||||
strcpy(&buffer[sizeof(buffer) - 8], "<TRUNC>\n");
|
strcpy(&buffer[sizeof(buffer) - 9], "<TRUNC>\n");
|
||||||
|
length = sizeof(buffer) - 1;
|
||||||
|
}
|
||||||
|
|
||||||
// dump message
|
// dump message
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user