pem: Use chunk_map() instead of non-portable mmap()

This commit is contained in:
Martin Willi
2014-01-23 15:55:33 +01:00
parent ecdef634aa
commit 88fa7f62be
+6 -29
View File
@@ -25,7 +25,6 @@
#include <stddef.h> #include <stddef.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <utils/debug.h> #include <utils/debug.h>
@@ -418,39 +417,17 @@ static void *load_from_blob(chunk_t blob, credential_type_t type, int subtype,
static void *load_from_file(char *file, credential_type_t type, int subtype, static void *load_from_file(char *file, credential_type_t type, int subtype,
identification_t *subject, x509_flag_t flags) identification_t *subject, x509_flag_t flags)
{ {
void *cred = NULL; void *cred;
struct stat sb; chunk_t *chunk;
void *addr;
int fd;
fd = open(file, O_RDONLY); chunk = chunk_map(file, FALSE);
if (fd == -1) if (!chunk)
{ {
DBG1(DBG_LIB, " opening '%s' failed: %s", file, strerror(errno)); DBG1(DBG_LIB, " opening '%s' failed: %s", file, strerror(errno));
return NULL; return NULL;
} }
cred = load_from_blob(*chunk, type, subtype, subject, flags);
if (fstat(fd, &sb) == -1) chunk_unmap(chunk);
{
DBG1(DBG_LIB, " getting file size of '%s' failed: %s", file,
strerror(errno));
close(fd);
return NULL;
}
addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED)
{
DBG1(DBG_LIB, " mapping '%s' failed: %s", file, strerror(errno));
close(fd);
return NULL;
}
cred = load_from_blob(chunk_create(addr, sb.st_size), type, subtype,
subject, flags);
munmap(addr, sb.st_size);
close(fd);
return cred; return cred;
} }