1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "interface.h"
22 #include "directory.h"
27 #include "playerData.h"
29 #include "sig_handlers.h"
33 #include "permission.h"
34 #include "replayGain.h"
35 #include "inputPlugin.h"
36 #include "audioOutput.h"
37 #include "inputStream.h"
38 #include "state_file.h"
40 #include "tagTracker.h"
42 #include "../config.h"
44 #include "normalize.h"
48 #include <sys/select.h>
49 #include <sys/types.h>
61 #define SYSTEM_CONFIG_FILE_LOCATION "/etc/mpd.conf"
62 #define USER_CONFIG_FILE_LOCATION "/.mpdconf"
64 typedef struct _Options {
73 * from git-1.3.0, needed for solaris
76 static int setenv(const char *name, const char *value, int replace)
79 size_t namelen, valuelen;
86 oldval = getenv(name);
91 namelen = strlen(name);
92 valuelen = strlen(value);
93 envstr = xmalloc((namelen + valuelen + 2));
97 memcpy(envstr, name, namelen);
98 envstr[namelen] = '=';
99 memcpy(envstr + namelen + 1, value, valuelen);
100 envstr[namelen + valuelen + 1] = 0;
102 out = putenv(envstr);
103 /* putenv(3) makes the argument string part of the environment,
104 * and changing that string modifies the environment --- which
105 * means we do not own that storage anymore. Do not free
111 #endif /* HAVE_SETENV */
113 static void usage(char *argv[])
116 ERROR(" %s [options] <conf file>\n", argv[0]);
117 ERROR(" %s [options] (searches for ~%s then %s)\n",
118 argv[0], USER_CONFIG_FILE_LOCATION, SYSTEM_CONFIG_FILE_LOCATION);
121 ERROR(" --help this usage statement\n");
122 ERROR(" --kill kill the currently running mpd session\n");
124 (" --create-db force (re)creation of database and exit\n");
126 (" --no-create-db don't create database, even if it doesn't exist\n");
127 ERROR(" --no-daemon don't detach from console\n");
128 ERROR(" --stdout print messages to stdout and stderr\n");
129 ERROR(" --verbose verbose logging\n");
130 ERROR(" --version prints version information\n");
133 static void version(void)
135 LOG("mpd (MPD: Music Player Daemon) %s\n", VERSION);
137 LOG("Copyright (C) 2003-2007 Warren Dukes <warren.dukes@gmail.com>\n");
138 LOG("This is free software; see the source for copying conditions. There is NO\n");
139 LOG("warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
141 LOG("Supported formats:\n");
144 printAllInputPluginSuffixes(stdout);
147 LOG("Supported outputs:\n");
149 printAllOutputPluginTypes(stdout);
152 static void parseOptions(int argc, char **argv, Options * options)
156 options->verbose = 0;
158 options->stdOutput = 0;
159 options->createDB = 0;
165 if (strncmp(argv[i], "--", 2) == 0) {
166 if (strcmp(argv[i], "--help") == 0) {
169 } else if (strcmp(argv[i], "--kill") == 0) {
172 } else if (strcmp(argv[i], "--no-daemon") == 0) {
175 } else if (strcmp(argv[i], "--stdout") == 0) {
176 options->stdOutput = 1;
178 } else if (strcmp(argv[i], "--create-db") == 0) {
179 options->stdOutput = 1;
180 options->createDB = 1;
182 } else if (strcmp(argv[i], "--no-create-db") ==
184 options->createDB = -1;
186 } else if (strcmp(argv[i], "--verbose") == 0) {
187 options->verbose = 1;
189 } else if (strcmp(argv[i], "--version") == 0) {
194 "unknown command line option: %s\n",
206 readConf(argv[argc - 1]);
208 } else if (argcLeft == 1) {
210 char *homedir = getenv("HOME");
211 char userfile[MAXPATHLEN + 1] = "";
212 if (homedir && (strlen(homedir) +
213 strlen(USER_CONFIG_FILE_LOCATION)) <
215 strcpy(userfile, homedir);
216 strcat(userfile, USER_CONFIG_FILE_LOCATION);
218 if (strlen(userfile) && (0 == stat(userfile, &st))) {
221 } else if (0 == stat(SYSTEM_CONFIG_FILE_LOCATION, &st)) {
222 readConf(SYSTEM_CONFIG_FILE_LOCATION);
232 static void closeAllFDs(void)
235 int fds = getdtablesize();
237 for (i = 3; i < fds; i++)
241 static void changeToUser(void)
243 ConfigParam *param = getConfigParam(CONF_USER);
245 if (param && strlen(param->value)) {
247 struct passwd *userpwd;
248 if ((userpwd = getpwnam(param->value)) == NULL) {
249 FATAL("no such user \"%s\" at line %i\n", param->value,
253 if (setgid(userpwd->pw_gid) == -1) {
254 FATAL("cannot setgid for user \"%s\" at line %i: %s\n",
255 param->value, param->line, strerror(errno));
258 /* init suplementary groups
259 * (must be done before we change our uid)
261 if (initgroups(param->value, userpwd->pw_gid) == -1) {
262 WARNING("cannot init supplementary groups "
263 "of user \"%s\" at line %i: %s\n",
264 param->value, param->line, strerror(errno));
269 if (setuid(userpwd->pw_uid) == -1) {
270 FATAL("cannot change to uid of user "
271 "\"%s\" at line %i: %s\n",
272 param->value, param->line, strerror(errno));
275 /* this is needed by libs such as arts */
276 if (userpwd->pw_dir) {
277 setenv("HOME", userpwd->pw_dir, 1);
282 static void openDB(Options * options, char *argv0)
284 if (options->createDB > 0 || readDirectoryDB() < 0) {
285 if (options->createDB < 0) {
286 FATAL("can't open db file and using "
287 "\"--no-create-db\" command line option\n"
288 "try running \"%s --create-db\"\n", argv0);
291 if (checkDirectoryDB() < 0)
294 if (writeDirectoryDB() < 0)
296 if (options->createDB)
301 static void daemonize(Options * options)
304 ConfigParam *pidFileParam = parseConfigFilePath(CONF_PID_FILE, 0);
307 /* do this before daemon'izing so we can fail gracefully if we can't
308 * write to the pid file */
309 DEBUG("opening pid file\n");
310 fp = fopen(pidFileParam->value, "w+");
312 FATAL("could not open %s \"%s\" (at line %i) for writing: %s\n",
313 CONF_PID_FILE, pidFileParam->value,
314 pidFileParam->line, strerror(errno));
318 if (options->daemon) {
326 FATAL("problems fork'ing for daemon!\n");
329 if (chdir("/") < 0) {
330 FATAL("problems changing to root directory\n");
334 FATAL("problems setsid'ing\n");
342 FATAL("problems fork'ing for daemon!\n");
345 DEBUG("daemonized!\n");
349 DEBUG("writing pid file\n");
350 fprintf(fp, "%lu\n", (unsigned long)getpid());
355 static void cleanUpPidFile(void)
357 ConfigParam *pidFileParam = parseConfigFilePath(CONF_PID_FILE, 0);
362 DEBUG("cleaning up pid file\n");
364 unlink(pidFileParam->value);
367 static void killFromPidFile(char *cmd, int killOption)
370 ConfigParam *pidFileParam = parseConfigFilePath(CONF_PID_FILE, 0);
374 FATAL("no pid_file specified in the config file\n");
377 fp = fopen(pidFileParam->value, "r");
379 FATAL("unable to open %s \"%s\": %s\n",
380 CONF_PID_FILE, pidFileParam->value, strerror(errno));
382 if (fscanf(fp, "%i", &pid) != 1) {
383 FATAL("unable to read the pid from file \"%s\"\n",
384 pidFileParam->value);
388 if (kill(pid, SIGTERM)) {
389 FATAL("unable to kill proccess %i: %s\n", pid, strerror(errno));
394 int main(int argc, char *argv[])
403 parseOptions(argc, argv, &options);
406 killFromPidFile(argv[0], options.kill);
410 initLog(options.verbose);
412 if (options.createDB <= 0)
417 open_log_files(options.stdOutput);
424 openDB(&options, argv[0]);
432 initReplayGainState();
438 setup_log_output(options.stdOutput);
447 while (COMMAND_RETURN_KILL != doIOForInterfaces()) {
448 if (COMMAND_RETURN_KILL == handlePendingSignals())
450 syncPlayerAndPlaylist();
451 closeOldInterfaces();
452 readDirectoryDBIfUpdateIsFinished();
459 closeAllListenSockets();
464 DEBUG("closeMp3Directory took %f seconds\n",
465 ((float)(clock()-start))/CLOCKS_PER_SEC);
467 finishNormalization();
474 finishInputPlugins();