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
|
|
|
/**
|
|
|
|
* \file main.c
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* this file.
|
|
|
|
*
|
|
|
|
* \todo Refactor daap_handler()
|
|
|
|
*/
|
|
|
|
|
2004-11-12 02:27:05 -05:00
|
|
|
/** \mainpage mt-daapd
|
2004-11-12 01:38:05 -05:00
|
|
|
* \section about_section About
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
* - <a href="http://sf.net/projects/mt-daapd">Project page on SourceForge</a>
|
|
|
|
* - <a href="http://mt-daapd.sf.net">Home page</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>
|
2004-02-25 11:13:37 -05:00
|
|
|
#include <grp.h>
|
2003-11-03 14:02:00 -05:00
|
|
|
#include <limits.h>
|
2003-12-01 10:27:40 -05:00
|
|
|
#include <pthread.h>
|
2003-12-29 18:39:18 -05:00
|
|
|
#include <pwd.h>
|
2004-02-14 19:51:11 -05:00
|
|
|
#include <restart.h>
|
2003-12-01 10:27:40 -05:00
|
|
|
#include <signal.h>
|
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>
|
2003-11-03 14:02:00 -05:00
|
|
|
#include <unistd.h>
|
2003-10-13 11:03:14 -04:00
|
|
|
|
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>
|
2003-10-19 16:03:54 -04:00
|
|
|
|
|
|
|
#include "configfile.h"
|
2003-11-04 01:11:00 -05:00
|
|
|
#include "db-memory.h"
|
2003-10-30 17:42:11 -05:00
|
|
|
#include "daap.h"
|
|
|
|
#include "daap-proto.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"
|
2003-12-05 00:59:19 -05:00
|
|
|
#include "playlist.h"
|
2005-02-21 03:10:37 -05:00
|
|
|
#include "ssc.h"
|
2004-04-14 02:17:22 -04:00
|
|
|
#include "dynamic-art.h"
|
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 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Where the default configfile is. On the NSLU2 running unslung,
|
|
|
|
* thats in /opt, not /etc. */
|
2004-06-14 15:01:06 -04:00
|
|
|
#ifndef DEFAULT_CONFIGFILE
|
2004-10-25 00:50:59 -04:00
|
|
|
#ifdef NSLU2
|
2004-11-12 01:38:05 -05:00
|
|
|
#define DEFAULT_CONFIGFILE "/opt/etc/mt-daapd/mt-daapd.conf"
|
2004-10-25 00:50:59 -04:00
|
|
|
#else
|
2003-12-29 17:09:15 -05:00
|
|
|
#define DEFAULT_CONFIGFILE "/etc/mt-daapd.conf"
|
2004-06-14 15:01:06 -04:00
|
|
|
#endif
|
2004-10-25 00:50:59 -04:00
|
|
|
#endif
|
2004-06-14 15:01:06 -04:00
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/** Where to dump the pidfile */
|
2004-06-14 15:01:06 -04:00
|
|
|
#ifndef PIDFILE
|
|
|
|
#define PIDFILE "/var/run/mt-daapd.pid"
|
|
|
|
#endif
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/** You say po-tay-to, I say po-tat-o */
|
2004-01-04 16:16:20 -05:00
|
|
|
#ifndef SIGCLD
|
|
|
|
# define SIGCLD SIGCHLD
|
|
|
|
#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
|
|
|
|
2003-12-29 17:09:15 -05:00
|
|
|
/*
|
|
|
|
* Forwards
|
|
|
|
*/
|
2004-11-12 01:38:05 -05:00
|
|
|
static int daemon_start(void);
|
|
|
|
static void write_pid_file(void);
|
|
|
|
static void usage(char *program);
|
|
|
|
static void *signal_handler(void *arg);
|
2004-11-14 01:44:20 -05:00
|
|
|
static int start_signal_handler(pthread_t *handler_tid);
|
2004-11-12 02:27:05 -05:00
|
|
|
static void daap_handler(WS_CONNINFO *pwsc);
|
|
|
|
static int daap_auth(char *username, char *password);
|
2004-11-12 01:38:05 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles authentication for the daap server. This isn't the
|
|
|
|
* authenticator for the web admin page, but rather the iTunes
|
|
|
|
* authentication when trying to connect to the server. Note that most
|
|
|
|
* of this is actually handled in the web server registration, which
|
|
|
|
* decides when to apply the authentication or not. If you mess with
|
|
|
|
* when and where the webserver applies auth or not, you'll likely
|
|
|
|
* break something. It seems that some requests must be authed, and others
|
|
|
|
* not. If you apply authentication somewhere that iTunes doesn't expect
|
|
|
|
* it, it happily disconnects.
|
2003-12-09 00:48:30 -05:00
|
|
|
*
|
2004-11-12 01:38:05 -05:00
|
|
|
* \param username The username passed by iTunes
|
|
|
|
* \param password The password passed by iTunes
|
|
|
|
* \returns 1 if auth successful, 0 otherwise
|
2003-12-09 00:48:30 -05:00
|
|
|
*/
|
|
|
|
int daap_auth(char *username, char *password) {
|
2004-01-27 00:30:25 -05:00
|
|
|
if((password == NULL) &&
|
|
|
|
((config.readpassword == NULL) || (strlen(config.readpassword)==0)))
|
2003-12-09 00:48:30 -05:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if(password == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return !strcasecmp(password,config.readpassword);
|
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* This handles requests that are daap-related. For example,
|
|
|
|
* /server-info, /login, etc. This should really be split up
|
|
|
|
* into multiple functions, and perhaps moved into daap.c
|
2003-10-30 17:42:11 -05:00
|
|
|
*
|
2004-11-12 01:38:05 -05:00
|
|
|
* \param pwsc Webserver connection info, passed from the webserver
|
2004-11-12 02:27:05 -05:00
|
|
|
*
|
|
|
|
* \todo Decomplexify this!
|
2003-10-30 17:42:11 -05:00
|
|
|
*/
|
|
|
|
void daap_handler(WS_CONNINFO *pwsc) {
|
|
|
|
int close;
|
2004-02-14 19:51:11 -05:00
|
|
|
DAAP_BLOCK *root;
|
2003-11-04 01:11:00 -05:00
|
|
|
int clientrev;
|
2003-10-30 17:42:11 -05:00
|
|
|
|
2003-11-11 21:59:45 -05:00
|
|
|
/* for the /databases URI */
|
|
|
|
char *uri;
|
2004-12-07 22:20:48 -05:00
|
|
|
unsigned long int db_index;
|
|
|
|
unsigned long int playlist_index;
|
|
|
|
unsigned long int item=0;
|
2003-11-11 21:59:45 -05:00
|
|
|
char *first, *last;
|
2004-05-21 09:56:04 -04:00
|
|
|
char* index = 0;
|
2003-11-11 21:59:45 -05:00
|
|
|
int streaming=0;
|
2004-12-29 00:44:32 -05:00
|
|
|
int compress =0;
|
|
|
|
int start_time;
|
|
|
|
int end_time;
|
|
|
|
int bytes_written;
|
2003-11-11 21:59:45 -05:00
|
|
|
|
|
|
|
MP3FILE *pmp3;
|
|
|
|
int file_fd;
|
2005-02-21 03:10:37 -05:00
|
|
|
FILE *file_ptr; /* for possible conv filter */
|
2003-11-13 23:56:20 -05:00
|
|
|
int session_id=0;
|
2003-11-11 21:59:45 -05:00
|
|
|
|
2004-04-14 02:17:22 -04:00
|
|
|
int img_fd;
|
2004-10-15 16:10:39 -04:00
|
|
|
struct stat sb;
|
|
|
|
long img_size;
|
2004-04-14 02:17:22 -04:00
|
|
|
|
2004-04-28 19:51:26 -04:00
|
|
|
off_t offset=0;
|
|
|
|
off_t real_len;
|
|
|
|
off_t file_len;
|
2003-12-01 01:55:05 -05:00
|
|
|
|
2005-02-21 03:10:37 -05:00
|
|
|
char *real_path;
|
|
|
|
|
2004-11-22 02:14:37 -05:00
|
|
|
int bytes_copied=0;
|
|
|
|
|
2004-12-30 23:57:33 -05:00
|
|
|
GZIP_STREAM *gz;
|
|
|
|
|
2003-10-30 17:42:11 -05:00
|
|
|
close=pwsc->close;
|
2003-11-11 21:59:45 -05:00
|
|
|
pwsc->close=1; /* in case we have any errors */
|
|
|
|
root=NULL;
|
2003-10-30 17:42:11 -05:00
|
|
|
|
|
|
|
ws_addresponseheader(pwsc,"Accept-Ranges","bytes");
|
2003-12-01 15:06:09 -05:00
|
|
|
ws_addresponseheader(pwsc,"DAAP-Server","mt-daapd/%s",VERSION);
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_addresponseheader(pwsc,"Content-Type","application/x-dmap-tagged");
|
|
|
|
|
2003-11-13 23:56:20 -05:00
|
|
|
if(ws_getvar(pwsc,"session-id")) {
|
|
|
|
session_id=atoi(ws_getvar(pwsc,"session-id"));
|
|
|
|
}
|
2003-10-30 17:42:11 -05:00
|
|
|
|
|
|
|
if(!strcasecmp(pwsc->uri,"/server-info")) {
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Sending server info");
|
2004-04-28 14:45:56 -04:00
|
|
|
root=daap_response_server_info(config.servername,
|
|
|
|
ws_getrequestheader(pwsc,"Client-DAAP-Version"));
|
2003-10-30 17:42:11 -05:00
|
|
|
} else if (!strcasecmp(pwsc->uri,"/content-codes")) {
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Sending content codes");
|
2003-10-30 17:42:11 -05:00
|
|
|
root=daap_response_content_codes();
|
|
|
|
} else if (!strcasecmp(pwsc->uri,"/login")) {
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Logging in");
|
2004-04-13 00:23:36 -04:00
|
|
|
root=daap_response_login(pwsc->hostname);
|
2003-10-30 17:42:11 -05:00
|
|
|
} else if (!strcasecmp(pwsc->uri,"/update")) {
|
2003-11-05 13:57:13 -05:00
|
|
|
if(!ws_getvar(pwsc,"delta")) { /* first check */
|
2003-11-04 01:11:00 -05:00
|
|
|
clientrev=db_version() - 1;
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Sending database");
|
2003-11-04 01:11:00 -05:00
|
|
|
} else {
|
2003-11-05 13:57:13 -05:00
|
|
|
clientrev=atoi(ws_getvar(pwsc,"delta"));
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Waiting for DB updates");
|
2003-11-04 01:11:00 -05:00
|
|
|
}
|
2003-11-26 01:10:58 -05:00
|
|
|
root=daap_response_update(pwsc->fd,clientrev);
|
2004-04-28 19:51:26 -04:00
|
|
|
if((ws_getvar(pwsc,"delta")) && (root==NULL)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_WS,"Client %s disconnected\n",pwsc->hostname);
|
2004-04-28 14:55:22 -04:00
|
|
|
config_set_status(pwsc,session_id,NULL);
|
|
|
|
pwsc->close=1;
|
|
|
|
return;
|
2004-04-13 00:23:36 -04:00
|
|
|
}
|
2003-10-30 17:42:11 -05:00
|
|
|
} else if (!strcasecmp(pwsc->uri,"/logout")) {
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,NULL);
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_returnerror(pwsc,204,"Logout Successful");
|
|
|
|
return;
|
2003-11-11 21:59:45 -05:00
|
|
|
} else if(strcmp(pwsc->uri,"/databases")==0) {
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Sending database info");
|
2003-11-17 11:38:44 -05:00
|
|
|
root=daap_response_dbinfo(config.servername);
|
2004-05-21 09:56:04 -04:00
|
|
|
if(0 != (index = ws_getvar(pwsc, "index")))
|
|
|
|
daap_handle_index(root, index);
|
2003-11-11 21:59:45 -05:00
|
|
|
} else if(strncmp(pwsc->uri,"/databases/",11) == 0) {
|
|
|
|
|
|
|
|
/* the /databases/ uri will either be:
|
|
|
|
*
|
|
|
|
* /databases/id/items, which returns items in a db
|
|
|
|
* /databases/id/containers, which returns a container
|
|
|
|
* /databases/id/containers/id/items, which returns playlist elements
|
|
|
|
* /databases/id/items/id.mp3, to spool an mp3
|
2004-11-13 15:58:30 -05:00
|
|
|
* /databases/id/browse/category
|
2003-11-11 21:59:45 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
uri = strdup(pwsc->uri);
|
|
|
|
first=(char*)&uri[11];
|
|
|
|
last=first;
|
|
|
|
while((*last) && (*last != '/')) {
|
|
|
|
last++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*last) {
|
|
|
|
*last='\0';
|
2004-12-07 22:20:48 -05:00
|
|
|
db_index=atoll(first);
|
2003-11-11 21:59:45 -05:00
|
|
|
|
|
|
|
last++;
|
|
|
|
|
|
|
|
if(strncasecmp(last,"items/",6)==0) {
|
|
|
|
/* streaming */
|
|
|
|
first=last+6;
|
|
|
|
while((*last) && (*last != '.'))
|
|
|
|
last++;
|
|
|
|
|
|
|
|
if(*last == '.') {
|
|
|
|
*last='\0';
|
2004-12-07 22:20:48 -05:00
|
|
|
item=atoll(first);
|
2003-11-11 21:59:45 -05:00
|
|
|
streaming=1;
|
2004-12-07 22:20:48 -05:00
|
|
|
DPRINTF(E_DBG,L_DAAP|L_WS,"Streaming request for id %lu\n",item);
|
2003-11-11 21:59:45 -05:00
|
|
|
}
|
|
|
|
free(uri);
|
|
|
|
} else if (strncasecmp(last,"items",5)==0) {
|
|
|
|
/* songlist */
|
|
|
|
free(uri);
|
2004-05-21 09:56:04 -04:00
|
|
|
// pass the meta field request for processing
|
2004-06-14 15:01:06 -04:00
|
|
|
// pass the query request for processing
|
|
|
|
root=daap_response_songlist(ws_getvar(pwsc,"meta"),
|
|
|
|
ws_getvar(pwsc,"query"));
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Sending songlist");
|
2003-11-11 21:59:45 -05:00
|
|
|
} else if (strncasecmp(last,"containers/",11)==0) {
|
|
|
|
/* playlist elements */
|
|
|
|
first=last + 11;
|
|
|
|
last=first;
|
|
|
|
while((*last) && (*last != '/')) {
|
|
|
|
last++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*last) {
|
|
|
|
*last='\0';
|
2004-12-07 22:20:48 -05:00
|
|
|
playlist_index=atoll(first);
|
2004-05-21 09:56:04 -04:00
|
|
|
// pass the meta list info for processing
|
|
|
|
root=daap_response_playlist_items(playlist_index,
|
2004-06-14 15:01:06 -04:00
|
|
|
ws_getvar(pwsc,"meta"),
|
|
|
|
ws_getvar(pwsc,"query"));
|
2003-11-11 21:59:45 -05:00
|
|
|
}
|
|
|
|
free(uri);
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Sending playlist info");
|
2003-11-11 21:59:45 -05:00
|
|
|
} else if (strncasecmp(last,"containers",10)==0) {
|
|
|
|
/* list of playlists */
|
|
|
|
free(uri);
|
2003-11-17 11:38:44 -05:00
|
|
|
root=daap_response_playlists(config.servername);
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Sending playlist info");
|
2004-06-14 15:01:06 -04:00
|
|
|
} else if (strncasecmp(last,"browse/",7)==0) {
|
|
|
|
config_set_status(pwsc,session_id,"Compiling browse info");
|
|
|
|
root = daap_response_browse(last + 7,
|
|
|
|
ws_getvar(pwsc, "filter"));
|
|
|
|
config_set_status(pwsc,session_id,"Sending browse info");
|
|
|
|
free(uri);
|
2003-11-11 21:59:45 -05:00
|
|
|
}
|
|
|
|
}
|
2004-05-21 09:56:04 -04:00
|
|
|
|
|
|
|
// prune the full list if an index range was specified
|
|
|
|
if(0 != (index = ws_getvar(pwsc, "index")))
|
|
|
|
daap_handle_index(root, index);
|
2003-10-30 17:42:11 -05:00
|
|
|
}
|
|
|
|
|
2003-11-11 21:59:45 -05:00
|
|
|
if((!root)&&(!streaming)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS|L_DAAP,"Bad request -- root=%x, streaming=%d\n",root,streaming);
|
2003-11-03 15:34:18 -05:00
|
|
|
ws_returnerror(pwsc,400,"Invalid Request");
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,NULL);
|
2003-10-30 17:42:11 -05:00
|
|
|
return;
|
2003-11-03 15:34:18 -05:00
|
|
|
}
|
2003-10-30 17:42:11 -05:00
|
|
|
|
|
|
|
pwsc->close=close;
|
|
|
|
|
2003-11-11 21:59:45 -05:00
|
|
|
if(!streaming) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Satisfying request\n");
|
2004-03-01 16:12:20 -05:00
|
|
|
|
2004-12-29 00:44:32 -05:00
|
|
|
if((config.compress) && ws_testrequestheader(pwsc,"Accept-Encoding","gzip") && root->reported_size >= 1000) {
|
2003-11-11 21:59:45 -05:00
|
|
|
compress=1;
|
2004-12-29 00:44:32 -05:00
|
|
|
}
|
2003-10-30 17:42:11 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS|L_DAAP,"Serializing\n");
|
2004-12-29 00:44:32 -05:00
|
|
|
start_time = time(NULL);
|
|
|
|
if (compress) {
|
|
|
|
DPRINTF(E_DBG,L_WS|L_DAAP,"Using compression: %s\n", pwsc->uri);
|
2004-12-30 23:57:33 -05:00
|
|
|
gz = gzip_alloc();
|
2004-12-29 00:44:32 -05:00
|
|
|
daap_serialize(root,pwsc->fd,gz);
|
|
|
|
gzip_compress(gz);
|
|
|
|
bytes_written = gz->bytes_out;
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
|
|
|
|
ws_addresponseheader(pwsc,"Content-Length","%d",bytes_written);
|
|
|
|
ws_addresponseheader(pwsc,"Content-Encoding","gzip");
|
|
|
|
DPRINTF(E_DBG,L_WS,"Emitting headers\n");
|
|
|
|
ws_emitheaders(pwsc);
|
|
|
|
if (gzip_close(gz,pwsc->fd) != bytes_written) {
|
|
|
|
DPRINTF(E_LOG,L_WS|L_DAAP,"Error compressing data\n");
|
|
|
|
}
|
|
|
|
DPRINTF(E_DBG,L_WS|L_DAAP,"Compression ratio: %f\n",((double) bytes_written)/(8.0 + root->reported_size))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bytes_written = root->reported_size + 8;
|
|
|
|
ws_addresponseheader(pwsc,"Content-Length","%d",bytes_written);
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
|
|
|
|
DPRINTF(E_DBG,L_WS,"Emitting headers\n");
|
|
|
|
ws_emitheaders(pwsc);
|
|
|
|
daap_serialize(root,pwsc->fd,NULL);
|
|
|
|
}
|
|
|
|
end_time = time(NULL);
|
|
|
|
DPRINTF(E_DBG,L_WS|L_DAAP,"Sent %d bytes in %d seconds\n",bytes_written,end_time-start_time);
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS|L_DAAP,"Done, freeing\n");
|
2003-12-03 16:31:34 -05:00
|
|
|
daap_free(root);
|
2003-11-11 21:59:45 -05:00
|
|
|
} else {
|
|
|
|
/* stream out the song */
|
|
|
|
pwsc->close=1;
|
2003-10-30 17:42:11 -05:00
|
|
|
|
2003-12-01 01:55:05 -05:00
|
|
|
if(ws_getrequestheader(pwsc,"range")) {
|
2004-04-28 19:51:26 -04:00
|
|
|
offset=(off_t)atol(ws_getrequestheader(pwsc,"range") + 6);
|
2003-12-01 01:55:05 -05:00
|
|
|
}
|
|
|
|
|
2003-11-11 21:59:45 -05:00
|
|
|
pmp3=db_find(item);
|
|
|
|
if(!pmp3) {
|
2004-12-07 22:20:48 -05:00
|
|
|
DPRINTF(E_LOG,L_DAAP|L_WS|L_DB,"Could not find requested item %lu\n",item);
|
2003-11-11 21:59:45 -05:00
|
|
|
ws_returnerror(pwsc,404,"File Not Found");
|
2005-02-21 03:10:37 -05:00
|
|
|
} else if ((real_path=server_side_convert_path(pmp3->path)) != NULL) {
|
|
|
|
// The file should be converted in the server side.
|
|
|
|
DPRINTF(E_WARN,L_WS,"Thread %d: Autoconvert file %s for client\n",
|
|
|
|
pwsc->threadno,real_path);
|
2005-02-26 19:41:55 -05:00
|
|
|
file_ptr = server_side_convert_open(real_path,
|
|
|
|
offset,
|
|
|
|
pmp3->song_length);
|
2005-02-21 03:10:37 -05:00
|
|
|
if (file_ptr) {
|
|
|
|
file_fd = fileno(file_ptr);
|
|
|
|
} else {
|
|
|
|
file_fd = -1;
|
|
|
|
}
|
|
|
|
if(file_fd == -1) {
|
|
|
|
if (file_ptr) {
|
|
|
|
server_side_convert_close(file_ptr);
|
|
|
|
}
|
|
|
|
pwsc->error=errno;
|
|
|
|
DPRINTF(E_WARN,L_WS,
|
|
|
|
"Thread %d: Error opening %s for conversion\n",
|
|
|
|
pwsc->threadno,real_path);
|
|
|
|
ws_returnerror(pwsc,404,"Not found");
|
|
|
|
config_set_status(pwsc,session_id,NULL);
|
|
|
|
db_dispose(pmp3);
|
|
|
|
free(pmp3);
|
|
|
|
free(real_path);
|
|
|
|
} else {
|
|
|
|
// DWB: fix content-type to correctly reflect data
|
|
|
|
// content type (dmap tagged) should only be used on
|
|
|
|
// dmap protocol requests, not the actually song data
|
|
|
|
if(pmp3->type)
|
|
|
|
ws_addresponseheader(pwsc,"Content-Type","audio/%s",
|
|
|
|
pmp3->type);
|
|
|
|
// Also content-length -heade would be nice, but since
|
|
|
|
// we don't really know it here, so let's leave it out.
|
|
|
|
ws_addresponseheader(pwsc,"Connection","Close");
|
|
|
|
|
|
|
|
if(!offset)
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
|
|
|
|
else {
|
|
|
|
// This is actually against the protocol, since
|
|
|
|
// range MUST be explicit according to HTTP-standard
|
|
|
|
// Seems to work at least with iTunes.
|
|
|
|
ws_addresponseheader(pwsc,
|
|
|
|
"Content-Range","bytes %ld-*/*",
|
|
|
|
(long)offset);
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 206 Partial Content\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_emitheaders(pwsc);
|
|
|
|
|
|
|
|
config_set_status(pwsc,session_id,
|
|
|
|
"Streaming file via convert filter '%s'",
|
|
|
|
pmp3->fname);
|
|
|
|
DPRINTF(E_LOG,L_WS,
|
|
|
|
"Session %d: Streaming file '%s' to %s (offset %ld)\n",
|
|
|
|
session_id,pmp3->fname, pwsc->hostname,(long)offset);
|
|
|
|
|
|
|
|
if(!offset)
|
|
|
|
config.stats.songs_served++; /* FIXME: remove stat races */
|
|
|
|
if((bytes_copied=copyfile(file_fd,pwsc->fd)) == -1) {
|
|
|
|
DPRINTF(E_INF,L_WS,
|
|
|
|
"Error copying converted file to remote... %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
} else {
|
|
|
|
DPRINTF(E_INF,L_WS,
|
|
|
|
"Finished streaming converted file to remote\n");
|
|
|
|
}
|
|
|
|
server_side_convert_close(file_ptr);
|
|
|
|
config_set_status(pwsc,session_id,NULL);
|
|
|
|
db_dispose(pmp3);
|
|
|
|
free(pmp3);
|
|
|
|
free(real_path);
|
|
|
|
}
|
2003-11-11 21:59:45 -05:00
|
|
|
} else {
|
|
|
|
/* got the file, let's open and serve it */
|
|
|
|
file_fd=r_open2(pmp3->path,O_RDONLY);
|
|
|
|
if(file_fd == -1) {
|
|
|
|
pwsc->error=errno;
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_WARN,L_WS,"Thread %d: Error opening %s: %s\n",
|
2003-11-11 21:59:45 -05:00
|
|
|
pwsc->threadno,pmp3->path,strerror(errno));
|
|
|
|
ws_returnerror(pwsc,404,"Not found");
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,NULL);
|
2004-11-13 15:58:30 -05:00
|
|
|
db_dispose(pmp3);
|
|
|
|
free(pmp3);
|
2003-11-11 21:59:45 -05:00
|
|
|
} else {
|
2004-04-28 19:51:26 -04:00
|
|
|
real_len=lseek(file_fd,0,SEEK_END);
|
2003-12-01 01:55:05 -05:00
|
|
|
lseek(file_fd,0,SEEK_SET);
|
2004-10-15 16:10:39 -04:00
|
|
|
|
|
|
|
/* Re-adjust content length for cover art */
|
|
|
|
if((config.artfilename) &&
|
|
|
|
((img_fd=da_get_image_fd(pmp3->path)) != -1)) {
|
|
|
|
fstat(img_fd, &sb);
|
|
|
|
img_size = sb.st_size;
|
|
|
|
|
|
|
|
if (strncasecmp(pmp3->type,"mp3",4) ==0) {
|
|
|
|
/*PENDING*/
|
|
|
|
} else if (strncasecmp(pmp3->type, "m4a", 4) == 0) {
|
|
|
|
real_len += img_size + 24;
|
|
|
|
|
|
|
|
if (offset > img_size + 24) {
|
|
|
|
offset -= img_size + 24;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-28 19:51:26 -04:00
|
|
|
file_len = real_len - offset;
|
2003-12-01 01:55:05 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Length of file (remaining) is %ld\n",
|
2003-12-01 01:55:05 -05:00
|
|
|
pwsc->threadno,(long)file_len);
|
2004-05-21 09:56:04 -04:00
|
|
|
|
|
|
|
// DWB: fix content-type to correctly reflect data
|
|
|
|
// content type (dmap tagged) should only be used on
|
|
|
|
// dmap protocol requests, not the actually song data
|
|
|
|
if(pmp3->type)
|
2004-06-14 15:01:06 -04:00
|
|
|
ws_addresponseheader(pwsc,"Content-Type","audio/%s",pmp3->type);
|
2004-05-21 09:56:04 -04:00
|
|
|
|
2003-12-01 01:55:05 -05:00
|
|
|
ws_addresponseheader(pwsc,"Content-Length","%ld",(long)file_len);
|
2003-11-11 21:59:45 -05:00
|
|
|
ws_addresponseheader(pwsc,"Connection","Close");
|
2003-12-01 01:55:05 -05:00
|
|
|
|
2004-05-21 09:56:04 -04:00
|
|
|
|
2004-04-28 19:51:26 -04:00
|
|
|
if(!offset)
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
|
|
|
|
else {
|
|
|
|
ws_addresponseheader(pwsc,"Content-Range","bytes %ld-%ld/%ld",
|
|
|
|
(long)offset,(long)real_len,
|
|
|
|
(long)real_len+1);
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 206 Partial Content\r\n");
|
|
|
|
}
|
|
|
|
|
2003-11-11 21:59:45 -05:00
|
|
|
ws_emitheaders(pwsc);
|
|
|
|
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,"Streaming file '%s'",pmp3->fname);
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_WS,"Session %d: Streaming file '%s' to %s (offset %d)\n",
|
2004-04-28 19:51:26 -04:00
|
|
|
session_id,pmp3->fname, pwsc->hostname,(long)offset);
|
2004-04-14 02:17:22 -04:00
|
|
|
|
|
|
|
if(!offset)
|
|
|
|
config.stats.songs_served++; /* FIXME: remove stat races */
|
|
|
|
|
2004-04-28 14:45:56 -04:00
|
|
|
if((config.artfilename) &&
|
2004-10-15 16:10:39 -04:00
|
|
|
(!offset) &&
|
2004-04-28 14:45:56 -04:00
|
|
|
((img_fd=da_get_image_fd(pmp3->path)) != -1)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
if (strncasecmp(pmp3->type,"mp3",4) ==0) {
|
|
|
|
DPRINTF(E_INF,L_WS|L_ART,"Dynamic add artwork to %s (fd %d)\n",
|
|
|
|
pmp3->fname, img_fd);
|
|
|
|
da_attach_image(img_fd, pwsc->fd, file_fd, offset);
|
|
|
|
} else if (strncasecmp(pmp3->type, "m4a", 4) == 0) {
|
|
|
|
DPRINTF(E_INF,L_WS|L_ART,"Dynamic add artwork to %s (fd %d)\n",
|
|
|
|
pmp3->fname, img_fd);
|
|
|
|
da_aac_attach_image(img_fd, pwsc->fd, file_fd, offset);
|
|
|
|
}
|
2004-04-14 03:04:05 -04:00
|
|
|
} else if(offset) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_INF,L_WS,"Seeking to offset %ld\n",(long)offset);
|
2004-04-28 19:51:26 -04:00
|
|
|
lseek(file_fd,offset,SEEK_SET);
|
2003-12-01 01:55:05 -05:00
|
|
|
}
|
2004-11-13 02:14:26 -05:00
|
|
|
|
2004-11-22 02:14:37 -05:00
|
|
|
if((bytes_copied=copyfile(file_fd,pwsc->fd)) == -1) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_INF,L_WS,"Error copying file to remote... %s\n",
|
2004-02-22 22:27:49 -05:00
|
|
|
strerror(errno));
|
2004-11-22 02:14:37 -05:00
|
|
|
} else {
|
|
|
|
DPRINTF(E_INF,L_WS,"Finished streaming file to remote: %d bytes\n",
|
|
|
|
bytes_copied);
|
2004-02-22 22:27:49 -05:00
|
|
|
}
|
2004-11-22 02:14:37 -05:00
|
|
|
|
2003-11-13 23:56:20 -05:00
|
|
|
config_set_status(pwsc,session_id,NULL);
|
2003-11-11 21:59:45 -05:00
|
|
|
r_close(file_fd);
|
2004-11-13 15:58:30 -05:00
|
|
|
db_dispose(pmp3);
|
|
|
|
free(pmp3);
|
2003-11-11 21:59:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-10-30 17:42:11 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_WS|L_DAAP,"Finished serving DAAP response\n");
|
2003-11-20 16:58:22 -05:00
|
|
|
|
2003-10-30 17:42:11 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Fork and exit. Stolen pretty much straight from Stevens.
|
2003-12-29 17:09:15 -05:00
|
|
|
*/
|
2004-11-11 14:17:02 -05:00
|
|
|
int daemon_start(void) {
|
2003-12-29 17:09:15 -05:00
|
|
|
int childpid, fd;
|
|
|
|
|
|
|
|
signal(SIGTTOU, SIG_IGN);
|
|
|
|
signal(SIGTTIN, SIG_IGN);
|
|
|
|
signal(SIGTSTP, SIG_IGN);
|
|
|
|
|
|
|
|
// Fork and exit
|
|
|
|
if ((childpid = fork()) < 0) {
|
|
|
|
fprintf(stderr, "Can't fork!\n");
|
|
|
|
return -1;
|
|
|
|
} else if (childpid > 0)
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
#ifdef SETPGRP_VOID
|
|
|
|
setpgrp();
|
|
|
|
#else
|
|
|
|
setpgrp(0,0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TIOCNOTTY
|
|
|
|
if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
|
|
|
|
ioctl(fd, TIOCNOTTY, (char *) NULL);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if((fd = open("/dev/null", O_RDWR, 0)) != -1) {
|
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
2004-12-21 23:59:34 -05:00
|
|
|
dup2(fd, STDERR_FILENO);
|
2003-12-29 17:09:15 -05:00
|
|
|
if (fd > 2)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
for (fd = 0; fd < FOPEN_MAX; fd++)
|
|
|
|
close(fd);
|
|
|
|
*/
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
chdir("/");
|
|
|
|
umask(0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
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");
|
2003-12-05 00:59:19 -05:00
|
|
|
printf(" -p Parse playlist 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");
|
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
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Drop privs. This allows mt-daapd to run as a non-privileged user.
|
|
|
|
* Hopefully this will limit the damage it could do if exploited
|
|
|
|
* remotely. Note that only the user need be specified. GID
|
|
|
|
* is set to the primary group of the user.
|
2004-01-19 23:41:20 -05:00
|
|
|
*
|
2004-11-12 01:38:05 -05:00
|
|
|
* \param user user to run as (or UID)
|
2004-01-19 23:41:20 -05:00
|
|
|
*/
|
|
|
|
int drop_privs(char *user) {
|
|
|
|
int err;
|
|
|
|
struct passwd *pw=NULL;
|
|
|
|
|
|
|
|
/* drop privs */
|
|
|
|
if(getuid() == (uid_t)0) {
|
2004-10-25 00:50:59 -04:00
|
|
|
if(atoi(user)) {
|
|
|
|
pw=getpwuid((uid_t)atoi(user)); /* doh! */
|
|
|
|
} else {
|
|
|
|
pw=getpwnam(config.runas);
|
|
|
|
}
|
|
|
|
|
2004-01-19 23:41:20 -05:00
|
|
|
if(pw) {
|
|
|
|
if(initgroups(user,pw->pw_gid) != 0 ||
|
|
|
|
setgid(pw->pw_gid) != 0 ||
|
|
|
|
setuid(pw->pw_uid) != 0) {
|
|
|
|
err=errno;
|
|
|
|
fprintf(stderr,"Couldn't change to %s, gid=%d, uid=%d\n",
|
|
|
|
user,pw->pw_gid, pw->pw_uid);
|
|
|
|
errno=err;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err=errno;
|
|
|
|
fprintf(stderr,"Couldn't lookup user %s\n",user);
|
|
|
|
errno=err;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Wait for signals and flag the main process. This is
|
|
|
|
* a thread handler for the signal processing thread. It
|
|
|
|
* does absolutely nothing except wait for signals. The rest
|
|
|
|
* of the threads are running with signals blocked, so this thread
|
|
|
|
* is guaranteed to catch all the signals. It sets flags in
|
|
|
|
* the config structure that the main thread looks for. Specifically,
|
|
|
|
* the stop flag (from an INT signal), and the reload flag (from HUP).
|
|
|
|
* \param arg NULL, but required of a thread procedure
|
2004-04-19 02:19:46 -04:00
|
|
|
*/
|
|
|
|
void *signal_handler(void *arg) {
|
|
|
|
sigset_t intmask;
|
|
|
|
int sig;
|
2004-11-11 14:17:02 -05:00
|
|
|
int status;
|
2004-04-19 02:19:46 -04:00
|
|
|
|
|
|
|
config.stop=0;
|
|
|
|
config.reload=0;
|
2005-01-30 01:58:36 -05:00
|
|
|
config.pid=getpid();
|
2004-04-19 02:19:46 -04:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_WARN,L_MAIN,"Signal handler started\n");
|
2004-04-19 02:19:46 -04:00
|
|
|
|
|
|
|
while(!config.stop) {
|
|
|
|
if((sigemptyset(&intmask) == -1) ||
|
2004-11-11 14:17:02 -05:00
|
|
|
(sigaddset(&intmask, SIGCLD) == -1) ||
|
2004-04-19 02:19:46 -04:00
|
|
|
(sigaddset(&intmask, SIGINT) == -1) ||
|
|
|
|
(sigaddset(&intmask, SIGHUP) == -1) ||
|
|
|
|
(sigwait(&intmask, &sig) == -1)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_MAIN,"Error waiting for signals. Aborting\n");
|
2004-04-19 02:19:46 -04:00
|
|
|
} else {
|
|
|
|
/* process the signal */
|
|
|
|
switch(sig) {
|
2004-11-11 14:17:02 -05:00
|
|
|
case SIGCLD:
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Got CLD signal. Reaping\n");
|
2005-02-21 03:10:37 -05:00
|
|
|
while (wait3(&status, WNOHANG, NULL) > 0) {
|
|
|
|
}
|
2004-11-11 14:17:02 -05:00
|
|
|
break;
|
2004-04-19 02:19:46 -04:00
|
|
|
case SIGINT:
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Got INT signal. Notifying daap server.\n");
|
2004-04-19 02:19:46 -04:00
|
|
|
config.stop=1;
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case SIGHUP:
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Got HUP signal. Notifying daap server.\n");
|
2004-04-19 02:19:46 -04:00
|
|
|
config.reload=1;
|
|
|
|
break;
|
2004-04-22 00:20:30 -04:00
|
|
|
default:
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"What am I doing here?\n");
|
2004-04-22 00:20:30 -04:00
|
|
|
break;
|
2004-04-19 02:19:46 -04:00
|
|
|
}
|
|
|
|
}
|
2004-11-14 01:44:20 -05:00
|
|
|
}
|
2004-04-19 02:19:46 -04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-11-12 01:38:05 -05:00
|
|
|
/**
|
|
|
|
* Block signals, then start the signal handler. The
|
|
|
|
* signal handler started by spawning a new thread on
|
|
|
|
* signal_handler().
|
2004-04-19 02:19:46 -04:00
|
|
|
*
|
2004-11-14 01:44:20 -05:00
|
|
|
* \returns 0 on success, -1 on failure with errno set
|
2004-04-19 02:19:46 -04:00
|
|
|
*/
|
2004-11-14 01:44:20 -05:00
|
|
|
int start_signal_handler(pthread_t *handler_tid) {
|
2004-04-19 02:19:46 -04:00
|
|
|
int error;
|
|
|
|
sigset_t set;
|
|
|
|
|
|
|
|
if((sigemptyset(&set) == -1) ||
|
|
|
|
(sigaddset(&set,SIGINT) == -1) ||
|
|
|
|
(sigaddset(&set,SIGHUP) == -1) ||
|
2004-11-11 14:27:38 -05:00
|
|
|
(sigaddset(&set,SIGCLD) == -1) ||
|
2004-04-19 02:19:46 -04:00
|
|
|
(sigprocmask(SIG_BLOCK, &set, NULL) == -1)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Error setting signal set\n");
|
2004-04-19 02:19:46 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-14 01:44:20 -05:00
|
|
|
if(error=pthread_create(handler_tid, NULL, signal_handler, NULL)) {
|
2004-04-19 02:19:46 -04:00
|
|
|
errno=error;
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Error creating signal_handler thread\n");
|
2004-04-19 02:19:46 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-14 01:44:20 -05:00
|
|
|
/* we'll not detach this... let's join it */
|
|
|
|
//pthread_detach(handler_tid);
|
2004-04-19 02:19:46 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2003-12-29 17:09:15 -05:00
|
|
|
char *configfile=DEFAULT_CONFIGFILE;
|
2003-10-13 11:03:14 -04:00
|
|
|
WSCONFIG ws_config;
|
|
|
|
WSHANDLE server;
|
2003-12-05 00:59:19 -05:00
|
|
|
int parseonly=0;
|
2003-12-29 17:09:15 -05:00
|
|
|
int foreground=0;
|
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;
|
2004-09-19 02:01:38 -04:00
|
|
|
int old_song_count;
|
2004-12-06 19:24:39 -05:00
|
|
|
int force_non_root=0;
|
2004-11-14 01:44:20 -05:00
|
|
|
pthread_t signal_tid;
|
2004-02-23 19:34:04 -05:00
|
|
|
|
2004-12-09 00:07:09 -05:00
|
|
|
int pid_fd;
|
2005-02-04 02:37:48 -05:00
|
|
|
FILE *pid_fp=NULL;
|
2004-12-09 00:07:09 -05:00
|
|
|
|
2004-01-04 16:57:38 -05:00
|
|
|
config.use_mdns=1;
|
2004-04-13 00:23:36 -04:00
|
|
|
err_debuglevel=1;
|
2003-10-19 16:03:54 -04:00
|
|
|
|
2004-12-06 19:24:39 -05:00
|
|
|
while((option=getopt(argc,argv,"D:d:c:mpfry")) != -1) {
|
2003-10-19 16:03:54 -04:00
|
|
|
switch(option) {
|
|
|
|
case 'd':
|
|
|
|
err_debuglevel=atoi(optarg);
|
|
|
|
break;
|
2004-11-13 03:05:27 -05:00
|
|
|
case 'D':
|
|
|
|
if(err_setdebugmask(optarg)) {
|
|
|
|
usage(argv[0]);
|
2004-12-06 19:24:39 -05:00
|
|
|
exit(EXIT_FAILURE);
|
2004-11-13 03:05:27 -05:00
|
|
|
}
|
|
|
|
break;
|
2003-12-29 17:09:15 -05:00
|
|
|
case 'f':
|
|
|
|
foreground=1;
|
|
|
|
break;
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
case 'c':
|
|
|
|
configfile=optarg;
|
|
|
|
break;
|
2003-10-30 17:42:11 -05:00
|
|
|
|
|
|
|
case 'm':
|
2004-01-04 16:57:38 -05:00
|
|
|
config.use_mdns=0;
|
2003-10-30 17:42:11 -05:00
|
|
|
break;
|
|
|
|
|
2003-12-05 00:59:19 -05:00
|
|
|
case 'p':
|
|
|
|
parseonly=1;
|
2004-09-12 19:20:29 -04:00
|
|
|
foreground=1;
|
2003-12-05 00:59:19 -05:00
|
|
|
break;
|
|
|
|
|
2004-06-14 15:01:06 -04:00
|
|
|
case 'r':
|
|
|
|
reload=1;
|
|
|
|
break;
|
|
|
|
|
2004-12-06 19:24:39 -05:00
|
|
|
case 'y':
|
|
|
|
force_non_root=1;
|
|
|
|
break;
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-06 19:24:39 -05:00
|
|
|
if((getuid()) && (!force_non_root)) {
|
|
|
|
fprintf(stderr,"You are not root. This is almost certainly wrong. If you are\n"
|
|
|
|
"sure you want to do this, use the -y command-line switch\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/* read the configfile, if specified, otherwise
|
|
|
|
* try defaults */
|
2004-03-26 15:59:28 -05:00
|
|
|
config.stats.start_time=start_time=time(NULL);
|
2004-03-08 16:27:38 -05:00
|
|
|
|
2003-12-29 17:09:15 -05:00
|
|
|
if(config_read(configfile)) {
|
2004-12-06 19:24:39 -05:00
|
|
|
fprintf(stderr,"Error reading config file (%s)\n",configfile);
|
2003-12-29 17:09:15 -05:00
|
|
|
exit(EXIT_FAILURE);
|
2003-10-19 16:03:54 -04:00
|
|
|
}
|
2003-11-05 13:57:13 -05:00
|
|
|
|
2004-12-20 23:38:15 -05:00
|
|
|
if(!foreground) {
|
|
|
|
if(config.logfile) {
|
|
|
|
err_setdest(config.logfile,LOGDEST_LOGFILE);
|
|
|
|
} else {
|
|
|
|
err_setdest("mt-daapd",LOGDEST_SYSLOG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-09 00:07:09 -05:00
|
|
|
#ifndef WITHOUT_MDNS
|
|
|
|
if((config.use_mdns) && (!parseonly)) {
|
|
|
|
DPRINTF(E_LOG,L_MAIN,"Starting rendezvous daemon\n");
|
|
|
|
if(rend_init(config.runas)) {
|
|
|
|
DPRINTF(E_FATAL,L_MAIN|L_REND,"Error in rend_init: %s\n",strerror(errno));
|
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
|
|
|
|
2004-12-09 00:07:09 -05:00
|
|
|
/* open the pidfile, so it can be written once we detach */
|
2005-01-24 02:42:42 -05:00
|
|
|
if((!foreground) && (!force_non_root)) {
|
2004-12-09 00:07:09 -05:00
|
|
|
if(-1 == (pid_fd = open(PIDFILE,O_CREAT | O_WRONLY | O_TRUNC, 0644)))
|
|
|
|
DPRINTF(E_FATAL,L_MAIN,"Error opening pidfile (%s): %s\n",PIDFILE,strerror(errno));
|
|
|
|
|
|
|
|
if(0 == (pid_fp = fdopen(pid_fd, "w")))
|
|
|
|
DPRINTF(E_FATAL,L_MAIN,"fdopen: %s\n",strerror(errno));
|
2004-12-21 23:59:34 -05:00
|
|
|
|
2005-01-07 00:57:52 -05:00
|
|
|
daemon_start();
|
|
|
|
|
2005-01-30 01:58:36 -05:00
|
|
|
/* just to be on the safe side... */
|
|
|
|
config.pid=0;
|
2004-12-09 00:07:09 -05:00
|
|
|
}
|
|
|
|
|
2004-12-21 23:59:34 -05:00
|
|
|
/* DWB: shouldn't this be done after dropping privs? */
|
|
|
|
if(db_open(config.dbdir, reload))
|
|
|
|
DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_open: %s\n",strerror(errno));
|
|
|
|
|
|
|
|
|
2004-12-09 00:07:09 -05:00
|
|
|
// Drop privs here
|
|
|
|
if(drop_privs(config.runas)) {
|
|
|
|
DPRINTF(E_FATAL,L_MAIN,"Error in drop_privs: %s\n",strerror(errno));
|
2004-06-14 15:01:06 -04:00
|
|
|
}
|
|
|
|
|
2004-04-19 02:19:46 -04:00
|
|
|
/* block signals and set up the signal handling thread */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Starting signal handler\n");
|
2004-11-14 01:44:20 -05:00
|
|
|
if(start_signal_handler(&signal_tid)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_MAIN,"Error starting signal handler %s\n",strerror(errno));
|
2004-04-19 02:19:46 -04:00
|
|
|
}
|
|
|
|
|
2005-01-30 01:58:36 -05:00
|
|
|
|
2005-02-04 02:37:48 -05:00
|
|
|
if(pid_fp) {
|
|
|
|
/* wait to for config.pid to be set by the signal handler */
|
|
|
|
while(!config.pid) {
|
|
|
|
sleep(1);
|
|
|
|
}
|
2005-01-30 01:58:36 -05:00
|
|
|
|
2005-02-04 02:37:48 -05:00
|
|
|
fprintf(pid_fp,"%d\n",config.pid);
|
|
|
|
fclose(pid_fp);
|
|
|
|
}
|
2005-01-30 01:58:36 -05:00
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_PL,"Loading playlists\n");
|
2003-11-04 01:11:00 -05:00
|
|
|
|
2003-12-05 00:59:19 -05:00
|
|
|
if(config.playlist)
|
|
|
|
pl_load(config.playlist);
|
|
|
|
|
|
|
|
if(parseonly) {
|
|
|
|
if(!pl_error) {
|
2003-12-29 17:09:15 -05:00
|
|
|
fprintf(stderr,"Parsed successfully.\n");
|
2003-12-05 00:59:19 -05:00
|
|
|
pl_dump();
|
|
|
|
}
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
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");
|
2004-04-06 09:44:26 -04:00
|
|
|
if(db_init()) {
|
2004-11-13 02:14:26 -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
|
|
|
}
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_SCAN,"Starting mp3 scan\n");
|
2003-11-04 01:11:00 -05:00
|
|
|
if(scan_init(config.mp3dir)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_MAIN|L_SCAN,"Error scanning MP3 files: %s\n",strerror(errno));
|
2003-11-04 01:11:00 -05:00
|
|
|
}
|
|
|
|
|
2003-10-19 16:03:54 -04:00
|
|
|
/* start up the web server */
|
|
|
|
ws_config.web_root=config.web_root;
|
|
|
|
ws_config.port=config.port;
|
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",
|
2004-04-13 00:23:36 -04:00
|
|
|
config.web_root, config.port);
|
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
server=ws_start(&ws_config);
|
|
|
|
if(!server) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_FATAL,L_MAIN|L_WS,"Error staring web server: %s\n",strerror(errno));
|
2003-10-13 11:03:14 -04:00
|
|
|
}
|
|
|
|
|
2003-10-30 17:42:11 -05:00
|
|
|
ws_registerhandler(server, "^.*$",config_handler,config_auth,1);
|
|
|
|
ws_registerhandler(server, "^/server-info$",daap_handler,NULL,0);
|
|
|
|
ws_registerhandler(server, "^/content-codes$",daap_handler,NULL,0);
|
2003-12-09 00:48:30 -05:00
|
|
|
ws_registerhandler(server,"^/login$",daap_handler,daap_auth,0);
|
|
|
|
ws_registerhandler(server,"^/update$",daap_handler,daap_auth,0);
|
|
|
|
ws_registerhandler(server,"^/databases$",daap_handler,daap_auth,0);
|
2004-02-09 16:32:30 -05:00
|
|
|
ws_registerhandler(server,"^/logout$",daap_handler,NULL,0);
|
|
|
|
ws_registerhandler(server,"^/databases/.*",daap_handler,NULL,0);
|
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 */
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_REND,"Registering rendezvous names\n");
|
2004-01-19 23:41:20 -05:00
|
|
|
rend_register(config.servername,"_daap._tcp",config.port);
|
|
|
|
rend_register(config.servername,"_http._tcp",config.port);
|
|
|
|
}
|
2004-10-30 12:42:20 -04:00
|
|
|
#endif
|
2004-01-19 23:41:20 -05:00
|
|
|
|
2004-03-08 16:27:38 -05:00
|
|
|
end_time=time(NULL);
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Scanned %d songs in %d seconds\n",db_get_song_count(),
|
2004-03-08 16:27:38 -05:00
|
|
|
end_time-start_time);
|
|
|
|
|
2003-11-26 01:10:58 -05:00
|
|
|
config.stop=0;
|
|
|
|
|
2004-04-19 02:19:46 -04:00
|
|
|
while(!config.stop) {
|
2004-09-15 00:58:08 -04:00
|
|
|
if((config.rescan_interval) && (rescan_counter > config.rescan_interval)) {
|
2004-10-25 00:50:59 -04:00
|
|
|
if((config.always_scan) || (config_get_session_count())) {
|
|
|
|
config.reload=1;
|
|
|
|
} else {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_DBG,L_MAIN|L_SCAN|L_DB,"Skipped bground scan... no users\n");
|
2004-09-15 00:58:08 -04:00
|
|
|
}
|
|
|
|
rescan_counter=0;
|
|
|
|
}
|
|
|
|
|
2004-04-19 02:19:46 -04:00
|
|
|
if(config.reload) {
|
2004-09-19 02:01:38 -04:00
|
|
|
old_song_count = db_get_song_count();
|
|
|
|
start_time=time(NULL);
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Rescanning database\n");
|
2004-04-28 14:55:22 -04:00
|
|
|
if(scan_init(config.mp3dir)) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Error rescanning... exiting\n");
|
2004-04-28 14:55:22 -04:00
|
|
|
config.stop=1;
|
|
|
|
}
|
2004-09-19 01:18:17 -04:00
|
|
|
config.reload=0;
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_INF,L_MAIN|L_DB|L_SCAN,"Scanned %d songs (was %d) in %d seconds\n",
|
|
|
|
db_get_song_count(),old_song_count,time(NULL)-start_time);
|
2004-04-19 02:19:46 -04:00
|
|
|
}
|
2004-09-15 00:58:08 -04:00
|
|
|
|
|
|
|
sleep(MAIN_SLEEP_INTERVAL);
|
|
|
|
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) {
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN|L_REND,"Stopping rendezvous daemon\n");
|
2004-01-19 23:41:20 -05:00
|
|
|
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
|
|
|
|
2004-11-14 01:44:20 -05:00
|
|
|
|
|
|
|
DPRINTF(E_LOG,L_MAIN,"Stopping signal handler\n");
|
|
|
|
if(!pthread_kill(signal_tid,SIGINT)) {
|
|
|
|
pthread_join(signal_tid,NULL);
|
|
|
|
}
|
|
|
|
|
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");
|
2003-11-26 01:10:58 -05:00
|
|
|
ws_stop(server);
|
2005-01-07 00:37:46 -05:00
|
|
|
*/
|
2003-11-26 01:10:58 -05:00
|
|
|
|
|
|
|
config_close();
|
|
|
|
|
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-02-14 19:51:11 -05:00
|
|
|
#ifdef DEBUG_MEMORY
|
2003-11-26 01:10:58 -05:00
|
|
|
fprintf(stderr,"Leaked memory:\n");
|
2003-11-23 01:10:25 -05:00
|
|
|
err_leakcheck();
|
|
|
|
#endif
|
|
|
|
|
2004-11-13 02:14:26 -05:00
|
|
|
DPRINTF(E_LOG,L_MAIN,"Done!\n");
|
2004-04-13 00:23:36 -04:00
|
|
|
|
2004-11-13 03:05:27 -05:00
|
|
|
err_setdest(NULL,LOGDEST_STDERR);
|
2003-12-29 18:39:18 -05:00
|
|
|
|
2003-10-13 11:03:14 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|