* "<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:
Axel Dörfler 2006-01-24 20:25:38 +00:00
parent d97476d155
commit 91845703f8
2 changed files with 35 additions and 22 deletions

View File

@ -1,7 +1,7 @@
/*
** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
/*
* Copyright 2003-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "SyslogDaemon.h"
@ -28,22 +28,23 @@ SyslogDaemon::SyslogDaemon()
}
void
void
SyslogDaemon::ReadyToRun()
{
fPort = create_port(256, SYSLOG_PORT_NAME);
fDaemon = spawn_thread(daemon_thread, "daemon", B_NORMAL_PRIORITY, this);
resume_thread(fDaemon);
if (fPort >= B_OK && fDaemon >= B_OK) {
init_syslog_output(this);
init_listener_output(this);
resume_thread(fDaemon);
} else
Quit();
}
void
void
SyslogDaemon::AboutRequested()
{
BPath path;
@ -72,7 +73,7 @@ SyslogDaemon::AboutRequested()
}
bool
bool
SyslogDaemon::QuitRequested()
{
delete_port(fPort);
@ -84,7 +85,7 @@ SyslogDaemon::QuitRequested()
}
void
void
SyslogDaemon::MessageReceived(BMessage *msg)
{
switch (msg->what) {
@ -109,17 +110,17 @@ SyslogDaemon::MessageReceived(BMessage *msg)
}
void
void
SyslogDaemon::AddHandler(handler_func function)
{
fHandlers.AddItem((void *)function);
}
void
void
SyslogDaemon::Daemon()
{
char buffer[4096];
char buffer[SYSLOG_MESSAGE_BUFFER_SIZE + 1];
syslog_message &message = *(syslog_message *)buffer;
int32 code;
@ -134,6 +135,9 @@ SyslogDaemon::Daemon()
if (bytesRead < (ssize_t)sizeof(syslog_message) || code != SYSLOG_MESSAGE)
continue;
// add terminating null byte
message.message[bytesRead - sizeof(syslog_message)] = '\0';
fHandlerLock.Lock();
for (int32 i = fHandlers.CountItems(); i-- > 0;) {
@ -147,7 +151,7 @@ SyslogDaemon::Daemon()
}
int32
int32
SyslogDaemon::daemon_thread(void *data)
{
((SyslogDaemon *)data)->Daemon();
@ -155,7 +159,7 @@ SyslogDaemon::daemon_thread(void *data)
}
int
int
main(int argc, char **argv)
{
SyslogDaemon daemon;

View File

@ -1,7 +1,7 @@
/*
** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
/*
* Copyright 2003-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "syslog_output.h"
@ -35,6 +35,10 @@ thread_id sLastThread;
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
prepare_output()
{
@ -119,7 +123,7 @@ syslog_output(syslog_message &message)
return;
}
char buffer[4096];
char buffer[SYSLOG_MESSAGE_BUFFER_SIZE + 64];
#if 0
// 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);
// 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
if (strlen(message.message) > sizeof(buffer) - pos - 1)
strcpy(&buffer[sizeof(buffer) - 8], "<TRUNC>\n");
if (strlen(message.message) > sizeof(buffer) - pos - 1) {
strcpy(&buffer[sizeof(buffer) - 9], "<TRUNC>\n");
length = sizeof(buffer) - 1;
}
// dump message