From 270520b4a2eac8725c8575c3180964289722e191 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Tue, 19 Dec 2017 10:59:40 +0100 Subject: schulung_tools: add various demos and tools Different tools have been used by various trainers as demos. Put all these into master so they are available to all trainers. ipc_pipe: ipc demo using pipes ipc_shm: ipc demo using shared memory libduma: source and instructions for compiling libduma matrix: demo of good and bad cache access mtrace: patch and infos for using mtrace with ASLR rtex: demo of handling page faults Signed-off-by: John Ogness --- schulung_tools/ipc_shm/shm.c | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 schulung_tools/ipc_shm/shm.c (limited to 'schulung_tools/ipc_shm/shm.c') diff --git a/schulung_tools/ipc_shm/shm.c b/schulung_tools/ipc_shm/shm.c new file mode 100644 index 0000000..777fe79 --- /dev/null +++ b/schulung_tools/ipc_shm/shm.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "shm.h" + +#define SHARE_NAME "/ipc.shm" + +/* open and return a pointer to shared data */ +struct share_data *get_share(void) +{ + struct share_data *data; + int fd; + + fd = shm_open(SHARE_NAME, O_RDWR, 0); + if (fd == -1) + return NULL; + + data = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); + if (data == MAP_FAILED) { + close(fd); + return NULL; + } + + close(fd); + + return data; +} + +/* initialize shared data */ +void init_share(struct share_data *data) +{ + pthread_mutexattr_t mattr; + pthread_condattr_t cattr; + + pthread_mutexattr_init(&mattr); + pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT); + pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); + pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST); + pthread_mutex_init(&data->m, &mattr); + + pthread_condattr_init(&cattr); + pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); + pthread_cond_init(&data->c, &cattr); +} + +/* create a zero'd out file of size 1 page */ +int create_share(void) +{ + int ret; + int fd; + + fd = shm_open(SHARE_NAME, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR); + if (fd == -1) + return -1; + + ret = ftruncate(fd, sysconf(_SC_PAGESIZE)); + + close(fd); + + return ret; +} + +/* remove the share */ +void remove_share(void) +{ + shm_unlink(SHARE_NAME); +} -- cgit v1.2.3