owntone-server/src/main.c

586 lines
13 KiB
C
Raw Normal View History

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
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2003-11-03 14:02:00 -05:00
#include <errno.h>
#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>
#include <string.h>
2003-11-03 14:02:00 -05:00
#include <unistd.h>
2003-10-13 11:03:14 -04:00
#include <sys/stat.h>
#include <sys/types.h>
2004-02-14 19:51:11 -05:00
#include <sys/wait.h>
#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-23 17:43:01 -04:00
#include "rend.h"
2003-10-13 11:03:14 -04:00
#include "webserver.h"
#include "playlist.h"
2003-10-13 11:03:14 -04:00
2003-12-29 17:09:15 -05:00
#define DEFAULT_CONFIGFILE "/etc/mt-daapd.conf"
2004-01-04 16:16:20 -05:00
#ifndef SIGCLD
# define SIGCLD SIGCHLD
#endif
/*
* Globals
*/
CONFIG config;
2003-12-29 17:09:15 -05:00
/*
* Forwards
*/
RETSIGTYPE sig_child(int signal);
int daemon_start(int reap_children);
2003-12-09 00:48:30 -05:00
/*
* daap_auth
*
* Auth handler for the daap server
*/
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);
}
2003-10-30 17:42:11 -05:00
/*
* daap_handler
*
* Handle daap-related web pages
*/
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;
int db_index;
int playlist_index;
2004-02-14 19:51:11 -05:00
int item=0;
2003-11-11 21:59:45 -05:00
char *first, *last;
int streaming=0;
MP3FILE *pmp3;
int file_fd;
2003-11-13 23:56:20 -05:00
int session_id=0;
2003-11-11 21:59:45 -05:00
2003-12-01 15:06:09 -05:00
off_t offset=0;
2003-12-01 01:55:05 -05:00
off_t file_len;
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");
2003-11-17 11:38:44 -05:00
root=daap_response_server_info(config.servername);
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");
2003-10-30 17:42:11 -05:00
root=daap_response_login();
} else if (!strcasecmp(pwsc->uri,"/update")) {
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 {
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);
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);
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
*/
uri = strdup(pwsc->uri);
first=(char*)&uri[11];
last=first;
while((*last) && (*last != '/')) {
last++;
}
if(*last) {
*last='\0';
db_index=atoi(first);
last++;
if(strncasecmp(last,"items/",6)==0) {
/* streaming */
first=last+6;
while((*last) && (*last != '.'))
last++;
if(*last == '.') {
*last='\0';
item=atoi(first);
streaming=1;
}
free(uri);
} else if (strncasecmp(last,"items",5)==0) {
/* songlist */
free(uri);
root=daap_response_songlist();
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';
playlist_index=atoi(first);
root=daap_response_playlist_items(playlist_index);
}
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");
2003-11-11 21:59:45 -05:00
}
}
2003-10-30 17:42:11 -05:00
}
2003-11-11 21:59:45 -05:00
if((!root)&&(!streaming)) {
2003-11-14 14:19:18 -05:00
DPRINTF(ERR_DEBUG,"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-03-01 16:12:20 -05:00
DPRINTF(ERR_DEBUG,"Satisfying request\n");
2003-11-11 21:59:45 -05:00
ws_addresponseheader(pwsc,"Content-Length","%d",root->reported_size + 8);
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
2004-03-01 16:12:20 -05:00
DPRINTF(ERR_DEBUG,"Emitting headers\n");
2003-11-11 21:59:45 -05:00
ws_emitheaders(pwsc);
2003-10-30 17:42:11 -05:00
2003-11-11 21:59:45 -05:00
/*
if(ws_testrequestheader(pwsc,"Accept-Encoding","gzip")) {
ws_addresponseheader(pwsc,"Content-Encoding","gzip");
compress=1;
}
*/
2003-10-30 17:42:11 -05:00
2004-03-01 16:12:20 -05:00
DPRINTF(ERR_DEBUG,"Serializing\n");
2003-11-11 21:59:45 -05:00
daap_serialize(root,pwsc->fd,0);
2004-03-01 16:12:20 -05:00
DPRINTF(ERR_DEBUG,"Done, freeing\n");
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")) {
offset=atol(ws_getrequestheader(pwsc,"range") + 6);
DPRINTF(ERR_DEBUG,"Thread %d: Skipping to byte %ld\n",pwsc->threadno,offset);
}
2003-11-11 21:59:45 -05:00
pmp3=db_find(item);
if(!pmp3) {
ws_returnerror(pwsc,404,"File Not Found");
} 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;
DPRINTF(ERR_WARN,"Thread %d: Error opening %s: %s\n",
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);
2003-11-11 21:59:45 -05:00
} else {
2003-12-01 01:55:05 -05:00
file_len=lseek(file_fd,0,SEEK_END);
lseek(file_fd,0,SEEK_SET);
file_len -= offset;
DPRINTF(ERR_DEBUG,"Thread %d: Length of file (remaining) is %ld\n",
pwsc->threadno,(long)file_len);
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
ws_writefd(pwsc,"HTTP/1.1 200 OK\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);
2003-12-01 15:06:09 -05:00
DPRINTF(ERR_INFO,"Streaming %s\n",pmp3->fname);
2004-03-26 15:59:28 -05:00
config.stats.songs_served++; /* FIXME: remove stat races */
2003-12-01 01:55:05 -05:00
if(offset) {
2004-02-22 22:27:49 -05:00
DPRINTF(ERR_INFO,"Seeking to offset %d\n",offset);
2003-12-01 01:55:05 -05:00
lseek(file_fd,offset,SEEK_SET);
}
2004-02-22 22:27:49 -05:00
if(copyfile(file_fd,pwsc->fd)) {
DPRINTF(ERR_INFO,"Error copying file to remote... %s\n",
strerror(errno));
}
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);
}
}
}
2003-10-30 17:42:11 -05:00
DPRINTF(ERR_DEBUG,"Finished serving DAAP response\n");
2003-10-30 17:42:11 -05:00
return;
}
2003-12-29 17:09:15 -05:00
/*
* sig_child
*
* reap children
*/
RETSIGTYPE sig_child(int signal)
{
int status;
while (wait(&status)) {
};
}
/*
* daemon_start
*
* This is pretty much stolen straight from Stevens
*/
int daemon_start(int reap_children)
{
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);
dup2(fd, STDERR_FILENO);
if (fd > 2)
close(fd);
}
/*
for (fd = 0; fd < FOPEN_MAX; fd++)
close(fd);
*/
errno = 0;
chdir("/");
umask(0);
if (reap_children) {
signal(SIGCLD, sig_child);
}
return 0;
}
/*
* usage
*
2003-12-29 17:09:15 -05:00
* print usage message
*/
void usage(char *program) {
printf("Usage: %s [options]\n\n",program);
printf("Options:\n");
printf(" -d <number> Debuglevel (0-9)\n");
2004-01-04 16:57:38 -05:00
printf(" -m Disable mDNS\n");
printf(" -c <file> Use configfile specified");
printf(" -p Parse playlist file\n");
2003-12-29 17:09:15 -05:00
printf(" -f Run in foreground\n");
printf("\n\n");
}
2004-01-19 23:41:20 -05:00
/*
* drop_privs
*
* drop privs to a specific user
*/
int drop_privs(char *user) {
int err;
struct passwd *pw=NULL;
/* drop privs */
if(getuid() == (uid_t)0) {
pw=getpwnam(config.runas);
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;
}
2003-10-13 11:03:14 -04:00
int main(int argc, char *argv[]) {
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;
int parseonly=0;
2003-12-29 17:09:15 -05:00
int foreground=0;
2004-03-08 16:27:38 -05:00
int start_time;
int end_time;
2004-02-23 19:34:04 -05:00
2004-01-04 16:57:38 -05:00
config.use_mdns=1;
2003-10-23 17:43:01 -04:00
2004-01-04 16:32:22 -05:00
fprintf(stderr,"mt-daapd: version %s\n",VERSION);
2003-12-29 17:09:15 -05:00
fprintf(stderr,"Copyright (c) 2003 Ron Pedde. All rights reserved\n");
fprintf(stderr,"Portions Copyright (c) 1999-2001 Apple Computer, Inc. All rights Reserved.\n\n");
2004-02-14 19:51:11 -05:00
while((option=getopt(argc,argv,"d:c:mpf")) != -1) {
switch(option) {
case 'd':
err_debuglevel=atoi(optarg);
break;
2003-12-29 17:09:15 -05:00
case 'f':
foreground=1;
break;
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;
case 'p':
parseonly=1;
break;
default:
usage(argv[0]);
exit(EXIT_FAILURE);
break;
}
}
/* 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)) {
perror("config_read");
exit(EXIT_FAILURE);
}
if((config.use_mdns) && (!parseonly)) {
2004-04-06 09:44:26 -04:00
fprintf(stderr,"Starting rendezvous daemon -- indexing "
"mp3 files... wait.\n");
2004-01-19 23:41:20 -05:00
if(rend_init(config.runas)) {
perror("rend_init");
2003-12-29 18:39:18 -05:00
exit(EXIT_FAILURE);
}
2003-12-03 15:18:01 -05:00
}
2004-04-06 09:44:26 -04:00
2004-04-06 11:26:46 -04:00
if(db_open(config.dbdir)) {
2004-04-06 09:44:26 -04:00
perror("db_open");
exit(EXIT_FAILURE);
}
2004-01-19 23:41:20 -05:00
// Drop privs here
if(drop_privs(config.runas)) {
perror("drop_privs");
exit(EXIT_FAILURE);
}
2004-03-08 15:36:07 -05:00
DPRINTF(ERR_DEBUG,"Loading playlists...\n");
2003-11-04 01:11:00 -05:00
if(config.playlist)
pl_load(config.playlist);
2004-03-08 15:36:07 -05:00
DPRINTF(ERR_DEBUG,"Initializing database\n");
if(parseonly) {
if(!pl_error) {
2003-12-29 17:09:15 -05:00
fprintf(stderr,"Parsed successfully.\n");
pl_dump();
}
exit(EXIT_SUCCESS);
}
2004-03-08 15:36:07 -05:00
/* Initialize the database before starting */
2004-04-06 09:44:26 -04:00
if(db_init()) {
2004-03-08 15:36:07 -05:00
perror("db_init");
exit(EXIT_FAILURE);
}
2003-12-29 17:09:15 -05:00
/* will want to detach before we start scanning mp3 files */
if(!foreground) {
log_setdest("mt-daapd",LOGDEST_SYSLOG);
daemon_start(1);
}
2003-11-04 01:11:00 -05:00
if(scan_init(config.mp3dir)) {
2003-12-29 17:09:15 -05:00
log_err(1,"Error scanning MP3 files: %s\n",strerror(errno));
2003-11-04 01:11:00 -05:00
exit(EXIT_FAILURE);
}
/* 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
server=ws_start(&ws_config);
if(!server) {
2003-12-29 17:09:15 -05:00
log_err(1,"Error starting web server: %s\n",strerror(errno));
2003-10-13 11:03:14 -04:00
return EXIT_FAILURE;
}
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);
2004-01-19 23:41:20 -05:00
if(config.use_mdns) { /* register services */
DPRINTF(ERR_DEBUG,"Registering rendezvous names\n");
rend_register(config.servername,"_daap._tcp",config.port);
rend_register(config.servername,"_http._tcp",config.port);
}
2004-03-08 16:27:38 -05:00
end_time=time(NULL);
log_err(0,"Scanned %d songs in %d seconds\n",db_get_song_count(),
end_time-start_time);
2003-11-26 01:10:58 -05:00
config.stop=0;
while(!config.stop)
sleep(10);
2003-10-23 17:43:01 -04:00
2003-11-26 01:10:58 -05:00
if(config.use_mdns) {
2003-12-29 18:39:18 -05:00
if(foreground) fprintf(stderr,"Killing rendezvous daemon\n");
2004-01-19 23:41:20 -05:00
rend_stop();
2003-10-13 11:03:14 -04:00
}
2003-12-29 18:39:18 -05:00
if(foreground) fprintf(stderr,"Stopping webserver\n");
2003-11-26 01:10:58 -05:00
ws_stop(server);
config_close();
2003-12-29 18:39:18 -05:00
if(foreground) fprintf(stderr,"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
2003-12-29 18:39:18 -05:00
if(foreground) fprintf(stderr,"\nDone\n");
log_err(0,"Exiting");
2003-10-13 11:03:14 -04:00
return EXIT_SUCCESS;
}