*) add support for pid files

*) we now require pid_file to specified in the config
*) new prefered method for killing mpd: mpd --kill
*) cleaned up some nastiness with decode_pid handling

git-svn-id: https://svn.musicpd.org/mpd/trunk@3042 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Warren Dukes 2005-03-09 03:13:33 +00:00
parent a849136820
commit 4c7c6ca4a6
5 changed files with 127 additions and 39 deletions

View File

@ -8,6 +8,7 @@ playlist_directory "~/music"
db_file "~/.mpd/mpd.db" db_file "~/.mpd/mpd.db"
log_file "~/.mpd/mpd.log" log_file "~/.mpd/mpd.log"
error_file "~/.mpd/mpd.error" error_file "~/.mpd/mpd.error"
pid_file "~/.mpd/mpd.pid"
########################################################## ##########################################################
########################################################## ##########################################################

View File

@ -116,6 +116,7 @@ void initConf() {
configEntriesList = makeList((ListFreeDataFunc *)freeConfigEntry, 1); configEntriesList = makeList((ListFreeDataFunc *)freeConfigEntry, 1);
registerConfigParam(CONF_PORT, 0, 0); registerConfigParam(CONF_PORT, 0, 0);
registerConfigParam(CONF_PID_FILE, 0, 0);
registerConfigParam(CONF_MUSIC_DIR, 0, 0); registerConfigParam(CONF_MUSIC_DIR, 0, 0);
registerConfigParam(CONF_PLAYLIST_DIR, 0, 0); registerConfigParam(CONF_PLAYLIST_DIR, 0, 0);
registerConfigParam(CONF_LOG_FILE, 0, 0); registerConfigParam(CONF_LOG_FILE, 0, 0);

View File

@ -22,6 +22,7 @@
#include "../config.h" #include "../config.h"
#define CONF_PORT "port" #define CONF_PORT "port"
#define CONF_PID_FILE "pid_file"
#define CONF_MUSIC_DIR "music_directory" #define CONF_MUSIC_DIR "music_directory"
#define CONF_PLAYLIST_DIR "playlist_directory" #define CONF_PLAYLIST_DIR "playlist_directory"
#define CONF_LOG_FILE "log_file" #define CONF_LOG_FILE "log_file"

View File

@ -37,40 +37,46 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
volatile int * volatile decode_pid = NULL; static int decode_pid = 0;
void decodeSigHandler(int sig, siginfo_t * si, void * v) { void decodeSigHandler(int sig, siginfo_t * si, void * v) {
if(sig==SIGCHLD) { if(sig==SIGCHLD) {
int status; int status;
if(decode_pid && *decode_pid==wait3(&status,WNOHANG,NULL)) { if(decode_pid==wait3(&status,WNOHANG,NULL)) {
if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM && if(WIFSIGNALED(status)) {
WTERMSIG(status)!=SIGINT) if(WTERMSIG(status)!=SIGTERM &&
{ WTERMSIG(status)!=SIGINT)
ERROR("decode process died from signal: %i\n", {
WTERMSIG(status)); ERROR("decode process died from "
"signal: %i\n",
WTERMSIG(status));
}
} }
*decode_pid = 0; decode_pid = 0;
getPlayerData()->playerControl.decode_pid = 0;
} }
} }
/* this is causing problems for alsa and other things that generate
* extra processes. it causes them to not die */
/*else if(sig==SIGTERM && si->si_pid==getppid()) {
DEBUG("player/decoder got SIGTERM from parent process\n");*/
else if(sig==SIGTERM) { else if(sig==SIGTERM) {
if(decode_pid) { int pid = decode_pid;
int pid = *decode_pid; if(pid > 0) {
if(pid>0) kill(pid,SIGTERM); DEBUG("player (or child) got SIGTERM\n");
kill(pid,SIGTERM);
} }
else DEBUG("decoder (or child) got SIGTERM\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
else if(sig==SIGINT) {
if(decode_pid > 0) {
DEBUG("player (or child) got SIGINT\n");
}
else DEBUG("decoder (or child) got SIGINT\n");
}
} }
void stopDecode(DecoderControl * dc) { void stopDecode(DecoderControl * dc) {
if(decode_pid && *decode_pid>0 && if(decode_pid>0 && (dc->start || dc->state!=DECODE_STATE_STOP)) {
(dc->start || dc->state!=DECODE_STATE_STOP))
{
dc->stop = 1; dc->stop = 1;
while(decode_pid && *decode_pid>0 && dc->stop) my_usleep(10000); while(decode_pid>0 && dc->stop) my_usleep(10000);
} }
} }
@ -110,7 +116,7 @@ int calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af) {
#define handleDecodeStart() \ #define handleDecodeStart() \
if(decodeWaitedOn) { \ if(decodeWaitedOn) { \
if(dc->state!=DECODE_STATE_START && *decode_pid > 0 && \ if(dc->state!=DECODE_STATE_START && decode_pid > 0 && \
dc->error==DECODE_ERROR_NOERROR) \ dc->error==DECODE_ERROR_NOERROR) \
{ \ { \
decodeWaitedOn = 0; \ decodeWaitedOn = 0; \
@ -131,7 +137,7 @@ int calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af) {
cb->audioFormat.channels/ \ cb->audioFormat.channels/ \
cb->audioFormat.sampleRate; \ cb->audioFormat.sampleRate; \
} \ } \
else if(dc->state!=DECODE_STATE_START || *decode_pid <= 0) { \ else if(dc->state!=DECODE_STATE_START || decode_pid <= 0) { \
strncpy(pc->erroredUrl, pc->utf8url, MAXPATHLEN); \ strncpy(pc->erroredUrl, pc->utf8url, MAXPATHLEN); \
pc->erroredUrl[MAXPATHLEN] = '\0'; \ pc->erroredUrl[MAXPATHLEN] = '\0'; \
pc->error = PLAYER_ERROR_FILE; \ pc->error = PLAYER_ERROR_FILE; \
@ -151,7 +157,7 @@ int waitOnDecode(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
pc->currentUrl[MAXPATHLEN] = '\0'; pc->currentUrl[MAXPATHLEN] = '\0';
MpdTag * tag = NULL; MpdTag * tag = NULL;
while(decode_pid && *decode_pid>0 && dc->start) my_usleep(10000); while(decode_pid>0 && dc->start) my_usleep(10000);
if(dc->start || dc->error!=DECODE_ERROR_NOERROR) { if(dc->start || dc->error!=DECODE_ERROR_NOERROR) {
strncpy(pc->erroredUrl, pc->utf8url, MAXPATHLEN); strncpy(pc->erroredUrl, pc->utf8url, MAXPATHLEN);
@ -181,7 +187,7 @@ int decodeSeek(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
{ {
int ret = -1; int ret = -1;
if(decode_pid && *decode_pid>0) { if(decode_pid>0) {
if(dc->state==DECODE_STATE_STOP || dc->error || if(dc->state==DECODE_STATE_STOP || dc->error ||
strcmp(dc->utf8url, pc->utf8url)!=0) strcmp(dc->utf8url, pc->utf8url)!=0)
{ {
@ -193,7 +199,7 @@ int decodeSeek(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
dc->start = 1; dc->start = 1;
waitOnDecode(pc,dc,cb,decodeWaitedOn); waitOnDecode(pc,dc,cb,decodeWaitedOn);
} }
if(*decode_pid>0 && dc->state!=DECODE_STATE_STOP && if(decode_pid>0 && dc->state!=DECODE_STATE_STOP &&
dc->seekable) dc->seekable)
{ {
*next = -1; *next = -1;
@ -203,7 +209,7 @@ int decodeSeek(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
dc->seekWhere = 0 > dc->seekWhere ? 0 : dc->seekWhere; dc->seekWhere = 0 > dc->seekWhere ? 0 : dc->seekWhere;
dc->seekError = 0; dc->seekError = 0;
dc->seek = 1; dc->seek = 1;
while(*decode_pid>0 && dc->seek) my_usleep(10000); while(decode_pid>0 && dc->seek) my_usleep(10000);
if(!dc->seekError) { if(!dc->seekError) {
pc->elapsedTime = dc->seekWhere; pc->elapsedTime = dc->seekWhere;
ret = 0; ret = 0;
@ -369,14 +375,11 @@ void decodeStart(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
} }
int decoderInit(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) { int decoderInit(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
int pid;
decode_pid = &(pc->decode_pid);
blockSignals(); blockSignals();
pid = fork(); getPlayerData()->playerControl.decode_pid = 0;
decode_pid = fork();
if(pid==0) { if(decode_pid==0) {
/* CHILD */ /* CHILD */
unblockSignals(); unblockSignals();
@ -396,7 +399,7 @@ int decoderInit(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
/* END OF CHILD */ /* END OF CHILD */
} }
else if(pid<0) { else if(decode_pid<0) {
unblockSignals(); unblockSignals();
strncpy(pc->erroredUrl, pc->utf8url, MAXPATHLEN); strncpy(pc->erroredUrl, pc->utf8url, MAXPATHLEN);
pc->erroredUrl[MAXPATHLEN] = '\0'; pc->erroredUrl[MAXPATHLEN] = '\0';
@ -404,7 +407,7 @@ int decoderInit(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
return -1; return -1;
} }
*decode_pid = pid; getPlayerData()->playerControl.decode_pid = decode_pid;
unblockSignals(); unblockSignals();
return 0; return 0;
@ -488,7 +491,7 @@ void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb) {
pc->play = 0; pc->play = 0;
kill(getppid(),SIGUSR1); kill(getppid(),SIGUSR1);
while(*decode_pid>0 && cb->end-cb->begin<bbp && while(decode_pid>0 && cb->end-cb->begin<bbp &&
cb->end!=buffered_chunks-1 && cb->end!=buffered_chunks-1 &&
dc->state!=DECODE_STATE_STOP) dc->state!=DECODE_STATE_STOP)
{ {
@ -642,7 +645,7 @@ void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb) {
kill(getppid(),SIGUSR1); kill(getppid(),SIGUSR1);
} }
} }
else if(*decode_pid<=0 || else if(decode_pid<=0 ||
(dc->state==DECODE_STATE_STOP && !dc->start)) (dc->state==DECODE_STATE_STOP && !dc->start))
{ {
quit = 1; quit = 1;
@ -678,7 +681,7 @@ void decode() {
dc->stop = 0; dc->stop = 0;
dc->start = 1; dc->start = 1;
if(decode_pid==NULL || *decode_pid<=0) { if(decode_pid<=0) {
if(decoderInit(pc,cb,dc)<0) return; if(decoderInit(pc,cb,dc)<0) return;
} }

View File

@ -55,6 +55,7 @@
#define USER_CONFIG_FILE_LOCATION "/.mpdconf" #define USER_CONFIG_FILE_LOCATION "/.mpdconf"
typedef struct _Options { typedef struct _Options {
int kill;
int daemon; int daemon;
int stdOutput; int stdOutput;
int createDB; int createDB;
@ -70,11 +71,12 @@ void usage(char * argv[]) {
ERROR("\n"); ERROR("\n");
ERROR("options:\n"); ERROR("options:\n");
ERROR(" --help this usage statement\n"); ERROR(" --help this usage statement\n");
ERROR(" --create-db force (re)creation database and exit\n");
ERROR(" --kill kill mpd\n");
ERROR(" --no-create-db don't create database\n");
ERROR(" --no-daemon don't detach from console\n"); ERROR(" --no-daemon don't detach from console\n");
ERROR(" --stdout print msgs to stdout and stderr\n"); ERROR(" --stdout print msgs to stdout and stderr\n");
ERROR(" --create-db force (re)creation database and exit\n");
/*ERROR(" --update-db create database and exit\n");*/ /*ERROR(" --update-db create database and exit\n");*/
ERROR(" --no-create-db don't create database\n");
ERROR(" --verbose verbose logging\n"); ERROR(" --verbose verbose logging\n");
ERROR(" --version prints version information\n"); ERROR(" --version prints version information\n");
} }
@ -99,6 +101,7 @@ void parseOptions(int argc, char ** argv, Options * options) {
options->stdOutput = 0; options->stdOutput = 0;
options->createDB = 0; options->createDB = 0;
options->updateDB = 0; options->updateDB = 0;
options->kill = 0;
if(argc>1) { if(argc>1) {
int i = 1; int i = 1;
@ -108,6 +111,10 @@ void parseOptions(int argc, char ** argv, Options * options) {
usage(argv); usage(argv);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
else if(strcmp(argv[i],"--kill")==0) {
options->kill = 1;
argcLeft--;
}
else if(strcmp(argv[i],"--no-daemon")==0) { else if(strcmp(argv[i],"--no-daemon")==0) {
options->daemon = 0; options->daemon = 0;
argcLeft--; argcLeft--;
@ -282,6 +289,20 @@ void openDB(Options * options, char * argv0) {
} }
void daemonize(Options * options) { void daemonize(Options * options) {
FILE * fp;
ConfigParam * pidFileParam = parseConfigFilePath(CONF_PID_FILE, 1);
/* do this before daemon'izing so we can fail gracefully if we can't
* write to the pid file */
DEBUG("opening pid file\n");
fp = fopen(pidFileParam->value, "w+");
if(!fp) {
ERROR("could not open %s \"%s\" (at line %i) for writing: %s\n",
CONF_PID_FILE, pidFileParam->value,
pidFileParam->line, strerror(errno));
exit(EXIT_FAILURE);
}
if(options->daemon) { if(options->daemon) {
int pid; int pid;
@ -309,8 +330,14 @@ void daemonize(Options * options) {
else if(pid<0) { else if(pid<0) {
ERROR("problems fork'ing for daemon!\n"); ERROR("problems fork'ing for daemon!\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
DEBUG("daemonized!\n");
} }
DEBUG("writing pid file\n");
fprintf(fp, "%lu\n", (unsigned long)getpid());
fclose(fp);
} }
void setupLogOutput(Options * options, FILE * out, FILE * err) { void setupLogOutput(Options * options, FILE * out, FILE * err) {
@ -349,6 +376,58 @@ void setupLogOutput(Options * options, FILE * out, FILE * err) {
} }
} }
void cleanUpPidFile() {
ConfigParam * pidFileParam = parseConfigFilePath(CONF_PID_FILE, 1);
DEBUG("cleaning up pid file\n");
unlink(pidFileParam->value);
}
void killMpdFromPidFile(char * cmd) {
struct stat st_cmd;
struct stat st_exe;
ConfigParam * pidFileParam = parseConfigFilePath(CONF_PID_FILE, 1);
int pid;
char buf[32];
FILE * fp = fopen(pidFileParam->value,"r");
if(!fp) {
ERROR("unable to open %s \"%s\" (config line %i): %s\n",
CONF_PID_FILE, pidFileParam->value,
pidFileParam->line);
exit(EXIT_FAILURE);
}
if(fscanf(fp, "%i", &pid) != 1) {
ERROR("unable to read the pid from file \"%s\"\n",
pidFileParam->value);
exit(EXIT_FAILURE);
}
fclose(fp);
memset(buf, 0, 32);
snprintf(buf, 31, "/proc/%i/exe", pid);
if(stat(cmd, &st_cmd)) {
ERROR("unable to stat file \"%s\"\n", cmd);
exit(EXIT_FAILURE);
}
if(stat(buf, &st_exe)) {
ERROR("unable to kill proccess %i (%s: %s)\n", pid, buf,
strerror(errno));
exit(EXIT_FAILURE);
}
if(st_exe.st_dev != st_cmd.st_dev || st_exe.st_ino != st_cmd.st_ino) {
ERROR("%s doesn't appear to be running as pid %i\n",
cmd, pid);
exit(EXIT_FAILURE);
}
kill(pid, SIGTERM);
exit(EXIT_SUCCESS);
}
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
FILE * out; FILE * out;
FILE * err; FILE * err;
@ -360,6 +439,8 @@ int main(int argc, char * argv[]) {
parseOptions(argc, argv, &options); parseOptions(argc, argv, &options);
if(options.kill) killMpdFromPidFile(argv[0]);
initStats(); initStats();
initTagConfig(); initTagConfig();
initLog(); initLog();
@ -421,6 +502,7 @@ int main(int argc, char * argv[]) {
finishPermissions(); finishPermissions();
finishCommands(); finishCommands();
finishInputPlugins(); finishInputPlugins();
cleanUpPidFile();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }