|
Packit Service |
102278 |
/* Copyright (C) 2005 Red Hat, Inc. */
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Object: dbase_activedb_t (Active/Kernel)
|
|
Packit Service |
102278 |
* Extends: dbase_llist_t (Linked List)
|
|
Packit Service |
102278 |
* Implements: dbase_t (Database)
|
|
Packit Service |
102278 |
*/
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
struct dbase_activedb;
|
|
Packit Service |
102278 |
typedef struct dbase_activedb dbase_t;
|
|
Packit Service |
102278 |
#define DBASE_DEFINED
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
#include <stdlib.h>
|
|
Packit Service |
102278 |
#include <string.h>
|
|
Packit Service |
102278 |
#include <errno.h>
|
|
Packit Service |
102278 |
#include "debug.h"
|
|
Packit Service |
102278 |
#include "handle.h"
|
|
Packit Service |
102278 |
#include "database_activedb.h"
|
|
Packit Service |
102278 |
#include "database_llist.h"
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* ACTIVEDB dbase */
|
|
Packit Service |
102278 |
struct dbase_activedb {
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Parent object - must always be
|
|
Packit Service |
102278 |
* the first field - here we are using
|
|
Packit Service |
102278 |
* a linked list to store the records */
|
|
Packit Service |
102278 |
dbase_llist_t llist;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* ACTIVEDB extension */
|
|
Packit Service |
102278 |
record_activedb_table_t *ratable;
|
|
Packit Service |
102278 |
};
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
static int dbase_activedb_cache(semanage_handle_t * handle,
|
|
Packit Service |
102278 |
dbase_activedb_t * dbase)
|
|
Packit Service |
102278 |
{
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
record_table_t *rtable = dbase_llist_get_rtable(&dbase->llist);
|
|
Packit Service |
102278 |
record_activedb_table_t *ratable = dbase->ratable;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
record_t **records = NULL;
|
|
Packit Service |
102278 |
unsigned int rcount = 0;
|
|
Packit Service |
102278 |
unsigned int i = 0;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Already cached */
|
|
Packit Service |
102278 |
if (!dbase_llist_needs_resync(handle, &dbase->llist))
|
|
Packit Service |
102278 |
return STATUS_SUCCESS;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Update cache serial */
|
|
Packit Service |
102278 |
dbase_llist_cache_init(&dbase->llist);
|
|
Packit Service |
102278 |
if (dbase_llist_set_serial(handle, &dbase->llist) < 0)
|
|
Packit Service |
102278 |
goto err;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Fetch the entire list */
|
|
Packit Service |
102278 |
if (ratable->read_list(handle, &records, &rcount) < 0)
|
|
Packit Service |
102278 |
goto err;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Add records one by one */
|
|
Packit Service |
102278 |
for (; i < rcount; i++) {
|
|
Packit Service |
102278 |
if (dbase_llist_cache_prepend(handle, &dbase->llist, records[i])
|
|
Packit Service |
102278 |
< 0)
|
|
Packit Service |
102278 |
goto err;
|
|
Packit Service |
102278 |
rtable->free(records[i]);
|
|
Packit Service |
102278 |
}
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
free(records);
|
|
Packit Service |
102278 |
return STATUS_SUCCESS;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
err:
|
|
Packit Service |
102278 |
ERR(handle, "could not cache active database");
|
|
Packit Service |
102278 |
for (; i < rcount; i++)
|
|
Packit Service |
102278 |
rtable->free(records[i]);
|
|
Packit Service |
102278 |
dbase_llist_drop_cache(&dbase->llist);
|
|
Packit Service |
102278 |
free(records);
|
|
Packit Service |
102278 |
return STATUS_ERR;
|
|
Packit Service |
102278 |
}
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
static int dbase_activedb_flush(semanage_handle_t * handle,
|
|
Packit Service |
102278 |
dbase_activedb_t * dbase)
|
|
Packit Service |
102278 |
{
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
record_table_t *rtable = dbase_llist_get_rtable(&dbase->llist);
|
|
Packit Service |
102278 |
record_activedb_table_t *ratable = dbase->ratable;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
record_t **records = NULL;
|
|
Packit Service |
102278 |
unsigned int rcount = 0;
|
|
Packit Service |
102278 |
unsigned int i;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Not cached, or not modified - flush is not necessary */
|
|
Packit Service |
102278 |
if (!dbase_llist_is_modified(&dbase->llist))
|
|
Packit Service |
102278 |
return STATUS_SUCCESS;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Fetch list */
|
|
Packit Service |
102278 |
if (dbase_llist_list(handle, &dbase->llist, &records, &rcount) < 0)
|
|
Packit Service |
102278 |
goto err;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Commit */
|
|
Packit Service |
102278 |
if (ratable->commit_list(handle, records, rcount) < 0)
|
|
Packit Service |
102278 |
goto err;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
for (i = 0; i < rcount; i++)
|
|
Packit Service |
102278 |
rtable->free(records[i]);
|
|
Packit Service |
102278 |
free(records);
|
|
Packit Service |
102278 |
dbase_llist_set_modified(&dbase->llist, 0);
|
|
Packit Service |
102278 |
return STATUS_SUCCESS;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
err:
|
|
Packit Service |
102278 |
for (i = 0; i < rcount; i++)
|
|
Packit Service |
102278 |
rtable->free(records[i]);
|
|
Packit Service |
102278 |
free(records);
|
|
Packit Service |
102278 |
ERR(handle, "could not flush active database");
|
|
Packit Service |
102278 |
return STATUS_ERR;
|
|
Packit Service |
102278 |
}
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
int dbase_activedb_init(semanage_handle_t * handle,
|
|
Packit Service |
102278 |
record_table_t * rtable,
|
|
Packit Service |
102278 |
record_activedb_table_t * ratable,
|
|
Packit Service |
102278 |
dbase_activedb_t ** dbase)
|
|
Packit Service |
102278 |
{
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
dbase_activedb_t *tmp_dbase =
|
|
Packit Service |
102278 |
(dbase_activedb_t *) malloc(sizeof(dbase_activedb_t));
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
if (!tmp_dbase)
|
|
Packit Service |
102278 |
goto omem;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
tmp_dbase->ratable = ratable;
|
|
Packit Service |
102278 |
dbase_llist_init(&tmp_dbase->llist, rtable, &SEMANAGE_ACTIVEDB_DTABLE);
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
*dbase = tmp_dbase;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
return STATUS_SUCCESS;
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
omem:
|
|
Packit Service |
102278 |
ERR(handle, "out of memory, could not initialize active database");
|
|
Packit Service |
102278 |
free(tmp_dbase);
|
|
Packit Service |
102278 |
return STATUS_ERR;
|
|
Packit Service |
102278 |
}
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Release dbase resources */
|
|
Packit Service |
102278 |
void dbase_activedb_release(dbase_activedb_t * dbase)
|
|
Packit Service |
102278 |
{
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
dbase_llist_drop_cache(&dbase->llist);
|
|
Packit Service |
102278 |
free(dbase);
|
|
Packit Service |
102278 |
}
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* ACTIVEDB dbase - method table implementation */
|
|
Packit Service |
102278 |
dbase_table_t SEMANAGE_ACTIVEDB_DTABLE = {
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Cache/Transactions */
|
|
Packit Service |
102278 |
.cache = dbase_activedb_cache,
|
|
Packit Service |
102278 |
.drop_cache = (void *)dbase_llist_drop_cache,
|
|
Packit Service |
102278 |
.flush = dbase_activedb_flush,
|
|
Packit Service |
102278 |
.is_modified = (void *)dbase_llist_is_modified,
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Database API */
|
|
Packit Service |
102278 |
.iterate = (void *)dbase_llist_iterate,
|
|
Packit Service |
102278 |
.exists = (void *)dbase_llist_exists,
|
|
Packit Service |
102278 |
.list = (void *)dbase_llist_list,
|
|
Packit Service |
102278 |
.add = (void *)dbase_llist_add,
|
|
Packit Service |
102278 |
.set = (void *)dbase_llist_set,
|
|
Packit Service |
102278 |
.del = (void *)dbase_llist_del,
|
|
Packit Service |
102278 |
.clear = (void *)dbase_llist_clear,
|
|
Packit Service |
102278 |
.modify = (void *)dbase_llist_modify,
|
|
Packit Service |
102278 |
.query = (void *)dbase_llist_query,
|
|
Packit Service |
102278 |
.count = (void *)dbase_llist_count,
|
|
Packit Service |
102278 |
|
|
Packit Service |
102278 |
/* Polymorphism */
|
|
Packit Service |
102278 |
.get_rtable = (void *)dbase_llist_get_rtable
|
|
Packit Service |
102278 |
};
|