Blame src/database_file.c

Packit 366192
/* Copyright (C) 2005 Red Hat, Inc. */
Packit 366192
Packit 366192
/* Object: dbase_file_t (File)
Packit 366192
 * Extends: dbase_llist_t (Linked List) 
Packit 366192
 * Implements: dbase_t (Database)
Packit 366192
 */
Packit 366192
Packit 366192
struct dbase_file;
Packit 366192
typedef struct dbase_file dbase_t;
Packit 366192
#define DBASE_DEFINED
Packit 366192
Packit 366192
#include <stdlib.h>
Packit 366192
#include <stddef.h>
Packit 366192
#include <string.h>
Packit 366192
#include <errno.h>
Packit 366192
#include <stdio.h>
Packit 366192
#include <stdio_ext.h>
Packit 366192
#include "debug.h"
Packit 366192
#include "handle.h"
Packit 366192
#include "parse_utils.h"
Packit 366192
#include "database_file.h"
Packit 366192
#include "database_llist.h"
Packit 366192
#include "semanage_store.h"
Packit 366192
Packit 366192
/* FILE dbase */
Packit 366192
struct dbase_file {
Packit 366192
Packit 366192
	/* Parent object - must always be 
Packit 366192
	 * the first field - here we are using
Packit 366192
	 * a linked list to store the records */
Packit 366192
	dbase_llist_t llist;
Packit 366192
Packit 366192
	/* Backing path for read-only[0] and transaction[1] */
Packit 366192
	const char *path[2];
Packit 366192
Packit 366192
	/* FILE extension */
Packit 366192
	record_file_table_t *rftable;
Packit 366192
};
Packit 366192
Packit 366192
static int dbase_file_cache(semanage_handle_t * handle, dbase_file_t * dbase)
Packit 366192
{
Packit 366192
Packit 366192
	record_table_t *rtable = dbase_llist_get_rtable(&dbase->llist);
Packit 366192
	record_file_table_t *rftable = dbase->rftable;
Packit 366192
Packit 366192
	record_t *process_record = NULL;
Packit 366192
	int pstatus = STATUS_SUCCESS;
Packit 366192
Packit 366192
	parse_info_t *parse_info = NULL;
Packit 366192
	const char *fname = NULL;
Packit 366192
Packit 366192
	/* Already cached */
Packit 366192
	if (!dbase_llist_needs_resync(handle, &dbase->llist))
Packit 366192
		return STATUS_SUCCESS;
Packit 366192
Packit 366192
	/* Update cache serial */
Packit 366192
	dbase_llist_cache_init(&dbase->llist);
Packit 366192
	if (dbase_llist_set_serial(handle, &dbase->llist) < 0)
Packit 366192
		goto err;
Packit 366192
Packit 366192
	fname = dbase->path[handle->is_in_transaction];
Packit 366192
Packit 366192
	if (parse_init(handle, fname, NULL, &parse_info) < 0)
Packit 366192
		goto err;
Packit 366192
Packit 366192
	if (parse_open(handle, parse_info) < 0)
Packit 366192
		goto err;
Packit 366192
Packit 366192
	/* Main processing loop */
Packit 366192
	do {
Packit 366192
Packit 366192
		/* Create record */
Packit 366192
		if (rtable->create(handle, &process_record) < 0)
Packit 366192
			goto err;
Packit 366192
Packit 366192
		/* Parse record */
Packit 366192
		pstatus = rftable->parse(handle, parse_info, process_record);
Packit 366192
Packit 366192
		/* Parse error */
Packit 366192
		if (pstatus < 0)
Packit 366192
			goto err;
Packit 366192
Packit 366192
		/* End of file */
Packit 366192
		else if (pstatus == STATUS_NODATA)
Packit 366192
			break;
Packit 366192
Packit 366192
		/* Prepend to cache */
Packit 366192
		if (dbase_llist_cache_prepend(handle, &dbase->llist,
Packit 366192
					      process_record) < 0)
Packit 366192
			goto err;
Packit 366192
Packit 366192
		rtable->free(process_record);
Packit 366192
		process_record = NULL;
Packit 366192
Packit 366192
	} while (pstatus != STATUS_NODATA);
Packit 366192
Packit 366192
	rtable->free(process_record);
Packit 366192
	parse_close(parse_info);
Packit 366192
	parse_release(parse_info);
Packit 366192
	return STATUS_SUCCESS;
Packit 366192
Packit 366192
      err:
Packit 366192
	ERR(handle, "could not cache file database");
Packit 366192
	rtable->free(process_record);
Packit 366192
	if (parse_info) {
Packit 366192
		parse_close(parse_info);
Packit 366192
		parse_release(parse_info);
Packit 366192
	}
Packit 366192
	dbase_llist_drop_cache(&dbase->llist);
Packit 366192
	return STATUS_ERR;
Packit 366192
}
Packit 366192
Packit 366192
/* Flush database to file */
Packit 366192
static int dbase_file_flush(semanage_handle_t * handle, dbase_file_t * dbase)
Packit 366192
{
Packit 366192
Packit 366192
	record_file_table_t *rftable = dbase->rftable;
Packit 366192
Packit 366192
	cache_entry_t *ptr;
Packit 366192
	const char *fname = NULL;
Packit 366192
	FILE *str = NULL;
Packit 366192
	mode_t mask;
Packit 366192
Packit 366192
	if (!dbase_llist_is_modified(&dbase->llist))
Packit 366192
		return STATUS_SUCCESS;
Packit 366192
Packit 366192
	fname = dbase->path[handle->is_in_transaction];
Packit 366192
Packit 366192
	mask = umask(0077);
Packit 366192
	str = fopen(fname, "w");
Packit 366192
	umask(mask);
Packit 366192
	if (!str) {
Packit 366192
		ERR(handle, "could not open %s for writing: %s",
Packit 366192
		    fname, strerror(errno));
Packit 366192
		goto err;
Packit 366192
	}
Packit 366192
	__fsetlocking(str, FSETLOCKING_BYCALLER);
Packit 366192
Packit 366192
	if (fprintf(str, "# This file is auto-generated by libsemanage\n"
Packit 366192
		    "# Do not edit directly.\n\n") < 0) {
Packit 366192
Packit 366192
		ERR(handle, "could not write file header for %s", fname);
Packit 366192
		goto err;
Packit 366192
	}
Packit 366192
Packit 366192
	for (ptr = dbase->llist.cache_tail; ptr != NULL; ptr = ptr->prev) {
Packit 366192
		if (rftable->print(handle, ptr->data, str) < 0)
Packit 366192
			goto err;
Packit 366192
	}
Packit 366192
Packit 366192
	dbase_llist_set_modified(&dbase->llist, 0);
Packit 366192
	fclose(str);
Packit 366192
	return STATUS_SUCCESS;
Packit 366192
Packit 366192
      err:
Packit 366192
	if (str != NULL)
Packit 366192
		fclose(str);
Packit 366192
Packit 366192
	ERR(handle, "could not flush database to file");
Packit 366192
	return STATUS_ERR;
Packit 366192
}
Packit 366192
Packit 366192
int dbase_file_init(semanage_handle_t * handle,
Packit 366192
		    const char *path_ro,
Packit 366192
		    const char *path_rw,
Packit 366192
		    record_table_t * rtable,
Packit 366192
		    record_file_table_t * rftable, dbase_file_t ** dbase)
Packit 366192
{
Packit 366192
Packit 366192
	dbase_file_t *tmp_dbase = (dbase_file_t *) malloc(sizeof(dbase_file_t));
Packit 366192
Packit 366192
	if (!tmp_dbase)
Packit 366192
		goto omem;
Packit 366192
Packit 366192
	tmp_dbase->path[0] = path_ro;
Packit 366192
	tmp_dbase->path[1] = path_rw;
Packit 366192
	tmp_dbase->rftable = rftable;
Packit 366192
	dbase_llist_init(&tmp_dbase->llist, rtable, &SEMANAGE_FILE_DTABLE);
Packit 366192
Packit 366192
	*dbase = tmp_dbase;
Packit 366192
Packit 366192
	return STATUS_SUCCESS;
Packit 366192
Packit 366192
      omem:
Packit 366192
	ERR(handle, "out of memory, could not initialize file database");
Packit 366192
	free(tmp_dbase);
Packit 366192
	return STATUS_ERR;
Packit 366192
}
Packit 366192
Packit 366192
/* Release dbase resources */
Packit 366192
void dbase_file_release(dbase_file_t * dbase)
Packit 366192
{
Packit 366192
Packit 366192
	dbase_llist_drop_cache(&dbase->llist);
Packit 366192
	free(dbase);
Packit 366192
}
Packit 366192
Packit 366192
/* FILE dbase - method table implementation */
Packit 366192
dbase_table_t SEMANAGE_FILE_DTABLE = {
Packit 366192
Packit 366192
	/* Cache/Transactions */
Packit 366192
	.cache = dbase_file_cache,
Packit 366192
	.drop_cache = (void *)dbase_llist_drop_cache,
Packit 366192
	.flush = dbase_file_flush,
Packit 366192
	.is_modified = (void *)dbase_llist_is_modified,
Packit 366192
Packit 366192
	/* Database API */
Packit 366192
	.iterate = (void *)dbase_llist_iterate,
Packit 366192
	.exists = (void *)dbase_llist_exists,
Packit 366192
	.list = (void *)dbase_llist_list,
Packit 366192
	.add = (void *)dbase_llist_add,
Packit 366192
	.set = (void *)dbase_llist_set,
Packit 366192
	.del = (void *)dbase_llist_del,
Packit 366192
	.clear = (void *)dbase_llist_clear,
Packit 366192
	.modify = (void *)dbase_llist_modify,
Packit 366192
	.query = (void *)dbase_llist_query,
Packit 366192
	.count = (void *)dbase_llist_count,
Packit 366192
Packit 366192
	/* Polymorphism */
Packit 366192
	.get_rtable = (void *)dbase_llist_get_rtable
Packit 366192
};