Blame src/logic/mdso_map_input.c

cde03b
/****************************************************************/
cde03b
/*  mdso: midipix dso scavenger                                 */
3e816c
/*  Copyright (C) 2015--2016  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>
51430e
#include "mdso_errinfo_impl.h"
cde03b
cde03b
int mdso_map_input(
51430e
	const struct mdso_driver_ctx *	dctx,
51430e
	int				fd,
51430e
	const char *			path,
51430e
	int				prot,
51430e
	struct mdso_input *		map)
cde03b
{
f2df67
	struct stat	st;
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)
51430e
		return MDSO_SYSTEM_ERROR(dctx);
cde03b
f2df67
	if ((ret = fstat(fd,&st) < 0) && fnew)
cde03b
		close(fd);
cde03b
cde03b
	if (ret < 0)
51430e
		return MDSO_SYSTEM_ERROR(dctx);
cde03b
f2df67
	map->size = st.st_size;
cde03b
	map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0);
cde03b
cde03b
	if (fnew)
cde03b
		close(fd);
cde03b
51430e
	return (map->addr == MAP_FAILED)
51430e
		? MDSO_SYSTEM_ERROR(dctx)
51430e
		: 0;
cde03b
}
cde03b
cde03b
int mdso_unmap_input(struct mdso_input * map)
cde03b
{
cde03b
	return munmap(map->addr,map->size);
cde03b
}