mirror of
https://github.com/owntone/owntone-server.git
synced 2025-03-31 01:33:44 -04:00
General main.c cleanup
This commit is contained in:
parent
64cd2ee383
commit
6e64e54429
197
src/main.c
197
src/main.c
@ -19,67 +19,32 @@
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/**
|
||||
* @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 files.
|
||||
*/
|
||||
|
||||
/** @mainpage mt-daapd
|
||||
* @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://www.mt-daapd.org">Home page</a>
|
||||
* - <a href="http://sf.net/projects/mt-daapd">Project page on SourceForge</a>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#ifdef HAVE_STDINT_H
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#ifdef HAVE_DIRENT_H
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
#ifdef HAVE_UNISTD_H
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#ifdef HAVE_SYS_WAIT_H
|
||||
#include <sys/wait.h>
|
||||
#endif
|
||||
#include <sys/signalfd.h>
|
||||
|
||||
#include <signal.h>
|
||||
#include <limits.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
#include <sys/signalfd.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <getopt.h>
|
||||
#include <event.h>
|
||||
#include <libavformat/avformat.h>
|
||||
|
||||
#include "conffile.h"
|
||||
#include "logger.h"
|
||||
#include "misc.h"
|
||||
@ -88,52 +53,24 @@
|
||||
#include "db-generic.h"
|
||||
#include "mdns_avahi.h"
|
||||
|
||||
#ifdef HAVE_GETOPT_H
|
||||
# include "getopt.h"
|
||||
#endif
|
||||
|
||||
#include <event.h>
|
||||
#include <libavformat/avformat.h>
|
||||
|
||||
|
||||
/** Seconds to sleep before checking for a shutdown or reload */
|
||||
#define MAIN_SLEEP_INTERVAL 2
|
||||
|
||||
/** Let's hope if you have no atoll, you only have 32 bit inodes... */
|
||||
#if !HAVE_ATOLL
|
||||
# define atoll(a) atol(a)
|
||||
#endif
|
||||
|
||||
#ifndef PIDFILE
|
||||
#define PIDFILE "/var/run/mt-daapd.pid"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Globals
|
||||
*/
|
||||
struct event_base *evbase_main;
|
||||
static int main_exit;
|
||||
|
||||
|
||||
/*
|
||||
* Forwards
|
||||
*/
|
||||
static void usage(char *program);
|
||||
|
||||
/**
|
||||
* Print usage information to stdout
|
||||
*
|
||||
* \param program name of program (argv[0])
|
||||
*/
|
||||
void usage(char *program) {
|
||||
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 configfile specified\n");
|
||||
printf(" -P <file> Write the PID to specified file\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 Yes, go ahead and run as non-root user\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");
|
||||
@ -142,7 +79,6 @@ void usage(char *program) {
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
daemonize(int background, char *runas, char *pidfile)
|
||||
{
|
||||
@ -270,12 +206,12 @@ signal_cb(int fd, short event, void *arg)
|
||||
struct signalfd_siginfo info;
|
||||
int status;
|
||||
|
||||
sa_ign.sa_handler=SIG_IGN;
|
||||
sa_ign.sa_flags=0;
|
||||
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;
|
||||
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)
|
||||
@ -309,37 +245,22 @@ signal_cb(int fd, short event, void *arg)
|
||||
event_base_loopbreak(evbase_main);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int option;
|
||||
char *configfile=CONFFILE;
|
||||
int reload=0;
|
||||
char *configfile = CONFFILE;
|
||||
int reload = 0;
|
||||
int background;
|
||||
int force_non_root=0;
|
||||
int skip_initial=1;
|
||||
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 *perr = NULL;
|
||||
char *txtrecord[10];
|
||||
char *pidfile = PIDFILE;
|
||||
sigset_t sigs;
|
||||
@ -348,22 +269,26 @@ int main(int argc, char *argv[]) {
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
int err;
|
||||
|
||||
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) {
|
||||
while ((option = getopt(argc, argv, "D:d:c:P:frysiub:v")) != -1)
|
||||
{
|
||||
switch (option)
|
||||
{
|
||||
case 'b':
|
||||
ffid=optarg;
|
||||
ffid = optarg;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
loglevel = atoi(optarg);
|
||||
ret = safe_atoi(optarg, &i);
|
||||
if (ret < 0)
|
||||
fprintf(stderr, "Error: loglevel must be an integer in '-d %s'\n", optarg);
|
||||
else
|
||||
loglevel = i;
|
||||
break;
|
||||
|
||||
case 'D':
|
||||
@ -375,7 +300,7 @@ int main(int argc, char *argv[]) {
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
configfile=optarg;
|
||||
configfile = optarg;
|
||||
break;
|
||||
|
||||
case 'P':
|
||||
@ -383,19 +308,19 @@ int main(int argc, char *argv[]) {
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
reload=1;
|
||||
reload = 1;
|
||||
break;
|
||||
|
||||
case 's':
|
||||
skip_initial=0;
|
||||
skip_initial = 0;
|
||||
break;
|
||||
|
||||
case 'y':
|
||||
force_non_root=1;
|
||||
force_non_root = 1;
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
fprintf(stderr,"Firefly Media Server: Version %s\n",VERSION);
|
||||
fprintf(stdout, "Firefly Media Server: Version %s\n",VERSION);
|
||||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
|
||||
@ -406,8 +331,9 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
if((getuid()) && (!force_non_root)) {
|
||||
fprintf(stderr,"You are not root. This is almost certainly wrong. "
|
||||
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);
|
||||
@ -500,10 +426,11 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
/* this will require that the db be readable by the runas user */
|
||||
err = db_open(&perr, "sqlite3", "/var/cache/mt-daapd"); /* FIXME */
|
||||
ret = db_open(&perr, "sqlite3", "/var/cache/mt-daapd"); /* FIXME */
|
||||
|
||||
if(err) {
|
||||
DPRINTF(E_LOG,L_MAIN,"Error opening db: %s\n",perr);
|
||||
if (ret != 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_MAIN, "Error opening db: %s\n", perr);
|
||||
|
||||
mdns_deinit();
|
||||
conffile_unload();
|
||||
@ -512,9 +439,10 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
/* Initialize the database before starting */
|
||||
DPRINTF(E_LOG,L_MAIN,"Initializing database\n");
|
||||
if(db_init(reload)) {
|
||||
DPRINTF(E_FATAL,L_MAIN,"Error in db_init: %s\n",strerror(errno));
|
||||
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 */
|
||||
@ -624,7 +552,7 @@ int main(int argc, char *argv[]) {
|
||||
/* Run the loop */
|
||||
event_base_dispatch(evbase_main);
|
||||
|
||||
DPRINTF(E_LOG,L_MAIN,"Stopping gracefully\n");
|
||||
DPRINTF(E_LOG, L_MAIN, "Stopping gracefully\n");
|
||||
|
||||
DPRINTF(E_LOG, L_MAIN, "HTTPd deinit\n");
|
||||
httpd_deinit();
|
||||
@ -637,7 +565,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
conffile_unload();
|
||||
|
||||
DPRINTF(E_LOG,L_MAIN,"Closing database\n");
|
||||
DPRINTF(E_LOG, L_MAIN, "Closing database\n");
|
||||
db_deinit();
|
||||
|
||||
if (background)
|
||||
@ -647,10 +575,9 @@ int main(int argc, char *argv[]) {
|
||||
DPRINTF(E_WARN, L_MAIN, "Could not unlink PID file %s: %s\n", pidfile, strerror(errno));
|
||||
}
|
||||
|
||||
DPRINTF(E_LOG,L_MAIN,"Done!\n");
|
||||
DPRINTF(E_LOG, L_MAIN, "Exiting.\n");
|
||||
|
||||
logger_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user