aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWarren Dukes <warren.dukes@gmail.com>2005-03-09 03:13:33 +0000
committerWarren Dukes <warren.dukes@gmail.com>2005-03-09 03:13:33 +0000
commit4c7c6ca4a659519494af941af5adb90f8b01317a (patch)
tree0301a5a80f34fab059c15d4cca39938d8dca2833
parenta849136820badac142c299abfc8cfe65fbd64896 (diff)
downloadmpd-4c7c6ca4a659519494af941af5adb90f8b01317a.tar.gz
mpd-4c7c6ca4a659519494af941af5adb90f8b01317a.tar.xz
mpd-4c7c6ca4a659519494af941af5adb90f8b01317a.zip
*) 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
Diffstat (limited to '')
-rw-r--r--doc/mpdconf.example1
-rw-r--r--src/conf.c1
-rw-r--r--src/conf.h1
-rw-r--r--src/decode.c75
-rw-r--r--src/main.c88
5 files changed, 127 insertions, 39 deletions
diff --git a/doc/mpdconf.example b/doc/mpdconf.example
index f4f64a76e..4d4b19827 100644
--- a/doc/mpdconf.example
+++ b/doc/mpdconf.example
@@ -8,6 +8,7 @@ playlist_directory "~/music"
db_file "~/.mpd/mpd.db"
log_file "~/.mpd/mpd.log"
error_file "~/.mpd/mpd.error"
+pid_file "~/.mpd/mpd.pid"
##########################################################
##########################################################
diff --git a/src/conf.c b/src/conf.c
index 2d5bcc5a4..18d6c16fb 100644
--- a/src/conf.c
+++ b/src/conf.c
@@ -116,6 +116,7 @@ void initConf() {
configEntriesList = makeList((ListFreeDataFunc *)freeConfigEntry, 1);
registerConfigParam(CONF_PORT, 0, 0);
+ registerConfigParam(CONF_PID_FILE, 0, 0);
registerConfigParam(CONF_MUSIC_DIR, 0, 0);
registerConfigParam(CONF_PLAYLIST_DIR, 0, 0);
registerConfigParam(CONF_LOG_FILE, 0, 0);
diff --git a/src/conf.h b/src/conf.h
index ca9eb058e..36d7b429e 100644
--- a/src/conf.h
+++ b/src/conf.h
@@ -22,6 +22,7 @@
#include "../config.h"
#define CONF_PORT "port"
+#define CONF_PID_FILE "pid_file"
#define CONF_MUSIC_DIR "music_directory"
#define CONF_PLAYLIST_DIR "playlist_directory"
#define CONF_LOG_FILE "log_file"
diff --git a/src/decode.c b/src/decode.c
index b0e08dc34..83d0936c9 100644
--- a/src/decode.c
+++ b/src/decode.c
@@ -37,40 +37,46 @@
#include <unistd.h>
#include <string.h>
-volatile int * volatile decode_pid = NULL;
+static int decode_pid = 0;
void decodeSigHandler(int sig, siginfo_t * si, void * v) {
if(sig==SIGCHLD) {
int status;
- if(decode_pid && *decode_pid==wait3(&status,WNOHANG,NULL)) {
- if(WIFSIGNALED(status) && WTERMSIG(status)!=SIGTERM &&
- WTERMSIG(status)!=SIGINT)
- {
- ERROR("decode process died from signal: %i\n",
- WTERMSIG(status));
+ if(decode_pid==wait3(&status,WNOHANG,NULL)) {
+ if(WIFSIGNALED(status)) {
+ if(WTERMSIG(status)!=SIGTERM &&
+ WTERMSIG(status)!=SIGINT)
+ {
+ 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) {
- if(decode_pid) {
- int pid = *decode_pid;
- if(pid>0) kill(pid,SIGTERM);
+ int pid = decode_pid;
+ if(pid > 0) {
+ DEBUG("player (or child) got SIGTERM\n");
+ kill(pid,SIGTERM);
}
+ else DEBUG("decoder (or child) got SIGTERM\n");
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) {
- if(decode_pid && *decode_pid>0 &&
- (dc->start || dc->state!=DECODE_STATE_STOP))
- {
+ if(decode_pid>0 && (dc->start || dc->state!=DECODE_STATE_STOP)) {
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() \
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) \
{ \
decodeWaitedOn = 0; \
@@ -131,7 +137,7 @@ int calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af) {
cb->audioFormat.channels/ \
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); \
pc->erroredUrl[MAXPATHLEN] = '\0'; \
pc->error = PLAYER_ERROR_FILE; \
@@ -151,7 +157,7 @@ int waitOnDecode(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
pc->currentUrl[MAXPATHLEN] = '\0';
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) {
strncpy(pc->erroredUrl, pc->utf8url, MAXPATHLEN);
@@ -181,7 +187,7 @@ int decodeSeek(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
{
int ret = -1;
- if(decode_pid && *decode_pid>0) {
+ if(decode_pid>0) {
if(dc->state==DECODE_STATE_STOP || dc->error ||
strcmp(dc->utf8url, pc->utf8url)!=0)
{
@@ -193,7 +199,7 @@ int decodeSeek(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
dc->start = 1;
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)
{
*next = -1;
@@ -203,7 +209,7 @@ int decodeSeek(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb,
dc->seekWhere = 0 > dc->seekWhere ? 0 : dc->seekWhere;
dc->seekError = 0;
dc->seek = 1;
- while(*decode_pid>0 && dc->seek) my_usleep(10000);
+ while(decode_pid>0 && dc->seek) my_usleep(10000);
if(!dc->seekError) {
pc->elapsedTime = dc->seekWhere;
ret = 0;
@@ -369,14 +375,11 @@ void decodeStart(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
}
int decoderInit(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
-
- int pid;
- decode_pid = &(pc->decode_pid);
-
blockSignals();
- pid = fork();
+ getPlayerData()->playerControl.decode_pid = 0;
+ decode_pid = fork();
- if(pid==0) {
+ if(decode_pid==0) {
/* CHILD */
unblockSignals();
@@ -396,7 +399,7 @@ int decoderInit(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
exit(EXIT_SUCCESS);
/* END OF CHILD */
}
- else if(pid<0) {
+ else if(decode_pid<0) {
unblockSignals();
strncpy(pc->erroredUrl, pc->utf8url, MAXPATHLEN);
pc->erroredUrl[MAXPATHLEN] = '\0';
@@ -404,7 +407,7 @@ int decoderInit(PlayerControl * pc, OutputBuffer * cb, DecoderControl * dc) {
return -1;
}
- *decode_pid = pid;
+ getPlayerData()->playerControl.decode_pid = decode_pid;
unblockSignals();
return 0;
@@ -488,7 +491,7 @@ void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb) {
pc->play = 0;
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 &&
dc->state!=DECODE_STATE_STOP)
{
@@ -642,7 +645,7 @@ void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer * cb) {
kill(getppid(),SIGUSR1);
}
}
- else if(*decode_pid<=0 ||
+ else if(decode_pid<=0 ||
(dc->state==DECODE_STATE_STOP && !dc->start))
{
quit = 1;
@@ -678,7 +681,7 @@ void decode() {
dc->stop = 0;
dc->start = 1;
- if(decode_pid==NULL || *decode_pid<=0) {
+ if(decode_pid<=0) {
if(decoderInit(pc,cb,dc)<0) return;
}
diff --git a/src/main.c b/src/main.c
index 563bbf2ff..50726af85 100644
--- a/src/main.c
+++ b/src/main.c
@@ -55,6 +55,7 @@
#define USER_CONFIG_FILE_LOCATION "/.mpdconf"
typedef struct _Options {
+ int kill;
int daemon;
int stdOutput;
int createDB;
@@ -70,11 +71,12 @@ void usage(char * argv[]) {
ERROR("\n");
ERROR("options:\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(" --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(" --no-create-db don't create database\n");
ERROR(" --verbose verbose logging\n");
ERROR(" --version prints version information\n");
}
@@ -99,6 +101,7 @@ void parseOptions(int argc, char ** argv, Options * options) {
options->stdOutput = 0;
options->createDB = 0;
options->updateDB = 0;
+ options->kill = 0;
if(argc>1) {
int i = 1;
@@ -108,6 +111,10 @@ void parseOptions(int argc, char ** argv, Options * options) {
usage(argv);
exit(EXIT_SUCCESS);
}
+ else if(strcmp(argv[i],"--kill")==0) {
+ options->kill = 1;
+ argcLeft--;
+ }
else if(strcmp(argv[i],"--no-daemon")==0) {
options->daemon = 0;
argcLeft--;
@@ -282,6 +289,20 @@ void openDB(Options * options, char * argv0) {
}
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) {
int pid;
@@ -309,8 +330,14 @@ void daemonize(Options * options) {
else if(pid<0) {
ERROR("problems fork'ing for daemon!\n");
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) {
@@ -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[]) {
FILE * out;
FILE * err;
@@ -360,6 +439,8 @@ int main(int argc, char * argv[]) {
parseOptions(argc, argv, &options);
+ if(options.kill) killMpdFromPidFile(argv[0]);
+
initStats();
initTagConfig();
initLog();
@@ -421,6 +502,7 @@ int main(int argc, char * argv[]) {
finishPermissions();
finishCommands();
finishInputPlugins();
+ cleanUpPidFile();
return EXIT_SUCCESS;
}