1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <perk/perk.h>
#include "perk_impl.h"
static int pe_free_unit_ctx_impl(struct pe_unit_ctx_impl * 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_impl * ctx;
int prot;
if (!dctx || !(ctx = calloc(sizeof(*ctx),1)))
return -1;
prot = (dctx->cctx->actflags & PERK_ACTION_MAP_READWRITE)
? PROT_READ | PROT_WRITE
: PROT_READ;
if (pe_map_raw_image(dctx->cctx->ioctx->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 = ctx->path;
ctx->ioctx.prot = prot;
ctx->uctx.path = &ctx->path;
ctx->uctx.map = &ctx->map;
ctx->uctx.meta = ctx->meta;
ctx->uctx.cctx = &ctx->cctx;
*pctx = &ctx->uctx;
return 0;
}
void pe_free_unit_ctx(struct pe_unit_ctx * ctx)
{
struct pe_unit_ctx_impl * ictx;
uintptr_t addr;
if (ctx) {
addr = (uintptr_t)ctx - offsetof(struct pe_unit_ctx_impl,uctx);
ictx = (struct pe_unit_ctx_impl *)addr;
pe_free_unit_ctx_impl(ictx,0);
}
}
|