Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
epgdb: load - mmap - add SIGBUS recovery (when the file cannot be rea…
…d), fixes #3238
  • Loading branch information
perexg committed Nov 3, 2015
1 parent 70a5d62 commit d01071f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/epgdb.c
Expand Up @@ -21,6 +21,8 @@
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <setjmp.h>

#include "tvheadend.h"
#include "queue.h"
Expand Down Expand Up @@ -143,6 +145,16 @@ _epgdb_v2_process( char **sect, htsmsg_t *m, epggrab_stats_t *stats )
}
}

/*
* Recovery
*/
static sigjmp_buf epg_mmap_env;

static void epg_mmap_sigbus (int sig, siginfo_t *siginfo, void *ptr)
{
siglongjmp(epg_mmap_env, 1);
}

/*
* Load data
*/
Expand All @@ -154,6 +166,7 @@ void epg_init ( void )
uint8_t *mem, *rp;
epggrab_stats_t stats;
int ver = EPG_DB_VERSION;
struct sigaction act, oldact;
char *sect = NULL;

/* Find the right file (and version) */
Expand All @@ -168,6 +181,15 @@ void epg_init ( void )
tvhlog(LOG_DEBUG, "epgdb", "database does not exist");
return;
}

memset (&act, 0, sizeof(act));
act.sa_sigaction = epg_mmap_sigbus;
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGBUS, &act, &oldact)) {
tvhlog(LOG_ERR, "epgdb", "failed to install SIGBUS handler");
close(fd);
return;
}

/* Map file to memory */
if ( fstat(fd, &st) != 0 ) {
Expand All @@ -185,6 +207,13 @@ void epg_init ( void )
goto end;
}

if (sigsetjmp(epg_mmap_env, 1)) {
tvhlog(LOG_ERR, "epgdb", "failed to read from mapped file");
if (mem)
munmap(mem, st.st_size);
goto end;
}

/* Process */
memset(&stats, 0, sizeof(stats));
while ( remain > 4 ) {
Expand Down Expand Up @@ -245,6 +274,7 @@ void epg_init ( void )
/* Close file */
munmap(mem, st.st_size);
end:
sigaction(SIGBUS, &oldact, NULL);
close(fd);
}

Expand Down

0 comments on commit d01071f

Please sign in to comment.