diff -Naurp --exclude CVS --exclude ChangeLog --exclude direct_api.c --exclude semanage_store.c --exclude VERSION --exclude libsemanage.map --exclude 'module_record*' --exclude 'database_directory*' --exclude Makefile old/libsemanage/src/database_policydb.c new/libsemanage/src/database_policydb.c --- old/libsemanage/src/database_policydb.c 2005-10-25 20:17:53.000000000 -0400 +++ new/libsemanage/src/database_policydb.c 2005-10-26 00:07:17.000000000 -0400 @@ -5,14 +5,10 @@ typedef struct dbase_policydb dbase_t; #include #include #include -#include -#include -#include -#include #include -#include -#include + #include + #include "database_policydb.h" #include "semanage_store.h" #include "handle.h" @@ -61,11 +57,9 @@ static int dbase_policydb_cache( semanage_handle_t* handle, dbase_policydb_t* dbase) { - int fd = -1; - struct stat sb; - void* data = NULL; + FILE* fp = NULL; sepol_policydb_t* policydb = NULL; - + sepol_policy_file_t* pf = NULL; char* fname = NULL; /* Already cached */ @@ -75,52 +69,51 @@ static int dbase_policydb_cache( if (construct_filename(handle, dbase, &fname) < 0) goto err; - /* Open file */ - fd = open(fname, O_RDONLY); - if (fd < 0) { - ERR(handle, "could not open %s for reading: %s", - fname, strerror(errno)); + if (sepol_policydb_create(&policydb) < 0) { + ERR(handle, "could not create policydb object"); goto err; } - /* Stat */ - if (fstat(fd, &sb) < 0) { - ERR(handle, "could not stat %s: %s", + /* Try opening file + * ENOENT is not fatal - we just create an empty policydb */ + fp = fopen(fname, "rb"); + if (fp == NULL && errno != ENOENT) { + ERR(handle, "could not open %s for reading: %s", fname, strerror(errno)); goto err; } + + /* If the file was opened successfully, read a policydb */ + if (fp != NULL) { + if (sepol_policy_file_create(&pf) < 0) { + ERR(handle, "could not create policy file object"); + goto err; + } - /* Map file */ - data = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); - if (data == MAP_FAILED) { - ERR(handle, "could not map policy: %s", strerror(errno)); - goto err; - } + sepol_policy_file_set_fp(pf, fp); + sepol_policy_file_set_handle(pf, handle->sepolh); - /* Create policydb image */ - if (sepol_policydb_create(&policydb)) - goto omem; - if (sepol_policydb_from_image(handle->sepolh, data, sb.st_size, policydb) < 0) - goto err; + if (sepol_policydb_read(policydb, pf) < 0) + goto err; + + sepol_policy_file_free(pf); + fclose(fp); + } + + /* Either way, update the database policydb */ dbase->policydb = policydb; - close(fd); - munmap(data, sb.st_size); free(fname); dbase->cached = 1; return STATUS_SUCCESS; - omem: - ERR(handle, "out of memory"); - err: ERR(handle, "unable to cache policy database from %s", fname); - if (fd > 0) - close(fd); - if (data != NULL) - munmap(data, sb.st_size); - - free(policydb); + + if (fp) + fclose(fp); + sepol_policydb_free(policydb); + sepol_policy_file_free(pf); free(fname); return STATUS_ERR;