* Update coding style.
This commit is contained in:
parent
aee83a3ee6
commit
1320eb4ea6
180
minidlna.c
180
minidlna.c
@ -111,16 +111,15 @@ OpenAndConfHTTPSocket(unsigned short port)
|
||||
/* Initialize client type cache */
|
||||
memset(&clients, 0, sizeof(struct client_cache_s));
|
||||
|
||||
if( (s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
|
||||
s = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (s < 0)
|
||||
{
|
||||
DPRINTF(E_ERROR, L_GENERAL, "socket(http): %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
|
||||
{
|
||||
DPRINTF(E_WARN, L_GENERAL, "setsockopt(http, SO_REUSEADDR): %s\n", strerror(errno));
|
||||
}
|
||||
|
||||
memset(&listenname, 0, sizeof(struct sockaddr_in));
|
||||
listenname.sin_family = AF_INET;
|
||||
@ -214,22 +213,19 @@ parselanaddr(struct lan_addr_s * lan_addr, const char * str)
|
||||
static void
|
||||
getfriendlyname(char *buf, int len)
|
||||
{
|
||||
char * dot = NULL;
|
||||
char * hn = calloc(1, 256);
|
||||
char *p = NULL;
|
||||
char hn[256];
|
||||
int off;
|
||||
|
||||
if( gethostname(hn, 256) == 0 )
|
||||
if (gethostname(hn, sizeof(hn)) == 0)
|
||||
{
|
||||
strncpyt(buf, hn, len);
|
||||
dot = strchr(buf, '.');
|
||||
if( dot )
|
||||
*dot = '\0';
|
||||
p = strchr(buf, '.');
|
||||
if (p)
|
||||
*p = '\0';
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy(buf, "Unknown");
|
||||
}
|
||||
free(hn);
|
||||
|
||||
off = strlen(buf);
|
||||
off += snprintf(buf+off, len-off, ": ");
|
||||
@ -312,7 +308,7 @@ getfriendlyname(char * buf, int len)
|
||||
}
|
||||
|
||||
static int
|
||||
open_db(sqlite3 **thisdb)
|
||||
open_db(sqlite3 **sq3)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
int new_db = 0;
|
||||
@ -324,11 +320,9 @@ open_db(sqlite3 **thisdb)
|
||||
make_dir(db_path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
|
||||
}
|
||||
if (sqlite3_open(path, &db) != SQLITE_OK)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_GENERAL, "ERROR: Failed to open sqlite database! Exiting...\n");
|
||||
}
|
||||
if (thisdb)
|
||||
*thisdb = db;
|
||||
if (sq3)
|
||||
*sq3 = db;
|
||||
sqlite3_busy_timeout(db, 5000);
|
||||
sql_exec(db, "pragma page_size = 4096");
|
||||
sql_exec(db, "pragma journal_mode = OFF");
|
||||
@ -496,7 +490,7 @@ init(int argc, char * * argv)
|
||||
/* first check if "-f" option is used */
|
||||
for (i=2; i<argc; i++)
|
||||
{
|
||||
if(0 == strcmp(argv[i-1], "-f"))
|
||||
if (strcmp(argv[i-1], "-f") == 0)
|
||||
{
|
||||
optionsfile = argv[i];
|
||||
options_flag = 1;
|
||||
@ -527,8 +521,7 @@ init(int argc, char * * argv)
|
||||
if(access(optionsfile, F_OK) == 0 || options_flag)
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Error reading configuration file %s\n", optionsfile);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
for (i=0; i<num_options; i++)
|
||||
{
|
||||
switch (ary_options[i].id)
|
||||
@ -546,24 +539,19 @@ init(int argc, char * * argv)
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n",
|
||||
MAX_LAN_ADDR, word);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case UPNPLISTENING_IP:
|
||||
if (n_lan_addr < MAX_LAN_ADDR)
|
||||
{
|
||||
if(parselanaddr(&lan_addr[n_lan_addr],
|
||||
ary_options[i].value) == 0)
|
||||
if (parselanaddr(&lan_addr[n_lan_addr], ary_options[i].value) == 0)
|
||||
n_lan_addr++;
|
||||
}
|
||||
else
|
||||
{
|
||||
DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n",
|
||||
MAX_LAN_ADDR, ary_options[i].value);
|
||||
}
|
||||
break;
|
||||
case UPNPPORT:
|
||||
runtime_vars.port = atoi(ary_options[i].value);
|
||||
@ -595,29 +583,20 @@ init(int argc, char * * argv)
|
||||
types = 0;
|
||||
while (*path)
|
||||
{
|
||||
if( *path == 'A' || *path == 'a' )
|
||||
{
|
||||
types |= TYPE_AUDIO;
|
||||
}
|
||||
else if( *path == 'V' || *path == 'v' )
|
||||
{
|
||||
types |= TYPE_VIDEO;
|
||||
}
|
||||
else if( *path == 'P' || *path == 'p' )
|
||||
{
|
||||
types |= TYPE_IMAGES;
|
||||
}
|
||||
else if( *path == ',' )
|
||||
if (*path == ',')
|
||||
{
|
||||
path++;
|
||||
break;
|
||||
}
|
||||
else if (*path == 'A' || *path == 'a')
|
||||
types |= TYPE_AUDIO;
|
||||
else if (*path == 'V' || *path == 'v')
|
||||
types |= TYPE_VIDEO;
|
||||
else if (*path == 'P' || *path == 'p')
|
||||
types |= TYPE_IMAGES;
|
||||
else
|
||||
{
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Media directory entry not understood [%s]\n",
|
||||
ary_options[i].value);
|
||||
break;
|
||||
}
|
||||
path++;
|
||||
}
|
||||
}
|
||||
@ -631,17 +610,15 @@ init(int argc, char * * argv)
|
||||
media_dir = calloc(1, sizeof(struct media_dir_s));
|
||||
media_dir->path = strdup(path);
|
||||
media_dir->types = types;
|
||||
if( !media_dirs )
|
||||
{
|
||||
media_dirs = media_dir;
|
||||
}
|
||||
else
|
||||
if (media_dirs)
|
||||
{
|
||||
struct media_dir_s *all_dirs = media_dirs;
|
||||
while( all_dirs->next )
|
||||
all_dirs = all_dirs->next;
|
||||
all_dirs->next = media_dir;
|
||||
}
|
||||
else
|
||||
media_dirs = media_dir;
|
||||
break;
|
||||
case UPNPALBUMART_NAMES:
|
||||
for (string = ary_options[i].value; (word = strtok(string, "/")); string = NULL)
|
||||
@ -654,17 +631,15 @@ init(int argc, char * * argv)
|
||||
this_name->wildcard = 1;
|
||||
}
|
||||
this_name->name = strdup(word);
|
||||
if( !album_art_names )
|
||||
{
|
||||
album_art_names = this_name;
|
||||
}
|
||||
else
|
||||
if (album_art_names)
|
||||
{
|
||||
struct album_art_name_s * all_names = album_art_names;
|
||||
while( all_names->next )
|
||||
all_names = all_names->next;
|
||||
all_names->next = this_name;
|
||||
}
|
||||
else
|
||||
album_art_names = this_name;
|
||||
}
|
||||
break;
|
||||
case UPNPDBDIR:
|
||||
@ -673,10 +648,7 @@ init(int argc, char * * argv)
|
||||
path = (ary_options[i].value);
|
||||
make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
|
||||
if (access(path, F_OK) != 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Database path not accessible! [%s]\n", path);
|
||||
break;
|
||||
}
|
||||
strncpyt(db_path, path, PATH_MAX);
|
||||
break;
|
||||
case UPNPLOGDIR:
|
||||
@ -685,10 +657,7 @@ init(int argc, char * * argv)
|
||||
path = (ary_options[i].value);
|
||||
make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
|
||||
if (access(path, F_OK) != 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Log path not accessible! [%s]\n", path);
|
||||
break;
|
||||
}
|
||||
strncpyt(log_path, path, PATH_MAX);
|
||||
break;
|
||||
case UPNPLOGLEVEL:
|
||||
@ -707,8 +676,7 @@ init(int argc, char * * argv)
|
||||
SETFLAG(DLNA_STRICT_MASK);
|
||||
break;
|
||||
case ROOT_CONTAINER:
|
||||
switch( ary_options[i].value[0] )
|
||||
{
|
||||
switch (ary_options[i].value[0]) {
|
||||
case '.':
|
||||
runtime_vars.root_container = NULL;
|
||||
break;
|
||||
@ -742,7 +710,8 @@ init(int argc, char * * argv)
|
||||
break;
|
||||
case USER_ACCOUNT:
|
||||
uid = strtol(ary_options[i].value, &string, 0);
|
||||
if (*string) {
|
||||
if (*string)
|
||||
{
|
||||
/* Symbolic username given, not UID. */
|
||||
struct passwd *entry = getpwnam(ary_options[i].value);
|
||||
if (!entry)
|
||||
@ -755,7 +724,6 @@ init(int argc, char * * argv)
|
||||
optionsfile);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (log_path[0] == '\0')
|
||||
{
|
||||
if (db_path[0] == '\0')
|
||||
@ -850,11 +818,9 @@ init(int argc, char * * argv)
|
||||
n_lan_addr++;
|
||||
}
|
||||
else
|
||||
{
|
||||
DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n",
|
||||
MAX_LAN_ADDR, argv[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
|
||||
break;
|
||||
@ -865,15 +831,13 @@ init(int argc, char * * argv)
|
||||
int j;
|
||||
i++;
|
||||
if (getifaddr(argv[i], ip_addr, sizeof(ip_addr)) < 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Required network interface '%s' not found.\n",
|
||||
argv[i]);
|
||||
}
|
||||
for (j=0; j<n_lan_addr; j++)
|
||||
{
|
||||
struct lan_addr_s tmpaddr;
|
||||
parselanaddr(&tmpaddr, ip_addr);
|
||||
if(0 == strcmp(lan_addr[j].str, tmpaddr.str))
|
||||
if(strcmp(lan_addr[j].str, tmpaddr.str) == 0)
|
||||
address_already_there = 1;
|
||||
}
|
||||
if (address_already_there)
|
||||
@ -884,11 +848,9 @@ init(int argc, char * * argv)
|
||||
n_lan_addr++;
|
||||
}
|
||||
else
|
||||
{
|
||||
DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n",
|
||||
MAX_LAN_ADDR, argv[i]);
|
||||
}
|
||||
}
|
||||
else
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
|
||||
break;
|
||||
@ -904,12 +866,12 @@ init(int argc, char * * argv)
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Failed to clean old file cache. EXITING\n");
|
||||
break;
|
||||
case 'u':
|
||||
if(i+1 == argc)
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
|
||||
else {
|
||||
if (i+1 != argc)
|
||||
{
|
||||
i++;
|
||||
uid = strtol(argv[i], &string, 0);
|
||||
if (*string) {
|
||||
if (*string)
|
||||
{
|
||||
/* Symbolic username given, not UID. */
|
||||
struct passwd *entry = getpwnam(argv[i]);
|
||||
if (!entry)
|
||||
@ -917,6 +879,9 @@ init(int argc, char * * argv)
|
||||
uid = entry->pw_uid;
|
||||
}
|
||||
}
|
||||
else
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]);
|
||||
break;
|
||||
break;
|
||||
#ifdef __linux__
|
||||
case 'S':
|
||||
@ -938,15 +903,13 @@ init(int argc, char * * argv)
|
||||
(getifaddr("eth0", ip_addr, sizeof(ip_addr)) < 0) &&
|
||||
(getifaddr("eth1", ip_addr, sizeof(ip_addr)) < 0))
|
||||
{
|
||||
DPRINTF(E_OFF, L_GENERAL, "No IP address automatically detected!\n");
|
||||
DPRINTF(E_OFF, L_GENERAL, "No IP address automatically detected\n");
|
||||
}
|
||||
if (*ip_addr && parselanaddr(&lan_addr[n_lan_addr], ip_addr) == 0)
|
||||
{
|
||||
n_lan_addr++;
|
||||
}
|
||||
}
|
||||
|
||||
if( (n_lan_addr==0) || (runtime_vars.port<=0) )
|
||||
if (!n_lan_addr || runtime_vars.port <= 0)
|
||||
{
|
||||
DPRINTF(E_ERROR, L_GENERAL, "Usage:\n\t"
|
||||
"%s [-d] [-v] [-f config_file]\n"
|
||||
@ -977,9 +940,7 @@ init(int argc, char * * argv)
|
||||
log_level = log_str;
|
||||
}
|
||||
else if (!log_level)
|
||||
{
|
||||
log_level = log_str;
|
||||
}
|
||||
|
||||
/* Set the default log file path to NULL (stdout) */
|
||||
path = NULL;
|
||||
@ -1022,7 +983,7 @@ init(int argc, char * * argv)
|
||||
else
|
||||
strcpy(presentationurl, "/");
|
||||
|
||||
/* set signal handler */
|
||||
/* set signal handlers */
|
||||
memset(&sa, 0, sizeof(struct sigaction));
|
||||
sa.sa_handler = sigterm;
|
||||
if (sigaction(SIGTERM, &sa, NULL))
|
||||
@ -1047,7 +1008,7 @@ init(int argc, char * * argv)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
int ret, i;
|
||||
int sudp = -1, shttpl = -1;
|
||||
int snotify[MAX_LAN_ADDR];
|
||||
LIST_HEAD(httplisthead, upnphttp) upnphttphead;
|
||||
@ -1077,7 +1038,8 @@ main(int argc, char * * argv)
|
||||
textdomain("minidlna");
|
||||
#endif
|
||||
|
||||
if (init(argc, argv) != 0)
|
||||
ret = init(argc, argv);
|
||||
if (ret != 0)
|
||||
return 1;
|
||||
|
||||
DPRINTF(E_WARN, L_GENERAL, "Starting " SERVER_NAME " version " MINIDLNA_VERSION ".\n");
|
||||
@ -1088,73 +1050,62 @@ main(int argc, char * * argv)
|
||||
|
||||
LIST_INIT(&upnphttphead);
|
||||
|
||||
i = open_db(NULL);
|
||||
if( i == 0 )
|
||||
ret = open_db(NULL);
|
||||
if (ret == 0)
|
||||
{
|
||||
updateID = sql_get_int_field(db, "SELECT UPDATE_ID from SETTINGS");
|
||||
check_db(db, i, &scanner_pid);
|
||||
if (updateID == -1)
|
||||
ret = -1;
|
||||
}
|
||||
check_db(db, ret, &scanner_pid);
|
||||
signal(SIGCHLD, &sigchld);
|
||||
#ifdef HAVE_INOTIFY
|
||||
if( GETFLAG(INOTIFY_MASK) )
|
||||
{
|
||||
if (!sqlite3_threadsafe() || sqlite3_libversion_number() < 3005001)
|
||||
{
|
||||
DPRINTF(E_ERROR, L_GENERAL, "SQLite library is not threadsafe! "
|
||||
"Inotify will be disabled.\n");
|
||||
}
|
||||
else if( pthread_create(&inotify_thread, NULL, start_inotify, NULL) )
|
||||
{
|
||||
else if (pthread_create(&inotify_thread, NULL, start_inotify, NULL) != 0)
|
||||
DPRINTF(E_FATAL, L_GENERAL, "ERROR: pthread_create() failed for start_inotify. EXITING\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
sudp = OpenAndConfSSDPReceiveSocket();
|
||||
if (sudp < 0)
|
||||
{
|
||||
DPRINTF(E_INFO, L_GENERAL, "Failed to open socket for receiving SSDP. Trying to use MiniSSDPd\n");
|
||||
if(SubmitServicesToMiniSSDPD(lan_addr[0].str, runtime_vars.port) < 0) {
|
||||
if (SubmitServicesToMiniSSDPD(lan_addr[0].str, runtime_vars.port) < 0)
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Failed to connect to MiniSSDPd. EXITING");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* open socket for HTTP connections. Listen on the 1st LAN address */
|
||||
shttpl = OpenAndConfHTTPSocket(runtime_vars.port);
|
||||
if (shttpl < 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Failed to open socket for HTTP. EXITING\n");
|
||||
}
|
||||
DPRINTF(E_WARN, L_GENERAL, "HTTP listening on port %d\n", runtime_vars.port);
|
||||
|
||||
/* open socket for sending notifications */
|
||||
if (OpenAndConfSSDPNotifySockets(snotify) < 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Failed to open sockets for sending SSDP notify "
|
||||
"messages. EXITING\n");
|
||||
}
|
||||
|
||||
#ifdef TIVO_SUPPORT
|
||||
if (GETFLAG(TIVO_MASK))
|
||||
{
|
||||
DPRINTF(E_WARN, L_GENERAL, "TiVo support is enabled.\n");
|
||||
/* Add TiVo-specific randomize function to sqlite */
|
||||
if( sqlite3_create_function(db, "tivorandom", 1, SQLITE_UTF8, NULL, &TiVoRandomSeedFunc, NULL, NULL) != SQLITE_OK )
|
||||
{
|
||||
ret = sqlite3_create_function(db, "tivorandom", 1, SQLITE_UTF8, NULL, &TiVoRandomSeedFunc, NULL, NULL);
|
||||
if (ret != SQLITE_OK)
|
||||
DPRINTF(E_ERROR, L_TIVO, "ERROR: Failed to add sqlite randomize function for TiVo!\n");
|
||||
}
|
||||
/* open socket for sending Tivo notifications */
|
||||
sbeacon = OpenAndConfTivoBeaconSocket();
|
||||
if(sbeacon < 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_GENERAL, "Failed to open sockets for sending Tivo beacon notify "
|
||||
"messages. EXITING\n");
|
||||
}
|
||||
tivo_bcast.sin_family = AF_INET;
|
||||
tivo_bcast.sin_addr.s_addr = htonl(getBcastAddress());
|
||||
tivo_bcast.sin_port = htons(2190);
|
||||
}
|
||||
else
|
||||
{
|
||||
sbeacon = -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
SendSSDPGoodbye(snotify, n_lan_addr);
|
||||
@ -1193,10 +1144,8 @@ main(int argc, char * * argv)
|
||||
timeout.tv_sec--;
|
||||
}
|
||||
else
|
||||
{
|
||||
timeout.tv_usec = lastnotifytime.tv_usec - timeofday.tv_usec;
|
||||
}
|
||||
}
|
||||
#ifdef TIVO_SUPPORT
|
||||
if (GETFLAG(TIVO_MASK))
|
||||
{
|
||||
@ -1212,21 +1161,17 @@ main(int argc, char * * argv)
|
||||
/* Beacons should be sent every 5 seconds or so for the first minute,
|
||||
* then every minute or so thereafter. */
|
||||
if (beacon_interval == 5 && (timeofday.tv_sec - startup_time) > 60)
|
||||
{
|
||||
beacon_interval = 60;
|
||||
}
|
||||
}
|
||||
else if (timeout.tv_sec > (lastbeacontime.tv_sec + beacon_interval + 1 - timeofday.tv_sec))
|
||||
{
|
||||
timeout.tv_sec = lastbeacontime.tv_sec + beacon_interval - timeofday.tv_sec;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (scanning)
|
||||
{
|
||||
if( !scanner_pid || kill(scanner_pid, 0) )
|
||||
if (!scanner_pid || kill(scanner_pid, 0) != 0)
|
||||
{
|
||||
scanning = 0;
|
||||
updateID++;
|
||||
@ -1267,14 +1212,13 @@ main(int argc, char * * argv)
|
||||
#ifdef DEBUG
|
||||
/* for debug */
|
||||
if (i > 1)
|
||||
{
|
||||
DPRINTF(E_DEBUG, L_GENERAL, "%d active incoming HTTP connections\n", i);
|
||||
}
|
||||
#endif
|
||||
FD_ZERO(&writeset);
|
||||
upnpevents_selectfds(&readset, &writeset, &max_fd);
|
||||
|
||||
if(select(max_fd+1, &readset, &writeset, 0, &timeout) < 0)
|
||||
ret = select(max_fd+1, &readset, &writeset, 0, &timeout);
|
||||
if (ret < 0)
|
||||
{
|
||||
if(quitting) goto shutdown;
|
||||
if(errno == EINTR) continue;
|
||||
@ -1310,12 +1254,9 @@ main(int argc, char * * argv)
|
||||
/* process active HTTP connections */
|
||||
for (e = upnphttphead.lh_first; e != NULL; e = e->entries.le_next)
|
||||
{
|
||||
if( (e->socket >= 0) && (e->state <= 2)
|
||||
&&(FD_ISSET(e->socket, &readset)) )
|
||||
{
|
||||
if ((e->socket >= 0) && (e->state <= 2) && (FD_ISSET(e->socket, &readset)))
|
||||
Process_upnphttp(e);
|
||||
}
|
||||
}
|
||||
/* process incoming HTTP connections */
|
||||
if (shttpl >= 0 && FD_ISSET(shttpl, &readset))
|
||||
{
|
||||
@ -1353,7 +1294,7 @@ main(int argc, char * * argv)
|
||||
}
|
||||
}
|
||||
/* delete finished HTTP connections */
|
||||
for(e = upnphttphead.lh_first; e != NULL; )
|
||||
for (e = upnphttphead.lh_first; e != NULL; e = next)
|
||||
{
|
||||
next = e->entries.le_next;
|
||||
if(e->state >= 100)
|
||||
@ -1361,7 +1302,6 @@ main(int argc, char * * argv)
|
||||
LIST_REMOVE(e, entries);
|
||||
Delete_upnphttp(e);
|
||||
}
|
||||
e = next;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user