From 67d50b6ea662fcb959bb63a8278187b79dfcaa02 Mon Sep 17 00:00:00 2001 From: Jonas Kramer Date: Sun, 30 Aug 2009 18:46:45 +0200 Subject: [PATCH] Cleaned up exit functions. --- source/include/interface.h | 2 ++ source/interface.c | 21 +++++++++++++++++++-- source/main.c | 14 ++------------ source/sckif.c | 2 +- source/service.c | 13 +++++++++++-- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/source/include/interface.h b/source/include/interface.h index 6b25427..ce74306 100644 --- a/source/include/interface.h +++ b/source/include/interface.h @@ -20,5 +20,7 @@ extern void shownp(void); extern void tag(struct hash); extern int rate(const char *); extern char * shellescape(const char *); +extern void quit(); +extern void unlinknp(); #endif diff --git a/source/interface.c b/source/interface.c index c6420b6..4940eae 100644 --- a/source/interface.c +++ b/source/interface.c @@ -39,6 +39,8 @@ extern time_t pausetime; +void unlinknp(void); + struct hash track; char * shellescape(const char *); @@ -90,8 +92,7 @@ void interface(int interactive) { break; case 'Q': - unlink(rcpath("session")); - exit(EXIT_SUCCESS); + quit(); case 'i': if(playfork) { @@ -474,3 +475,19 @@ char * shellescape(const char * string) { return escaped; } + + +void quit() { + unlink(rcpath("session")); + unlinknp(); + exit(EXIT_SUCCESS); +} + +void unlinknp(void) { + /* Remove now-playing file. */ + if(haskey(& rc, "np-file")) { + const char * np = value(& rc, "np-file"); + if(np != NULL) + unlink(np); + } +} diff --git a/source/main.c b/source/main.c index ca003c1..8da5491 100644 --- a/source/main.c +++ b/source/main.c @@ -63,7 +63,6 @@ static void forcequit(int); static void help(const char *, int); static void playsig(int); static void stopsig(int); -static void unlinknp(void); pid_t ppid = 0; @@ -317,8 +316,7 @@ int main(int argc, char ** argv) { unlinknp(); if(delayquit) { - unlink(rcpath("session")); - exit(EXIT_SUCCESS); + quit(); } } pausetime = 0; @@ -601,7 +599,7 @@ static void cleanup(void) { static void forcequit(int sig) { if(sig == SIGINT) { fputs("Try to press Q next time you want to quit.\n", stderr); - exit(-1); + exit(EXIT_FAILURE); } } @@ -623,11 +621,3 @@ static void stopsig(int sig) { } } -static void unlinknp(void) { - /* Remove now-playing file. */ - if(haskey(& rc, "np-file")) { - const char * np = value(& rc, "np-file"); - if(np != NULL) - unlink(np); - } -} diff --git a/source/sckif.c b/source/sckif.c index 3fdf24a..b333563 100644 --- a/source/sckif.c +++ b/source/sckif.c @@ -237,7 +237,7 @@ void execcmd(const char * cmd, char * reply) { break; case 4: - exit(EXIT_SUCCESS); + quit(); case 5: if(* (cmd + 5)) diff --git a/source/service.c b/source/service.c index 2f873c4..94f8184 100644 --- a/source/service.c +++ b/source/service.c @@ -238,6 +238,7 @@ int station(const char * stationURL) { */ int play(struct playlist * list) { unsigned i; + int pipefd[2]; char * keys [] = { "creator", "title", "album", "duration", "station", "lastfm:trackauth", "trackpage", "artistpage", "albumpage", @@ -261,16 +262,16 @@ int play(struct playlist * list) { for(i = 0; i < (sizeof(keys) / sizeof(char *)); ++i) set(& track, keys[i], value(& list->track->track, keys[i])); - int pipefd[2]; if(pipe(pipefd) != 0) return !0; + playfork = fork(); if(!playfork) { - close(pipefd[1]); FILE * fd = NULL; const char * location = value(& list->track->track, "location"); + close(pipefd[1]); rmsckif(); subfork = 0; @@ -279,8 +280,13 @@ int play(struct playlist * list) { fetch(location, & fd, NULL, NULL); if(fd != NULL) { + /* + If there was an error, tell the main process about it by + sending SIGUSR2. + */ if(!playback(fd, pipefd[0])) kill(getppid(), SIGUSR2); + close(pipefd[0]); fshutdown(& fd); } @@ -288,9 +294,12 @@ int play(struct playlist * list) { exit(EXIT_SUCCESS); } + close(pipefd[0]); + if(playpipe != 0) close(playpipe); + playpipe = pipefd[1]; return !0; -- 2.11.4.GIT