Skip to content

Commit a150dad

Browse files
committed
Optimize event formatting
In auditd-events.c, format_raw knows the length of the format_buffer. Fix the function to remember and return the length so we can get rid of a call to strlen. Optimize trimming newlines in format_raw. Turns out auparse makes its own copy of a buffer when using AUSOURCE_BUFFER. So, stop making a copy with strdup. This reduces memory churn.
1 parent d543ab5 commit a150dad

1 file changed

Lines changed: 34 additions & 26 deletions

File tree

src/auditd-event.c

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -278,20 +278,26 @@ static void replace_event_msg(struct auditd_event *e, const char *buf)
278278
* text buffer that's formatted for writing to disk. If there
279279
* is an error the return value is NULL.
280280
*/
281-
static const char *format_raw(const struct audit_reply *rep)
281+
static int format_raw(const struct audit_reply *rep)
282282
{
283-
char *ptr;
283+
char *ptr;
284+
int nlen;
284285

285286
if (rep == NULL) {
286287
if (config->node_name_format != N_NONE)
287-
snprintf(format_buf, FORMAT_BUF_LEN - 32,
288+
nlen = snprintf(format_buf, FORMAT_BUF_LEN - 32,
288289
"node=%s type=DAEMON_ERR op=format-raw msg=NULL res=failed",
289290
config->node_name);
290291
else
291-
snprintf(format_buf, MAX_AUDIT_MESSAGE_LENGTH,
292+
nlen = snprintf(format_buf, MAX_AUDIT_MESSAGE_LENGTH,
292293
"type=DAEMON_ERR op=format-raw msg=NULL res=failed");
294+
295+
if (nlen < 1) {
296+
format_buf[0] = 0;
297+
return 0;
298+
}
293299
} else {
294-
int len, nlen;
300+
int len;
295301
const char *type, *message;
296302
char unknown[32];
297303
type = audit_msg_type_to_name(rep->type);
@@ -312,23 +318,33 @@ static const char *format_raw(const struct audit_reply *rep)
312318
// MAX_AUDIT_MESSAGE_LENGTH is too small
313319
if (config->node_name_format != N_NONE)
314320
nlen = snprintf(format_buf, FORMAT_BUF_LEN - 32,
315-
"node=%s type=%s msg=%.*s\n",
321+
"node=%s type=%s msg=%.*s",
316322
config->node_name, type, len, message);
317323
else
318324
nlen = snprintf(format_buf,
319325
MAX_AUDIT_MESSAGE_LENGTH - 32,
320326
"type=%s msg=%.*s", type, len, message);
321327

328+
if (nlen < 1) {
329+
format_buf[0] = 0;
330+
return 0;
331+
}
332+
322333
/* Replace \n with space so it looks nicer. */
323334
ptr = format_buf;
324-
while ((ptr = strchr(ptr, 0x0A)) != NULL)
325-
*ptr = ' ';
335+
while (*ptr) {
336+
if (*ptr == '\n')
337+
*ptr = ' ';
338+
ptr++;
339+
}
326340

327341
/* Trim trailing space off since it wastes space */
328-
if (format_buf[nlen-1] == ' ')
342+
if (format_buf[nlen-1] == ' ') {
329343
format_buf[nlen-1] = 0;
344+
nlen--;
345+
}
330346
}
331-
return format_buf;
347+
return nlen;
332348
}
333349

334350
static int sep_done = 0;
@@ -423,33 +439,25 @@ static const char *format_enrich(const struct audit_reply *rep)
423439
} else {
424440
int rc, rtype;
425441
size_t mlen, len;
426-
char *message;
442+
427443
// Do raw format to get event started
428-
format_raw(rep);
444+
mlen = format_raw(rep);
429445

430446
// How much room is left?
431-
mlen = strlen(format_buf);
432447
len = FORMAT_BUF_LEN - mlen;
433448
if (len <= MIN_SPACE_LEFT)
434449
return format_buf;
435450

436-
// create copy to parse up
437-
format_buf[mlen] = 0x0A;
438-
format_buf[mlen+1] = 0;
439-
message = strdup(format_buf);
440-
format_buf[mlen] = 0;
441-
442451
// init auparse
443452
if (au == NULL) {
444-
au = auparse_init(AUSOURCE_BUFFER, message);
445-
if (au == NULL) {
446-
free(message);
453+
au = auparse_init(AUSOURCE_BUFFER, format_buf);
454+
if (au == NULL)
447455
return format_buf;
448-
}
456+
449457
auparse_set_escape_mode(au, AUPARSE_ESC_RAW);
450458
auparse_set_eoe_timeout(config->end_of_event_timeout);
451459
} else
452-
auparse_new_buffer(au, message, mlen+1);
460+
auparse_new_buffer(au, format_buf, mlen+1);
453461
sep_done = 0;
454462

455463
// Loop over all fields while possible to add field
@@ -503,7 +511,6 @@ static const char *format_enrich(const struct audit_reply *rep)
503511
default:
504512
break;
505513
}
506-
free(message);
507514
}
508515
return format_buf;
509516
}
@@ -515,7 +522,8 @@ void format_event(struct auditd_event *e)
515522
switch (config->log_format)
516523
{
517524
case LF_RAW:
518-
buf = format_raw(&e->reply);
525+
format_raw(&e->reply);
526+
buf = format_buf;
519527
break;
520528
case LF_ENRICHED:
521529
buf = format_enrich(&e->reply);

0 commit comments

Comments
 (0)