2015-10-30 16:50:14 +03:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* WinPR Logger
|
|
|
|
*
|
|
|
|
* Copyright © 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright © 2015 David FORT <contact@hardening-consulting.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2015-10-30 16:50:14 +03:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
#include "JournaldAppender.h"
|
|
|
|
|
2015-10-30 16:50:14 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <systemd/sd-journal.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/environment.h>
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2015-11-01 23:34:03 +03:00
|
|
|
{
|
2015-11-09 19:56:44 +03:00
|
|
|
WLOG_APPENDER_COMMON();
|
2019-11-06 17:24:51 +03:00
|
|
|
char* identifier;
|
|
|
|
FILE* stream;
|
2022-02-14 16:59:22 +03:00
|
|
|
} wLogJournaldAppender;
|
2015-10-30 16:50:14 +03:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_JournaldAppender_Open(wLog* log, wLogAppender* appender)
|
2015-10-30 16:50:14 +03:00
|
|
|
{
|
2015-11-01 23:34:03 +03:00
|
|
|
int fd;
|
2019-11-06 17:24:51 +03:00
|
|
|
wLogJournaldAppender* journaldAppender;
|
2015-11-01 23:34:03 +03:00
|
|
|
|
2015-10-30 16:50:14 +03:00
|
|
|
if (!log || !appender)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
journaldAppender = (wLogJournaldAppender*)appender;
|
|
|
|
if (journaldAppender->stream)
|
2015-11-01 23:34:03 +03:00
|
|
|
return TRUE;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
fd = sd_journal_stream_fd(journaldAppender->identifier, LOG_INFO, 1);
|
2015-11-01 23:34:03 +03:00
|
|
|
if (fd < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
journaldAppender->stream = fdopen(fd, "w");
|
|
|
|
if (!journaldAppender->stream)
|
2015-11-01 23:34:03 +03:00
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
setbuffer(journaldAppender->stream, NULL, 0);
|
2015-10-30 16:50:14 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_JournaldAppender_Close(wLog* log, wLogAppender* appender)
|
2015-10-30 16:50:14 +03:00
|
|
|
{
|
|
|
|
if (!log || !appender)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL WLog_JournaldAppender_WriteMessage(wLog* log, wLogAppender* appender,
|
|
|
|
wLogMessage* message)
|
2015-10-30 16:50:14 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
char* formatStr;
|
2015-11-09 19:56:44 +03:00
|
|
|
wLogJournaldAppender* journaldAppender;
|
2022-02-28 13:44:25 +03:00
|
|
|
char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
|
2015-10-30 16:50:14 +03:00
|
|
|
|
|
|
|
if (!log || !appender || !message)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
journaldAppender = (wLogJournaldAppender*)appender;
|
2015-11-09 19:56:44 +03:00
|
|
|
|
2015-10-30 16:50:14 +03:00
|
|
|
switch (message->Level)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
case WLOG_TRACE:
|
|
|
|
case WLOG_DEBUG:
|
|
|
|
formatStr = "<7>%s%s\n";
|
|
|
|
break;
|
|
|
|
case WLOG_INFO:
|
|
|
|
formatStr = "<6>%s%s\n";
|
|
|
|
break;
|
|
|
|
case WLOG_WARN:
|
|
|
|
formatStr = "<4>%s%s\n";
|
|
|
|
break;
|
|
|
|
case WLOG_ERROR:
|
|
|
|
formatStr = "<3>%s%s\n";
|
|
|
|
break;
|
|
|
|
case WLOG_FATAL:
|
|
|
|
formatStr = "<2>%s%s\n";
|
|
|
|
break;
|
|
|
|
case WLOG_OFF:
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: unknown level %" PRIu32 "\n", __FUNCTION__, message->Level);
|
|
|
|
return FALSE;
|
2015-10-30 16:50:14 +03:00
|
|
|
}
|
|
|
|
|
2016-03-11 01:41:12 +03:00
|
|
|
message->PrefixString = prefix;
|
|
|
|
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
|
|
|
|
|
|
|
|
if (message->Level != WLOG_OFF)
|
|
|
|
fprintf(journaldAppender->stream, formatStr, message->PrefixString, message->TextString);
|
2015-10-30 16:50:14 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL WLog_JournaldAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
|
|
|
|
wLogMessage* message)
|
2015-10-30 16:50:14 +03:00
|
|
|
{
|
|
|
|
if (!log || !appender || !message)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL WLog_JournaldAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
|
|
|
|
wLogMessage* message)
|
2015-10-30 16:50:14 +03:00
|
|
|
{
|
|
|
|
if (!log || !appender || !message)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL WLog_JournaldAppender_Set(wLogAppender* appender, const char* setting, void* value)
|
2015-11-09 19:56:44 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
wLogJournaldAppender* journaldAppender = (wLogJournaldAppender*)appender;
|
2015-11-09 19:56:44 +03:00
|
|
|
|
2019-10-29 13:25:44 +03:00
|
|
|
/* Just check the value string is not empty */
|
|
|
|
if (!value || (strnlen(value, 2) == 0))
|
2015-11-09 19:56:44 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (strcmp("identifier", setting))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* If the stream is already open the identifier can't be changed */
|
|
|
|
if (journaldAppender->stream)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (journaldAppender->identifier)
|
|
|
|
free(journaldAppender->identifier);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return ((journaldAppender->identifier = _strdup((const char*)value)) != NULL);
|
2015-11-09 19:56:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void WLog_JournaldAppender_Free(wLogAppender* appender)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
wLogJournaldAppender* journaldAppender;
|
2015-11-09 19:56:44 +03:00
|
|
|
if (appender)
|
|
|
|
{
|
|
|
|
journaldAppender = (wLogJournaldAppender*)appender;
|
|
|
|
if (journaldAppender->stream)
|
|
|
|
fclose(journaldAppender->stream);
|
|
|
|
free(journaldAppender->identifier);
|
|
|
|
free(journaldAppender);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wLogAppender* WLog_JournaldAppender_New(wLog* log)
|
2015-10-30 16:50:14 +03:00
|
|
|
{
|
|
|
|
wLogJournaldAppender* appender;
|
|
|
|
DWORD nSize;
|
2019-05-08 13:58:01 +03:00
|
|
|
LPCSTR name = "WLOG_JOURNALD_ID";
|
2015-10-30 16:50:14 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
appender = (wLogJournaldAppender*)calloc(1, sizeof(wLogJournaldAppender));
|
2015-10-30 16:50:14 +03:00
|
|
|
if (!appender)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
appender->Type = WLOG_APPENDER_JOURNALD;
|
2015-11-09 19:56:44 +03:00
|
|
|
appender->Open = WLog_JournaldAppender_Open;
|
|
|
|
appender->Close = WLog_JournaldAppender_Close;
|
|
|
|
appender->WriteMessage = WLog_JournaldAppender_WriteMessage;
|
|
|
|
appender->WriteDataMessage = WLog_JournaldAppender_WriteDataMessage;
|
|
|
|
appender->WriteImageMessage = WLog_JournaldAppender_WriteImageMessage;
|
|
|
|
appender->Set = WLog_JournaldAppender_Set;
|
|
|
|
appender->Free = WLog_JournaldAppender_Free;
|
2015-10-30 16:50:14 +03:00
|
|
|
|
|
|
|
nSize = GetEnvironmentVariableA(name, NULL, 0);
|
|
|
|
if (nSize)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
appender->identifier = (LPSTR)malloc(nSize);
|
2015-11-01 23:34:03 +03:00
|
|
|
if (!appender->identifier)
|
2017-03-03 14:37:27 +03:00
|
|
|
goto error_open;
|
2015-10-30 16:50:14 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (GetEnvironmentVariableA(name, appender->identifier, nSize) != nSize - 1)
|
2017-03-03 14:37:27 +03:00
|
|
|
goto error_open;
|
2015-11-01 23:34:03 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!WLog_JournaldAppender_Open(log, (wLogAppender*)appender))
|
2015-11-01 23:34:03 +03:00
|
|
|
goto error_open;
|
2015-10-30 16:50:14 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return (wLogAppender*)appender;
|
2015-10-30 16:50:14 +03:00
|
|
|
|
2015-11-01 23:34:03 +03:00
|
|
|
error_open:
|
|
|
|
free(appender->identifier);
|
2015-10-30 16:50:14 +03:00
|
|
|
free(appender);
|
|
|
|
return NULL;
|
|
|
|
}
|