|
|
81e8b3 |
#include <stdint.h>
|
|
|
d7ed3e |
#include <stddef.h>
|
|
|
81e8b3 |
#include <stdlib.h>
|
|
|
81e8b3 |
#include <string.h>
|
|
|
81e8b3 |
#include <sys/mman.h>
|
|
|
d7ed3e |
|
|
|
81e8b3 |
#include <perk/perk.h>
|
|
|
d7ed3e |
#include "perk_impl.h"
|
|
|
81e8b3 |
|
|
|
d7ed3e |
static int pe_free_unit_ctx_impl(struct pe_unit_ctx_impl * ctx, int status)
|
|
|
81e8b3 |
{
|
|
|
81e8b3 |
if (ctx) {
|
|
|
81e8b3 |
pe_free_image_meta(ctx->meta);
|
|
|
81e8b3 |
pe_unmap_raw_image(&ctx->map);
|
|
|
81e8b3 |
free(ctx);
|
|
|
81e8b3 |
}
|
|
|
81e8b3 |
|
|
|
81e8b3 |
return status;
|
|
|
81e8b3 |
}
|
|
|
81e8b3 |
|
|
|
81e8b3 |
int pe_get_unit_ctx(
|
|
|
81e8b3 |
struct pe_driver_ctx * dctx,
|
|
|
81e8b3 |
const char * path,
|
|
|
81e8b3 |
struct pe_unit_ctx ** pctx)
|
|
|
81e8b3 |
{
|
|
|
d7ed3e |
struct pe_unit_ctx_impl * ctx;
|
|
|
81e8b3 |
int prot;
|
|
|
81e8b3 |
|
|
|
81e8b3 |
if (!dctx || !(ctx = calloc(sizeof(*ctx),1)))
|
|
|
81e8b3 |
return -1;
|
|
|
81e8b3 |
|
|
|
c61328 |
prot = (dctx->cctx->actflags & PERK_ACTION_MAP_READWRITE)
|
|
|
81e8b3 |
? PROT_READ | PROT_WRITE
|
|
|
81e8b3 |
: PROT_READ;
|
|
|
81e8b3 |
|
|
|
76aabf |
if (pe_map_raw_image(dctx->cctx->ioctx->fdin,path,prot,&ctx->map))
|
|
|
81e8b3 |
return pe_free_unit_ctx_impl(ctx,-1);
|
|
|
81e8b3 |
|
|
|
81e8b3 |
if (pe_get_image_meta(&ctx->map,&ctx->meta))
|
|
|
81e8b3 |
return pe_free_unit_ctx_impl(ctx,-1);
|
|
|
81e8b3 |
|
|
|
c61328 |
memcpy(&ctx->cctx,dctx->cctx,
|
|
|
81e8b3 |
sizeof(ctx->cctx));
|
|
|
81e8b3 |
|
|
|
d7ed3e |
ctx->path = ctx->path;
|
|
|
76aabf |
ctx->ioctx.prot = prot;
|
|
|
81e8b3 |
|
|
|
d7ed3e |
ctx->uctx.path = &ctx->path;
|
|
|
d7ed3e |
ctx->uctx.map = &ctx->map;
|
|
|
d7ed3e |
ctx->uctx.meta = ctx->meta;
|
|
|
d7ed3e |
ctx->uctx.cctx = &ctx->cctx;
|
|
|
d7ed3e |
|
|
|
d7ed3e |
*pctx = &ctx->uctx;
|
|
|
81e8b3 |
return 0;
|
|
|
81e8b3 |
}
|
|
|
81e8b3 |
|
|
|
81e8b3 |
void pe_free_unit_ctx(struct pe_unit_ctx * ctx)
|
|
|
81e8b3 |
{
|
|
|
d7ed3e |
struct pe_unit_ctx_impl * ictx;
|
|
|
d7ed3e |
uintptr_t addr;
|
|
|
d7ed3e |
|
|
|
d7ed3e |
if (ctx) {
|
|
|
d7ed3e |
addr = (uintptr_t)ctx - offsetof(struct pe_unit_ctx_impl,uctx);
|
|
|
d7ed3e |
ictx = (struct pe_unit_ctx_impl *)addr;
|
|
|
d7ed3e |
pe_free_unit_ctx_impl(ictx,0);
|
|
|
d7ed3e |
}
|
|
|
81e8b3 |
}
|