Skip to content

Commit 755c827

Browse files
Add feed_has_data, get_record_num, and get/goto_field_num to python bindings
1 parent a483abf commit 755c827

File tree

2 files changed

+115
-12
lines changed

2 files changed

+115
-12
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- In ausearch, correct subject/object search to be an and if both are given
1010
- Adjust formats for 64 bit time_t
1111
- Fix segfault in python bindings around the feed API
12+
- Add feed_has_data, get_record_num, and get/goto_field_num to python bindings
1213

1314
3.1.1
1415
- Add user friendly keywords for signals to auditctl

bindings/python/auparse_python.c

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ void callback_data_destroy(void *user_data)
284284
}
285285
}
286286

287+
/*
288+
* This function is hard coded into the python bindings for the
289+
* AuParser_add_callback function as the receiver of any callbacks. It
290+
* gets the data from auparse and builds up a python function call based
291+
* on the saved data set during the add callback.
292+
*/
287293
static void auparse_callback(auparse_state_t *au,
288294
auparse_cb_event_t cb_event_type, void *user_data)
289295
{
@@ -528,6 +534,25 @@ AuParser_feed(AuParser *self, PyObject *args)
528534
return NULL;
529535
}
530536

537+
/********************************
538+
* auparse_feed_age_events
539+
********************************/
540+
PyDoc_STRVAR(feed_age_events_doc,
541+
"feed_age_events() age events by the clock\n\
542+
\n\
543+
feed_age_events() should be called to timeout events by the clock.\n\
544+
Any newly complete events will be sent to the callback function.\n\
545+
\n\
546+
Returns None.\n\
547+
");
548+
static PyObject *
549+
AuParser_feed_age_events(AuParser *self)
550+
{
551+
PARSER_CHECK;
552+
auparse_feed_age_events(self->au);
553+
Py_RETURN_NONE;
554+
}
555+
531556
/********************************
532557
* auparse_flush_feed
533558
********************************/
@@ -571,22 +596,21 @@ AuParser_feed_has_data(AuParser *self)
571596
}
572597

573598
/********************************
574-
* auparse_feed_age_events
599+
* auparse_feed_has_data
575600
********************************/
576-
PyDoc_STRVAR(feed_age_events_doc,
577-
"feed_age_events() age events by the clock\n\
578-
\n\
579-
feed_age_events() should be called to timeout events by the clock.\n\
580-
Any newly complete events will be sent to the callback function.\n\
601+
PyDoc_STRVAR(feed_has_ready_event_doc,
602+
"feed_has_ready_event() determines if there are any events that are\n\
603+
ready to emit.\n\
581604
\n\
582-
Returns None.\n\
605+
Returns True if event is ready and false otherwise.\n\
583606
");
584607
static PyObject *
585-
AuParser_feed_age_events(AuParser *self)
608+
AuParser_feed_has_ready_event(AuParser *self)
586609
{
587610
PARSER_CHECK;
588-
auparse_feed_age_events(self->au);
589-
Py_RETURN_NONE;
611+
if (auparse_feed_has_ready_event(self->au) == 0)
612+
Py_RETURN_FALSE;
613+
Py_RETURN_TRUE;
590614
}
591615

592616
/********************************
@@ -1023,7 +1047,7 @@ No Return value, raises exception (EnvironmentError) on error.\n\
10231047
static PyObject *
10241048
AuParser_search_add_regex(AuParser *self, PyObject *args)
10251049
{
1026-
const char* regexp;
1050+
const char *regexp;
10271051
int result;
10281052

10291053
if (!PyArg_ParseTuple(args, "s", &regexp)) return NULL;
@@ -1680,6 +1704,28 @@ AuParser_next_record(AuParser *self)
16801704
return NULL;
16811705
}
16821706

1707+
/********************************
1708+
* auparse_get_record_num
1709+
********************************/
1710+
PyDoc_STRVAR(get_record_num_doc,
1711+
"get_record_num() get one based record number where auparse is currently at\n\
1712+
The record numbering will reset back to 1 each time a new event is processed.\n\
1713+
Raises exception (RuntimeError) on error.\n\
1714+
");
1715+
static PyObject *
1716+
AuParser_get_record_num(AuParser *self)
1717+
{
1718+
unsigned int value;
1719+
1720+
PARSER_CHECK;
1721+
value = auparse_get_record_num(self->au);
1722+
if (value == 0) {
1723+
PyErr_SetString(PyExc_RuntimeError, "No record number");
1724+
return NULL;
1725+
}
1726+
return Py_BuildValue("I", value);
1727+
}
1728+
16831729
/********************************
16841730
* auparse_goto_record_num
16851731
********************************/
@@ -1933,7 +1979,6 @@ AuParser_find_field(AuParser *self, PyObject *args)
19331979
return Py_BuildValue("s", value);
19341980
}
19351981

1936-
const char *auparse_find_field_next(auparse_state_t *au);
19371982
/********************************
19381983
* auparse_find_field_next
19391984
********************************/
@@ -1961,6 +2006,59 @@ AuParser_find_field_next(AuParser *self)
19612006
return Py_BuildValue("s", value);
19622007
}
19632008

2009+
/********************************
2010+
* auparse_get_field_num
2011+
********************************/
2012+
PyDoc_STRVAR(get_field_num_doc,
2013+
"get_field_num() get one based record number where auparse is currently at\n\
2014+
The record numbering will reset back to 1 each time a new event is processed.\n\
2015+
Raises exception (RuntimeError) on error.\n\
2016+
");
2017+
static PyObject *
2018+
AuParser_get_field_num(AuParser *self)
2019+
{
2020+
unsigned int value;
2021+
2022+
PARSER_CHECK;
2023+
value = auparse_get_field_num(self->au);
2024+
if (value == 0) {
2025+
PyErr_SetString(PyExc_RuntimeError, "No field number");
2026+
return NULL;
2027+
}
2028+
return Py_BuildValue("I", value);
2029+
}
2030+
2031+
/********************************
2032+
* auparse_goto_field_num
2033+
********************************/
2034+
PyDoc_STRVAR(goto_field_num_doc,
2035+
"goto_field_num() Move field cursor to specific position.\n\
2036+
\n\
2037+
goto_field_num() will move the internal library cursors to point\n\
2038+
to a specific physical field number. Fields within the same record are\n\
2039+
numbered starting from 1. This is generally not needed but there are\n\
2040+
some cases where one may want precise control over the exact field\n\
2041+
being looked at.\n\
2042+
\n\
2043+
Returns True on success, False if no more fields in current event\n\
2044+
Raises exception (EnvironmentError) on error.\n\
2045+
");
2046+
static PyObject *
2047+
AuParser_goto_field_num(AuParser *self, PyObject *args)
2048+
{
2049+
int result;
2050+
unsigned int num;
2051+
2052+
if (!PyArg_ParseTuple(args, "i", &num)) return NULL;
2053+
PARSER_CHECK;
2054+
result = auparse_goto_field_num(self->au, num);
2055+
2056+
if (result > 0) Py_RETURN_TRUE;
2057+
if (result == 0) Py_RETURN_FALSE;
2058+
PyErr_SetFromErrno(PyExc_EnvironmentError);
2059+
return NULL;
2060+
}
2061+
19642062
/********************************
19652063
* auparse_get_field_name
19662064
********************************/
@@ -2177,6 +2275,7 @@ static PyMethodDef AuParser_methods[] = {
21772275
{"feed", (PyCFunction)AuParser_feed, METH_VARARGS, feed_doc},
21782276
{"flush_feed", (PyCFunction)AuParser_flush_feed, METH_NOARGS, flush_feed_doc},
21792277
{"feed_has_data", (PyCFunction)AuParser_feed_has_data, METH_NOARGS, feed_has_data_doc},
2278+
{"feed_has_ready_event", (PyCFunction)AuParser_feed_has_ready_event, METH_NOARGS, feed_has_ready_event_doc},
21802279
{"feed_age_events", (PyCFunction)AuParser_feed_age_events, METH_NOARGS, feed_age_events_doc},
21812280
{"add_callback", (PyCFunction)AuParser_add_callback, METH_VARARGS, add_callback_doc},
21822281
{"set_escape_mode", (PyCFunction)AuParser_set_escape_mode, METH_VARARGS, set_escape_mode_doc},
@@ -2213,6 +2312,7 @@ static PyMethodDef AuParser_methods[] = {
22132312
{"get_num_records", (PyCFunction)AuParser_get_num_records, METH_NOARGS, get_num_records_doc},
22142313
{"first_record", (PyCFunction)AuParser_first_record, METH_NOARGS, first_record_doc},
22152314
{"next_record", (PyCFunction)AuParser_next_record, METH_NOARGS, next_record_doc},
2315+
{"get_record_num", (PyCFunction)AuParser_get_record_num, METH_NOARGS, get_record_num_doc},
22162316
{"goto_record_num", (PyCFunction)AuParser_goto_record_num, METH_VARARGS, goto_record_num_doc},
22172317
{"get_type", (PyCFunction)AuParser_get_type, METH_NOARGS, get_type_doc},
22182318
{"get_type_name", (PyCFunction)AuParser_get_type_name, METH_NOARGS, get_type_name_doc},
@@ -2223,6 +2323,8 @@ static PyMethodDef AuParser_methods[] = {
22232323
{"get_num_fields", (PyCFunction)AuParser_get_num_fields, METH_NOARGS, get_num_fields_doc},
22242324
{"get_record_text", (PyCFunction)AuParser_get_record_text, METH_NOARGS, get_record_text_doc},
22252325
{"find_field_next", (PyCFunction)AuParser_find_field_next, METH_NOARGS, find_field_next_doc},
2326+
{"get_field_num", (PyCFunction)AuParser_get_field_num, METH_NOARGS, get_field_num_doc},
2327+
{"goto_field_num", (PyCFunction)AuParser_goto_field_num, METH_VARARGS, goto_field_num_doc},
22262328
{"find_field", (PyCFunction)AuParser_find_field, METH_VARARGS, find_field_doc},
22272329
{"get_field_name", (PyCFunction)AuParser_get_field_name, METH_NOARGS, get_field_name_doc},
22282330
{"get_field_str", (PyCFunction)AuParser_get_field_str, METH_NOARGS, get_field_str_doc},

0 commit comments

Comments
 (0)