Skip to content

Commit 1e47a7e

Browse files
authored
Merge pull request #262 from bigbrett/logging-invocations
Logging: part 2
2 parents 33bc5c8 + c47bb19 commit 1e47a7e

File tree

11 files changed

+281
-144
lines changed

11 files changed

+281
-144
lines changed

port/posix/posix_log_file.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,6 @@
3939

4040
#ifdef WOLFHSM_CFG_LOGGING
4141

42-
/* Helper function to convert log level to string */
43-
static const char* posixLogFile_LevelToString(whLogLevel level)
44-
{
45-
switch (level) {
46-
case WH_LOG_LEVEL_INFO:
47-
return "INFO";
48-
case WH_LOG_LEVEL_ERROR:
49-
return "ERROR";
50-
case WH_LOG_LEVEL_SECEVENT:
51-
return "SECEVENT";
52-
default:
53-
return "UNKNOWN";
54-
}
55-
}
56-
5742
/* Helper function to convert string to log level */
5843
static whLogLevel posixLogFile_StringToLevel(const char* str)
5944
{
@@ -146,7 +131,7 @@ int posixLogFile_AddEntry(void* c, const whLogEntry* entry)
146131
/* Format log entry: TIMESTAMP|LEVEL|FILE:LINE|FUNCTION|MESSAGE\n */
147132
len = snprintf(buffer, sizeof(buffer), "%llu|%s|%s:%u|%s|%.*s\n",
148133
(unsigned long long)entry->timestamp,
149-
posixLogFile_LevelToString(entry->level),
134+
wh_Log_LevelToString(entry->level),
150135
entry->file ? entry->file : "", entry->line,
151136
entry->function ? entry->function : "", (int)entry->msg_len,
152137
entry->msg);

src/wh_log.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@
3636

3737
#ifdef WOLFHSM_CFG_LOGGING
3838

39+
const char* wh_Log_LevelToString(whLogLevel level)
40+
{
41+
switch (level) {
42+
case WH_LOG_LEVEL_INFO:
43+
return "INFO";
44+
case WH_LOG_LEVEL_ERROR:
45+
return "ERROR";
46+
case WH_LOG_LEVEL_SECEVENT:
47+
return "SECEVENT";
48+
default:
49+
return "UNKNOWN";
50+
}
51+
}
52+
3953
void wh_Log_AddMsg(whLogContext* ctx, whLogLevel level, const char* file,
4054
const char* function, uint32_t line, const char* src,
4155
size_t src_len)

src/wh_log_printf.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (C) 2024 wolfSSL Inc.
3+
*
4+
* This file is part of wolfHSM.
5+
*
6+
* wolfHSM is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* wolfHSM is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with wolfHSM. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
/*
20+
* src/wh_log_printf.c
21+
*
22+
* Printf-style logging backend implementation
23+
*/
24+
25+
#include <stddef.h> /* For NULL */
26+
#include <string.h> /* For memset, memcpy */
27+
28+
#include "wolfhsm/wh_settings.h"
29+
30+
#include "wolfhsm/wh_log_printf.h"
31+
#include "wolfhsm/wh_error.h"
32+
#include "wolfhsm/wh_log.h"
33+
34+
#ifdef WOLFHSM_CFG_LOGGING
35+
36+
int whLogPrintf_Init(void* c, const void* cf)
37+
{
38+
whLogPrintfContext* context = (whLogPrintfContext*)c;
39+
const whLogPrintfConfig* config = (const whLogPrintfConfig*)cf;
40+
41+
if (context == NULL) {
42+
return WH_ERROR_BADARGS;
43+
}
44+
45+
/* Initialize context */
46+
memset(context, 0, sizeof(*context));
47+
48+
/* Copy config if provided, otherwise use defaults */
49+
if (config != NULL) {
50+
context->logIfNotDebug = config->logIfNotDebug;
51+
}
52+
else {
53+
context->logIfNotDebug = 0;
54+
}
55+
56+
context->initialized = 1;
57+
58+
return WH_ERROR_OK;
59+
}
60+
61+
62+
int whLogPrintf_AddEntry(void* c, const whLogEntry* entry)
63+
{
64+
whLogPrintfContext* context = (whLogPrintfContext*)c;
65+
66+
if ((context == NULL) || (entry == NULL)) {
67+
return WH_ERROR_BADARGS;
68+
}
69+
70+
if (!context->initialized) {
71+
return WH_ERROR_ABORTED;
72+
}
73+
74+
/* Conditional logging:
75+
* - If logIfNotDebug is true: always log
76+
* - If logIfNotDebug is false: only log if WOLFHSM_CFG_DEBUG is defined
77+
*/
78+
#ifndef WOLFHSM_CFG_DEBUG
79+
if (!context->logIfNotDebug) {
80+
return WH_ERROR_OK;
81+
}
82+
#endif
83+
84+
/* Format: [TIMESTAMP] [LEVEL] [FILE:LINE FUNC] MESSAGE */
85+
(void)WOLFHSM_CFG_PRINTF(
86+
"[%llu] [%s] [%s:%u %s] %.*s\n", (unsigned long long)entry->timestamp,
87+
wh_Log_LevelToString(entry->level),
88+
(entry->file != NULL) ? entry->file : "", entry->line,
89+
(entry->function != NULL) ? entry->function : "",
90+
(entry->msg_len <= WOLFHSM_CFG_LOG_MSG_MAX)
91+
? (int)(entry->msg_len)
92+
: (int)WOLFHSM_CFG_LOG_MSG_MAX,
93+
entry->msg);
94+
95+
return WH_ERROR_OK;
96+
}
97+
98+
#endif /* WOLFHSM_CFG_LOGGING */

src/wh_server.c

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
325325
uint16_t seq = 0;
326326
uint16_t size = 0;
327327
uint8_t* data = NULL;
328+
int handlerRc = 0;
328329

329330
if (server == NULL) {
330331
return WH_ERROR_BADARGS;
@@ -342,7 +343,7 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
342343
int rc = wh_CommServer_RecvRequest(server->comm, &magic, &kind, &seq,
343344
&size, data);
344345
/* Got a packet? */
345-
if (rc == 0) {
346+
if (rc == WH_ERROR_OK) {
346347
group = WH_MESSAGE_GROUP(kind);
347348
action = WH_MESSAGE_ACTION(kind);
348349
switch (group) {
@@ -407,33 +408,56 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
407408
#endif /* WOLFHSM_CFG_CERTIFICATE_MANAGER && !WOLFHSM_CFG_NO_CRYPTO */
408409

409410
default:
410-
/* Unknown group. Return empty packet*/
411-
/* TODO: Respond with aux error flag */
411+
/* Unknown group. Return empty packet */
412+
rc = WH_ERROR_NOTIMPL;
413+
data = NULL;
412414
size = 0;
413415
}
414416

415-
/* Send a response */
416-
/* TODO: Respond with ErrorResponse if handler returns an error */
417+
/* Capture handler result for logging. The response packet already
418+
* contains the error code for the client in the resp.rc field. */
419+
handlerRc = rc;
420+
421+
/* Handle cancellation by modifying response kind */
417422
#ifdef WOLFHSM_CFG_CANCEL_API
418-
if (rc == WH_ERROR_CANCEL) {
423+
if (handlerRc == WH_ERROR_CANCEL) {
419424
/* notify the client that their request was canceled */
420425
kind = WH_MESSAGE_KIND(WH_MESSAGE_GROUP_CANCEL, 0);
421426
size = 0;
422427
data = NULL;
423-
/* reset RC so the cancellation response is sent */
424-
rc = 0;
425428
}
426429
#endif
427-
if (rc == 0) {
428-
do {
429-
rc = wh_CommServer_SendResponse(server->comm, magic, kind, seq,
430-
size, data);
431-
} while (rc == WH_ERROR_NOTREADY);
432-
}
430+
431+
/* Always send the response to the client, regardless of handler error.
432+
* The response packet contains the operational error code for the
433+
* client in the resp.rc field. */
434+
do {
435+
rc = wh_CommServer_SendResponse(server->comm, magic, kind, seq,
436+
size, data);
437+
} while (rc == WH_ERROR_NOTREADY);
438+
439+
/* Log error code from request handler, if present */
440+
WH_LOG_ON_ERROR_F(&server->log, WH_LOG_LEVEL_ERROR, handlerRc,
441+
"Handler (group=%d, action=%d, seq=%d) returned %d",
442+
group, action, seq, handlerRc);
443+
(void)handlerRc; /* suppress unused var warning */
444+
445+
/* Log error code from sending response, if present */
446+
WH_LOG_ON_ERROR_F(
447+
&server->log, WH_LOG_LEVEL_ERROR, rc,
448+
"SendResponse failed for (group=%d, action=%d, seq=%d): %d", group,
449+
action, seq, rc);
450+
451+
/* Handler errors are logged above via handlerRc but don't affect
452+
* return code. Errors from SendResponse are propagated back to the
453+
* caller in rc */
454+
}
455+
else if (rc != WH_ERROR_NOTREADY) {
456+
/* Log error code from processing request, if present */
433457
WH_LOG_ON_ERROR_F(
434458
&server->log, WH_LOG_LEVEL_ERROR, rc,
435-
"Request Handler for (group=%d, action=%d) Returned Error: %d",
436-
group, action, rc);
459+
"RecvRequest failed for (group=%d, action=%d, seq=%d): %d", group,
460+
action, seq, rc);
437461
}
438462

439463
return rc;

src/wh_server_counter.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ int wh_Server_HandleCounter(whServerContext* server, uint16_t magic,
8080
resp.counter = *counter;
8181
}
8282
resp.rc = ret;
83-
/* TODO: are there any fatal server errors? */
84-
ret = WH_ERROR_OK;
8583

8684
(void)wh_MessageCounter_TranslateInitResponse(
8785
magic, &resp, (whMessageCounter_InitResponse*)resp_packet);
@@ -139,9 +137,6 @@ int wh_Server_HandleCounter(whServerContext* server, uint16_t magic,
139137
magic, &resp, (whMessageCounter_IncrementResponse*)resp_packet);
140138

141139
*out_resp_size = sizeof(resp);
142-
143-
/* TODO: are there any fatal server errors? */
144-
ret = WH_ERROR_OK;
145140
} break;
146141

147142
case WH_COUNTER_READ: {
@@ -178,9 +173,6 @@ int wh_Server_HandleCounter(whServerContext* server, uint16_t magic,
178173
magic, &resp, (whMessageCounter_ReadResponse*)resp_packet);
179174

180175
*out_resp_size = sizeof(resp);
181-
182-
/* TODO: are there any fatal server errors? */
183-
ret = WH_ERROR_OK;
184176
} break;
185177

186178
case WH_COUNTER_DESTROY: {
@@ -211,9 +203,6 @@ int wh_Server_HandleCounter(whServerContext* server, uint16_t magic,
211203
magic, &resp, (whMessageCounter_DestroyResponse*)resp_packet);
212204

213205
*out_resp_size = sizeof(resp);
214-
215-
/* TODO: are there any fatal server errors? */
216-
ret = WH_ERROR_OK;
217206
} break;
218207

219208
default:

src/wh_server_crypto.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4239,16 +4239,6 @@ int wh_Server_HandleCryptoRequest(whServerContext* ctx, uint16_t magic,
42394239

42404240
WH_DEBUG_SERVER_VERBOSE("End ret:%d\n", ret);
42414241

4242-
/* Since crypto error codes are propagated to the client in the response
4243-
* packet, return success to the caller unless a cancellation has occurred
4244-
*/
4245-
#ifdef WOLFHSM_CFG_CANCEL_API
4246-
if (ret != WH_ERROR_CANCEL) {
4247-
ret = WH_ERROR_OK;
4248-
}
4249-
#else
4250-
ret = WH_ERROR_OK;
4251-
#endif
42524242
return ret;
42534243
}
42544244

@@ -5732,16 +5722,7 @@ int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic,
57325722

57335723

57345724
WH_DEBUG_SERVER_VERBOSE("Crypto DMA request. Action:%u\n", action);
5735-
/* Since crypto error codes are propagated to the client in the response
5736-
* packet, return success to the caller unless a cancellation has occurred
5737-
*/
5738-
#ifdef WOLFHSM_CFG_CANCEL_API
5739-
if (ret != WH_ERROR_CANCEL) {
5740-
ret = WH_ERROR_OK;
5741-
}
5742-
#else
5743-
ret = WH_ERROR_OK;
5744-
#endif
5725+
57455726
return ret;
57465727
}
57475728
#endif /* WOLFHSM_CFG_DMA */

0 commit comments

Comments
 (0)