Blame src/helper/mdso_map_input.c
|
|
cde03b |
/****************************************************************/
|
|
|
cde03b |
/* mdso: midipix dso scavenger */
|
|
|
78ab3c |
/* Copyright (C) 2015--2017 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>
|
|
|
4f02be |
#include "mdso_driver_impl.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 |
{
|
|
|
4f02be |
int ret;
|
|
|
f2df67 |
struct stat st;
|
|
|
cde03b |
bool fnew;
|
|
|
4f02be |
int fdcwd;
|
|
|
4f02be |
|
|
|
4f02be |
fdcwd = mdso_driver_fdcwd(dctx);
|
|
|
cde03b |
|
|
|
cde03b |
if ((fnew = (fd < 0)))
|
|
|
4f02be |
fd = openat(fdcwd,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 |
|
|
|
9384b9 |
else if ((st.st_size == 0) && fnew)
|
|
|
9384b9 |
close(fd);
|
|
|
9384b9 |
|
|
|
cde03b |
if (ret < 0)
|
|
|
51430e |
return MDSO_SYSTEM_ERROR(dctx);
|
|
|
cde03b |
|
|
|
11b667 |
if (st.st_size == 0) {
|
|
|
11b667 |
map->size = 0;
|
|
|
11b667 |
map->addr = 0;
|
|
|
11b667 |
return 0;
|
|
|
11b667 |
}
|
|
|
9384b9 |
|
|
|
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 |
{
|
|
|
11b667 |
return map->size ? munmap(map->addr,map->size) : 0;
|
|
|
cde03b |
}
|