Close file descriptors after mmap

Now is no longer needed to increase the limit of the maximum number of
file descriptors opened when loading large traces.
This commit is contained in:
Rodrigo Arias 2021-11-29 16:02:09 +01:00
parent 50081b0dc0
commit ca9bbf6401
2 changed files with 25 additions and 8 deletions

6
emu.h
View File

@ -267,9 +267,6 @@ struct ovni_ethread {
/* The process associated with this thread */ /* The process associated with this thread */
struct ovni_eproc *proc; struct ovni_eproc *proc;
/* Stream fd */
int stream_fd;
enum ethread_state state; enum ethread_state state;
int is_running; int is_running;
int is_active; int is_active;
@ -296,6 +293,9 @@ struct ovni_ethread {
/* These pointers keep a linked list of threads in each CPU */ /* These pointers keep a linked list of threads in each CPU */
struct ovni_ethread *prev; struct ovni_ethread *prev;
struct ovni_ethread *next; struct ovni_ethread *next;
/* Trace file path */
char tracefile[PATH_MAX];
}; };
/* State of each emulated process */ /* State of each emulated process */

27
trace.c
View File

@ -85,13 +85,15 @@ load_thread(struct ovni_ethread *thread, struct ovni_eproc *proc, int index, int
thread->gindex = total_threads++; thread->gindex = total_threads++;
thread->state = TH_ST_UNKNOWN; thread->state = TH_ST_UNKNOWN;
thread->proc = proc; thread->proc = proc;
thread->stream_fd = open(filepath, O_RDWR);
if(thread->stream_fd == -1) if(strlen(filepath) >= PATH_MAX)
{ {
perror("open"); err("filepath too large: %s\n", filepath);
return -1; return -1;
} }
strcpy(thread->tracefile, filepath);
return 0; return 0;
} }
@ -388,8 +390,15 @@ static int
load_stream_buf(struct ovni_stream *stream, struct ovni_ethread *thread) load_stream_buf(struct ovni_stream *stream, struct ovni_ethread *thread)
{ {
struct stat st; struct stat st;
int fd;
if(fstat(thread->stream_fd, &st) < 0) if((fd = open(thread->tracefile, O_RDWR)) == -1)
{
perror("open");
return -1;
}
if(fstat(fd, &st) < 0)
{ {
perror("fstat"); perror("fstat");
return -1; return -1;
@ -402,12 +411,13 @@ load_stream_buf(struct ovni_stream *stream, struct ovni_ethread *thread)
stream->buf = NULL; stream->buf = NULL;
stream->active = 0; stream->active = 0;
/* No need to do anything else */
return 0; return 0;
} }
stream->size = st.st_size; stream->size = st.st_size;
stream->buf = mmap(NULL, stream->size, PROT_READ | PROT_WRITE, stream->buf = mmap(NULL, stream->size, PROT_READ | PROT_WRITE,
MAP_SHARED, thread->stream_fd, 0); MAP_SHARED, fd, 0);
if(stream->buf == MAP_FAILED) if(stream->buf == MAP_FAILED)
{ {
@ -417,6 +427,13 @@ load_stream_buf(struct ovni_stream *stream, struct ovni_ethread *thread)
stream->active = 1; stream->active = 1;
/* No need to keep the fd open */
if(close(fd))
{
perror("close");
return -1;
}
return 0; return 0;
} }