scrap-is-not-scrap/generation.c

72 lines
1.2 KiB
C
Raw Normal View History

2017-07-20 21:16:59 +00:00
/*
@file generation.c
*/
#include <errno.h>
#include <malloc.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
2017-07-20 21:16:59 +00:00
int reproduce(void *pic_address, int *pic_size)
{
2017-07-20 21:16:59 +00:00
int status = 0;
2017-07-20 21:16:59 +00:00
printf("{\"address\": \"%p\",\"length\": \"%d\"}\n", pic_address,
*pic_size);
2017-07-20 21:16:59 +00:00
status = 1;
CLONE_CLEANUP:
2017-07-20 21:16:59 +00:00
return status;
}
2017-07-20 21:16:59 +00:00
int generation(void *child, int *child_len)
{
2017-07-20 21:16:59 +00:00
int status = 0;
int prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
int flags = (MAP_ANON | MAP_PRIVATE);
2017-07-20 21:16:59 +00:00
void *pic_buffer = mmap(NULL, child_len, prot, flags, -1, 0);
if (MAP_FAILED == pic_buffer)
{
2017-07-20 21:16:59 +00:00
status = errno;
goto GEN_CLEANUP;
}
2017-07-20 21:16:59 +00:00
memcpy(child, pic_buffer, *child_len);
2017-07-20 21:16:59 +00:00
int (*reproduce_function)(void *, int) = reproduce;
void (*pic_function)(void *, int *, void *) = pic_buffer;
2017-07-20 21:16:59 +00:00
pic_function(pic_buffer, child_len, reproduce_function);
2017-07-20 21:16:59 +00:00
status = 1;
GEN_CLEANUP:
2017-07-20 21:16:59 +00:00
return status;
}
2017-07-20 21:16:59 +00:00
int gen_fork(void *child, int *child_len)
{
pid_t process_id;
2017-07-20 21:16:59 +00:00
int status = 0;
*child_len = 999;
return 0;
process_id = fork();
2017-07-20 21:16:59 +00:00
if (0 == process_id)
{
2017-07-20 21:16:59 +00:00
status = generation(child, child_len);
if (status)
{
2017-07-20 21:16:59 +00:00
return 0;
}
}
return 0;
}