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,
char ** argArray)
{
incrPlaylistVersion();
return updateMp3Directory(fp);
return updateInit(fp);
}
int handleNext(FILE * fp, unsigned int * permission, int argArrayLength,

View File

@ -26,14 +26,21 @@
#include "conf.h"
#include "stats.h"
#include "playlist.h"
#include "listen.h"
#include "interface.h"
#include "volume.h"
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#define DIRECTORY_DIR "directory: "
#define DIRECTORY_MTIME "mtime: "
@ -63,6 +70,8 @@ Directory * mp3rootDirectory = NULL;
char directorydb[MAXPATHLEN+1];
int directory_updatePid = 0;
DirectoryList * newDirectoryList();
int addToDirectory(Directory * directory, char * shortname, char * name);
@ -79,6 +88,61 @@ void deleteEmptyDirectoriesInDirectory(Directory * directory);
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 * directory;

View File

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

View File

@ -66,10 +66,7 @@ void resetPlayer() {
getPlayerData()->playerControl.decode_pid = 0;
}
void player_sigHandler(int signal) {
if(signal==SIGCHLD) {
int status;
int pid = wait3(&status,WNOHANG,NULL);
void player_sigChldHandler(int pid, int status) {
if(player_pid==pid) {
if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM) {
ERROR("player process died from a "
@ -78,8 +75,7 @@ void player_sigHandler(int signal) {
}
resetPlayer();
}
else if(pid==getPlayerData()->playerControl.decode_pid &&
player_pid<=0)
else if(pid==getPlayerData()->playerControl.decode_pid && player_pid<=0)
{
if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM) {
ERROR("(caught by master parent) "
@ -89,7 +85,6 @@ void player_sigHandler(int signal) {
}
getPlayerData()->playerControl.decode_pid = 0;
}
}
}
int playerInit() {

View File

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

View File

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

View File

@ -22,6 +22,10 @@
#include "directory.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_hupSa;
@ -41,6 +45,15 @@ void hupSigHandler(int signal) {
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() {
struct sigaction sa;
@ -50,12 +63,14 @@ void initSigHandlers() {
sigaction(SIGPIPE,&sa,NULL);
sa.sa_handler = usr1SigHandler;
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);
sa.sa_handler = hupSigHandler;
sigaction(SIGHUP,&sa,&original_hupSa);
sa.sa_handler = termSigHandler;
/*sigaddset(&sa.sa_mask,SIGTERM);*/
sigaction(SIGTERM,&sa,&original_termSa);
}
@ -70,6 +85,7 @@ void blockSignals() {
sigemptyset(&sset);
sigaddset(&sset,SIGCHLD);
sigaddset(&sset,SIGUSR1);
sigaddset(&sset,SIGHUP);
sigprocmask(SIG_BLOCK,&sset,NULL);
}
@ -79,6 +95,7 @@ void unblockSignals() {
sigemptyset(&sset);
sigaddset(&sset,SIGCHLD);
sigaddset(&sset,SIGUSR1);
sigaddset(&sset,SIGHUP);
sigprocmask(SIG_UNBLOCK,&sset,NULL);
}
@ -87,6 +104,7 @@ void blockTermSignal() {
sigemptyset(&sset);
sigaddset(&sset,SIGTERM);
sigaddset(&sset,SIGHUP);
sigprocmask(SIG_BLOCK,&sset,NULL);
}
@ -95,5 +113,6 @@ void unblockTermSignal() {
sigemptyset(&sset);
sigaddset(&sset,SIGTERM);
sigaddset(&sset,SIGHUP);
sigprocmask(SIG_UNBLOCK,&sset,NULL);
}