#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <bsm/audit.h>
#include <bsm/libbsm.h>
#include <synch.h>
#define ALLOC_INIT (600)
#define ALLOC_INCR (100)
static int alloc_map();
static int load_map();
static int realloc_map();
typedef struct event_map {
au_event_t event;
au_class_t class;
} event_map_t;
static event_map_t *event_map;
static uint_t alloc_count;
static uint_t event_count;
static mutex_t mutex_au_preselect = DEFAULTMUTEX;
int
au_preselect(au_event_t au_event, au_mask_t *au_mask_p, int sorf, int flag)
{
static char been_here_before;
register int i;
register au_class_t comp_class;
(void) mutex_lock(&mutex_au_preselect);
if (!been_here_before) {
if (alloc_map() == -1) {
(void) mutex_unlock(&mutex_au_preselect);
return (-1);
}
if (load_map() == -1) {
(void) mutex_unlock(&mutex_au_preselect);
return (-1);
}
been_here_before = 1;
}
if (flag == AU_PRS_REREAD) {
if (load_map() == -1) {
(void) mutex_unlock(&mutex_au_preselect);
return (-1);
}
}
if (sorf == AU_PRS_SUCCESS)
comp_class = au_mask_p->am_success;
else if (sorf == AU_PRS_FAILURE)
comp_class = au_mask_p->am_failure;
else
comp_class = au_mask_p->am_success | au_mask_p->am_failure;
for (i = 0; i < event_count; i++) {
if (event_map[i].event == au_event) {
if (event_map[i].class & comp_class) {
(void) mutex_unlock(&mutex_au_preselect);
return (1);
} else {
(void) mutex_unlock(&mutex_au_preselect);
return (0);
}
}
}
(void) mutex_unlock(&mutex_au_preselect);
return (-1);
}
static int
alloc_map()
{
if ((event_map = (event_map_t *)
calloc(ALLOC_INIT, (size_t)sizeof (event_map_t))) ==
(event_map_t *)NULL)
return (-1);
else
alloc_count = ALLOC_INIT;
return (0);
}
static int
load_map()
{
register au_event_ent_t *evp;
event_count = 0;
setauevent();
while ((evp = getauevent()) != (au_event_ent_t *)NULL) {
if (event_count == alloc_count && realloc_map() == -1) {
endauevent();
return (-1);
}
event_map[event_count].event = evp->ae_number;
event_map[event_count].class = evp->ae_class;
++event_count;
}
endauevent();
return (0);
}
static int
realloc_map()
{
uint_t new_alloc_count;
event_map_t *new_event_map;
new_alloc_count = alloc_count + ALLOC_INCR;
if (new_alloc_count <= alloc_count) {
errno = ENOMEM;
return (-1);
}
if ((new_event_map = recallocarray(event_map, alloc_count,
new_alloc_count, sizeof (event_map_t))) == NULL)
return (-1);
alloc_count = new_alloc_count;
event_map = new_event_map;
return (0);
}