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
|
|
|
|
*/
|
|
|
|
|
2008-12-30 16:28:07 +01:00
|
|
|
#include "daemon.h"
|
2008-08-28 20:02:43 +02:00
|
|
|
#include "client.h"
|
2008-10-14 22:38:14 +02:00
|
|
|
#include "idle.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "command.h"
|
|
|
|
#include "playlist.h"
|
2008-10-08 11:07:35 +02:00
|
|
|
#include "database.h"
|
2008-10-08 10:48:48 +02:00
|
|
|
#include "update.h"
|
2008-08-26 08:27:09 +02:00
|
|
|
#include "player_thread.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "listen.h"
|
2008-12-27 19:03:33 +01:00
|
|
|
#include "cmdline.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "conf.h"
|
|
|
|
#include "path.h"
|
2008-10-15 19:36:33 +02:00
|
|
|
#include "mapper.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "playerData.h"
|
2008-11-02 14:12:52 +01:00
|
|
|
#include "pipe.h"
|
2008-08-26 08:44:12 +02:00
|
|
|
#include "decoder_thread.h"
|
2008-08-26 08:45:14 +02:00
|
|
|
#include "decoder_control.h"
|
2008-08-26 08:44:38 +02:00
|
|
|
#include "player_control.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "stats.h"
|
|
|
|
#include "sig_handlers.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "volume.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "permission.h"
|
2008-11-11 15:55:34 +01:00
|
|
|
#include "replay_gain.h"
|
2008-08-26 08:27:08 +02:00
|
|
|
#include "decoder_list.h"
|
2008-10-26 19:38:50 +01:00
|
|
|
#include "input_stream.h"
|
2006-07-31 01:32:47 +02:00
|
|
|
#include "state_file.h"
|
2004-11-10 22:58:27 +01:00
|
|
|
#include "tag.h"
|
2004-11-11 03:36:25 +01:00
|
|
|
#include "dbUtils.h"
|
2004-04-16 03:06:14 +02:00
|
|
|
#include "../config.h"
|
2006-07-27 02:50:59 +02:00
|
|
|
#include "normalize.h"
|
2007-01-11 21:41:17 +01:00
|
|
|
#include "zeroconf.h"
|
2008-04-12 11:46:11 +02:00
|
|
|
#include "main_notify.h"
|
2008-12-28 21:02:30 +01:00
|
|
|
#include "dirvec.h"
|
2008-12-28 21:02:42 +01:00
|
|
|
#include "songvec.h"
|
2008-12-28 22:09:42 +01:00
|
|
|
#include "tag_pool.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
|
|
|
#include "archive_list.h"
|
|
|
|
#endif
|
|
|
|
|
2008-10-28 20:33:56 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2008-12-29 17:43:00 +01:00
|
|
|
|
|
|
|
#ifndef WIN32
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2008-12-29 17:43:00 +01:00
|
|
|
#endif
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2008-11-05 18:37:39 +01:00
|
|
|
#ifdef HAVE_LOCALE
|
|
|
|
#include <locale.h>
|
|
|
|
#endif
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void changeToUser(void)
|
|
|
|
{
|
2008-12-29 17:43:00 +01:00
|
|
|
#ifndef WIN32
|
2006-07-19 20:04:15 +02:00
|
|
|
ConfigParam *param = getConfigParam(CONF_USER);
|
|
|
|
|
|
|
|
if (param && strlen(param->value)) {
|
|
|
|
/* get uid */
|
|
|
|
struct passwd *userpwd;
|
|
|
|
if ((userpwd = getpwnam(param->value)) == NULL) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_error("no such user \"%s\" at line %i",
|
|
|
|
param->value, param->line);
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
if (setgid(userpwd->pw_gid) == -1) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_error("cannot setgid for user \"%s\" at line %i: %s",
|
|
|
|
param->value, param->line, strerror(errno));
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
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) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_warning("cannot init supplementary groups "
|
|
|
|
"of user \"%s\" at line %i: %s",
|
|
|
|
param->value, param->line, strerror(errno));
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
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) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_error("cannot change to uid of user "
|
|
|
|
"\"%s\" at line %i: %s",
|
|
|
|
param->value, param->line, strerror(errno));
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
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) {
|
2008-12-29 17:37:40 +01:00
|
|
|
g_setenv("HOME", userpwd->pw_dir, true);
|
2004-10-31 05:34:54 +01:00
|
|
|
}
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
2008-12-29 17:43:00 +01:00
|
|
|
#endif
|
2004-06-02 06:05:09 +02:00
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
static void openDB(Options * options, char *argv0)
|
|
|
|
{
|
2008-10-08 11:07:55 +02:00
|
|
|
if (options->createDB > 0 || db_load() < 0) {
|
2006-07-19 20:04:15 +02:00
|
|
|
if (options->createDB < 0) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_error("can't open db file and using "
|
|
|
|
"\"--no-create-db\" command line option; "
|
|
|
|
"try running \"%s --create-db\"", argv0);
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
2008-10-08 11:07:55 +02:00
|
|
|
if (db_check() < 0)
|
2006-07-19 20:04:15 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2008-10-08 11:07:55 +02:00
|
|
|
db_init();
|
|
|
|
if (db_save() < 0)
|
2006-07-19 20:04:15 +02:00
|
|
|
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 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
|
|
|
|
2008-12-29 17:35:40 +01:00
|
|
|
g_debug("cleaning up pid file");
|
2006-07-19 20:04:15 +02:00
|
|
|
|
2005-03-09 04:13:33 +01:00
|
|
|
unlink(pidFileParam->value);
|
|
|
|
}
|
|
|
|
|
2008-08-26 08:27:02 +02:00
|
|
|
static void killFromPidFile(void)
|
2006-07-19 20:04:15 +02:00
|
|
|
{
|
2008-12-29 17:43:00 +01:00
|
|
|
#ifndef WIN32
|
2006-07-19 20:04:15 +02:00
|
|
|
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) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_error("no pid_file specified in the config file");
|
2006-06-05 22:05:24 +02:00
|
|
|
}
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
fp = fopen(pidFileParam->value, "r");
|
|
|
|
if (!fp) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_error("unable to open %s \"%s\": %s",
|
|
|
|
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) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_error("unable to read the pid from file \"%s\"",
|
|
|
|
pidFileParam->value);
|
2005-03-09 04:13:33 +01:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
if (kill(pid, SIGTERM)) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_error("unable to kill proccess %i: %s",
|
|
|
|
pid, strerror(errno));
|
2005-03-09 04:13:33 +01:00
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
2008-12-29 17:43:00 +01:00
|
|
|
#else
|
|
|
|
g_error("--kill is not available on WIN32");
|
|
|
|
#endif
|
2005-03-09 04:13:33 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2008-11-21 18:36:33 +01:00
|
|
|
GTimer *save_state_timer;
|
2004-06-02 06:05:09 +02:00
|
|
|
|
2008-12-30 16:28:13 +01:00
|
|
|
daemonize_close_stdin();
|
|
|
|
|
2008-11-05 18:37:39 +01:00
|
|
|
#ifdef HAVE_LOCALE
|
|
|
|
/* initialize locale */
|
|
|
|
setlocale(LC_CTYPE,"");
|
|
|
|
#endif
|
|
|
|
|
2008-11-05 18:21:49 +01:00
|
|
|
/* enable GLib's thread safety code */
|
|
|
|
g_thread_init(NULL);
|
|
|
|
|
2008-12-28 21:02:14 +01:00
|
|
|
idle_init();
|
2008-12-28 21:02:30 +01:00
|
|
|
dirvec_init();
|
2008-12-28 21:02:42 +01:00
|
|
|
songvec_init();
|
2008-12-28 22:09:42 +01:00
|
|
|
tag_pool_init();
|
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)
|
2008-08-26 08:27:02 +02:00
|
|
|
killFromPidFile();
|
2005-03-09 04:13:33 +01:00
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
initStats();
|
2008-08-29 09:38:21 +02:00
|
|
|
tag_lib_init();
|
2008-12-28 19:48:53 +01:00
|
|
|
log_init(options.verbose, options.stdOutput);
|
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
|
|
|
|
2008-11-05 18:21:52 +01:00
|
|
|
path_global_init();
|
2008-10-15 19:36:33 +02:00
|
|
|
mapper_init();
|
2006-08-06 08:40:11 +02:00
|
|
|
initPermissions();
|
|
|
|
initPlaylist();
|
2008-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2008-12-16 21:42:34 +01:00
|
|
|
archive_plugin_init_all();
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2008-08-26 08:27:09 +02:00
|
|
|
decoder_plugin_init_all();
|
2008-11-27 19:34:54 +01:00
|
|
|
update_global_init();
|
2006-08-06 08:40:11 +02:00
|
|
|
|
2008-10-09 19:11:54 +02:00
|
|
|
init_main_notify();
|
|
|
|
|
2006-08-06 08:40:11 +02:00
|
|
|
openDB(&options, argv[0]);
|
|
|
|
|
2008-10-22 21:40:44 +02:00
|
|
|
command_init();
|
2006-08-06 08:40:11 +02:00
|
|
|
initPlayerData();
|
2008-08-26 08:45:14 +02:00
|
|
|
pc_init(buffered_before_play);
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_init(buffered_chunks, &pc.notify);
|
2008-08-26 08:45:14 +02:00
|
|
|
dc_init();
|
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();
|
2008-08-28 20:03:02 +02:00
|
|
|
client_manager_init();
|
2008-11-11 15:55:34 +01:00
|
|
|
replay_gain_global_init();
|
2006-07-27 02:50:59 +02:00
|
|
|
initNormalization();
|
2008-10-26 20:34:47 +01:00
|
|
|
input_stream_global_init();
|
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();
|
2008-08-26 08:44:29 +02:00
|
|
|
decoder_thread_start();
|
2008-08-26 08:27:09 +02:00
|
|
|
player_create();
|
2006-07-31 01:32:47 +02:00
|
|
|
read_state_file();
|
2005-08-23 14:01:37 +02:00
|
|
|
|
2008-11-21 18:36:33 +01:00
|
|
|
save_state_timer = g_timer_new();
|
|
|
|
|
2008-08-28 20:03:02 +02:00
|
|
|
while (COMMAND_RETURN_KILL != client_manager_io() &&
|
2008-03-26 11:37:02 +01:00
|
|
|
COMMAND_RETURN_KILL != handlePendingSignals()) {
|
2008-10-14 22:38:14 +02:00
|
|
|
unsigned flags;
|
|
|
|
|
2006-07-19 20:04:15 +02:00
|
|
|
syncPlayerAndPlaylist();
|
2008-08-28 20:03:02 +02:00
|
|
|
client_manager_expire();
|
2008-09-23 22:37:18 +02:00
|
|
|
reap_update_task();
|
2008-10-14 22:38:14 +02:00
|
|
|
|
|
|
|
/* send "idle" notificaions to all subscribed
|
|
|
|
clients */
|
|
|
|
flags = idle_get();
|
|
|
|
if (flags != 0)
|
|
|
|
client_manager_idle_add(flags);
|
2008-11-21 18:36:33 +01:00
|
|
|
|
|
|
|
if (g_timer_elapsed(save_state_timer, NULL) >= 5 * 60) {
|
2008-12-29 17:35:40 +01:00
|
|
|
g_debug("Saving state file");
|
2008-11-21 18:36:33 +01:00
|
|
|
write_state_file();
|
|
|
|
g_timer_start(save_state_timer);
|
|
|
|
}
|
2006-07-19 20:04:15 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-11-25 17:55:04 +01:00
|
|
|
g_timer_destroy(save_state_timer);
|
|
|
|
|
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();
|
2008-08-28 20:03:02 +02:00
|
|
|
client_manager_deinit();
|
2006-07-19 20:04:15 +02:00
|
|
|
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();
|
2008-10-08 11:07:55 +02:00
|
|
|
db_finish();
|
2008-12-29 17:35:40 +01:00
|
|
|
g_debug("db_finish took %f seconds",
|
|
|
|
((float)(clock()-start))/CLOCKS_PER_SEC);
|
2006-07-18 17:50:10 +02:00
|
|
|
|
2008-09-24 07:14:11 +02:00
|
|
|
deinit_main_notify();
|
|
|
|
|
2008-10-26 19:30:13 +01:00
|
|
|
input_stream_global_finish();
|
2006-07-27 02:50:59 +02:00
|
|
|
finishNormalization();
|
2006-07-19 20:04:15 +02:00
|
|
|
finishAudioDriver();
|
|
|
|
finishAudioConfig();
|
|
|
|
finishVolume();
|
2008-10-15 19:36:33 +02:00
|
|
|
mapper_finish();
|
2008-11-05 18:21:52 +01:00
|
|
|
path_global_finish();
|
2004-02-24 00:41:20 +01:00
|
|
|
finishPermissions();
|
2008-09-24 07:14:11 +02:00
|
|
|
dc_deinit();
|
|
|
|
pc_deinit();
|
2008-10-22 21:40:44 +02:00
|
|
|
command_finish();
|
2008-11-27 19:34:54 +01:00
|
|
|
update_global_finish();
|
2008-08-26 08:27:09 +02:00
|
|
|
decoder_plugin_deinit_all();
|
2008-12-27 14:33:41 +01:00
|
|
|
#ifdef ENABLE_ARCHIVE
|
2008-12-16 21:42:34 +01:00
|
|
|
archive_plugin_deinit_all();
|
2008-12-27 14:33:41 +01:00
|
|
|
#endif
|
2008-11-02 14:18:34 +01:00
|
|
|
music_pipe_free();
|
2005-03-09 04:13:33 +01:00
|
|
|
cleanUpPidFile();
|
2005-11-18 13:09:05 +01:00
|
|
|
finishConf();
|
2008-12-28 22:09:42 +01:00
|
|
|
tag_pool_deinit();
|
2008-12-28 21:02:42 +01:00
|
|
|
songvec_deinit();
|
2008-12-28 21:02:30 +01:00
|
|
|
dirvec_deinit();
|
2008-12-28 21:02:14 +01:00
|
|
|
idle_deinit();
|
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
|
|
|
}
|