diff options
author | midipix <writeonce@midipix.org> | 2015-12-17 04:11:07 -0500 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2016-11-11 00:22:28 -0500 |
commit | cde03b7121400bd61167e8db5303b3cd0ff03a0c (patch) | |
tree | 58a4ff993fefddf8d963f6a3dd5d1ae6760e2df9 /src/driver | |
parent | 08279ea75fe76bdccb9809030ed3e5b261c9d557 (diff) | |
download | mdso-cde03b7121400bd61167e8db5303b3cd0ff03a0c.tar.bz2 mdso-cde03b7121400bd61167e8db5303b3cd0ff03a0c.tar.xz |
created skeleton.
Diffstat (limited to 'src/driver')
-rw-r--r-- | src/driver/mdso_driver_ctx.c | 177 | ||||
-rw-r--r-- | src/driver/mdso_unit_ctx.c | 62 |
2 files changed, 239 insertions, 0 deletions
diff --git a/src/driver/mdso_driver_ctx.c b/src/driver/mdso_driver_ctx.c new file mode 100644 index 0000000..c88bbf4 --- /dev/null +++ b/src/driver/mdso_driver_ctx.c @@ -0,0 +1,177 @@ +/****************************************************************/ +/* mdso: midipix dso scavenger */ +/* Copyright (C) 2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.MDSO. */ +/****************************************************************/ + +#include <stdint.h> +#include <unistd.h> +#include <fcntl.h> + +#define ARGV_DRIVER + +#include <mdso/mdso.h> +#include "mdso_driver_impl.h" +#include "argv/argv.h" + +extern const struct argv_option mdso_default_options[]; + +struct mdso_driver_ctx_alloc { + struct argv_meta * meta; + struct mdso_driver_ctx_impl ctx; + uint64_t guard; + const char * units[]; +}; + +static uint32_t mdso_argv_flags(uint32_t flags) +{ + uint32_t ret = 0; + + if (flags & MDSO_DRIVER_VERBOSITY_NONE) + ret |= ARGV_VERBOSITY_NONE; + + if (flags & MDSO_DRIVER_VERBOSITY_ERRORS) + ret |= ARGV_VERBOSITY_ERRORS; + + if (flags & MDSO_DRIVER_VERBOSITY_STATUS) + ret |= ARGV_VERBOSITY_STATUS; + + return ret; +} + +static int mdso_driver_usage( + const char * program, + const char * arg, + const struct argv_option * options, + struct argv_meta * meta) +{ + char header[512]; + + snprintf(header,sizeof(header), + "Usage: %s [options] <file>...\n" "Options:\n", + program); + + argv_usage(stdout,header,options,arg); + argv_free(meta); + + return MDSO_USAGE; +} + +static struct mdso_driver_ctx_impl * mdso_driver_ctx_alloc(struct argv_meta * meta, size_t nunits) +{ + struct mdso_driver_ctx_alloc * ictx; + size_t size; + struct argv_entry * entry; + const char ** units; + + size = sizeof(struct mdso_driver_ctx_alloc); + size += (nunits+1)*sizeof(const char *); + + if (!(ictx = calloc(size,1))) + return 0; + + for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++) + if (!entry->fopt) + *units++ = entry->arg; + + ictx->ctx.ctx.units = ictx->units; + return &ictx->ctx; +} + +int mdso_get_driver_ctx_fail(struct argv_meta * meta) +{ + argv_free(meta); + return -1; +} + +int mdso_get_driver_ctx( + const char ** argv, + const char ** envp, + uint32_t flags, + struct mdso_driver_ctx ** pctx) +{ + struct mdso_driver_ctx_impl * ctx; + struct mdso_common_ctx cctx; + const struct argv_option * options; + struct argv_meta * meta; + struct argv_entry * entry; + size_t nunits; + const char * program; + + options = mdso_default_options; + + if (!(meta = argv_get(argv,options,mdso_argv_flags(flags)))) + return -1; + + nunits = 0; + program = argv_program_name(argv[0]); + memset(&cctx,0,sizeof(cctx)); + + if (!argv[1] && (flags & MDSO_DRIVER_VERBOSITY_USAGE)) + return mdso_driver_usage(program,0,options,meta); + + /* get options, count units */ + for (entry=meta->entries; entry->fopt || entry->arg; entry++) { + if (entry->fopt) { + switch (entry->tag) { + case TAG_HELP: + if (flags & MDSO_DRIVER_VERBOSITY_USAGE) + return mdso_driver_usage(program,entry->arg,options,meta); + + case TAG_VERSION: + cctx.drvflags |= MDSO_DRIVER_VERSION; + break; + } + } else + nunits++; + } + + if (!(ctx = mdso_driver_ctx_alloc(meta,nunits))) + return mdso_get_driver_ctx_fail(meta); + + ctx->ctx.program = program; + ctx->ctx.cctx = &ctx->cctx; + + memcpy(&ctx->cctx,&cctx,sizeof(cctx)); + *pctx = &ctx->ctx; + return MDSO_OK; +} + +int mdso_create_driver_ctx( + const struct mdso_common_ctx * cctx, + struct mdso_driver_ctx ** pctx) +{ + struct argv_meta * meta; + struct mdso_driver_ctx_impl * ctx; + const char * argv[] = {"mdso_driver",0}; + + if (!(meta = argv_get(argv,mdso_default_options,0))) + return -1; + + if (!(ctx = mdso_driver_ctx_alloc(meta,0))) + return mdso_get_driver_ctx_fail(0); + + ctx->ctx.cctx = &ctx->cctx; + memcpy(&ctx->cctx,cctx,sizeof(*cctx)); + *pctx = &ctx->ctx; + return MDSO_OK; +} + +static void mdso_free_driver_ctx_impl(struct mdso_driver_ctx_alloc * ictx) +{ + argv_free(ictx->meta); + free(ictx); +} + +void mdso_free_driver_ctx(struct mdso_driver_ctx * ctx) +{ + struct mdso_driver_ctx_alloc * ictx; + uintptr_t addr; + + if (ctx) { + addr = (uintptr_t)ctx - offsetof(struct mdso_driver_ctx_alloc,ctx); + addr = addr - offsetof(struct mdso_driver_ctx_impl,ctx); + ictx = (struct mdso_driver_ctx_alloc *)addr; + mdso_free_driver_ctx_impl(ictx); + } +} diff --git a/src/driver/mdso_unit_ctx.c b/src/driver/mdso_unit_ctx.c new file mode 100644 index 0000000..870e060 --- /dev/null +++ b/src/driver/mdso_unit_ctx.c @@ -0,0 +1,62 @@ +/****************************************************************/ +/* mdso: midipix dso scavenger */ +/* Copyright (C) 2015 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.MDSO. */ +/****************************************************************/ + +#include <stdint.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> + +#include <mdso/mdso.h> +#include "mdso_driver_impl.h" + +static int mdso_free_unit_ctx_impl(struct mdso_unit_ctx_impl * ctx, int status) +{ + if (ctx) { + mdso_unmap_input(&ctx->map); + free(ctx); + } + + return status; +} + +int mdso_get_unit_ctx( + const struct mdso_driver_ctx * dctx, + const char * path, + struct mdso_unit_ctx ** pctx) +{ + struct mdso_unit_ctx_impl * ctx; + + if (!dctx || !(ctx = calloc(sizeof(*ctx),1))) + return -1; + + if (mdso_map_input(-1,path,PROT_READ,&ctx->map)) + return mdso_free_unit_ctx_impl(ctx,-1); + + memcpy(&ctx->cctx,dctx->cctx, + sizeof(ctx->cctx)); + + ctx->path = path; + + ctx->uctx.path = &ctx->path; + ctx->uctx.map = &ctx->map; + ctx->uctx.cctx = &ctx->cctx; + + *pctx = &ctx->uctx; + return 0; +} + +void mdso_free_unit_ctx(struct mdso_unit_ctx * ctx) +{ + struct mdso_unit_ctx_impl * ictx; + uintptr_t addr; + + if (ctx) { + addr = (uintptr_t)ctx - offsetof(struct mdso_unit_ctx_impl,uctx); + ictx = (struct mdso_unit_ctx_impl *)addr; + mdso_free_unit_ctx_impl(ictx,0); + } +} |