2003-10-13 11:03:14 -04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* Driver for multi-threaded daap server
|
|
|
|
*
|
2003-12-29 15:41:08 -05:00
|
|
|
* Copyright (C) 2003 Ron Pedde (ron@pedde.com)
|
2003-10-13 11:03:14 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
2006-02-26 03:46:24 -05:00
|
|
|
* @file main.c
|
2004-11-12 01:38:05 -05:00
|
|
|
*
|
|
|
|
* Driver for mt-daapd, including the main() function. This
|
|
|
|
* is responsible for kicking off the initial mp3 scan, starting
|
|
|
|
* up the signal handler, starting up the webserver, and waiting
|
|
|
|
* around for external events to happen (like a request to rescan,
|
|
|
|
* or a background rescan to take place.)
|
|
|
|
*
|
|
|
|
* It also contains the daap handling callback for the webserver.
|
|
|
|
* This should almost certainly be somewhere else, and is in
|
|
|
|
* desparate need of refactoring, but somehow continues to be in
|
2006-02-26 03:46:24 -05:00
|
|
|
* this files.
|
2004-11-12 01:38:05 -05:00
|
|
|
*/
|
|
|
|
|
2006-02-26 03:46:24 -05:00
|
|
|
/** @mainpage mt-daapd
|
|
|
|
* @section about_section About
|
2004-11-12 01:38:05 -05:00
|
|
|
*
|
|
|
|
* This is mt-daapd, an attempt to create an iTunes server for
|
|
|
|
* linux and other POSIXish systems. Maybe even Windows with cygwin,
|
|
|
|
* eventually.
|
|
|
|
*
|
|
|
|
* You might check these locations for more info:
|
2006-02-26 03:46:24 -05:00
|
|
|
* - <a href="http://www.mt-daapd.org">Home page</a>
|
2004-11-12 01:38:05 -05:00
|
|
|
* - <a href="http://sf.net/projects/mt-daapd">Project page on SourceForge</a>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-11-03 14:02:00 -05:00
|
|
|
#include <errno.h>
|
2003-10-19 16:03:54 -04:00
|
|
|
#include <fcntl.h>
|
2003-11-03 14:02:00 -05:00
|
|
|
#include <limits.h>
|
2003-12-01 10:27:40 -05:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
2006-03-05 03:09:27 -05:00
|
|
|
#include <stdarg.h>
|
2006-11-14 00:29:32 -05:00
|
|
|
#ifdef HAVE_STDINT_H
|
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
2003-10-13 11:03:14 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-10-19 16:03:54 -04:00
|
|
|
#include <string.h>
|
2006-10-08 14:05:31 -04:00
|
|
|
#ifdef HAVE_DIRENT_H
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
2006-02-26 03:46:24 -05:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2006-02-26 03:46:24 -05:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2004-02-14 19:51:11 -05:00
|
|
|
#include <sys/wait.h>
|
2006-02-26 03:46:24 -05:00
|
|
|
#endif
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2007-01-16 20:06:16 -05:00
|
|
|
#include "daapd.h"
|
|
|
|
|
2006-02-27 17:48:42 -05:00
|
|
|
#include "conf.h"
|
2003-10-19 16:03:54 -04:00
|
|
|
#include "configfile.h"
|
2003-10-13 11:03:14 -04:00
|
|
|
#include "err.h"
|
2003-11-04 01:11:00 -05:00
|
|
|
#include "mp3-scanner.h"
|
2003-10-13 11:03:14 -04:00
|
|
|
#include "webserver.h"
|
2006-02-26 03:46:24 -05:00
|
|
|
#include "restart.h"
|
2005-03-11 01:39:40 -05:00
|
|
|
#include "db-generic.h"
|
2006-02-26 03:46:24 -05:00
|
|
|
#include "os.h"
|
2006-04-19 04:32:18 -04:00
|
|
|
#include "plugin.h"
|
2006-07-12 00:10:21 -04:00
|
|
|
#include "util.h"
|
2007-04-13 17:37:42 -04:00
|
|
|
#include "upnp.h"
|
2007-07-31 00:34:33 -04:00
|
|
|
#include "io.h"
|
2006-02-26 03:46:24 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
# include "getopt.h"
|
|
|
|
#endif
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-10-30 12:42:20 -04:00
|
|
|
#ifndef WITHOUT_MDNS
|
|
|
|
# include "rend.h"
|
|
|
|
#endif
|
|
|
|
|
2004-11-12 02:27:05 -05:00
|
|
|
/** Seconds to sleep before checking for a shutdown or reload */
|
|
|
|
#define MAIN_SLEEP_INTERVAL 2
|
2005-02-05 15:54:55 -05:00
|
|
|
|
|
|
|
/** Let's hope if you have no atoll, you only have 32 bit inodes... */
|
|
|
|
#if !HAVE_ATOLL
|
|
|
|
# define atoll(a) atol(a)
|
|
|
|
#endif
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
2004-11-12 01:38:05 -05:00
|
|
|
CONFIG config; /**< Main configuration structure, as read from configfile */
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2006-01-04 15:30:44 -05:00
|
|
|
/*
|
2003-12-29 17:09:15 -05:00
|
|
|
* Forwards
|
|
|
|
*/
|
2004-11-12 01:38:05 -05:00
|
|
|
static void usage(char *program);
|
2006-04-20 02:52:21 -04:00
|
|
|
static void main_handler(WS_CONNINFO *pwsc);
|
2006-04-22 15:45:49 -04:00
|
|
|
static int main_auth(WS_CONNINFO *pwsc, char *username, char *password);
|
2006-05-24 01:14:58 -04:00
|
|
|
static void txt_add(char *txtrecord, char *fmt, ...);
|
2007-07-31 00:34:33 -04:00
|
|
|
static void main_io_errhandler(int level, char *msg);
|
|
|
|
static void main_ws_errhandler(int level, char *msg);
|
2006-05-29 03:57:45 -04:00
|
|
|
|
2006-03-05 03:09:27 -05:00
|
|
|
/**
|
|
|
|
* build a dns text string
|
|
|
|
*
|
|
|
|
* @param txtrecord buffer to append text record string to
|
|
|
|
* @param fmt sprintf-style format
|
|
|
|
*/
|
|
|
|
void txt_add(char *txtrecord, char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
char buff[256];
|
|
|
|
int len;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buff, sizeof(buff), fmt, ap);
|
|
|
|
va_end(ap);
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2006-03-07 01:07:34 -05:00
|
|
|
len = (int)strlen(buff);
|
2006-10-15 23:52:45 -04:00
|
|
|
if(len + strlen(txtrecord) > 255) {
|
|
|
|
DPRINTF(E_FATAL,L_MAIN,"dns-sd text string too long. Try a shorter "
|
|
|
|
"share name.\n");
|
|
|
|
}
|
|
|
|
|
2006-03-05 03:09:27 -05:00
|
|
|
end = txtrecord + strlen(txtrecord);
|
|
|
|
*end = len;
|
|
|
|
strcpy(end+1,buff);
|
|
|
|
}
|
|
|
|
|
2006-04-20 02:52:21 -04:00
|
|
|
void main_handler(WS_CONNINFO *pwsc) {
|
2006-05-05 03:38:13 -04:00
|
|
|
DPRINTF(E_DBG,L_MAIN,"in main_handler\n");
|
2006-04-20 02:52:21 -04:00
|
|
|
if(plugin_url_candispatch(pwsc)) {
|
2007-07-31 00:34:33 -04:00
|
|
|
DPRINTF(E_DBG,L_MAIN,"Dispatching %s to plugin\n",ws_uri(pwsc));
|
2006-04-20 02:52:21 -04:00
|
|
|
plugin_url_handle(pwsc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-07-31 00:34:33 -04:00
|
|
|
DPRINTF(E_DBG,L_MAIN,"Dispatching %s to config handler\n",ws_uri(pwsc));
|
2006-04-20 02:52:21 -04:00
|
|
|
config_handler(pwsc);
|
|
|
|
}
|
|
|
|
|
2006-04-22 15:45:49 -04:00
|
|
|
int main_auth(WS_CONNINFO *pwsc, char *username, char *password) {
|
2006-05-05 03:38:13 -04:00
|
|
|
DPRINTF(E_DBG,L_MAIN,"in main_auth\n");
|
2006-04-22 15:45:49 -04:00
|
|
|
if(plugin_url_candispatch(pwsc)) {
|
2007-07-31 00:34:33 -04:00
|
|
|
DPRINTF(E_DBG,L_MAIN,"Dispatching auth for %s to plugin\n",ws_uri(pwsc));
|
2006-04-22 15:45:49 -04:00
|
|
|
return plugin_auth_handle(pwsc,username,password);
|
|
|
|
}
|
|
|
|
|
2007-07-31 00:34:33 -04:00
|
|
|
DPRINTF(E_DBG,L_MAIN,"Dispatching auth for %s to config auth\n",ws_uri(pwsc));
|
2006-04-22 15:45:49 -04:00
|
|
|
return config_auth(pwsc, username, password);
|
|
|
|
}
|
2006-04-20 02:52:21 -04:00
|
|
|
|
2003-12-29 17:09:15 -05:00
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Print usage information to stdout
|
2003-10-19 16:03:54 -04:00
|
|
|
*
|
2004-11-12 01:38:05 -05:00
|
|
|
* \param program name of program (argv[0])
|
2003-10-19 16:03:54 -04:00
|
|
|
*/
|
|
|
|
void usage(char *program) {
|
|
|
|
printf("Usage: %s [options]\n\n",program);
|
|
|
|
printf("Options:\n");
|
2006-06-12 22:27:21 -04:00
|
|
|
printf(" -a Set cwd to app dir before starting\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
printf(" -d <number> Debuglevel (0-9)\n");
|
2004-11-13 03:05:27 -05:00
|
|
|
printf(" -D <mod,mod..> Debug modules\n");
|
2004-01-04 16:57:38 -05:00
|
|
|
printf(" -m Disable mDNS\n");
|
2005-01-24 02:42:42 -05:00
|
|
|
printf(" -c <file> Use configfile specified\n");
|
2005-10-26 01:10:19 -04:00
|
|
|
printf(" -P <file> Write the PID ot specified file\n");
|
2003-12-29 17:09:15 -05:00
|
|
|
printf(" -f Run in foreground\n");
|
2005-01-24 02:42:42 -05:00
|
|
|
printf(" -y Yes, go ahead and run as non-root user\n");
|
2006-07-15 01:56:32 -04:00
|
|
|
printf(" -b <id> ffid to be broadcast\n");
|
2007-01-10 20:28:18 -05:00
|
|
|
printf(" -V Display version information\n");
|
|
|
|
printf(" -k Kill a running daemon (based on pidfile)\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
printf("\n\n");
|
2004-11-13 03:05:27 -05:00
|
|
|
printf("Valid debug modules:\n");
|
|
|
|
printf(" config,webserver,database,scan,query,index,browse\n");
|
|
|
|
printf(" playlist,art,daap,main,rend,misc\n");
|
|
|
|
printf("\n\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
|
|
|
|
2007-04-04 16:34:29 -04:00
|
|
|
/**
|
|
|
|
* process a directory for plugins
|
|
|
|
*
|
|
|
|
* @returns TRUE if at least one plugin loaded successfully
|
|
|
|
*/
|
|
|
|
int load_plugin_dir(char *plugindir) {
|
|
|
|
DIR *d_plugin;
|
|
|
|
char de[sizeof(struct dirent) + MAXNAMLEN + 1]; /* ?? solaris */
|
|
|
|
struct dirent *pde;
|
|
|
|
char *pext;
|
|
|
|
char *perr=NULL;
|
|
|
|
int loaded=FALSE;
|
|
|
|
char plugin[PATH_MAX];
|
|
|
|
|
|
|
|
if((d_plugin=opendir(plugindir)) == NULL) {
|
|
|
|
DPRINTF(E_LOG,L_MAIN,"Error opening plugin dir %s. Ignoring\n",
|
|
|
|
plugindir);
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
while((readdir_r(d_plugin,(struct dirent *)de,&pde) != 1) && pde) {
|
|
|
|
pext = strrchr(pde->d_name,'.');
|
|
|
|
if((strcasecmp(pext,".so") == 0) ||
|
|
|
|
(strcasecmp(pext,".dylib") == 0) ||
|
|
|
|
(strcasecmp(pext,".dll") == 0)) {
|
|
|
|
/* must be a plugin */
|
|
|
|
snprintf(plugin,PATH_MAX,"%s%c%s",plugindir,
|
|
|
|
PATHSEP,pde->d_name);
|
|
|
|
if(plugin_load(&perr,plugin) != PLUGIN_E_SUCCESS) {
|
|
|
|
DPRINTF(E_LOG,L_MAIN,"Error loading plugin %s: %s\n",
|
|
|
|
plugin,perr);
|
|
|
|
free(perr);
|
|
|
|
perr = NULL;
|
|
|
|
} else {
|
|
|
|
loaded = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d_plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
2007-07-31 00:34:33 -04:00
|
|
|
/**
|
|
|
|
* set up an errorhandler for io errors
|
|
|
|
*
|
|
|
|
* @param int level of the error (0=fatal, 9=debug)
|
|
|
|
* @param msg the text error
|
|
|
|
*/
|
|
|
|
void main_io_errhandler(int level, char *msg) {
|
|
|
|
DPRINTF(level,L_MAIN,"%s",msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set up an errorhandler for webserver errors
|
|
|
|
*
|
|
|
|
* @param int level of the error (0=fatal, 9=debug)
|
|
|
|
* @param msg the text error
|
|
|
|
*/
|
|
|
|
void main_ws_errhandler(int level, char *msg) {
|
|
|
|
DPRINTF(level,L_WS,"%s",msg);
|
|
|
|
}
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Kick off the daap server and wait for events.
|
|
|
|
*
|
|
|
|
* This starts the initial db scan, sets up the signal
|
|
|
|
* handling, starts the webserver, then sits back and waits
|
|
|
|
* for events, as notified by the signal handler and the
|
|
|
|
* web interface. These events are communicated via flags
|
|
|
|
* in the config structure.
|
|
|
|
*
|
|
|
|
* \param argc count of command line arguments
|
|
|
|
* \param argv command line argument pointers
|
|
|
|
* \returns 0 on success, -1 otherwise
|
|
|
|
*
|
|
|
|
* \todo split out a ws_init and ws_start, so that the
|
|
|
|
* web space handlers can be registered before the webserver
|
|
|
|
* starts.
|
|
|
|
*
|
|
|
|
*/
|
2003-10-13 11:03:14 -04:00
|
|
|
int main(int argc, char *argv[]) {
|
2003-10-19 16:03:54 -04:00
|
|
|
int option;
|
2006-04-08 21:35:10 -04:00
|
|
|
char *configfile=CONFFILE;
|
2003-10-13 11:03:14 -04:00
|
|
|
WSCONFIG ws_config;
|
2004-06-14 15:01:06 -04:00
|
|
|
int reload=0;
|
2004-03-08 16:27:38 -05:00
|
|
|
int start_time;
|
|
|
|
int end_time;
|
2004-11-11 14:17:02 -05:00
|
|
|
int rescan_counter=0;
|
2006-01-04 15:30:44 -05:00
|
|
|
int old_song_count, song_count;
|
2004-12-06 19:24:39 -05:00
|
|
|
int force_non_root=0;
|
2006-05-01 04:06:19 -04:00
|
|
|
int skip_initial=1;
|
2007-01-09 13:12:39 -05:00
|
|
|
int kill_server=0;
|
2006-03-13 01:33:58 -05:00
|
|
|
int convert_conf=0;
|
2006-06-19 00:29:27 -04:00
|
|
|
char *db_type,*db_parms,*web_root,*runas, *tmp;
|
2006-04-12 01:18:55 -04:00
|
|
|
char **mp3_dir_array;
|
2006-05-29 03:57:45 -04:00
|
|
|
char *servername, *iface;
|
2006-07-15 01:56:32 -04:00
|
|
|
char *ffid = NULL;
|
2006-06-12 22:27:21 -04:00
|
|
|
int appdir = 0;
|
2007-04-04 16:34:29 -04:00
|
|
|
char *perr=NULL;
|
2006-03-05 03:09:27 -05:00
|
|
|
char txtrecord[255];
|
2007-04-04 16:34:29 -04:00
|
|
|
void *phandle;
|
2006-04-19 04:32:18 -04:00
|
|
|
char *plugindir;
|
|
|
|
|
2006-01-05 19:05:02 -05:00
|
|
|
int err;
|
2006-06-12 22:27:21 -04:00
|
|
|
char *apppath;
|
2004-12-09 00:07:09 -05:00
|
|
|
|
2006-06-19 00:25:08 -04:00
|
|
|
int debuglevel=0;
|
|
|
|
|
2004-01-04 16:57:38 -05:00
|
|
|
config.use_mdns=1;
|
2006-08-24 23:37:03 -04:00
|
|
|
err_setlevel(2);
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2006-03-04 21:02:15 -05:00
|
|
|
config.foreground=0;
|
2007-01-09 13:12:39 -05:00
|
|
|
while((option=getopt(argc,argv,"D:d:c:P:mfrysiuvab:Vk")) != -1) {
|
2005-11-07 00:58:05 -05:00
|
|
|
switch(option) {
|
2006-06-12 22:27:21 -04:00
|
|
|
case 'a':
|
|
|
|
appdir = 1;
|
|
|
|
break;
|
2006-08-24 23:37:03 -04:00
|
|
|
|
2006-07-15 01:56:32 -04:00
|
|
|
case 'b':
|
|
|
|
ffid=optarg;
|
|
|
|
break;
|
2006-08-24 23:37:03 -04:00
|
|
|
|
2005-11-07 00:58:05 -05:00
|
|
|
case 'd':
|
2006-06-19 00:25:08 -04:00
|
|
|
debuglevel = atoi(optarg);
|
|
|
|
err_setlevel(debuglevel);
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
2006-08-24 23:37:03 -04:00
|
|
|
|
2005-11-07 00:58:05 -05:00
|
|
|
case 'D':
|
|
|
|
if(err_setdebugmask(optarg)) {
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
2006-08-24 23:37:03 -04:00
|
|
|
|
2005-11-07 00:58:05 -05:00
|
|
|
case 'f':
|
2006-03-04 21:02:15 -05:00
|
|
|
config.foreground=1;
|
2006-05-30 19:46:43 -04:00
|
|
|
err_setdest(err_getdest() | LOGDEST_STDERR);
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
configfile=optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
config.use_mdns=0;
|
|
|
|
break;
|
|
|
|
|
2006-02-26 03:46:24 -05:00
|
|
|
#ifndef WIN32
|
2005-11-07 00:58:05 -05:00
|
|
|
case 'P':
|
2006-02-26 03:46:24 -05:00
|
|
|
os_set_pidfile(optarg);
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
2006-02-26 03:46:24 -05:00
|
|
|
#endif
|
2005-11-07 00:58:05 -05:00
|
|
|
case 'r':
|
|
|
|
reload=1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
2006-05-01 04:06:19 -04:00
|
|
|
skip_initial=0;
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'y':
|
|
|
|
force_non_root=1;
|
|
|
|
break;
|
|
|
|
|
2006-02-26 03:46:24 -05:00
|
|
|
#ifdef WIN32
|
|
|
|
case 'i':
|
|
|
|
os_register();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
os_unregister();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
#endif
|
2006-03-13 01:33:58 -05:00
|
|
|
case 'v':
|
|
|
|
convert_conf=1;
|
|
|
|
break;
|
2006-02-26 03:46:24 -05:00
|
|
|
|
2007-01-09 13:12:39 -05:00
|
|
|
case 'k':
|
|
|
|
kill_server=1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V':
|
|
|
|
fprintf(stderr,"Firefly Media Server: Version %s\n",VERSION);
|
2007-01-10 01:19:41 -05:00
|
|
|
exit(EXIT_SUCCESS);
|
2007-01-09 13:12:39 -05:00
|
|
|
break;
|
|
|
|
|
2005-11-07 00:58:05 -05:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
|
|
|
|
2006-03-13 01:33:58 -05:00
|
|
|
if((getuid()) && (!force_non_root) && (!convert_conf)) {
|
2006-08-24 23:37:03 -04:00
|
|
|
fprintf(stderr,"You are not root. This is almost certainly wrong. "
|
|
|
|
"If you are\nsure you want to do this, use the -y "
|
|
|
|
"command-line switch\n");
|
2005-11-07 00:58:05 -05:00
|
|
|
exit(EXIT_FAILURE);
|
2004-12-06 19:24:39 -05:00
|
|
|
}
|
|
|
|
|
2007-01-09 13:12:39 -05:00
|
|
|
|
|
|
|
if(kill_server) {
|
|
|
|
os_signal_server(S_STOP);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2007-07-31 00:34:33 -04:00
|
|
|
io_init();
|
|
|
|
io_set_errhandler(main_io_errhandler);
|
|
|
|
ws_set_errhandler(main_ws_errhandler);
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/* read the configfile, if specified, otherwise
|
|
|
|
* try defaults */
|
2006-02-26 03:46:24 -05:00
|
|
|
config.stats.start_time=start_time=(int)time(NULL);
|
2005-03-13 16:22:05 -05:00
|
|
|
config.stop=0;
|
2004-03-08 16:27:38 -05:00
|
|
|
|
2006-06-12 22:27:21 -04:00
|
|
|
/* set appdir first, that way config resolves relative to appdir */
|
|
|
|
if(appdir) {
|
|
|
|
apppath = os_apppath(argv[0]);
|
|
|
|
DPRINTF(E_INF,L_MAIN,"Changing cwd to %s\n",apppath);
|
|
|
|
chdir(apppath);
|
|
|
|
free(apppath);
|
|
|
|
configfile="mt-daapd.conf";
|
|
|
|
}
|
|
|
|
|
2006-02-27 17:48:42 -05:00
|
|
|
if(conf_read(configfile) != CONF_E_SUCCESS) {
|
2005-11-07 00:58:05 -05:00
|
|
|
fprintf(stderr,"Error reading config file (%s)\n",configfile);
|
|
|
|
exit(EXIT_FAILURE);
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
2003-11-05 13:57:13 -05:00
|
|
|
|
2006-06-19 00:25:08 -04:00
|
|
|
if(debuglevel) /* was specified, should override the config file */
|
|
|
|
err_setlevel(debuglevel);
|
|
|
|
|
2006-03-13 01:33:58 -05:00
|
|
|
if(convert_conf) {
|
|
|
|
fprintf(stderr,"Converting config file...\n");
|
|
|
|
if(!conf_write()) {
|
|
|
|
fprintf(stderr,"Error writing config file.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2006-11-11 16:47:43 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Firefly Version %s: Starting with debuglevel %d\n",
|
|
|
|
VERSION,err_getlevel());
|
2005-05-21 01:53:11 -04:00
|
|
|
|
2007-04-13 17:37:42 -04:00
|
|
|
|
2006-11-11 16:47:43 -05:00
|
|
|
/* load plugins before we drop privs? Maybe... let the
|
2006-04-19 04:32:18 -04:00
|
|
|
* plugins do stuff they might need to */
|
2006-04-22 14:22:41 -04:00
|
|
|
plugin_init();
|
2006-04-19 04:32:18 -04:00
|
|
|
if((plugindir=conf_alloc_string("plugins","plugin_dir",NULL)) != NULL) {
|
2006-10-08 14:05:31 -04:00
|
|
|
/* instead of specifying plugins, let's walk through the directory
|
|
|
|
* and load each of them */
|
2007-04-04 16:34:29 -04:00
|
|
|
if(!load_plugin_dir(plugindir)) {
|
|
|
|
DPRINTF(E_LOG,L_MAIN,"Warning: Could not load plugins\n");
|
2006-04-19 04:32:18 -04:00
|
|
|
}
|
2006-10-08 14:05:31 -04:00
|
|
|
free(plugindir);
|
2007-04-04 16:34:29 -04:00
|
|
|
} else {
|
|
|
|
if((!load_plugin_dir("/usr/lib/firefly")) &&
|
|
|
|
(!load_plugin_dir("/usr/lib/mt-daapd")) &&
|
|
|
|
(!load_plugin_dir("/usr/share/firefly/plugins")) &&
|
|
|
|
(!load_plugin_dir("/usr/share/mt-daapd/plugins")) &&
|
|
|
|
(!load_plugin_dir("/usr/local/share/firefly/plugins")) &&
|
|
|
|
(!load_plugin_dir("/usr/local/share/mt-daapd/plugins")) &&
|
|
|
|
(!load_plugin_dir("/opt/share/firefly/plugins")) &&
|
|
|
|
(!load_plugin_dir("/opt/share/mt-daapd/plugins")) &&
|
|
|
|
(!load_plugin_dir("plugins/.libs"))) {
|
|
|
|
DPRINTF(E_FATAL,L_MAIN,"plugins/plugin_dir not specified\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
phandle=NULL;
|
|
|
|
while((phandle=plugin_enum(phandle))) {
|
|
|
|
DPRINTF(E_LOG,L_MAIN,"Plugin loaded: %s\n",plugin_get_description(phandle));
|
2006-04-19 04:32:18 -04:00
|
|
|
}
|
|
|
|
|
2006-03-25 05:52:10 -05:00
|
|
|
runas = conf_alloc_string("general","runas","nobody");
|
|
|
|
|
2004-12-09 00:07:09 -05:00
|
|
|
#ifndef WITHOUT_MDNS
|
2005-03-11 01:39:40 -05:00
|
|
|
if(config.use_mdns) {
|
2005-11-07 00:58:05 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Starting rendezvous daemon\n");
|
2006-02-27 17:48:42 -05:00
|
|
|
if(rend_init(runas)) {
|
2006-03-25 05:52:10 -05:00
|
|
|
DPRINTF(E_FATAL,L_MAIN|L_REND,"Error in rend_init: %s\n",
|
|
|
|
strerror(errno));
|
2005-11-07 00:58:05 -05:00
|
|
|
}
|
2004-04-13 00:23:36 -04:00
|
|
|
}
|
2004-12-09 00:07:09 -05:00
|
|
|
#endif
|
2004-04-13 00:23:36 -04:00
|
|
|
|
2006-03-25 05:52:10 -05:00
|
|
|
if(!os_init(config.foreground,runas)) {
|
2006-02-26 03:46:24 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Could not initialize server\n");
|
|
|
|
os_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
2005-02-04 02:37:48 -05:00
|
|
|
}
|
2005-03-19 23:13:34 -05:00
|
|
|
|
2006-03-25 05:52:10 -05:00
|
|
|
free(runas);
|
|
|
|
|
2007-05-02 23:30:30 -04:00
|
|
|
#ifdef UPNP
|
|
|
|
upnp_init();
|
|
|
|
#endif
|
|
|
|
|
2005-03-19 23:13:34 -05:00
|
|
|
/* this will require that the db be readable by the runas user */
|
2006-03-25 05:52:10 -05:00
|
|
|
db_type = conf_alloc_string("general","db_type","sqlite");
|
|
|
|
db_parms = conf_alloc_string("general","db_parms","/var/cache/mt-daapd");
|
2006-02-27 17:48:42 -05:00
|
|
|
err=db_open(&perr,db_type,db_parms);
|
|
|
|
|
|
|
|
if(err) {
|
2006-04-10 00:27:52 -04:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_DB,"Error opening db: %s\n",perr);
|
|
|
|
#ifndef WITHOUT_MDNS
|
|
|
|
if(config.use_mdns) {
|
|
|
|
rend_stop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
os_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
2006-02-27 17:48:42 -05:00
|
|
|
}
|
2005-03-19 23:13:34 -05:00
|
|
|
|
2006-03-25 05:52:10 -05:00
|
|
|
free(db_type);
|
|
|
|
free(db_parms);
|
|
|
|
|
2004-03-08 15:36:07 -05:00
|
|
|
/* Initialize the database before starting */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_DB,"Initializing database\n");
|
2007-05-02 23:30:30 -04:00
|
|
|
if(db_init(reload)) {
|
2005-11-07 00:58:05 -05:00
|
|
|
DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_init: %s\n",strerror(errno));
|
2004-03-08 15:36:07 -05:00
|
|
|
}
|
2007-07-31 00:34:33 -04:00
|
|
|
|
|
|
|
err=db_get_song_count(&perr,&song_count);
|
|
|
|
if(err != DB_E_SUCCESS) {
|
|
|
|
DPRINTF(E_FATAL,L_MISC,"Error getting song count: %s\n",perr);
|
|
|
|
}
|
|
|
|
/* do a full reload if the db is empty */
|
|
|
|
if(!song_count)
|
|
|
|
reload = 1;
|
|
|
|
|
2006-04-12 01:18:55 -04:00
|
|
|
if(conf_get_array("general","mp3_dir",&mp3_dir_array)) {
|
2006-05-01 04:06:19 -04:00
|
|
|
if((!skip_initial) || (reload)) {
|
2006-04-12 01:18:55 -04:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_SCAN,"Starting mp3 scan\n");
|
2006-02-27 17:48:42 -05:00
|
|
|
|
2006-05-06 03:22:51 -04:00
|
|
|
plugin_event_dispatch(PLUGIN_EVENT_FULLSCAN_START,0,NULL,0);
|
2006-08-13 20:00:48 -04:00
|
|
|
start_time=(int) time(NULL);
|
2006-04-12 01:18:55 -04:00
|
|
|
if(scan_init(mp3_dir_array)) {
|
|
|
|
DPRINTF(E_LOG,L_MAIN|L_SCAN,"Error scanning MP3 files: %s\n",strerror(errno));
|
|
|
|
}
|
2006-08-13 20:00:48 -04:00
|
|
|
if(!config.stop) { /* don't send popup when shutting down */
|
2006-06-01 02:20:55 -04:00
|
|
|
plugin_event_dispatch(PLUGIN_EVENT_FULLSCAN_END,0,NULL,0);
|
2006-08-13 20:00:48 -04:00
|
|
|
err=db_get_song_count(&perr,&song_count);
|
|
|
|
end_time=(int) time(NULL);
|
|
|
|
DPRINTF(E_LOG,L_MAIN|L_SCAN,"Scanned %d songs in %d seconds\n",
|
|
|
|
song_count,end_time - start_time);
|
2006-06-01 02:20:55 -04:00
|
|
|
}
|
2005-11-07 00:58:05 -05:00
|
|
|
}
|
2006-04-12 01:18:55 -04:00
|
|
|
conf_dispose_array(mp3_dir_array);
|
2003-11-04 01:11:00 -05:00
|
|
|
}
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/* start up the web server */
|
2006-03-25 05:52:10 -05:00
|
|
|
web_root = conf_alloc_string("general","web_root",NULL);
|
2006-02-27 17:48:42 -05:00
|
|
|
ws_config.web_root=web_root;
|
2006-05-24 00:19:44 -04:00
|
|
|
ws_config.port=conf_get_int("general","port",0);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_WS,"Starting web server from %s on port %d\n",
|
2006-02-27 17:48:42 -05:00
|
|
|
ws_config.web_root, ws_config.port);
|
2004-04-13 00:23:36 -04:00
|
|
|
|
2007-07-31 00:34:33 -04:00
|
|
|
config.server=ws_init(&ws_config);
|
2005-11-07 00:58:05 -05:00
|
|
|
if(!config.server) {
|
2007-07-31 00:34:33 -04:00
|
|
|
/* pthreads or malloc error */
|
|
|
|
DPRINTF(E_FATAL,L_MAIN|L_WS,"Error initializing web server\n");
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
|
2007-07-31 00:34:33 -04:00
|
|
|
if(E_WS_SUCCESS != ws_start(config.server)) {
|
|
|
|
/* listen or pthread error */
|
|
|
|
DPRINTF(E_FATAL,L_MAIN|L_WS,"Error starting web server\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_registerhandler(config.server, "/",main_handler,main_auth,
|
|
|
|
0,1);
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2004-10-30 12:42:20 -04:00
|
|
|
#ifndef WITHOUT_MDNS
|
2004-01-19 23:41:20 -05:00
|
|
|
if(config.use_mdns) { /* register services */
|
2006-05-23 23:53:22 -04:00
|
|
|
servername = conf_get_servername();
|
2006-03-05 03:09:27 -05:00
|
|
|
|
|
|
|
memset(txtrecord,0,sizeof(txtrecord));
|
|
|
|
txt_add(txtrecord,"txtvers=1");
|
2006-07-12 00:10:21 -04:00
|
|
|
txt_add(txtrecord,"Database ID=%0X",util_djb_hash_str(servername));
|
|
|
|
txt_add(txtrecord,"Machine ID=%0X",util_djb_hash_str(servername));
|
2006-03-05 03:09:27 -05:00
|
|
|
txt_add(txtrecord,"Machine Name=%s",servername);
|
|
|
|
txt_add(txtrecord,"mtd-version=" VERSION);
|
2006-03-25 05:52:10 -05:00
|
|
|
txt_add(txtrecord,"iTSh Version=131073"); /* iTunes 6.0.4 */
|
|
|
|
txt_add(txtrecord,"Version=196610"); /* iTunes 6.0.4 */
|
2006-06-19 00:29:27 -04:00
|
|
|
tmp = conf_alloc_string("general","password",NULL);
|
|
|
|
if(tmp && (strlen(tmp)==0)) tmp=NULL;
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2006-06-19 00:29:27 -04:00
|
|
|
txt_add(txtrecord,"Password=%s",tmp ? "true" : "false");
|
|
|
|
if(tmp) free(tmp);
|
|
|
|
|
2006-05-28 02:11:37 -04:00
|
|
|
srand((unsigned int)time(NULL));
|
2006-07-15 01:56:32 -04:00
|
|
|
|
|
|
|
if(ffid) {
|
|
|
|
txt_add(txtrecord,"ffid=%s",ffid);
|
|
|
|
} else {
|
|
|
|
txt_add(txtrecord,"ffid=%08x",rand());
|
|
|
|
}
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2006-03-05 03:09:27 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_REND,"Registering rendezvous names\n");
|
2006-03-25 05:52:10 -05:00
|
|
|
iface = conf_alloc_string("general","interface","");
|
2006-09-24 23:20:22 -04:00
|
|
|
|
2006-03-05 03:09:27 -05:00
|
|
|
rend_register(servername,"_http._tcp",ws_config.port,iface,txtrecord);
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2006-05-24 01:14:58 -04:00
|
|
|
plugin_rend_register(servername,ws_config.port,iface,txtrecord);
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2006-03-25 05:52:10 -05:00
|
|
|
free(servername);
|
|
|
|
free(iface);
|
2004-01-19 23:41:20 -05:00
|
|
|
}
|
2004-10-30 12:42:20 -04:00
|
|
|
#endif
|
2004-01-19 23:41:20 -05:00
|
|
|
|
2006-02-26 03:46:24 -05:00
|
|
|
end_time=(int) time(NULL);
|
2004-03-08 16:27:38 -05:00
|
|
|
|
2006-03-07 01:01:59 -05:00
|
|
|
err=db_get_song_count(&perr,&song_count);
|
|
|
|
if(err != DB_E_SUCCESS) {
|
2006-03-06 02:48:53 -05:00
|
|
|
DPRINTF(E_FATAL,L_MISC,"Error getting song count: %s\n",perr);
|
|
|
|
}
|
|
|
|
|
2006-07-09 19:03:20 -04:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Serving %d songs. Startup complete in %d seconds\n",
|
2006-07-12 18:52:50 -04:00
|
|
|
song_count,end_time-start_time);
|
2004-03-08 16:27:38 -05:00
|
|
|
|
2006-10-27 16:39:39 -04:00
|
|
|
if(conf_get_int("general","rescan_interval",0) && (!reload) &&
|
|
|
|
(!conf_get_int("scanning","skip_first",0)))
|
2006-06-14 00:33:27 -04:00
|
|
|
config.reload = 1; /* force a reload on start */
|
2006-05-24 00:53:44 -04:00
|
|
|
|
2004-04-19 02:19:46 -04:00
|
|
|
while(!config.stop) {
|
2006-02-27 17:48:42 -05:00
|
|
|
if((conf_get_int("general","rescan_interval",0) &&
|
|
|
|
(rescan_counter > conf_get_int("general","rescan_interval",0)))) {
|
|
|
|
if((conf_get_int("general","always_scan",0)) ||
|
|
|
|
(config_get_session_count())) {
|
2005-11-07 00:58:05 -05:00
|
|
|
config.reload=1;
|
|
|
|
} else {
|
|
|
|
DPRINTF(E_DBG,L_MAIN|L_SCAN|L_DB,"Skipped bground scan... no users\n");
|
|
|
|
}
|
|
|
|
rescan_counter=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(config.reload) {
|
2006-01-04 15:30:44 -05:00
|
|
|
old_song_count = song_count;
|
2006-02-26 03:46:24 -05:00
|
|
|
start_time=(int) time(NULL);
|
2005-11-07 00:58:05 -05:00
|
|
|
|
|
|
|
DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Rescanning database\n");
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2006-04-12 01:18:55 -04:00
|
|
|
if(conf_get_array("general","mp3_dir",&mp3_dir_array)) {
|
2006-06-15 03:10:05 -04:00
|
|
|
if(config.full_reload) {
|
|
|
|
config.full_reload=0;
|
|
|
|
db_force_rescan(NULL);
|
|
|
|
}
|
|
|
|
|
2006-04-12 01:18:55 -04:00
|
|
|
if(scan_init(mp3_dir_array)) {
|
|
|
|
DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Error rescanning... bad path?\n");
|
|
|
|
}
|
|
|
|
conf_dispose_array(mp3_dir_array);
|
2005-11-07 00:58:05 -05:00
|
|
|
}
|
|
|
|
config.reload=0;
|
2006-01-04 15:30:44 -05:00
|
|
|
db_get_song_count(NULL,&song_count);
|
2006-08-16 22:25:41 -04:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Scanned %d songs (was %d) in "
|
2006-01-04 15:30:44 -05:00
|
|
|
"%d seconds\n",song_count,old_song_count,
|
|
|
|
time(NULL)-start_time);
|
2005-11-07 00:58:05 -05:00
|
|
|
}
|
|
|
|
|
2007-01-09 13:12:39 -05:00
|
|
|
os_wait(MAIN_SLEEP_INTERVAL);
|
2005-11-07 00:58:05 -05:00
|
|
|
rescan_counter += MAIN_SLEEP_INTERVAL;
|
2004-04-19 02:19:46 -04:00
|
|
|
}
|
2003-10-23 17:43:01 -04:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Stopping gracefully\n");
|
2004-04-13 00:23:36 -04:00
|
|
|
|
2004-10-30 12:42:20 -04:00
|
|
|
#ifndef WITHOUT_MDNS
|
2003-11-26 01:10:58 -05:00
|
|
|
if(config.use_mdns) {
|
2005-11-07 00:58:05 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_REND,"Stopping rendezvous daemon\n");
|
|
|
|
rend_stop();
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
2004-10-30 12:42:20 -04:00
|
|
|
#endif
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2007-05-02 23:30:30 -04:00
|
|
|
#ifdef UPNP
|
|
|
|
upnp_deinit();
|
|
|
|
#endif
|
|
|
|
|
2004-11-14 01:44:20 -05:00
|
|
|
|
2005-01-07 00:37:46 -05:00
|
|
|
/* Got to find a cleaner way to stop the web server.
|
|
|
|
* Closing the fd of the socking accepting doesn't necessarily
|
|
|
|
* cause the accept to fail on some libcs.
|
|
|
|
*
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_WS,"Stopping web server\n");
|
2005-11-07 00:58:05 -05:00
|
|
|
ws_stop(config.server);
|
2005-01-07 00:37:46 -05:00
|
|
|
*/
|
2006-03-25 05:52:10 -05:00
|
|
|
free(web_root);
|
2006-02-27 17:48:42 -05:00
|
|
|
conf_close();
|
2003-11-26 01:10:58 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_DB,"Closing database\n");
|
2003-11-26 01:10:58 -05:00
|
|
|
db_deinit();
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Done!\n");
|
2004-04-13 00:23:36 -04:00
|
|
|
|
2006-02-26 03:46:24 -05:00
|
|
|
os_deinit();
|
2007-07-31 00:34:33 -04:00
|
|
|
io_deinit();
|
2007-01-16 20:06:16 -05:00
|
|
|
mem_dump();
|
2003-10-13 11:03:14 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|