Blame src/logic/sfrt_map_input.c

f46039
#include <stdint.h>
f46039
#include <stdbool.h>
f46039
#include <unistd.h>
f46039
#include <fcntl.h>
f46039
#include <sys/mman.h>
f46039
#include <sys/types.h>
f46039
#include <sys/stat.h>
f46039
f46039
#include <sofort/sofort.h>
47a21b
#include "sofort_errinfo_impl.h"
f46039
f46039
int sfrt_map_input(
47a21b
	const struct sfrt_driver_ctx *	dctx,
47a21b
	int				fd,
47a21b
	const char *			path,
47a21b
	int				prot,
47a21b
	struct sfrt_input *		map)
f46039
{
02966c
	struct stat	st;
f46039
	bool		fnew;
f46039
	int		ret;
f46039
f46039
	if ((fnew = (fd < 0)))
f46039
		fd  = open(path,O_RDONLY | O_CLOEXEC);
f46039
f46039
	if (fd < 0)
47a21b
		return SFRT_SYSTEM_ERROR(dctx);
f46039
02966c
	if ((ret = fstat(fd,&st) < 0) && fnew)
f46039
		close(fd);
f46039
deb77f
	else if ((st.st_size == 0) && fnew)
deb77f
		close(fd);
deb77f
f46039
	if (ret < 0)
47a21b
		return SFRT_SYSTEM_ERROR(dctx);
f46039
deb77f
	else if (st.st_size == 0)
deb77f
		return SFRT_CUSTOM_ERROR(
deb77f
			dctx,SFRT_ERR_SOURCE_SIZE_ZERO);
deb77f
02966c
	map->size = st.st_size;
f46039
	map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0);
f46039
f46039
	if (fnew)
f46039
		close(fd);
f46039
47a21b
	return (map->addr == MAP_FAILED)
47a21b
		? SFRT_SYSTEM_ERROR(dctx)
47a21b
		: 0;
f46039
}
f46039
f46039
int sfrt_unmap_input(struct sfrt_input * map)
f46039
{
f46039
	return munmap(map->addr,map->size);
f46039
}