From c61328dfece5c7346aabd4bc0027a9eb8126dd08 Mon Sep 17 00:00:00 2001 From: midipix Date: Fri, 4 Dec 2015 21:23:24 -0500 Subject: API redesign 4/10: pe_common_ctx: protect the common context structure against direct modification. --- include/perk/perk.h | 2 +- src/driver/pe_driver_ctx.c | 17 +++++++++++------ src/driver/pe_unit_ctx.c | 6 +++--- src/internal/perk_impl.h | 16 ++++++++++++++++ src/perk.c | 6 +++--- 5 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 src/internal/perk_impl.h diff --git a/include/perk/perk.h b/include/perk/perk.h index e2001e1..f19e596 100644 --- a/include/perk/perk.h +++ b/include/perk/perk.h @@ -131,7 +131,7 @@ struct pe_driver_ctx { const char ** units; const char * program; const char * module; - struct pe_common_ctx cctx; + const struct pe_common_ctx * cctx; }; struct pe_unit_ctx { diff --git a/src/driver/pe_driver_ctx.c b/src/driver/pe_driver_ctx.c index 9daad39..9f9dd88 100644 --- a/src/driver/pe_driver_ctx.c +++ b/src/driver/pe_driver_ctx.c @@ -1,8 +1,10 @@ #include #include #include + #include #include +#include "perk_impl.h" #include "argv/argv.h" enum app_tags { @@ -47,7 +49,7 @@ struct pe_driver_ctx_alloc { struct pe_output_ctx outctx; struct pe_linker_ctx lnkctx; struct pe_server_ctx srvctx; - struct pe_driver_ctx ctx; + struct pe_driver_ctx_impl ctx; uint64_t guard; const char * units[]; }; @@ -85,7 +87,7 @@ static int pe_driver_usage( return PERK_USAGE; } -static struct pe_driver_ctx * pe_driver_ctx_alloc(struct argv_meta * meta, size_t nunits) +static struct pe_driver_ctx_impl * pe_driver_ctx_alloc(struct argv_meta * meta, size_t nunits) { struct pe_driver_ctx_alloc * ictx; size_t size; @@ -108,7 +110,7 @@ static struct pe_driver_ctx * pe_driver_ctx_alloc(struct argv_meta * meta, size_ if (!entry->fopt) *units++ = entry->arg; - ictx->ctx.units = ictx->units; + ictx->ctx.ctx.units = ictx->units; return &ictx->ctx; } @@ -124,7 +126,7 @@ int pe_get_driver_ctx( uint32_t flags, struct pe_driver_ctx ** pctx) { - struct pe_driver_ctx * ctx; + struct pe_driver_ctx_impl * ctx; struct argv_meta * meta; struct argv_entry * entry; size_t nunits; @@ -199,7 +201,7 @@ int pe_get_driver_ctx( if (pretty && !strcmp(pretty,"yaml")) fflags |= PERK_PRETTY_YAML; - ctx->program = program; + ctx->ctx.program = program; ctx->cctx.drvflags = dflags; ctx->cctx.fmtflags = fflags; ctx->cctx.output = output; @@ -212,7 +214,9 @@ int pe_get_driver_ctx( ctx->cctx.fddst = AT_FDCWD; ctx->cctx.fdtmp = AT_FDCWD; - *pctx = ctx; + ctx->ctx.cctx = &ctx->cctx; + + *pctx = &ctx->ctx; return PERK_OK; } @@ -245,6 +249,7 @@ void pe_free_driver_ctx(struct pe_driver_ctx * ctx) if (ctx) { addr = (uintptr_t)ctx - offsetof(struct pe_driver_ctx_alloc,ctx); + addr = addr - offsetof(struct pe_driver_ctx_impl,ctx); ictx = (struct pe_driver_ctx_alloc *)addr; pe_free_driver_ctx_impl(ictx); } diff --git a/src/driver/pe_unit_ctx.c b/src/driver/pe_unit_ctx.c index bed99c6..b86719c 100644 --- a/src/driver/pe_unit_ctx.c +++ b/src/driver/pe_unit_ctx.c @@ -26,17 +26,17 @@ int pe_get_unit_ctx( if (!dctx || !(ctx = calloc(sizeof(*ctx),1))) return -1; - prot = (dctx->cctx.actflags & PERK_ACTION_MAP_READWRITE) + prot = (dctx->cctx->actflags & PERK_ACTION_MAP_READWRITE) ? PROT_READ | PROT_WRITE : PROT_READ; - if (pe_map_raw_image(dctx->cctx.fdin,path,prot,&ctx->map)) + 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, + memcpy(&ctx->cctx,dctx->cctx, sizeof(ctx->cctx)); ctx->path = path; diff --git a/src/internal/perk_impl.h b/src/internal/perk_impl.h new file mode 100644 index 0000000..dd8ff4d --- /dev/null +++ b/src/internal/perk_impl.h @@ -0,0 +1,16 @@ +#ifndef PERK_IMPL_H +#define PERK_IMPL_H + +#include +#include +#include + +#include +#include + +struct pe_driver_ctx_impl { + struct pe_common_ctx cctx; + struct pe_driver_ctx ctx; +}; + +#endif diff --git a/src/perk.c b/src/perk.c index 652141b..c057fc2 100644 --- a/src/perk.c +++ b/src/perk.c @@ -15,9 +15,9 @@ static ssize_t perk_version(struct pe_driver_ctx * dctx) char buf[512]; size_t len; - if (dctx->cctx.fdout >= 0) { + if (dctx->cctx->fdout >= 0) { len = sprintf(buf,vermsg,dctx->program,PERK_GIT_VERSION); - return write(dctx->cctx.fdout,buf,len); + return write(dctx->cctx->fdout,buf,len); } else return fprintf(stdout,vermsg,dctx->program,PERK_GIT_VERSION); } @@ -68,7 +68,7 @@ static int perk_main(int argc, const char ** argv, const char ** envp) if ((ret = pe_get_driver_ctx(argv,envp,PERK_DRIVER_FLAGS,&dctx))) return (ret == PERK_USAGE) ? !--argc : 2; - if (dctx->cctx.drvflags & PERK_DRIVER_VERSION) + if (dctx->cctx->drvflags & PERK_DRIVER_VERSION) if ((perk_version(dctx)) < 0) return perk_exit(dctx,2); -- cgit v1.2.3