DJWLindenaar's fix race condition and some memory leaks patch

git-svn-id: https://svn.musicpd.org/mpd/trunk@3681 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Qball Cow 2005-11-18 12:09:05 +00:00
parent 32e5f4ca2b
commit 4b00c62587
7 changed files with 44 additions and 8 deletions

View File

@ -30,6 +30,7 @@
#define COMMAND_RETURN_KILL 10
#define COMMAND_RETURN_CLOSE 20
#define COMMAND_MASTER_READY 30
extern char * current_command;
extern int command_listNum;

View File

@ -112,6 +112,10 @@ void registerConfigParam(char * name, int repeatable, int block) {
insertInList(configEntriesList, name, entry);
}
void finishConf() {
freeList(configEntriesList);
}
void initConf() {
configEntriesList = makeList((ListFreeDataFunc *)freeConfigEntry, 1);
@ -295,6 +299,7 @@ void readConf(char * file) {
freeArgArray(array, numberOfArgs);
}
fclose(fp);
}
ConfigParam * getNextConfigParam(char * name, ConfigParam * last) {

View File

@ -73,6 +73,7 @@ typedef struct _ConfigParam {
} ConfigParam;
void initConf();
void finishConf();
void readConf(char * file);

View File

@ -42,6 +42,7 @@
#include <stdio.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
@ -306,6 +307,7 @@ void startMainProcess() {
mainPid = pid;
masterInitSigHandlers();
kill(mainPid, SIGUSR1);
while (masterHandlePendingSignals()!=COMMAND_RETURN_KILL)
waitOnSignals();
/* we're killed */
@ -320,6 +322,9 @@ void startMainProcess() {
finishPaths();
kill(mainPid, SIGTERM);
waitpid(mainPid,NULL,0);
finishConf();
myfprintfCloseLogFile();
exit(EXIT_SUCCESS);
} else if(pid<0) {
@ -483,8 +488,8 @@ void killFromPidFile(char * cmd, int killOption) {
}
int main(int argc, char * argv[]) {
FILE * out;
FILE * err;
FILE * out = NULL;
FILE * err = NULL;
Options options;
closeAllFDs();
@ -519,8 +524,7 @@ int main(int argc, char * argv[]) {
/* This is the main process which has
* been forked from the master process.
*/
initSigHandlers();
initPermissions();
@ -539,8 +543,12 @@ int main(int argc, char * argv[]) {
setupLogOutput(&options, out, err);
openVolumeDevice();
initSigHandlers();
/* wait for the master process to get ready so we can start
* playing if readPlaylistState thinks we should*/
while (COMMAND_MASTER_READY != handlePendingSignals())
my_usleep(1);
openVolumeDevice();
readPlaylistState();
@ -567,6 +575,8 @@ int main(int argc, char * argv[]) {
finishCommands();
finishInputPlugins();
cleanUpPidFile();
finishConf();
myfprintfCloseLogFile();
return EXIT_SUCCESS;
}

View File

@ -122,3 +122,10 @@ int myfprintfCloseAndOpenLogFile() {
return 0;
}
void myfprintfCloseLogFile() {
if(myfprintf_stdLogMode) {
while(fclose(myfprintf_out)<0 && errno==EINTR);
while(fclose(myfprintf_err)<0 && errno==EINTR);
}
}

View File

@ -29,4 +29,5 @@ void myfprintf(FILE * fp, char * format, ... );
int myfprintfCloseAndOpenLogFile();
void myfprintfCloseLogFile();
#endif

View File

@ -56,7 +56,17 @@ int masterHandlePendingSignals() {
}
int handlePendingSignals() {
if(signal_is_pending(SIGINT) || signal_is_pending(SIGTERM)) {
/* this SIGUSR1 signal comes before the KILL signals, because there if the process is
* looping, waiting for this signal, it will not respond to the KILL signal. This might be
* better implemented by using bit-wise defines and or'ing of the COMMAND_FOO as return.
*/
if (signal_is_pending(SIGUSR1)) {
signal_clear(SIGUSR1);
DEBUG("The master process is ready to receive signals\n");
return COMMAND_MASTER_READY;
}
if(signal_is_pending(SIGINT) || signal_is_pending(SIGTERM)) {
DEBUG("main process got SIGINT or SIGTERM, exiting\n");
return COMMAND_RETURN_KILL;
}
@ -135,6 +145,7 @@ void initSigHandlers() {
while(sigaction(SIGPIPE,&sa,NULL)<0 && errno==EINTR);
sa.sa_handler = chldSigHandler;
while(sigaction(SIGCHLD,&sa,NULL)<0 && errno==EINTR);
signal_handle(SIGUSR2);
signal_handle(SIGUSR1);
signal_handle(SIGINT);
signal_handle(SIGTERM);