2003-10-13 11:03:14 -04:00
|
|
|
/*
|
2009-04-08 07:45:57 -04:00
|
|
|
* Copyright (C) 2009 Julien BLACHE <jb@jblache.org>
|
2003-10-13 11:03:14 -04:00
|
|
|
*
|
2009-04-08 07:45:57 -04:00
|
|
|
* Pieces from mt-daapd:
|
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
|
2009-04-08 07:45:57 -04:00
|
|
|
#include <sys/signalfd.h>
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2009-04-08 13:22:55 -04:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
2007-01-16 20:06:16 -05:00
|
|
|
#include "daapd.h"
|
|
|
|
|
2009-04-18 11:30:14 -04:00
|
|
|
#include "conffile.h"
|
2006-02-27 17:48:42 -05:00
|
|
|
#include "conf.h"
|
2003-10-13 11:03:14 -04:00
|
|
|
#include "err.h"
|
2009-04-30 08:51:36 -04:00
|
|
|
#include "misc.h"
|
2009-04-19 13:29:44 -04:00
|
|
|
#include "filescanner.h"
|
2009-04-22 15:25:53 -04:00
|
|
|
#include "httpd.h"
|
2005-03-11 01:39:40 -05:00
|
|
|
#include "db-generic.h"
|
2006-02-26 03:46:24 -05:00
|
|
|
|
2009-04-07 13:32:47 -04:00
|
|
|
#include "mdns_avahi.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
|
|
|
|
2009-04-07 06:50:46 -04:00
|
|
|
#include <event.h>
|
2009-04-05 07:13:09 -04:00
|
|
|
#include <libavformat/avformat.h>
|
2004-10-30 12:42:20 -04:00
|
|
|
|
2009-04-07 06:50:46 -04:00
|
|
|
|
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
|
|
|
|
|
2009-04-08 13:22:55 -04:00
|
|
|
#ifndef PIDFILE
|
|
|
|
#define PIDFILE "/var/run/mt-daapd.pid"
|
|
|
|
#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
|
|
|
|
2009-04-07 11:37:12 -04:00
|
|
|
struct event_base *evbase_main;
|
|
|
|
|
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);
|
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");
|
2009-04-02 07:24:09 -04:00
|
|
|
printf(" -d <number> Debug level (0-9)\n");
|
2004-11-13 03:05:27 -05:00
|
|
|
printf(" -D <mod,mod..> Debug modules\n");
|
2005-01-24 02:42:42 -05:00
|
|
|
printf(" -c <file> Use configfile specified\n");
|
2009-04-02 07:24:09 -04:00
|
|
|
printf(" -P <file> Write the PID to 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");
|
2009-04-18 11:27:17 -04:00
|
|
|
printf(" -v Display version information\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
|
|
|
|
2009-04-08 13:22:55 -04:00
|
|
|
static int
|
|
|
|
daemonize(int foreground, char *runas, char *pidfile)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int fd;
|
|
|
|
pid_t childpid;
|
|
|
|
pid_t ret;
|
|
|
|
int iret;
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
if (geteuid() == (uid_t) 0)
|
|
|
|
{
|
|
|
|
pw = getpwnam(runas);
|
|
|
|
|
|
|
|
if (!pw)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "Could not lookup user %s: %s\n", runas, strerror(errno));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pw = NULL;
|
|
|
|
|
|
|
|
if (!foreground)
|
|
|
|
{
|
|
|
|
fp = fopen(pidfile, "w");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Error opening pidfile (%s): %s\n", pidfile, strerror(errno));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open("/dev/null", O_RDWR, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Error opening /dev/null: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
signal(SIGTTOU, SIG_IGN);
|
|
|
|
signal(SIGTTIN, SIG_IGN);
|
|
|
|
signal(SIGTSTP, SIG_IGN);
|
|
|
|
|
|
|
|
childpid = fork();
|
|
|
|
|
|
|
|
if (childpid > 0)
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
else if (childpid < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "Fork failed: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = setsid();
|
|
|
|
if (ret == (pid_t) -1)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "setsid() failed: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
|
|
|
|
if (fd > 2)
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
chdir("/");
|
|
|
|
umask(0);
|
|
|
|
|
|
|
|
fprintf(fp, "%d\n", getpid());
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_MAIN, "PID: %d\n", getpid());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pw)
|
|
|
|
{
|
|
|
|
iret = initgroups(runas, pw->pw_gid);
|
|
|
|
if (iret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "initgroups() failed: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iret = setgid(pw->pw_gid);
|
|
|
|
if (iret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "setgid() failed: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iret = setuid(pw->pw_uid);
|
|
|
|
if (iret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "setuid() failed: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-08 07:45:57 -04:00
|
|
|
static void
|
|
|
|
signal_cb(int fd, short event, void *arg)
|
|
|
|
{
|
|
|
|
struct sigaction sa_ign;
|
|
|
|
struct sigaction sa_dfl;
|
|
|
|
struct signalfd_siginfo info;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
sa_ign.sa_handler=SIG_IGN;
|
|
|
|
sa_ign.sa_flags=0;
|
|
|
|
sigemptyset(&sa_ign.sa_mask);
|
|
|
|
|
|
|
|
sa_dfl.sa_handler=SIG_DFL;
|
|
|
|
sa_dfl.sa_flags=0;
|
|
|
|
sigemptyset(&sa_dfl.sa_mask);
|
|
|
|
|
|
|
|
while (read(fd, &info, sizeof(struct signalfd_siginfo)) > 0)
|
|
|
|
{
|
|
|
|
switch (info.ssi_signo)
|
|
|
|
{
|
|
|
|
case SIGCHLD:
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Got SIGCHLD, reaping children\n");
|
|
|
|
|
|
|
|
while (wait3(&status, WNOHANG, NULL) > 0)
|
|
|
|
/* Nothing. */ ;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Got SIGTERM or SIGINT\n");
|
|
|
|
|
|
|
|
config.stop = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGHUP:
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Got SIGHUP\n");
|
|
|
|
|
|
|
|
if (!config.stop)
|
|
|
|
{
|
|
|
|
conf_reload();
|
|
|
|
err_reopen();
|
|
|
|
|
|
|
|
config.reload = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.stop)
|
|
|
|
event_base_loopbreak(evbase_main);
|
|
|
|
}
|
|
|
|
|
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;
|
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;
|
2009-04-07 06:50:46 -04:00
|
|
|
int 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;
|
2009-05-03 04:46:13 -04:00
|
|
|
char *db_type,*db_parms,*runas, *tmp;
|
|
|
|
int port;
|
2009-04-07 13:32:47 -04:00
|
|
|
char *servername;
|
2006-07-15 01:56:32 -04:00
|
|
|
char *ffid = NULL;
|
2007-04-04 16:34:29 -04:00
|
|
|
char *perr=NULL;
|
2009-04-08 10:35:59 -04:00
|
|
|
char *txtrecord[10];
|
2009-04-08 13:22:55 -04:00
|
|
|
char *pidfile = PIDFILE;
|
2009-04-08 07:45:57 -04:00
|
|
|
sigset_t sigs;
|
|
|
|
int sigfd;
|
|
|
|
struct event sig_event;
|
2009-04-07 13:32:47 -04:00
|
|
|
int ret;
|
2009-04-08 10:35:59 -04:00
|
|
|
int i;
|
2006-04-19 04:32:18 -04:00
|
|
|
|
2006-01-05 19:05:02 -05:00
|
|
|
int err;
|
2004-12-09 00:07:09 -05:00
|
|
|
|
2006-06-19 00:25:08 -04:00
|
|
|
int debuglevel=0;
|
|
|
|
|
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;
|
2009-04-18 11:27:17 -04:00
|
|
|
while((option=getopt(argc,argv,"D:d:c:P:frysiub:v")) != -1) {
|
2005-11-07 00:58:05 -05:00
|
|
|
switch(option) {
|
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 'P':
|
2009-04-08 13:22:55 -04:00
|
|
|
pidfile = optarg;
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
2009-04-02 05:04:33 -04:00
|
|
|
|
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;
|
|
|
|
|
2009-04-18 11:27:17 -04:00
|
|
|
case 'v':
|
2007-01-09 13:12:39 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-04-18 11:22:12 -04:00
|
|
|
if((getuid()) && (!force_non_root)) {
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-04-18 11:30:14 -04:00
|
|
|
ret = conffile_load(configfile);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Config file errors; please fix your config\n");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
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-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
|
|
|
|
2009-04-05 07:13:09 -04:00
|
|
|
/* initialize ffmpeg */
|
|
|
|
av_register_all();
|
2007-04-13 17:37:42 -04:00
|
|
|
|
2009-04-08 07:45:57 -04:00
|
|
|
/* Block signals for all threads except the main one */
|
|
|
|
sigemptyset(&sigs);
|
|
|
|
sigaddset(&sigs, SIGINT);
|
|
|
|
sigaddset(&sigs, SIGHUP);
|
|
|
|
sigaddset(&sigs, SIGCHLD);
|
|
|
|
sigaddset(&sigs, SIGTERM);
|
|
|
|
sigaddset(&sigs, SIGPIPE);
|
|
|
|
ret = pthread_sigmask(SIG_BLOCK, &sigs, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Error setting signal set\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2009-04-08 13:22:55 -04:00
|
|
|
/* Daemonize and drop privileges */
|
|
|
|
runas = conf_alloc_string("general", "runas", "nobody");
|
2006-03-25 05:52:10 -05:00
|
|
|
|
2009-04-08 13:22:55 -04:00
|
|
|
ret = daemonize(config.foreground, runas, pidfile);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Could not initialize server\n");
|
2009-04-08 08:00:40 -04:00
|
|
|
|
2009-04-08 13:22:55 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(runas);
|
2009-04-02 07:22:23 -04:00
|
|
|
|
2009-04-07 06:50:46 -04:00
|
|
|
/* Initialize libevent (after forking) */
|
2009-04-07 11:37:12 -04:00
|
|
|
evbase_main = event_init();
|
2009-04-07 06:50:46 -04:00
|
|
|
|
2009-04-07 13:32:47 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "mDNS init\n");
|
|
|
|
ret = mdns_init();
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN | L_REND, "mDNS init failed\n");
|
2004-04-13 00:23:36 -04:00
|
|
|
|
2009-04-07 13:32:47 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2006-03-25 05:52:10 -05:00
|
|
|
|
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);
|
2009-04-07 13:32:47 -04:00
|
|
|
|
|
|
|
mdns_deinit();
|
2006-04-10 00:27:52 -04:00
|
|
|
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-09-07 02:55:25 -04: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;
|
2007-09-07 02:55:25 -04:00
|
|
|
|
2009-04-19 13:29:44 -04:00
|
|
|
/* Spawn file scanner thread */
|
|
|
|
ret = filescanner_init();
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "File scanner thread failed to start\n");
|
2006-02-27 17:48:42 -05:00
|
|
|
|
2009-04-19 13:29:44 -04:00
|
|
|
mdns_deinit();
|
|
|
|
db_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2003-11-04 01:11:00 -05:00
|
|
|
|
2009-04-22 15:25:53 -04:00
|
|
|
/* Spawn HTTPd thread */
|
|
|
|
ret = httpd_init();
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "HTTPd thread failed to start\n");
|
|
|
|
|
|
|
|
mdns_deinit();
|
|
|
|
db_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2009-04-07 13:32:47 -04:00
|
|
|
/* Register mDNS services */
|
|
|
|
servername = conf_get_servername();
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2009-04-08 10:35:59 -04:00
|
|
|
for (i = 0; i < (sizeof(txtrecord) / sizeof(*txtrecord) - 1); i++)
|
|
|
|
{
|
|
|
|
txtrecord[i] = (char *)malloc(128);
|
|
|
|
if (!txtrecord[i])
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "Out of memory for TXT record\n");
|
|
|
|
|
2009-04-22 15:25:53 -04:00
|
|
|
httpd_deinit();
|
2009-04-19 13:29:44 -04:00
|
|
|
filescanner_deinit();
|
2009-04-08 10:35:59 -04:00
|
|
|
mdns_deinit();
|
2009-04-19 13:32:22 -04:00
|
|
|
db_deinit();
|
2009-04-08 10:35:59 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(txtrecord[i], 0, 128);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(txtrecord[0], 128, "txtvers=1");
|
2009-04-30 08:51:36 -04:00
|
|
|
snprintf(txtrecord[1], 128, "Database ID=%0X", djb_hash(servername, strlen(servername)));
|
|
|
|
snprintf(txtrecord[2], 128, "Machine ID=%0X", djb_hash(servername, strlen(servername)));
|
2009-04-08 10:35:59 -04:00
|
|
|
snprintf(txtrecord[3], 128, "Machine Name=%s", servername);
|
|
|
|
snprintf(txtrecord[4], 128, "mtd-version=%s", VERSION);
|
|
|
|
snprintf(txtrecord[5], 128, "iTSh Version=131073"); /* iTunes 6.0.4 */
|
|
|
|
snprintf(txtrecord[6], 128, "Version=196610"); /* iTunes 6.0.4 */
|
2006-06-19 00:29:27 -04:00
|
|
|
|
2009-04-08 04:01:41 -04:00
|
|
|
tmp = conf_alloc_string("general", "password", NULL);
|
2009-04-08 10:35:59 -04:00
|
|
|
snprintf(txtrecord[7], 128, "Password=%s", (tmp && (strlen(tmp) > 0)) ? "true" : "false");
|
2009-04-08 04:01:41 -04:00
|
|
|
if (tmp)
|
|
|
|
free(tmp);
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2009-04-07 13:32:47 -04:00
|
|
|
srand((unsigned int)time(NULL));
|
2006-09-24 23:20:22 -04:00
|
|
|
|
2009-04-08 10:35:59 -04:00
|
|
|
if (ffid)
|
|
|
|
snprintf(txtrecord[8], 128, "ffid=%s", ffid);
|
|
|
|
else
|
|
|
|
snprintf(txtrecord[8], 128, "ffid=%08x", rand());
|
|
|
|
|
|
|
|
txtrecord[9] = NULL;
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2009-04-07 13:32:47 -04:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_REND,"Registering rendezvous names\n");
|
2009-05-03 04:46:13 -04:00
|
|
|
|
|
|
|
port = conf_get_int("general", "port", 0);
|
|
|
|
|
2009-05-03 04:49:33 -04:00
|
|
|
/* Register web server service */
|
2009-05-03 04:46:13 -04:00
|
|
|
mdns_register(servername, "_http._tcp", port, txtrecord);
|
2009-05-03 04:49:33 -04:00
|
|
|
/* Register RSP service */
|
|
|
|
mdns_register(servername, "_rsp._tcp", port, txtrecord);
|
|
|
|
/* Register DAAP service */
|
|
|
|
mdns_register(servername, "_daap._tcp", port, txtrecord);
|
2006-11-11 16:47:43 -05:00
|
|
|
|
2009-04-08 10:35:59 -04:00
|
|
|
for (i = 0; i < (sizeof(txtrecord) / sizeof(*txtrecord) - 1); i++)
|
|
|
|
free(txtrecord[i]);
|
|
|
|
|
2009-04-07 13:32:47 -04:00
|
|
|
free(servername);
|
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
|
|
|
|
2009-04-08 07:45:57 -04:00
|
|
|
/* Set up signal fd */
|
|
|
|
sigfd = signalfd(-1, &sigs, SFD_NONBLOCK | SFD_CLOEXEC);
|
|
|
|
if (sigfd < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "Could not setup signalfd: %s\n", strerror(errno));
|
|
|
|
|
2009-04-22 15:25:53 -04:00
|
|
|
httpd_deinit();
|
2009-04-19 13:29:44 -04:00
|
|
|
filescanner_deinit();
|
2009-04-08 07:45:57 -04:00
|
|
|
mdns_deinit();
|
2009-04-19 13:32:22 -04:00
|
|
|
db_deinit();
|
2009-04-08 07:45:57 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
event_set(&sig_event, sigfd, EV_READ, signal_cb, NULL);
|
|
|
|
event_base_set(evbase_main, &sig_event);
|
|
|
|
event_add(&sig_event, NULL);
|
|
|
|
|
2009-04-07 06:50:46 -04:00
|
|
|
/* Run the loop */
|
2009-04-07 11:37:12 -04:00
|
|
|
event_base_dispatch(evbase_main);
|
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
|
|
|
|
2009-04-22 15:25:53 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "HTTPd deinit\n");
|
|
|
|
httpd_deinit();
|
|
|
|
|
2009-04-19 13:29:44 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "File scanner deinit\n");
|
|
|
|
filescanner_deinit();
|
|
|
|
|
2009-04-07 13:32:47 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN | L_REND, "mDNS deinit\n");
|
|
|
|
mdns_deinit();
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2009-04-18 11:30:14 -04:00
|
|
|
conffile_unload();
|
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
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|