make "update" command background/non-blocking

git-svn-id: https://svn.musicpd.org/mpd/trunk@665 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Warren Dukes 2004-04-11 01:53:25 +00:00
parent 171a7752a8
commit 12ee016607
7 changed files with 111 additions and 26 deletions

View File

@ -317,8 +317,7 @@ int handleSearch(FILE * fp, unsigned int * permission, int argArrayLength,
int handleUpdate(FILE * fp, unsigned int * permission, int argArrayLength, int handleUpdate(FILE * fp, unsigned int * permission, int argArrayLength,
char ** argArray) char ** argArray)
{ {
incrPlaylistVersion(); return updateInit(fp);
return updateMp3Directory(fp);
} }
int handleNext(FILE * fp, unsigned int * permission, int argArrayLength, int handleNext(FILE * fp, unsigned int * permission, int argArrayLength,

View File

@ -26,14 +26,21 @@
#include "conf.h" #include "conf.h"
#include "stats.h" #include "stats.h"
#include "playlist.h" #include "playlist.h"
#include "listen.h"
#include "interface.h"
#include "volume.h"
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <signal.h>
#define DIRECTORY_DIR "directory: " #define DIRECTORY_DIR "directory: "
#define DIRECTORY_MTIME "mtime: " #define DIRECTORY_MTIME "mtime: "
@ -63,6 +70,8 @@ Directory * mp3rootDirectory = NULL;
char directorydb[MAXPATHLEN+1]; char directorydb[MAXPATHLEN+1];
int directory_updatePid = 0;
DirectoryList * newDirectoryList(); DirectoryList * newDirectoryList();
int addToDirectory(Directory * directory, char * shortname, char * name); int addToDirectory(Directory * directory, char * shortname, char * name);
@ -79,6 +88,61 @@ void deleteEmptyDirectoriesInDirectory(Directory * directory);
int addSubDirectoryToDirectory(Directory * directory, char * shortname, char * name); int addSubDirectoryToDirectory(Directory * directory, char * shortname, char * name);
void directory_sigChldHandler(int pid, int status) {
if(directory_updatePid==pid) {
if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM) {
ERROR("update process died from a "
"non-TERM signal: %i\n",
WTERMSIG(status));
}
else if(WEXITSTATUS(status)==EXIT_SUCCESS) {
readDirectoryDB();
incrPlaylistVersion();
DEBUG("direcotry_sigChldHandler: "
"updated db succesffully\n");
}
directory_updatePid = 0;
}
}
int updateInit(FILE * fp) {
if(directory_updatePid > 0) {
myfprintf(fp,"%s already updating\n",COMMAND_RESPOND_ERROR);
return -1;
}
directory_updatePid = fork();
if(directory_updatePid==0) {
/* child */
struct sigaction sa;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE,&sa,NULL);
sigaction(SIGCHLD,&sa,NULL);
close(listenSocket);
freeAllInterfaces();
finishPlaylist();
finishVolume();
if(updateMp3Directory(stderr)) exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}
else if(directory_updatePid < 0) {
ERROR("updateInit: Problems forking()'ing\n");
myfprintf(fp,"%s problems trying to update\n",
COMMAND_RESPOND_ERROR);
directory_updatePid = 0;
return -1;
}
DEBUG("updateInit: fork()'d update child\n");
return 0;
}
Directory * newDirectory(Directory * parentDirectory, char * dirname, time_t mtime) { Directory * newDirectory(Directory * parentDirectory, char * dirname, time_t mtime) {
Directory * directory; Directory * directory;

View File

@ -28,6 +28,10 @@
extern char directorydb[MAXPATHLEN+1]; extern char directorydb[MAXPATHLEN+1];
void directory_sigChldHandler(int pid, int status);
int updateInit(FILE * fp);
void initMp3Directory(); void initMp3Directory();
void closeMp3Directory(); void closeMp3Directory();

View File

@ -66,29 +66,24 @@ void resetPlayer() {
getPlayerData()->playerControl.decode_pid = 0; getPlayerData()->playerControl.decode_pid = 0;
} }
void player_sigHandler(int signal) { void player_sigChldHandler(int pid, int status) {
if(signal==SIGCHLD) { if(player_pid==pid) {
int status; if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM) {
int pid = wait3(&status,WNOHANG,NULL); ERROR("player process died from a "
if(player_pid==pid) { "non-TERM signal: %i\n",
if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM) { WTERMSIG(status));
ERROR("player process died from a "
"non-TERM signal: %i\n",
WTERMSIG(status));
}
resetPlayer();
} }
else if(pid==getPlayerData()->playerControl.decode_pid && resetPlayer();
player_pid<=0) }
{ else if(pid==getPlayerData()->playerControl.decode_pid && player_pid<=0)
if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM) { {
ERROR("(caught by master parent) " if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM) {
"decode process died from a " ERROR("(caught by master parent) "
"non-TERM signal: %i\n", "decode process died from a "
WTERMSIG(status)); "non-TERM signal: %i\n",
} WTERMSIG(status));
getPlayerData()->playerControl.decode_pid = 0;
} }
getPlayerData()->playerControl.decode_pid = 0;
} }
} }

View File

@ -79,7 +79,7 @@ typedef struct _PlayerControl {
int decode_pid; int decode_pid;
} PlayerControl; } PlayerControl;
void player_sigHandler(int signal); void player_sigChldHandler(int pid, int status);
int playerPlay(FILE * fp, char * utf8file); int playerPlay(FILE * fp, char * utf8file);

View File

@ -144,7 +144,9 @@ void finishPlaylist() {
stopPlaylist(stderr); stopPlaylist(stderr);
clearPlaylist(stderr); clearPlaylist(stderr);
free(playlist.songs); free(playlist.songs);
playlist.songs = NULL;
free(playlist.order); free(playlist.order);
playlist.order = NULL;
} }
int clearPlaylist(FILE * fp) { int clearPlaylist(FILE * fp) {
@ -626,6 +628,8 @@ int deleteFromPlaylist(FILE * fp, int song) {
void deleteASongFromPlaylist(Song * song) { void deleteASongFromPlaylist(Song * song) {
int i; int i;
if(NULL==playlist.songs) return;
for(i=0;i<playlist.length;i++) { for(i=0;i<playlist.length;i++) {
if(song==playlist.songs[i]) { if(song==playlist.songs[i]) {

View File

@ -22,6 +22,10 @@
#include "directory.h" #include "directory.h"
#include <signal.h> #include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
struct sigaction original_termSa; struct sigaction original_termSa;
struct sigaction original_hupSa; struct sigaction original_hupSa;
@ -41,6 +45,15 @@ void hupSigHandler(int signal) {
readDirectoryDB(); readDirectoryDB();
} }
void chldSigHandler(int signal) {
int status;
int pid = wait3(&status,WNOHANG,NULL);
if(pid>0) {
player_sigChldHandler(pid,status);
directory_sigChldHandler(pid,status);
}
}
void initSigHandlers() { void initSigHandlers() {
struct sigaction sa; struct sigaction sa;
@ -50,12 +63,14 @@ void initSigHandlers() {
sigaction(SIGPIPE,&sa,NULL); sigaction(SIGPIPE,&sa,NULL);
sa.sa_handler = usr1SigHandler; sa.sa_handler = usr1SigHandler;
sigaction(SIGUSR1,&sa,NULL); sigaction(SIGUSR1,&sa,NULL);
sa.sa_handler = player_sigHandler; sigaddset(&sa.sa_mask,SIGTERM);
sigaddset(&sa.sa_mask,SIGHUP);
sigaddset(&sa.sa_mask,SIGCHLD);
sa.sa_handler = chldSigHandler;
sigaction(SIGCHLD,&sa,NULL); sigaction(SIGCHLD,&sa,NULL);
sa.sa_handler = hupSigHandler; sa.sa_handler = hupSigHandler;
sigaction(SIGHUP,&sa,&original_hupSa); sigaction(SIGHUP,&sa,&original_hupSa);
sa.sa_handler = termSigHandler; sa.sa_handler = termSigHandler;
/*sigaddset(&sa.sa_mask,SIGTERM);*/
sigaction(SIGTERM,&sa,&original_termSa); sigaction(SIGTERM,&sa,&original_termSa);
} }
@ -70,6 +85,7 @@ void blockSignals() {
sigemptyset(&sset); sigemptyset(&sset);
sigaddset(&sset,SIGCHLD); sigaddset(&sset,SIGCHLD);
sigaddset(&sset,SIGUSR1); sigaddset(&sset,SIGUSR1);
sigaddset(&sset,SIGHUP);
sigprocmask(SIG_BLOCK,&sset,NULL); sigprocmask(SIG_BLOCK,&sset,NULL);
} }
@ -79,6 +95,7 @@ void unblockSignals() {
sigemptyset(&sset); sigemptyset(&sset);
sigaddset(&sset,SIGCHLD); sigaddset(&sset,SIGCHLD);
sigaddset(&sset,SIGUSR1); sigaddset(&sset,SIGUSR1);
sigaddset(&sset,SIGHUP);
sigprocmask(SIG_UNBLOCK,&sset,NULL); sigprocmask(SIG_UNBLOCK,&sset,NULL);
} }
@ -87,6 +104,7 @@ void blockTermSignal() {
sigemptyset(&sset); sigemptyset(&sset);
sigaddset(&sset,SIGTERM); sigaddset(&sset,SIGTERM);
sigaddset(&sset,SIGHUP);
sigprocmask(SIG_BLOCK,&sset,NULL); sigprocmask(SIG_BLOCK,&sset,NULL);
} }
@ -95,5 +113,6 @@ void unblockTermSignal() {
sigemptyset(&sset); sigemptyset(&sset);
sigaddset(&sset,SIGTERM); sigaddset(&sset,SIGTERM);
sigaddset(&sset,SIGHUP);
sigprocmask(SIG_UNBLOCK,&sset,NULL); sigprocmask(SIG_UNBLOCK,&sset,NULL);
} }