Blame src/logic/mdso_map_input.c

cde03b
/****************************************************************/
cde03b
/*  mdso: midipix dso scavenger                                 */
cde03b
/*  Copyright (C) 2015  Z. Gilboa                               */
cde03b
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
cde03b
/****************************************************************/
cde03b
cde03b
#include <stdint.h>
cde03b
#include <stdbool.h>
cde03b
#include <unistd.h>
cde03b
#include <fcntl.h>
cde03b
#include <sys/mman.h>
cde03b
#include <sys/types.h>
cde03b
#include <sys/stat.h>
cde03b
cde03b
#include <mdso/mdso.h>
cde03b
cde03b
int mdso_map_input(
cde03b
	int			fd,
cde03b
	const char *		path,
cde03b
	int			prot,
cde03b
	struct mdso_input *	map)
cde03b
{
cde03b
	struct stat	stat;
cde03b
	bool		fnew;
cde03b
	int		ret;
cde03b
cde03b
	if ((fnew = (fd < 0)))
cde03b
		fd  = open(path,O_RDONLY | O_CLOEXEC);
cde03b
cde03b
	if (fd < 0)
cde03b
		return -1;
cde03b
cde03b
	if ((ret = fstat(fd,&stat) < 0) && fnew)
cde03b
		close(fd);
cde03b
cde03b
	if (ret < 0)
cde03b
		return -1;
cde03b
cde03b
	map->size = stat.st_size;
cde03b
	map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0);
cde03b
cde03b
	if (fnew)
cde03b
		close(fd);
cde03b
cde03b
	return (map->addr == MAP_FAILED) ? -1 : 0;
cde03b
}
cde03b
cde03b
int mdso_unmap_input(struct mdso_input * map)
cde03b
{
cde03b
	return munmap(map->addr,map->size);
cde03b
}