From e80d1c338b73b8c26fc9cf94da23a09776ea3bb6 Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Thu, 6 May 2010 16:01:08 +0000 Subject: [PATCH] Remove the watcher if user kills the terminal while watching. git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@556 db0b04b0-f4d1-0310-9a6d-de3e77497b0e --- dgamelaunch.c | 26 ++++++++++++++++++++++++++ dgamelaunch.h | 7 +++++++ 2 files changed, 33 insertions(+) diff --git a/dgamelaunch.c b/dgamelaunch.c index b4c44f7..7dff1c8 100644 --- a/dgamelaunch.c +++ b/dgamelaunch.c @@ -253,6 +253,11 @@ gen_inprogress_lock (int game, pid_t pid, char* ttyrec_filename) /* ************************************************************* */ +#ifdef USE_SHMEM +int hup_shm_idx = -1; +char *hup_shm_ttyrec_fn = NULL; +#endif + void catch_sighup (int signum) { @@ -262,6 +267,23 @@ catch_sighup (int signum) kill (child, SIGHUP); sleep (5); } +#ifdef USE_SHMEM + if (hup_shm_idx != -1) { + struct dg_shm *shm_dg_data = NULL; + struct dg_shm_game *shm_dg_game = NULL; + shm_init(&shm_dg_data, &shm_dg_game); + + shm_sem_wait(shm_dg_data); + if (shm_dg_game[hup_shm_idx].in_use && + !strcmp(shm_dg_game[hup_shm_idx].ttyrec_fn, hup_shm_ttyrec_fn) && + (shm_dg_game[hup_shm_idx].nwatchers > 0)) { + shm_dg_game[hup_shm_idx].nwatchers--; + } + shm_sem_post(shm_dg_data); + hup_shm_idx = -1; + free(hup_shm_ttyrec_fn); + } +#endif debug_write("catchup sighup"); graceful_exit (2); } @@ -872,6 +894,8 @@ watchgame: shm_dg_game[shm_idx].nwatchers++; games[idx]->nwatchers++; } + hup_shm_idx = shm_idx; + hup_shm_ttyrec_fn = strdup(games[idx]->ttyrec_fn); shm_sem_post(shm_dg_data); } #endif @@ -888,6 +912,8 @@ watchgame: setproctitle(""); #ifdef USE_SHMEM if (games[idx]->is_in_shm) { + hup_shm_idx = -1; + free(hup_shm_ttyrec_fn); shm_sem_wait(shm_dg_data); if (shm_dg_game[shm_idx].in_use && !strcmp(shm_dg_game[shm_idx].ttyrec_fn, games[idx]->ttyrec_fn) && diff --git a/dgamelaunch.h b/dgamelaunch.h index e3cace2..2aa6acb 100644 --- a/dgamelaunch.h +++ b/dgamelaunch.h @@ -251,6 +251,13 @@ extern void debug_write(char *str); extern struct dg_game **sort_games(struct dg_game **games, int len, dg_sortmode sortmode); int runmenuloop(struct dg_menu *menu); + +extern void shm_sem_wait(struct dg_shm *shm_dg_data); +extern void shm_sem_post(struct dg_shm *shm_dg_data); +extern void shm_update(struct dg_shm *shm_dg_data, struct dg_game **games, int len); +extern void shm_mk_keys(key_t *shm_key, key_t *shm_sem_key); +extern void shm_init(struct dg_shm **shm_dg_data, struct dg_shm_game **shm_dg_game); + extern int dgl_getch(void); extern void idle_alarm_set_enabled(int enabled); extern void idle_alarm_reset(void);