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
|
|
|
|
*/
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2009-06-05 09:25:43 -04:00
|
|
|
# include <config.h>
|
2003-10-19 16:03:54 -04:00
|
|
|
#endif
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-06-05 09:25:43 -04:00
|
|
|
#include <unistd.h>
|
2003-10-19 16:03:54 -04:00
|
|
|
#include <string.h>
|
2009-06-05 09:25:43 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2003-10-19 16:03:54 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2004-02-14 19:51:11 -05:00
|
|
|
#include <sys/wait.h>
|
2009-06-05 09:25:43 -04:00
|
|
|
#include <signal.h>
|
|
|
|
#include <limits.h>
|
2009-04-08 13:22:55 -04:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
#include <sys/signalfd.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <event.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
2009-04-18 11:30:14 -04:00
|
|
|
#include "conffile.h"
|
2009-05-08 11:46:32 -04:00
|
|
|
#include "logger.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"
|
2009-04-07 13:32:47 -04:00
|
|
|
#include "mdns_avahi.h"
|
|
|
|
|
2009-04-07 06:50:46 -04:00
|
|
|
|
2009-04-08 13:22:55 -04:00
|
|
|
#define PIDFILE "/var/run/mt-daapd.pid"
|
|
|
|
|
2009-04-07 11:37:12 -04:00
|
|
|
struct event_base *evbase_main;
|
2009-05-03 07:32:00 -04:00
|
|
|
static int main_exit;
|
|
|
|
|
2009-04-07 11:37:12 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
static void
|
|
|
|
usage(char *program)
|
|
|
|
{
|
|
|
|
printf("Usage: %s [options]\n\n",program);
|
|
|
|
printf("Options:\n");
|
|
|
|
printf(" -d <number> Log level (0-5)\n");
|
|
|
|
printf(" -D <dom,dom..> Log domains\n");
|
|
|
|
printf(" -c <file> Use <file> as the configfile\n");
|
|
|
|
printf(" -P <file> Write PID to specified file\n");
|
|
|
|
printf(" -f Run in foreground\n");
|
|
|
|
printf(" -y Start even if user is not root\n");
|
|
|
|
printf(" -b <id> ffid to be broadcast\n");
|
|
|
|
printf(" -v Display version information\n");
|
|
|
|
printf("\n\n");
|
|
|
|
printf("Available log domains:\n");
|
|
|
|
logger_domains();
|
|
|
|
printf("\n\n");
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
|
|
|
|
2009-04-08 13:22:55 -04:00
|
|
|
static int
|
2009-05-05 11:54:20 -04:00
|
|
|
daemonize(int background, char *runas, char *pidfile)
|
2009-04-08 13:22:55 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2009-05-05 11:54:20 -04:00
|
|
|
if (background)
|
2009-04-08 13:22:55 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-05-08 11:46:32 -04:00
|
|
|
logger_detach();
|
|
|
|
|
2009-04-08 13:22:55 -04:00
|
|
|
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;
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
sa_ign.sa_handler = SIG_IGN;
|
|
|
|
sa_ign.sa_flags = 0;
|
2009-04-08 07:45:57 -04:00
|
|
|
sigemptyset(&sa_ign.sa_mask);
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
sa_dfl.sa_handler = SIG_DFL;
|
|
|
|
sa_dfl.sa_flags = 0;
|
2009-04-08 07:45:57 -04:00
|
|
|
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");
|
|
|
|
|
2009-05-03 07:32:00 -04:00
|
|
|
main_exit = 1;
|
2009-04-08 07:45:57 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGHUP:
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Got SIGHUP\n");
|
|
|
|
|
2009-05-03 07:32:00 -04:00
|
|
|
if (!main_exit)
|
2009-05-08 11:46:32 -04:00
|
|
|
logger_reinit();
|
2009-04-08 07:45:57 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-03 07:32:00 -04:00
|
|
|
if (main_exit)
|
2009-04-08 07:45:57 -04:00
|
|
|
event_base_loopbreak(evbase_main);
|
|
|
|
}
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int option;
|
|
|
|
char *configfile = CONFFILE;
|
|
|
|
int reload = 0;
|
|
|
|
int background;
|
|
|
|
int force_non_root = 0;
|
|
|
|
int skip_initial = 1;
|
|
|
|
cfg_t *lib;
|
|
|
|
char *runas, *tmp;
|
|
|
|
int port;
|
|
|
|
char *servername;
|
|
|
|
char *ffid = NULL;
|
|
|
|
char *perr = NULL;
|
|
|
|
char *txtrecord[10];
|
|
|
|
char *pidfile = PIDFILE;
|
|
|
|
sigset_t sigs;
|
|
|
|
int sigfd;
|
|
|
|
struct event sig_event;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
int loglevel = -1;
|
|
|
|
char *logdomains = NULL;
|
|
|
|
char *logfile = NULL;
|
|
|
|
|
|
|
|
background = 1;
|
|
|
|
|
|
|
|
while ((option = getopt(argc, argv, "D:d:c:P:frysiub:v")) != -1)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'b':
|
|
|
|
ffid = optarg;
|
2006-07-15 01:56:32 -04:00
|
|
|
break;
|
2006-08-24 23:37:03 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
case 'd':
|
|
|
|
ret = safe_atoi(optarg, &i);
|
|
|
|
if (ret < 0)
|
|
|
|
fprintf(stderr, "Error: loglevel must be an integer in '-d %s'\n", optarg);
|
|
|
|
else
|
|
|
|
loglevel = i;
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
2006-08-24 23:37:03 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
case 'D':
|
2009-05-08 11:46:32 -04:00
|
|
|
logdomains = optarg;
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
2006-08-24 23:37:03 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
case 'f':
|
2009-05-05 11:54:20 -04:00
|
|
|
background = 0;
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
case 'c':
|
|
|
|
configfile = optarg;
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
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
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
case 'r':
|
|
|
|
reload = 1;
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
case 's':
|
|
|
|
skip_initial = 0;
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
case 'y':
|
|
|
|
force_non_root = 1;
|
2005-11-07 00:58:05 -05:00
|
|
|
break;
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
case 'v':
|
|
|
|
fprintf(stdout, "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;
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
default:
|
2005-11-07 00:58:05 -05:00
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
if ((getuid() != 0) && (!force_non_root))
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
exit(EXIT_FAILURE);
|
2004-12-06 19:24:39 -05:00
|
|
|
}
|
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
ret = logger_init(NULL, NULL, (loglevel < 0) ? E_LOG : loglevel);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Could not initialize log facility\n");
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
2006-02-27 17:48:42 -05:00
|
|
|
}
|
2005-03-19 23:13:34 -05:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
ret = conffile_load(configfile);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "Config file errors; please fix your config\n");
|
|
|
|
|
|
|
|
logger_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
2004-03-08 15:36:07 -05:00
|
|
|
}
|
2007-09-07 02:55:25 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
logger_deinit();
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
/* Reinit log facility with configfile values */
|
|
|
|
if (loglevel < 0)
|
|
|
|
loglevel = cfg_getint(cfg_getsec(cfg, "general"), "loglevel");
|
2003-11-26 01:10:58 -05:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
logfile = cfg_getstr(cfg_getsec(cfg, "general"), "logfile");
|
2009-05-04 11:51:42 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
ret = logger_init(logfile, logdomains, loglevel);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Could not reinitialize log facility with config file settings\n");
|
2004-04-13 00:23:36 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
conffile_unload();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2009-05-08 11:46:32 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
/* Set up libevent logging callback */
|
|
|
|
event_set_log_callback(logger_libevent);
|
2003-10-13 11:03:14 -04:00
|
|
|
|
2009-06-05 09:25:43 -04:00
|
|
|
DPRINTF(E_LOG, L_MAIN, "Firefly Version %s taking off\n", VERSION);
|
|
|
|
|
|
|
|
/* initialize ffmpeg */
|
|
|
|
av_register_all();
|
|
|
|
|
|
|
|
/* 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");
|
|
|
|
|
|
|
|
conffile_unload();
|
|
|
|
logger_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Daemonize and drop privileges */
|
|
|
|
runas = cfg_getstr(cfg_getsec(cfg, "general"), "uid");
|
|
|
|
|
|
|
|
ret = daemonize(background, runas, pidfile);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Could not initialize server\n");
|
|
|
|
|
|
|
|
conffile_unload();
|
|
|
|
logger_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize libevent (after forking) */
|
|
|
|
evbase_main = event_init();
|
|
|
|
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "mDNS init\n");
|
|
|
|
ret = mdns_init();
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "mDNS init failed\n");
|
|
|
|
|
|
|
|
conffile_unload();
|
|
|
|
logger_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this will require that the db be readable by the runas user */
|
|
|
|
ret = db_open(&perr, "sqlite3", "/var/cache/mt-daapd"); /* FIXME */
|
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "Error opening db: %s\n", perr);
|
|
|
|
|
|
|
|
mdns_deinit();
|
|
|
|
conffile_unload();
|
|
|
|
logger_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the database before starting */
|
|
|
|
DPRINTF(E_INFO, L_MAIN, "Initializing database\n");
|
|
|
|
if (db_init(reload))
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "Error in db_init: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Spawn file scanner thread */
|
|
|
|
ret = filescanner_init();
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "File scanner thread failed to start\n");
|
|
|
|
|
|
|
|
mdns_deinit();
|
|
|
|
db_deinit();
|
|
|
|
conffile_unload();
|
|
|
|
logger_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Spawn HTTPd thread */
|
|
|
|
ret = httpd_init();
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_FATAL, L_MAIN, "HTTPd thread failed to start\n");
|
|
|
|
|
|
|
|
filescanner_deinit();
|
|
|
|
mdns_deinit();
|
|
|
|
db_deinit();
|
|
|
|
conffile_unload();
|
|
|
|
logger_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register mDNS services */
|
|
|
|
lib = cfg_getnsec(cfg, "library", 0);
|
|
|
|
|
|
|
|
servername = cfg_getstr(lib, "name");
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
httpd_deinit();
|
|
|
|
filescanner_deinit();
|
|
|
|
mdns_deinit();
|
|
|
|
db_deinit();
|
|
|
|
conffile_unload();
|
|
|
|
logger_deinit();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(txtrecord[i], 0, 128);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(txtrecord[0], 128, "txtvers=1");
|
|
|
|
snprintf(txtrecord[1], 128, "Database ID=%0X", djb_hash(servername, strlen(servername)));
|
|
|
|
snprintf(txtrecord[2], 128, "Machine ID=%0X", djb_hash(servername, strlen(servername)));
|
|
|
|
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 */
|
|
|
|
|
|
|
|
tmp = cfg_getstr(lib, "password");
|
|
|
|
snprintf(txtrecord[7], 128, "Password=%s", (tmp) ? "true" : "false");
|
|
|
|
|
|
|
|
srand((unsigned int)time(NULL));
|
|
|
|
|
|
|
|
if (ffid)
|
|
|
|
snprintf(txtrecord[8], 128, "ffid=%s", ffid);
|
|
|
|
else
|
|
|
|
snprintf(txtrecord[8], 128, "ffid=%08x", rand());
|
|
|
|
|
|
|
|
txtrecord[9] = NULL;
|
|
|
|
|
|
|
|
DPRINTF(E_LOG,L_MAIN,"Registering rendezvous names\n");
|
|
|
|
|
|
|
|
port = cfg_getint(lib, "port");
|
|
|
|
|
|
|
|
/* Register web server service */
|
|
|
|
mdns_register(servername, "_http._tcp", port, txtrecord);
|
|
|
|
/* Register RSP service */
|
|
|
|
mdns_register(servername, "_rsp._tcp", port, txtrecord);
|
|
|
|
/* Register DAAP service */
|
|
|
|
mdns_register(servername, "_daap._tcp", port, txtrecord);
|
|
|
|
|
|
|
|
for (i = 0; i < (sizeof(txtrecord) / sizeof(*txtrecord) - 1); i++)
|
|
|
|
free(txtrecord[i]);
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
|
|
|
httpd_deinit();
|
|
|
|
filescanner_deinit();
|
|
|
|
mdns_deinit();
|
|
|
|
db_deinit();
|
|
|
|
conffile_unload();
|
|
|
|
logger_deinit();
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* Run the loop */
|
|
|
|
event_base_dispatch(evbase_main);
|
|
|
|
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Stopping gracefully\n");
|
|
|
|
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "HTTPd deinit\n");
|
|
|
|
httpd_deinit();
|
|
|
|
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "File scanner deinit\n");
|
|
|
|
filescanner_deinit();
|
|
|
|
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "mDNS deinit\n");
|
|
|
|
mdns_deinit();
|
|
|
|
|
|
|
|
conffile_unload();
|
|
|
|
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Closing database\n");
|
|
|
|
db_deinit();
|
|
|
|
|
|
|
|
if (background)
|
|
|
|
{
|
|
|
|
ret = unlink(pidfile);
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_WARN, L_MAIN, "Could not unlink PID file %s: %s\n", pidfile, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_LOG, L_MAIN, "Exiting.\n");
|
|
|
|
|
|
|
|
logger_deinit();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|