Skip to content

Commit 96862af

Browse files
committed
Add auplugin_setvbuf
The new auplugin_setvbuf functionality allows providing an external memory buffer and specifies how it should be released through a new enum auplugin_mem. The state struct now holds both the internal buffer and a pointer that defaults to it, enabling seamless use of external buffers.
1 parent 47de520 commit 96862af

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

auplugin/auplugin-fgets.c

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#include <unistd.h>
2727
#include <errno.h>
2828
#include <stdlib.h>
29+
#include <sys/mman.h>
2930
#include "libaudit.h"
31+
#include "auplugin.h"
3032

3133
/*
3234
* The theory of operation for this family of functions is that it
@@ -44,21 +46,27 @@
4446
#define BUF_SIZE 8192
4547

4648
struct auplugin_fgets_state {
47-
char buffer[2*BUF_SIZE+1];
49+
char internal[2*BUF_SIZE+1];
50+
char *buffer;
4851
char *current;
4952
char *eptr;
5053
int eof;
54+
enum auplugin_mem mem_type;
55+
size_t buff_size;
5156
};
5257

5358
static struct auplugin_fgets_state global_state;
5459
static int global_init_done;
5560

5661
static void auplugin_fgets_state_init(struct auplugin_fgets_state *st)
5762
{
58-
st->buffer[0] = '\0';
63+
st->buffer = st->internal;
64+
st->internal[0] = '\0';
5965
st->current = st->buffer;
6066
st->eptr = st->buffer + (2*BUF_SIZE);
6167
st->eof = 0;
68+
st->mem_type = MEM_SELF_MANAGED;
69+
st->buff_size = 2*BUF_SIZE;
6270
}
6371

6472
struct auplugin_fgets_state *auplugin_fgets_init(void)
@@ -69,8 +77,22 @@ struct auplugin_fgets_state *auplugin_fgets_init(void)
6977
return st;
7078
}
7179

80+
7281
void auplugin_fgets_destroy(struct auplugin_fgets_state *st)
7382
{
83+
if (st->buffer != st->internal) {
84+
switch (st->mem_type) {
85+
case MEM_MALLOC:
86+
free(st->buffer);
87+
break;
88+
case MEM_MMAP:
89+
munmap(st->buffer, st->buff_size);
90+
break;
91+
case MEM_SELF_MANAGED:
92+
default:
93+
break;
94+
}
95+
}
7496
free(st);
7597
}
7698

@@ -210,3 +232,20 @@ int auplugin_fgets(char *buf, size_t blen, int fd)
210232
return auplugin_fgets_r(&global_state, buf, blen, fd);
211233
}
212234

235+
void auplugin_setvbuf_r(struct auplugin_fgets_state *st, void *buf,
236+
size_t buff_size, enum auplugin_mem how)
237+
{
238+
st->buffer = buf ? buf : st->internal;
239+
st->current = st->buffer;
240+
st->eptr = st->buffer + buff_size;
241+
st->eof = 0;
242+
st->mem_type = how;
243+
st->buff_size = buff_size;
244+
}
245+
246+
void auplugin_setvbuf(void *buf, size_t buff_size, enum auplugin_mem how)
247+
{
248+
auplugin_fgets_ensure_global();
249+
auplugin_setvbuf_r(&global_state, buf, buff_size, how);
250+
}
251+

auplugin/auplugin.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ extern "C" {
4242

4343
typedef struct auplugin_fgets_state auplugin_fgets_state_t;
4444

45+
enum auplugin_mem {
46+
MEM_MALLOC,
47+
MEM_MMAP,
48+
MEM_SELF_MANAGED
49+
};
50+
4551
void auplugin_fgets_clear(void);
4652
int auplugin_fgets_eof(void);
4753
int auplugin_fgets_more(size_t blen);
4854
int auplugin_fgets(char *buf, size_t blen, int fd)
4955
__attr_access ((__write_only__, 1, 2));
56+
void auplugin_setvbuf(void *buf, size_t buff_size, enum auplugin_mem how)
57+
__attr_access ((__read_only__, 1, 2));
5058

5159
void auplugin_fgets_destroy(auplugin_fgets_state_t *st);
5260
auplugin_fgets_state_t *auplugin_fgets_init(void)
@@ -56,6 +64,9 @@ int auplugin_fgets_eof_r(auplugin_fgets_state_t *st);
5664
int auplugin_fgets_more_r(auplugin_fgets_state_t *st, size_t blen);
5765
int auplugin_fgets_r(auplugin_fgets_state_t *st, char *buf, size_t blen, int fd)
5866
__attr_access ((__write_only__, 2, 3));
67+
void auplugin_setvbuf_r(auplugin_fgets_state_t *st, void *buf, size_t buff_size,
68+
enum auplugin_mem how)
69+
__attr_access ((__read_only__, 2, 3));
5970

6071
#ifdef __cplusplus
6172
}

docs/auplugin_fgets.3

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.TH "AUPLUGIN_FGETS" "3" "June 2025" "Red Hat" "Linux Audit API"
22
.SH NAME
3-
auplugin_fgets, auplugin_fgets_more, auplugin_fgets_eof, auplugin_fgets_clear \- buffered line reader helpers
3+
auplugin_fgets, auplugin_fgets_more, auplugin_fgets_eof, auplugin_fgets_clear, auplugin_setvbuf, auplugin_setvbuf_r \- buffered line reader helpers
44
.SH SYNOPSIS
55
.B #include <auplugin.h>
66
.sp
@@ -11,6 +11,10 @@ auplugin_fgets, auplugin_fgets_more, auplugin_fgets_eof, auplugin_fgets_clear \-
1111
.BI "int auplugin_fgets_eof(void);"
1212
.br
1313
.B void auplugin_fgets_clear(void);
14+
.br
15+
.BI "void auplugin_setvbuf(void *" buf ", size_t buff_size, enum auplugin_mem " how ");"
16+
.br
17+
.BI "void auplugin_setvbuf_r(auplugin_fgets_state_t *" st ", void *" buf ", size_t buff_size, enum auplugin_mem " how ");"
1418
.SH DESCRIPTION
1519
.B auplugin_fgets
1620
reads from
@@ -34,6 +38,25 @@ indicates whether end of file was reached on
3438
.B auplugin_fgets_clear
3539
resets the internal buffer and EOF state, discarding any stored text.
3640
.PP
41+
.B auplugin_setvbuf
42+
points the internal buffer at
43+
.I buf
44+
and sets how it will be released when
45+
.B auplugin_fgets_destroy
46+
is called. The
47+
.I how
48+
parameter should be one of
49+
.B MEM_MALLOC,
50+
.B MEM_MMAP,
51+
or
52+
.B MEM_SELF_MANAGED.
53+
The default is
54+
.B MEM_SELF_MANAGED
55+
which means no action is taken on the memory block.
56+
The reentrant form
57+
.B auplugin_setvbuf_r
58+
operates on an explicit state handle.
59+
.PP
3760
These functions maintain static state and are therefore not thread safe.
3861
.SH RETURN VALUE
3962
.B auplugin_fgets

0 commit comments

Comments
 (0)