summaryrefslogtreecommitdiffhomepage
path: root/src/driver/pe_unit_ctx.c
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2015-11-22 23:07:39 -0500
committermidipix <writeonce@midipix.org>2016-11-10 23:35:26 -0500
commit81e8b3a78b680143e315b4380d794a25fefc9e32 (patch)
tree1cbd7fa98a180834862ec7662f03dcc92351db95 /src/driver/pe_unit_ctx.c
parente02651b57880bb32ea693abf434751a216df9464 (diff)
downloadperk-81e8b3a78b680143e315b4380d794a25fefc9e32.tar.bz2
perk-81e8b3a78b680143e315b4380d794a25fefc9e32.tar.xz
driver integration 8/9: driver code: initial implementation.
Diffstat (limited to 'src/driver/pe_unit_ctx.c')
-rw-r--r--src/driver/pe_unit_ctx.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/driver/pe_unit_ctx.c b/src/driver/pe_unit_ctx.c
new file mode 100644
index 0000000..5bcac3d
--- /dev/null
+++ b/src/driver/pe_unit_ctx.c
@@ -0,0 +1,52 @@
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <perk/perk.h>
+
+static int pe_free_unit_ctx_impl(struct pe_unit_ctx * ctx, int status)
+{
+ if (ctx) {
+ pe_free_image_meta(ctx->meta);
+ pe_unmap_raw_image(&ctx->map);
+ free(ctx);
+ }
+
+ return status;
+}
+
+int pe_get_unit_ctx(
+ struct pe_driver_ctx * dctx,
+ const char * path,
+ struct pe_unit_ctx ** pctx)
+{
+ struct pe_unit_ctx * ctx;
+ int prot;
+
+ if (!dctx || !(ctx = calloc(sizeof(*ctx),1)))
+ return -1;
+
+ prot = (dctx->cctx.actflags & PERK_ACTION_FLAG_MAP_READWRITE)
+ ? PROT_READ | PROT_WRITE
+ : PROT_READ;
+
+ if (pe_map_raw_image(dctx->cctx.fdin,path,prot,&ctx->map))
+ return pe_free_unit_ctx_impl(ctx,-1);
+
+ if (pe_get_image_meta(&ctx->map,&ctx->meta))
+ return pe_free_unit_ctx_impl(ctx,-1);
+
+ memcpy(&ctx->cctx,&dctx->cctx,
+ sizeof(ctx->cctx));
+
+ ctx->path = path;
+ ctx->cctx.prot = prot;
+
+ *pctx = ctx;
+ return 0;
+}
+
+void pe_free_unit_ctx(struct pe_unit_ctx * ctx)
+{
+ pe_free_unit_ctx_impl(ctx,0);
+}