2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "interface.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "playlist.h"
|
|
|
|
#include "directory.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "listen.h"
|
|
|
|
#include "conf.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "playerData.h"
|
|
|
|
#include "stats.h"
|
|
|
|
#include "sig_handlers.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "volume.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "permission.h"
|
2004-05-08 14:00:30 +02:00
|
|
|
#include "replayGain.h"
|
2004-05-31 03:21:17 +02:00
|
|
|
#include "inputPlugin.h"
|
2006-07-16 16:57:26 +02:00
|
|
|
#include "audioOutput.h"
|
2004-06-20 22:15:05 +02:00
|
|
|
#include "inputStream.h"
|
2006-07-31 01:32:47 +02:00
|
|
|
#include "state_file.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "tag.h"
|
|
|
|
#include "tagTracker.h"
|
2004-11-11 03:36:25 +01:00
|
|
|
#include "dbUtils.h"
|
2004-04-16 03:06:14 +02:00
|
|
|
#include "../config.h"
|
2005-12-31 04:23:46 +01:00
|
|
|
#include "utils.h"
|
2006-07-27 02:50:59 +02:00
|
|
|
#include "normalize.h"
|
2007-01-11 21:41:17 +01:00
|
|
|
#include "zeroconf.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#define SYSTEM_CONFIG_FILE_LOCATION "/etc/mpd.conf"
|
|
|
|
#define USER_CONFIG_FILE_LOCATION "/.mpdconf"
|
|
|
|
|
|
|
|
typedef struct _Options {
|
2005-03-09 04:13:33 +01:00
|
|
|
int kill;
|
2006-07-19 20:04:15 +02:00
|
|
|
int daemon;
|
|
|
|
int stdOutput;
|
|
|
|
int createDB;
|
2006-08-01 06:18:53 +02:00
|
|
|
int verbose;
|
2004-02-24 00:41:20 +01:00
|
|
|
} Options;
|
|
|
|
|
2008-04-12 06:14:45 +02:00
|
|
|
static Notify main_notify;
|
|
|
|
|
2006-04-23 00:18:15 +02:00
|
|
|
/*
|
|
|
|
* from git-1.3.0, needed for solaris
|
2006-04-22 23:30:48 +02:00
|
|
|
*/
|
2006-06-04 13:36:06 +02:00
|
|
|
#ifndef HAVE_SETENV
|
2006-07-15 15:16:52 +02:00
|
|
|
static int setenv(const char *name, const char *value, int replace)
|
2006-04-23 00:18:15 +02:00
|
|
|
{
|
|
|
|
int out;
|
|
|
|
size_t namelen, valuelen;
|
|
|
|
char *envstr;
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
if (!name || !value)
|
|
|
|
return -1;
|
2006-04-23 00:18:15 +02:00
|
|
|
if (!replace) {
|
|
|
|
char *oldval = NULL;
|
|
|
|
oldval = getenv(name);
|
2006-07-19 20:04:15 +02:00
|
|
|
if (oldval)
|
|
|
|
return 0;
|
2006-04-23 00:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namelen = strlen(name);
|
|
|
|
valuelen = strlen(value);
|
2006-08-26 08:25:57 +02:00
|
|
|
envstr = xmalloc((namelen + valuelen + 2));
|
2006-07-19 20:04:15 +02:00
|
|
|
if (!envstr)
|
|
|
|
return -1;
|
2006-04-23 00:18:15 +02:00
|
|
|
|
|
|
|
memcpy(envstr, name, namelen);
|
|
|
|
envstr[namelen] = '=';
|
|
|
|
memcpy(envstr + namelen + 1, value, valuelen);
|
|
|
|
envstr[namelen + valuelen + 1] = 0;
|
|
|
|
|
|
|
|
out = putenv(envstr);
|
|
|
|
/* putenv(3) makes the argument string part of the environment,
|
|
|
|
* and changing that string modifies the environment --- which
|
|
|
|
* means we do not own that storage anymore. Do not free
|
|
|
|
* envstr.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return out;
|
2006-04-22 23:30:48 +02:00
|
|
|
}
|
2006-07-20 20:53:56 +02:00
|
|
|
#endif /* HAVE_SETENV */
|
2006-07-19 20:04:15 +02:00
|
|
|
|
|
|
|
static void usage(char *argv[])
|
|
|
|
{
|
|
|
|
ERROR("usage:\n");
|
|
|
|
ERROR(" %s [options] <conf file>\n", argv[0]);
|
|
|
|
ERROR(" %s [options] (searches for ~%s then %s)\n",
|
|
|
|
argv[0], USER_CONFIG_FILE_LOCATION, SYSTEM_CONFIG_FILE_LOCATION);
|
|
|
|
ERROR("\n");
|
|
|
|
ERROR("options:\n");
|
|
|
|
ERROR(" --help this usage statement\n");
|
|
|
|
ERROR(" --kill kill the currently running mpd session\n");
|
|
|
|
ERROR
|
|
|
|
(" --create-db force (re)creation of database and exit\n");
|
|
|
|
ERROR
|
|
|
|
(" --no-create-db don't create database, even if it doesn't exist\n");
|
|
|
|
ERROR(" --no-daemon don't detach from console\n");
|
|
|
|
ERROR(" --stdout print messages to stdout and stderr\n");
|
|
|
|
ERROR(" --verbose verbose logging\n");
|
|
|
|
ERROR(" --version prints version information\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void version(void)
|
|
|
|
{
|
|
|
|
LOG("mpd (MPD: Music Player Daemon) %s\n", VERSION);
|
|
|
|
LOG("\n");
|
2007-04-05 05:22:33 +02:00
|
|
|
LOG("Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n");
|
2006-07-19 20:04:15 +02:00
|
|
|
LOG("This is free software; see the source for copying conditions. There is NO\n");
|
|
|
|
LOG("warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
|
|
|
|
LOG("\n");
|
|
|
|
LOG("Supported formats:\n");
|
|
|
|
|
|
|
|
initInputPlugins();
|
|
|
|
printAllInputPluginSuffixes(stdout);
|
|
|
|
|
|
|
|
LOG("\n");
|
|
|
|
LOG("Supported outputs:\n");
|
|
|
|
loadAudioDrivers();
|
|
|
|
printAllOutputPluginTypes(stdout);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void parseOptions(int argc, char **argv, Options * options)
|
|
|
|
{
|
|
|
|
int argcLeft = argc;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-08-03 03:57:52 +02:00
|
|
|
options->verbose = 0;
|
2006-07-19 20:04:15 +02:00
|
|
|
options->daemon = 1;
|
|
|
|
options->stdOutput = 0;
|
|
|
|
options->createDB = 0;
|
2005-03-09 04:13:33 +01:00
|
|
|
options->kill = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
if (argc > 1) {
|
|
|
|
int i = 1;
|
|
|
|
while (i < argc) {
|
|
|
|
if (strncmp(argv[i], "--", 2) == 0) {
|
|
|
|
if (strcmp(argv[i], "--help") == 0) {
|
|
|
|
usage(argv);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (strcmp(argv[i], "--kill") == 0) {
|
|
|
|
options->kill++;
|
|
|
|
argcLeft--;
|
|
|
|
} else if (strcmp(argv[i], "--no-daemon") == 0) {
|
|
|
|
options->daemon = 0;
|
|
|
|
argcLeft--;
|
|
|
|
} else if (strcmp(argv[i], "--stdout") == 0) {
|
|
|
|
options->stdOutput = 1;
|
|
|
|
argcLeft--;
|
|
|
|
} else if (strcmp(argv[i], "--create-db") == 0) {
|
|
|
|
options->stdOutput = 1;
|
|
|
|
options->createDB = 1;
|
|
|
|
argcLeft--;
|
|
|
|
} else if (strcmp(argv[i], "--no-create-db") ==
|
|
|
|
0) {
|
|
|
|
options->createDB = -1;
|
|
|
|
argcLeft--;
|
|
|
|
} else if (strcmp(argv[i], "--verbose") == 0) {
|
2006-08-01 06:18:53 +02:00
|
|
|
options->verbose = 1;
|
2006-07-19 20:04:15 +02:00
|
|
|
argcLeft--;
|
|
|
|
} else if (strcmp(argv[i], "--version") == 0) {
|
|
|
|
version();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else {
|
2006-07-31 01:32:39 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"unknown command line option: %s\n",
|
2006-07-19 20:04:15 +02:00
|
|
|
argv[i]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
i++;
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argcLeft <= 2) {
|
|
|
|
if (argcLeft == 2) {
|
|
|
|
readConf(argv[argc - 1]);
|
|
|
|
return;
|
|
|
|
} else if (argcLeft == 1) {
|
2005-03-06 20:00:58 +01:00
|
|
|
struct stat st;
|
2006-07-19 20:04:15 +02:00
|
|
|
char *homedir = getenv("HOME");
|
2007-12-28 03:56:25 +01:00
|
|
|
char userfile[MPD_PATH_MAX] = "";
|
2006-07-19 20:04:15 +02:00
|
|
|
if (homedir && (strlen(homedir) +
|
|
|
|
strlen(USER_CONFIG_FILE_LOCATION)) <
|
2007-12-28 03:56:25 +01:00
|
|
|
MPD_PATH_MAX) {
|
2006-07-19 20:04:15 +02:00
|
|
|
strcpy(userfile, homedir);
|
|
|
|
strcat(userfile, USER_CONFIG_FILE_LOCATION);
|
|
|
|
}
|
|
|
|
if (strlen(userfile) && (0 == stat(userfile, &st))) {
|
|
|
|
readConf(userfile);
|
2005-03-06 20:00:58 +01:00
|
|
|
return;
|
2006-07-19 20:04:15 +02:00
|
|
|
} else if (0 == stat(SYSTEM_CONFIG_FILE_LOCATION, &st)) {
|
|
|
|
readConf(SYSTEM_CONFIG_FILE_LOCATION);
|
2005-03-06 20:00:58 +01:00
|
|
|
return;
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
usage(argv);
|
|
|
|
exit(EXIT_FAILURE);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void closeAllFDs(void)
|
|
|
|
{
|
|
|
|
int i;
|
2004-06-20 06:37:17 +02:00
|
|
|
int fds = getdtablesize();
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
for (i = 3; i < fds; i++)
|
|
|
|
close(i);
|
2004-06-02 06:05:09 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void changeToUser(void)
|
|
|
|
{
|
|
|
|
ConfigParam *param = getConfigParam(CONF_USER);
|
|
|
|
|
|
|
|
if (param && strlen(param->value)) {
|
|
|
|
/* get uid */
|
|
|
|
struct passwd *userpwd;
|
|
|
|
if ((userpwd = getpwnam(param->value)) == NULL) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("no such user \"%s\" at line %i\n", param->value,
|
2006-07-19 20:04:15 +02:00
|
|
|
param->line);
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
if (setgid(userpwd->pw_gid) == -1) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("cannot setgid for user \"%s\" at line %i: %s\n",
|
2006-07-19 20:04:15 +02:00
|
|
|
param->value, param->line, strerror(errno));
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
#ifdef _BSD_SOURCE
|
2006-07-19 20:04:15 +02:00
|
|
|
/* init suplementary groups
|
|
|
|
* (must be done before we change our uid)
|
|
|
|
*/
|
|
|
|
if (initgroups(param->value, userpwd->pw_gid) == -1) {
|
|
|
|
WARNING("cannot init supplementary groups "
|
|
|
|
"of user \"%s\" at line %i: %s\n",
|
|
|
|
param->value, param->line, strerror(errno));
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
/* set uid */
|
|
|
|
if (setuid(userpwd->pw_uid) == -1) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("cannot change to uid of user "
|
2006-07-19 20:04:15 +02:00
|
|
|
"\"%s\" at line %i: %s\n",
|
|
|
|
param->value, param->line, strerror(errno));
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2005-03-06 20:00:58 +01:00
|
|
|
/* this is needed by libs such as arts */
|
2006-07-19 20:04:15 +02:00
|
|
|
if (userpwd->pw_dir) {
|
2004-10-31 05:34:54 +01:00
|
|
|
setenv("HOME", userpwd->pw_dir, 1);
|
|
|
|
}
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
2004-06-02 06:05:09 +02:00
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void openDB(Options * options, char *argv0)
|
|
|
|
{
|
|
|
|
if (options->createDB > 0 || readDirectoryDB() < 0) {
|
|
|
|
if (options->createDB < 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("can't open db file and using "
|
|
|
|
"\"--no-create-db\" command line option\n"
|
|
|
|
"try running \"%s --create-db\"\n", argv0);
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
|
|
|
flushWarningLog();
|
|
|
|
if (checkDirectoryDB() < 0)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
initMp3Directory();
|
|
|
|
if (writeDirectoryDB() < 0)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
if (options->createDB)
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2004-06-02 06:05:09 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void daemonize(Options * options)
|
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
ConfigParam *pidFileParam = parseConfigFilePath(CONF_PID_FILE, 0);
|
|
|
|
|
2006-06-05 22:05:24 +02:00
|
|
|
if (pidFileParam) {
|
|
|
|
/* 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+");
|
2006-07-19 20:04:15 +02:00
|
|
|
if (!fp) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("could not open %s \"%s\" (at line %i) for writing: %s\n",
|
2006-07-19 20:04:15 +02:00
|
|
|
CONF_PID_FILE, pidFileParam->value,
|
|
|
|
pidFileParam->line, strerror(errno));
|
2006-06-05 22:05:24 +02:00
|
|
|
}
|
2005-03-09 04:13:33 +01:00
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
if (options->daemon) {
|
|
|
|
int pid;
|
|
|
|
|
|
|
|
fflush(NULL);
|
|
|
|
pid = fork();
|
|
|
|
if (pid > 0)
|
|
|
|
_exit(EXIT_SUCCESS);
|
|
|
|
else if (pid < 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems fork'ing for daemon!\n");
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chdir("/") < 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems changing to root directory\n");
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (setsid() < 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems setsid'ing\n");
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fflush(NULL);
|
|
|
|
pid = fork();
|
|
|
|
if (pid > 0)
|
|
|
|
_exit(EXIT_SUCCESS);
|
|
|
|
else if (pid < 0) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("problems fork'ing for daemon!\n");
|
2005-03-09 04:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG("daemonized!\n");
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
2005-03-09 04:13:33 +01:00
|
|
|
|
2006-06-05 22:05:24 +02:00
|
|
|
if (pidFileParam) {
|
|
|
|
DEBUG("writing pid file\n");
|
|
|
|
fprintf(fp, "%lu\n", (unsigned long)getpid());
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2004-06-02 06:05:09 +02:00
|
|
|
}
|
2004-06-01 01:49:38 +02:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void cleanUpPidFile(void)
|
|
|
|
{
|
|
|
|
ConfigParam *pidFileParam = parseConfigFilePath(CONF_PID_FILE, 0);
|
2006-06-05 22:05:24 +02:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
if (!pidFileParam)
|
|
|
|
return;
|
2005-03-09 04:13:33 +01:00
|
|
|
|
|
|
|
DEBUG("cleaning up pid file\n");
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2005-03-09 04:13:33 +01:00
|
|
|
unlink(pidFileParam->value);
|
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void killFromPidFile(char *cmd, int killOption)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
ConfigParam *pidFileParam = parseConfigFilePath(CONF_PID_FILE, 0);
|
2005-03-09 04:13:33 +01:00
|
|
|
int pid;
|
|
|
|
|
2006-06-05 22:05:24 +02:00
|
|
|
if (!pidFileParam) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("no pid_file specified in the config file\n");
|
2006-06-05 22:05:24 +02:00
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
fp = fopen(pidFileParam->value, "r");
|
|
|
|
if (!fp) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("unable to open %s \"%s\": %s\n",
|
2006-07-19 20:04:15 +02:00
|
|
|
CONF_PID_FILE, pidFileParam->value, strerror(errno));
|
2005-03-09 04:13:33 +01:00
|
|
|
}
|
2006-07-19 20:04:15 +02:00
|
|
|
if (fscanf(fp, "%i", &pid) != 1) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("unable to read the pid from file \"%s\"\n",
|
2006-07-19 20:04:15 +02:00
|
|
|
pidFileParam->value);
|
2005-03-09 04:13:33 +01:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
if (kill(pid, SIGTERM)) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("unable to kill proccess %i: %s\n", pid, strerror(errno));
|
2005-03-09 04:13:33 +01:00
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2008-04-12 06:14:45 +02:00
|
|
|
void wakeup_main_task(void)
|
|
|
|
{
|
|
|
|
notifySignal(&main_notify);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait_main_task(void)
|
|
|
|
{
|
|
|
|
notifySignal(&main_notify);
|
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
Options options;
|
2006-08-07 22:49:20 +02:00
|
|
|
clock_t start;
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
closeAllFDs();
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
initConf();
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
parseOptions(argc, argv, &options);
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
if (options.kill)
|
|
|
|
killFromPidFile(argv[0], options.kill);
|
2005-03-09 04:13:33 +01:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
initStats();
|
2004-11-10 22:58:27 +01:00
|
|
|
initTagConfig();
|
2006-08-01 06:18:53 +02:00
|
|
|
initLog(options.verbose);
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2007-04-25 20:47:34 +02:00
|
|
|
if (options.createDB <= 0)
|
2006-07-19 20:04:15 +02:00
|
|
|
listenOnPort();
|
|
|
|
|
|
|
|
changeToUser();
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
open_log_files(options.stdOutput);
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2005-03-06 20:00:58 +01:00
|
|
|
initPaths();
|
2006-08-06 08:40:11 +02:00
|
|
|
initPermissions();
|
|
|
|
initPlaylist();
|
|
|
|
initInputPlugins();
|
|
|
|
|
|
|
|
openDB(&options, argv[0]);
|
|
|
|
|
|
|
|
initCommands();
|
|
|
|
initPlayerData();
|
2005-11-16 15:43:04 +01:00
|
|
|
initAudioConfig();
|
2006-07-19 20:04:15 +02:00
|
|
|
initAudioDriver();
|
2006-08-06 08:40:11 +02:00
|
|
|
initVolume();
|
|
|
|
initInterfaces();
|
|
|
|
initReplayGainState();
|
2006-07-27 02:50:59 +02:00
|
|
|
initNormalization();
|
2006-08-06 08:40:11 +02:00
|
|
|
initInputStream();
|
2006-07-21 19:48:54 +02:00
|
|
|
|
2008-04-12 06:14:45 +02:00
|
|
|
notifyInit(&main_notify);
|
|
|
|
|
2006-07-21 19:48:54 +02:00
|
|
|
daemonize(&options);
|
2005-11-16 15:43:04 +01:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
setup_log_output(options.stdOutput);
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2006-08-06 08:40:11 +02:00
|
|
|
initSigHandlers();
|
2005-11-18 13:09:05 +01:00
|
|
|
|
2007-06-04 19:41:18 +02:00
|
|
|
initZeroconf();
|
|
|
|
|
2005-11-18 13:09:05 +01:00
|
|
|
openVolumeDevice();
|
Initial cut of fork() => pthreads() for decoder and player
I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once. So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.
* The shared output buffer variables will still need locking
to guard against race conditions. So in this effect, we're probably
just as buggy as before. The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...
* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing
* I haven't tested HTTP streams yet, they should work.
* I've only tested ALSA and Icecast. ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.
* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)
* The non-blocking (*_nb) variants of the task management functions are
probably overused. They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.
* It currently segfaults on exit. I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles. At least the state file seems to work. We don't
do any cleanups of the threads on exit, yet.
* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.
* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread
At the beginning of every song, the main thread will set
a dirty flag and update the state file. This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.
* The main thread still wakes up every second on select()
to check for signals; which affects power management.
[merged r7138 from branches/ew]
git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
2008-04-12 06:08:00 +02:00
|
|
|
decoderInit();
|
2008-04-12 06:19:38 +02:00
|
|
|
playerInit(&getPlayerData()->playerControl);
|
2006-07-31 01:32:47 +02:00
|
|
|
read_state_file();
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2008-04-12 06:14:45 +02:00
|
|
|
notifyEnter(&main_notify);
|
|
|
|
|
2008-03-26 11:37:02 +01:00
|
|
|
while (COMMAND_RETURN_KILL != doIOForInterfaces() &&
|
|
|
|
COMMAND_RETURN_KILL != handlePendingSignals()) {
|
2006-07-19 20:04:15 +02:00
|
|
|
syncPlayerAndPlaylist();
|
|
|
|
closeOldInterfaces();
|
2004-04-12 01:07:43 +02:00
|
|
|
readDirectoryDBIfUpdateIsFinished();
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-31 01:32:47 +02:00
|
|
|
write_state_file();
|
2006-08-06 08:40:11 +02:00
|
|
|
playerKill();
|
2007-01-11 21:41:17 +01:00
|
|
|
finishZeroconf();
|
2006-07-19 20:04:15 +02:00
|
|
|
freeAllInterfaces();
|
|
|
|
closeAllListenSockets();
|
2006-08-15 01:31:08 +02:00
|
|
|
finishPlaylist();
|
2006-07-18 17:50:10 +02:00
|
|
|
|
2006-08-07 22:49:20 +02:00
|
|
|
start = clock();
|
2006-08-06 08:40:11 +02:00
|
|
|
closeMp3Directory();
|
|
|
|
DEBUG("closeMp3Directory took %f seconds\n",
|
|
|
|
((float)(clock()-start))/CLOCKS_PER_SEC);
|
2006-07-18 17:50:10 +02:00
|
|
|
|
2006-07-27 02:50:59 +02:00
|
|
|
finishNormalization();
|
2006-07-19 20:04:15 +02:00
|
|
|
finishAudioDriver();
|
|
|
|
finishAudioConfig();
|
|
|
|
finishVolume();
|
2004-02-24 00:41:20 +01:00
|
|
|
finishPaths();
|
|
|
|
finishPermissions();
|
2006-07-19 20:04:15 +02:00
|
|
|
finishCommands();
|
|
|
|
finishInputPlugins();
|
2005-03-09 04:13:33 +01:00
|
|
|
cleanUpPidFile();
|
2005-11-18 13:09:05 +01:00
|
|
|
finishConf();
|
2006-08-15 01:31:08 +02:00
|
|
|
freePlayerData();
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2006-08-01 06:18:53 +02:00
|
|
|
close_log_files();
|
2006-07-19 20:04:15 +02:00
|
|
|
return EXIT_SUCCESS;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|