Merge branch 'minidlna' into tomato-ND-USBmod
[tomato.git] / release / src / router / minidlna / minidlna.c
blobf9ea27f3138ece1245fbb89abe74fb88a19a996b
1 /* MiniDLNA project
3 * http://sourceforge.net/projects/minidlna/
5 * MiniDLNA media server
6 * Copyright (C) 2008-2009 Justin Maggard
8 * This file is part of MiniDLNA.
10 * MiniDLNA is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * MiniDLNA is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
22 * Portions of the code from the MiniUPnP project:
24 * Copyright (c) 2006-2007, Thomas Bernard
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions are met:
29 * * Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * * Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * * The name of the author may not be used to endorse or promote products
35 * derived from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
41 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 * POSSIBILITY OF SUCH DAMAGE.
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <stdio.h>
53 #include <ctype.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58 #include <fcntl.h>
59 #include <sys/file.h>
60 #include <sys/time.h>
61 #include <time.h>
62 #include <signal.h>
63 #include <sys/param.h>
64 #include <errno.h>
65 #include <pthread.h>
66 #include <pwd.h>
68 #include "config.h"
70 #ifdef ENABLE_NLS
71 #include <libintl.h>
72 #endif
74 #include "upnpglobalvars.h"
75 #include "sql.h"
76 #include "upnphttp.h"
77 #include "upnpdescgen.h"
78 #include "minidlnapath.h"
79 #include "getifaddr.h"
80 #include "upnpsoap.h"
81 #include "options.h"
82 #include "utils.h"
83 #include "minissdp.h"
84 #include "minidlnatypes.h"
85 #include "daemonize.h"
86 #include "upnpevents.h"
87 #include "scanner.h"
88 #include "inotify.h"
89 #include "log.h"
90 #ifdef TIVO_SUPPORT
91 #include "tivo_beacon.h"
92 #include "tivo_utils.h"
93 #endif
95 #if SQLITE_VERSION_NUMBER < 3005001
96 # warning "Your SQLite3 library appears to be too old! Please use 3.5.1 or newer."
97 # define sqlite3_threadsafe() 0
98 #endif
100 /* OpenAndConfHTTPSocket() :
101 * setup the socket used to handle incoming HTTP connections. */
102 static int
103 OpenAndConfHTTPSocket(unsigned short port)
105 int s;
106 int i = 1;
107 struct sockaddr_in listenname;
109 /* Initialize client type cache */
110 memset(&clients, 0, sizeof(struct client_cache_s));
112 if( (s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
114 DPRINTF(E_ERROR, L_GENERAL, "socket(http): %s\n", strerror(errno));
115 return -1;
118 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
120 DPRINTF(E_WARN, L_GENERAL, "setsockopt(http, SO_REUSEADDR): %s\n", strerror(errno));
123 memset(&listenname, 0, sizeof(struct sockaddr_in));
124 listenname.sin_family = AF_INET;
125 listenname.sin_port = htons(port);
126 listenname.sin_addr.s_addr = htonl(INADDR_ANY);
128 if(bind(s, (struct sockaddr *)&listenname, sizeof(struct sockaddr_in)) < 0)
130 DPRINTF(E_ERROR, L_GENERAL, "bind(http): %s\n", strerror(errno));
131 close(s);
132 return -1;
135 if(listen(s, 6) < 0)
137 DPRINTF(E_ERROR, L_GENERAL, "listen(http): %s\n", strerror(errno));
138 close(s);
139 return -1;
142 return s;
145 /* Handler for the SIGTERM signal (kill)
146 * SIGINT is also handled */
147 static void
148 sigterm(int sig)
150 /*int save_errno = errno;*/
151 signal(sig, SIG_IGN); /* Ignore this signal while we are quitting */
153 DPRINTF(E_WARN, L_GENERAL, "received signal %d, good-bye\n", sig);
155 quitting = 1;
156 /*errno = save_errno;*/
159 /* record the startup time, for returning uptime */
160 static void
161 set_startup_time(void)
163 startup_time = time(NULL);
166 /* parselanaddr()
167 * parse address with mask
168 * ex: 192.168.1.1/24
169 * return value :
170 * 0 : ok
171 * -1 : error */
172 static int
173 parselanaddr(struct lan_addr_s * lan_addr, const char * str)
175 const char * p;
176 int nbits = 24;
177 int n;
178 p = str;
179 while(*p && *p != '/' && !isspace(*p))
180 p++;
181 n = p - str;
182 if(*p == '/')
184 nbits = atoi(++p);
185 while(*p && !isspace(*p))
186 p++;
188 if(n>15)
190 DPRINTF(E_OFF, L_GENERAL, "Error parsing address/mask: %s\n", str);
191 return -1;
193 memcpy(lan_addr->str, str, n);
194 lan_addr->str[n] = '\0';
195 if(!inet_aton(lan_addr->str, &lan_addr->addr))
197 DPRINTF(E_OFF, L_GENERAL, "Error parsing address/mask: %s\n", str);
198 return -1;
200 lan_addr->mask.s_addr = htonl(nbits ? (0xffffffff << (32 - nbits)) : 0);
201 return 0;
204 void
205 getfriendlyname(char * buf, int len)
207 char * dot = NULL;
208 char * hn = calloc(1, 256);
209 int off;
211 if( gethostname(hn, 256) == 0 )
213 strncpy(buf, hn, len-1);
214 buf[len] = '\0';
215 dot = strchr(buf, '.');
216 if( dot )
217 *dot = '\0';
219 else
221 strcpy(buf, "Unknown");
223 free(hn);
225 off = strlen(buf);
226 off += snprintf(buf+off, len-off, ": ");
227 #ifdef READYNAS
228 FILE * info;
229 char ibuf[64], *key, *val;
230 snprintf(buf+off, len-off, "ReadyNAS");
231 info = fopen("/proc/sys/dev/boot/info", "r");
232 if( !info )
233 return;
234 while( (val = fgets(ibuf, 64, info)) != NULL )
236 key = strsep(&val, ": \t");
237 val = trim(val);
238 if( strcmp(key, "model") == 0 )
240 snprintf(buf+off, len-off, "%s", val);
241 key = strchr(val, ' ');
242 if( key )
244 strncpy(modelnumber, key+1, MODELNUMBER_MAX_LEN);
245 modelnumber[MODELNUMBER_MAX_LEN-1] = '\0';
246 *key = '\0';
248 snprintf(modelname, MODELNAME_MAX_LEN,
249 "Windows Media Connect compatible (%s)", val);
251 else if( strcmp(key, "serial") == 0 )
253 strncpy(serialnumber, val, SERIALNUMBER_MAX_LEN);
254 serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0';
255 if( serialnumber[0] == '\0' )
257 char mac_str[13];
258 if( getsyshwaddr(mac_str, sizeof(mac_str)) == 0 )
259 strcpy(serialnumber, mac_str);
260 else
261 strcpy(serialnumber, "0");
263 break;
266 fclose(info);
267 if( strcmp(modelnumber, "NVX") == 0 )
268 memcpy(pnpx_hwid+4, "01F2&amp;DEV_0101", 17);
269 else if( strcmp(modelnumber, "Pro") == 0 ||
270 strcmp(modelnumber, "Pro 6") == 0 ||
271 strncmp(modelnumber, "Ultra 6", 7) == 0 )
272 memcpy(pnpx_hwid+4, "01F2&amp;DEV_0102", 17);
273 else if( strcmp(modelnumber, "Pro 2") == 0 ||
274 strncmp(modelnumber, "Ultra 2", 7) == 0 )
275 memcpy(pnpx_hwid+4, "01F2&amp;DEV_0103", 17);
276 else if( strcmp(modelnumber, "Pro 4") == 0 ||
277 strncmp(modelnumber, "Ultra 4", 7) == 0 )
278 memcpy(pnpx_hwid+4, "01F2&amp;DEV_0104", 17);
279 else if( strcmp(modelnumber+1, "100") == 0 )
280 memcpy(pnpx_hwid+4, "01F2&amp;DEV_0105", 17);
281 else if( strcmp(modelnumber+1, "200") == 0 )
282 memcpy(pnpx_hwid+4, "01F2&amp;DEV_0106", 17);
283 #else
284 char * logname;
285 logname = getenv("LOGNAME");
286 #ifndef STATIC // Disable for static linking
287 if( !logname )
289 struct passwd * pwent;
290 pwent = getpwuid(getuid());
291 if( pwent )
292 logname = pwent->pw_name;
294 #endif
295 snprintf(buf+off, len-off, "%s", logname?logname:"Unknown");
296 #endif
300 open_db(void)
302 char path[PATH_MAX];
303 int new_db = 0;
305 snprintf(path, sizeof(path), "%s/files.db", db_path);
306 if( access(path, F_OK) != 0 )
308 new_db = 1;
309 make_dir(db_path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
311 if( sqlite3_open(path, &db) != SQLITE_OK )
313 DPRINTF(E_FATAL, L_GENERAL, "ERROR: Failed to open sqlite database! Exiting...\n");
315 sqlite3_busy_timeout(db, 5000);
316 sql_exec(db, "pragma page_size = 4096");
317 sql_exec(db, "pragma journal_mode = OFF");
318 sql_exec(db, "pragma synchronous = OFF;");
320 // this sets the sqlite database cache size
321 // original code had 8192 = 32MB - reduce it to 4MB
322 sql_exec(db, "pragma default_cache_size = 1024;");
323 return new_db;
326 /* init phase :
327 * 1) read configuration file
328 * 2) read command line arguments
329 * 3) daemonize
330 * 4) check and write pid file
331 * 5) set startup time stamp
332 * 6) compute presentation URL
333 * 7) set signal handlers */
334 static int
335 init(int argc, char * * argv)
337 int i;
338 int pid;
339 int debug_flag = 0;
340 int options_flag = 0;
341 struct sigaction sa;
342 /*const char * logfilename = 0;*/
343 const char * presurl = 0;
344 const char * optionsfile = "/etc/minidlna.conf";
345 char mac_str[13];
346 char * string, * word;
347 enum media_types type;
348 char * path;
349 char real_path[PATH_MAX];
350 char ext_ip_addr[INET_ADDRSTRLEN] = {'\0'};
352 /* first check if "-f" option is used */
353 for(i=2; i<argc; i++)
355 if(0 == strcmp(argv[i-1], "-f"))
357 optionsfile = argv[i];
358 options_flag = 1;
359 break;
363 /* set up uuid based on mac address */
364 if( getsyshwaddr(mac_str, sizeof(mac_str)) < 0 )
366 DPRINTF(E_OFF, L_GENERAL, "No MAC address found. Falling back to generic UUID.\n");
367 strcpy(mac_str, "554e4b4e4f57");
369 strcpy(uuidvalue+5, "4d696e69-444c-164e-9d41-");
370 strncat(uuidvalue, mac_str, 12);
372 getfriendlyname(friendly_name, FRIENDLYNAME_MAX_LEN);
374 runtime_vars.port = -1;
375 runtime_vars.notify_interval = 895; /* seconds between SSDP announces */
377 /* read options file first since
378 * command line arguments have final say */
379 if(readoptionsfile(optionsfile) < 0)
381 /* only error if file exists or using -f */
382 if(access(optionsfile, F_OK) == 0 || options_flag)
383 fprintf(stderr, "Error reading configuration file %s\n", optionsfile);
385 else
387 for(i=0; i<num_options; i++)
389 switch(ary_options[i].id)
391 case UPNPIFNAME:
392 if(getifaddr(ary_options[i].value, ext_ip_addr, INET_ADDRSTRLEN) >= 0)
394 if( *ext_ip_addr && parselanaddr(&lan_addr[n_lan_addr], ext_ip_addr) == 0 )
395 n_lan_addr++;
397 else
398 fprintf(stderr, "Interface %s not found, ignoring.\n", ary_options[i].value);
399 break;
400 case UPNPLISTENING_IP:
401 if(n_lan_addr < MAX_LAN_ADDR)
403 if(parselanaddr(&lan_addr[n_lan_addr],
404 ary_options[i].value) == 0)
405 n_lan_addr++;
407 else
409 fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n",
410 MAX_LAN_ADDR, ary_options[i].value);
412 break;
413 case UPNPPORT:
414 runtime_vars.port = atoi(ary_options[i].value);
415 break;
416 case UPNPPRESENTATIONURL:
417 presurl = ary_options[i].value;
418 break;
419 case UPNPNOTIFY_INTERVAL:
420 runtime_vars.notify_interval = atoi(ary_options[i].value);
421 break;
422 case UPNPSERIAL:
423 strncpy(serialnumber, ary_options[i].value, SERIALNUMBER_MAX_LEN);
424 serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0';
425 break;
426 case UPNPMODEL_NAME:
427 strncpy(modelname, ary_options[i].value, MODELNAME_MAX_LEN);
428 modelname[MODELNAME_MAX_LEN-1] = '\0';
429 break;
430 case UPNPMODEL_NUMBER:
431 strncpy(modelnumber, ary_options[i].value, MODELNUMBER_MAX_LEN);
432 modelnumber[MODELNUMBER_MAX_LEN-1] = '\0';
433 break;
434 case UPNPFRIENDLYNAME:
435 strncpy(friendly_name, ary_options[i].value, FRIENDLYNAME_MAX_LEN);
436 friendly_name[FRIENDLYNAME_MAX_LEN-1] = '\0';
437 break;
438 case UPNPMEDIADIR:
439 type = ALL_MEDIA;
440 char * myval = NULL;
441 switch( ary_options[i].value[0] )
443 case 'A':
444 case 'a':
445 if( ary_options[i].value[0] == 'A' || ary_options[i].value[0] == 'a' )
446 type = AUDIO_ONLY;
447 case 'V':
448 case 'v':
449 if( ary_options[i].value[0] == 'V' || ary_options[i].value[0] == 'v' )
450 type = VIDEO_ONLY;
451 case 'P':
452 case 'p':
453 if( ary_options[i].value[0] == 'P' || ary_options[i].value[0] == 'p' )
454 type = IMAGES_ONLY;
455 myval = index(ary_options[i].value, '/');
456 case '/':
457 path = realpath(myval ? myval:ary_options[i].value, real_path);
458 if( !path )
459 path = (myval ? myval:ary_options[i].value);
460 if( access(path, F_OK) != 0 )
462 fprintf(stderr, "Media directory not accessible! [%s]\n",
463 path);
464 break;
466 struct media_dir_s * this_dir = calloc(1, sizeof(struct media_dir_s));
467 this_dir->path = strdup(path);
468 this_dir->type = type;
469 if( !media_dirs )
471 media_dirs = this_dir;
473 else
475 struct media_dir_s * all_dirs = media_dirs;
476 while( all_dirs->next )
477 all_dirs = all_dirs->next;
478 all_dirs->next = this_dir;
480 break;
481 default:
482 fprintf(stderr, "Media directory entry not understood! [%s]\n",
483 ary_options[i].value);
484 break;
486 break;
487 case UPNPALBUMART_NAMES:
488 for( string = ary_options[i].value; (word = strtok(string, "/")); string = NULL ) {
489 struct album_art_name_s * this_name = calloc(1, sizeof(struct album_art_name_s));
490 int len = strlen(word);
491 if( word[len-1] == '*' )
493 word[len-1] = '\0';
494 this_name->wildcard = 1;
496 this_name->name = strdup(word);
497 if( !album_art_names )
499 album_art_names = this_name;
501 else
503 struct album_art_name_s * all_names = album_art_names;
504 while( all_names->next )
505 all_names = all_names->next;
506 all_names->next = this_name;
509 break;
510 case UPNPDBDIR:
511 path = realpath(ary_options[i].value, real_path);
512 if( !path )
513 path = (ary_options[i].value);
514 make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
515 if( access(path, F_OK) != 0 )
517 DPRINTF(E_FATAL, L_GENERAL, "Database path not accessible! [%s]\n", path);
518 break;
520 strncpy(db_path, path, PATH_MAX);
521 break;
522 case UPNPLOGDIR:
523 path = realpath(ary_options[i].value, real_path);
524 if( !path )
525 path = (ary_options[i].value);
526 make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
527 if( access(path, F_OK) != 0 )
529 DPRINTF(E_FATAL, L_GENERAL, "Log path not accessible! [%s]\n", path);
530 break;
532 strncpy(log_path, path, PATH_MAX);
533 break;
534 case UPNPINOTIFY:
535 if( (strcmp(ary_options[i].value, "yes") != 0) && !atoi(ary_options[i].value) )
536 CLEARFLAG(INOTIFY_MASK);
537 break;
538 case ENABLE_TIVO:
539 if( (strcmp(ary_options[i].value, "yes") == 0) || atoi(ary_options[i].value) )
540 SETFLAG(TIVO_MASK);
541 break;
542 case ENABLE_DLNA_STRICT:
543 if( (strcmp(ary_options[i].value, "yes") == 0) || atoi(ary_options[i].value) )
544 SETFLAG(DLNA_STRICT_MASK);
545 break;
546 default:
547 fprintf(stderr, "Unknown option in file %s\n",
548 optionsfile);
552 if( log_path[0] == '\0' )
554 if( db_path[0] == '\0' )
555 strncpy(log_path, DEFAULT_LOG_PATH, PATH_MAX);
556 else
557 strncpy(log_path, db_path, PATH_MAX);
559 if( db_path[0] == '\0' )
560 strncpy(db_path, DEFAULT_DB_PATH, PATH_MAX);
562 /* command line arguments processing */
563 for(i=1; i<argc; i++)
565 if(argv[i][0]!='-')
567 fprintf(stderr, "Unknown option: %s\n", argv[i]);
569 else if(strcmp(argv[i], "--help")==0)
571 runtime_vars.port = -1;
572 break;
574 else switch(argv[i][1])
576 case 't':
577 if(i+1 < argc)
578 runtime_vars.notify_interval = atoi(argv[++i]);
579 else
580 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
581 break;
582 case 's':
583 if(i+1 < argc)
584 strncpy(serialnumber, argv[++i], SERIALNUMBER_MAX_LEN);
585 else
586 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
587 serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0';
588 break;
589 case 'm':
590 if(i+1 < argc)
591 strncpy(modelnumber, argv[++i], MODELNUMBER_MAX_LEN);
592 else
593 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
594 modelnumber[MODELNUMBER_MAX_LEN-1] = '\0';
595 break;
596 /*case 'l':
597 logfilename = argv[++i];
598 break;*/
599 case 'p':
600 if(i+1 < argc)
601 runtime_vars.port = atoi(argv[++i]);
602 else
603 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
604 break;
605 case 'P':
606 if(i+1 < argc)
607 pidfilename = argv[++i];
608 else
609 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
610 break;
611 case 'd':
612 debug_flag = 1;
613 break;
614 case 'w':
615 if(i+1 < argc)
616 presurl = argv[++i];
617 else
618 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
619 break;
620 case 'a':
621 if(i+1 < argc)
623 int address_already_there = 0;
624 int j;
625 i++;
626 for(j=0; j<n_lan_addr; j++)
628 struct lan_addr_s tmpaddr;
629 parselanaddr(&tmpaddr, argv[i]);
630 if(0 == strcmp(lan_addr[j].str, tmpaddr.str))
631 address_already_there = 1;
633 if(address_already_there)
634 break;
635 if(n_lan_addr < MAX_LAN_ADDR)
637 if(parselanaddr(&lan_addr[n_lan_addr], argv[i]) == 0)
638 n_lan_addr++;
640 else
642 fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n",
643 MAX_LAN_ADDR, argv[i]);
646 else
647 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
648 break;
649 case 'i':
650 if(i+1 < argc)
652 int address_already_there = 0;
653 int j;
654 i++;
655 if( getifaddr(argv[i], ext_ip_addr, INET_ADDRSTRLEN) < 0 )
657 fprintf(stderr, "Network interface '%s' not found.\n",
658 argv[i]);
659 exit(-1);
661 for(j=0; j<n_lan_addr; j++)
663 struct lan_addr_s tmpaddr;
664 parselanaddr(&tmpaddr, ext_ip_addr);
665 if(0 == strcmp(lan_addr[j].str, tmpaddr.str))
666 address_already_there = 1;
668 if(address_already_there)
669 break;
670 if(n_lan_addr < MAX_LAN_ADDR)
672 if(parselanaddr(&lan_addr[n_lan_addr], ext_ip_addr) == 0)
673 n_lan_addr++;
675 else
677 fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n",
678 MAX_LAN_ADDR, argv[i]);
681 else
682 fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]);
683 break;
684 case 'f':
685 i++; /* discarding, the config file is already read */
686 break;
687 case 'h':
688 runtime_vars.port = -1; // triggers help display
689 break;
690 case 'R':
691 snprintf(real_path, sizeof(real_path), "rm -rf %s/files.db %s/art_cache", db_path, db_path);
692 system(real_path);
693 break;
694 case 'V':
695 printf("Version " MINIDLNA_VERSION "\n");
696 exit(0);
697 break;
698 default:
699 fprintf(stderr, "Unknown option: %s\n", argv[i]);
702 /* If no IP was specified, try to detect one */
703 if( n_lan_addr < 1 )
705 if( (getsysaddr(ext_ip_addr, INET_ADDRSTRLEN) < 0) &&
706 (getifaddr("eth0", ext_ip_addr, INET_ADDRSTRLEN) < 0) &&
707 (getifaddr("eth1", ext_ip_addr, INET_ADDRSTRLEN) < 0) )
709 DPRINTF(E_OFF, L_GENERAL, "No IP address automatically detected!\n");
711 if( *ext_ip_addr && parselanaddr(&lan_addr[n_lan_addr], ext_ip_addr) == 0 )
713 n_lan_addr++;
717 if( (n_lan_addr==0) || (runtime_vars.port<0) )
719 fprintf(stderr, "Usage:\n\t"
720 "%s [-d] [-f config_file]\n"
721 "\t\t[-a listening_ip] [-p port]\n"
722 /*"[-l logfile] " not functionnal */
723 "\t\t[-s serial] [-m model_number] \n"
724 "\t\t[-t notify_interval] [-P pid_filename]\n"
725 "\t\t[-w url] [-R] [-V] [-h]\n"
726 "\nNotes:\n\tNotify interval is in seconds. Default is 895 seconds.\n"
727 "\tDefault pid file is %s.\n"
728 "\tWith -d minidlna will run in debug mode (not daemonize).\n"
729 "\t-w sets the presentation url. Default is http address on port 80\n"
730 "\t-h displays this text\n"
731 "\t-R forces a full rescan\n"
732 "\t-V print the version number\n",
733 argv[0], pidfilename);
734 return 1;
737 if(debug_flag)
739 pid = getpid();
740 log_init(NULL, "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=debug");
742 else
744 #ifdef USE_DAEMON
745 if(daemon(0, 0)<0) {
746 perror("daemon()");
748 pid = getpid();
749 #else
750 pid = daemonize();
751 #endif
752 #ifdef READYNAS
753 log_init("/var/log/upnp-av.log", "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn");
754 #else
755 if( access(db_path, F_OK) != 0 )
756 make_dir(db_path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
757 sprintf(real_path, "%s/minidlna.log", log_path);
758 log_init(real_path, "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn");
759 #endif
762 if(checkforrunning(pidfilename) < 0)
764 DPRINTF(E_ERROR, L_GENERAL, "MiniDLNA is already running. EXITING.\n");
765 return 1;
768 set_startup_time();
770 /* presentation url */
771 if(presurl)
773 strncpy(presentationurl, presurl, PRESENTATIONURL_MAX_LEN);
774 presentationurl[PRESENTATIONURL_MAX_LEN-1] = '\0';
776 else
778 #ifdef READYNAS
779 snprintf(presentationurl, PRESENTATIONURL_MAX_LEN,
780 "http://%s/admin/", lan_addr[0].str);
781 #else
782 snprintf(presentationurl, PRESENTATIONURL_MAX_LEN,
783 "http://%s/", lan_addr[0].str);
784 #endif
787 /* set signal handler */
788 signal(SIGCLD, SIG_IGN);
789 memset(&sa, 0, sizeof(struct sigaction));
790 sa.sa_handler = sigterm;
791 if (sigaction(SIGTERM, &sa, NULL))
793 DPRINTF(E_FATAL, L_GENERAL, "Failed to set SIGTERM handler. EXITING.\n");
795 if (sigaction(SIGINT, &sa, NULL))
797 DPRINTF(E_FATAL, L_GENERAL, "Failed to set SIGINT handler. EXITING.\n");
800 if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
801 DPRINTF(E_FATAL, L_GENERAL, "Failed to ignore SIGPIPE signals. EXITING.\n");
804 writepidfile(pidfilename, pid);
806 return 0;
809 /* === main === */
810 /* process HTTP or SSDP requests */
812 main(int argc, char * * argv)
814 int i;
815 int sudp = -1, shttpl = -1;
816 int snotify[MAX_LAN_ADDR];
817 LIST_HEAD(httplisthead, upnphttp) upnphttphead;
818 struct upnphttp * e = 0;
819 struct upnphttp * next;
820 fd_set readset; /* for select() */
821 fd_set writeset;
822 struct timeval timeout, timeofday, lastnotifytime = {0, 0}, lastupdatetime = {0, 0};
823 int max_fd = -1;
824 int last_changecnt = 0;
825 short int new_db = 0;
826 pid_t scanner_pid = 0;
827 pthread_t inotify_thread = 0;
828 struct media_dir_s *media_path, *last_path;
829 struct album_art_name_s *art_names, *last_name;
830 #ifdef TIVO_SUPPORT
831 unsigned short int beacon_interval = 5;
832 int sbeacon = -1;
833 struct sockaddr_in tivo_bcast;
834 struct timeval lastbeacontime = {0, 0};
835 #endif
837 #ifdef ENABLE_NLS
838 setlocale(LC_MESSAGES, "");
839 setlocale(LC_CTYPE, "en_US.utf8");
840 DPRINTF(E_DEBUG, L_GENERAL, "Using locale dir %s\n", bindtextdomain("minidlna", getenv("TEXTDOMAINDIR")));
841 textdomain("minidlna");
842 #endif
844 if(init(argc, argv) != 0)
845 return 1;
847 #ifdef READYNAS
848 DPRINTF(E_WARN, L_GENERAL, "Starting " SERVER_NAME " version " MINIDLNA_VERSION ".\n");
849 unlink("/ramfs/.upnp-av_scan");
850 #else
851 DPRINTF(E_WARN, L_GENERAL, "Starting " SERVER_NAME " version " MINIDLNA_VERSION " [SQLite %s].\n", sqlite3_libversion());
852 unlink("/var/notice/dlna");
853 if( !sqlite3_threadsafe() )
855 DPRINTF(E_ERROR, L_GENERAL, "SQLite library is not threadsafe! "
856 "Scanning must be finished before file serving can begin, "
857 "and inotify will be disabled.\n");
859 if( sqlite3_libversion_number() < 3005001 )
861 DPRINTF(E_WARN, L_GENERAL, "SQLite library is old. Please use version 3.5.1 or newer.\n");
863 #endif
864 LIST_INIT(&upnphttphead);
866 new_db = open_db();
867 if( !new_db )
869 updateID = sql_get_int_field(db, "SELECT UPDATE_ID from SETTINGS");
871 if( sql_get_int_field(db, "pragma user_version") != DB_VERSION )
873 if( new_db )
875 DPRINTF(E_WARN, L_GENERAL, "Creating new database...\n");
877 else
879 DPRINTF(E_WARN, L_GENERAL, "Database version mismatch; need to recreate...\n");
881 sqlite3_close(db);
882 char *cmd;
883 asprintf(&cmd, "rm -rf %s/files.db %s/art_cache", db_path, db_path);
884 system(cmd);
885 free(cmd);
886 open_db();
887 if( CreateDatabase() != 0 )
889 DPRINTF(E_FATAL, L_GENERAL, "ERROR: Failed to create sqlite database! Exiting...\n");
891 #if USE_FORK
892 scanning = 1;
893 sqlite3_close(db);
894 scanner_pid = fork();
895 open_db();
896 if( !scanner_pid ) // child (scanner) process
898 start_scanner();
899 sqlite3_close(db);
900 media_path = media_dirs;
901 art_names = album_art_names;
902 while( media_path )
904 free(media_path->path);
905 last_path = media_path;
906 media_path = media_path->next;
907 free(last_path);
909 while( art_names )
911 free(art_names->name);
912 last_name = art_names;
913 art_names = art_names->next;
914 free(last_name);
916 freeoptions();
917 exit(EXIT_SUCCESS);
919 #else
920 start_scanner();
921 #endif
923 if( sqlite3_threadsafe() && sqlite3_libversion_number() >= 3005001 &&
924 GETFLAG(INOTIFY_MASK) && pthread_create(&inotify_thread, NULL, start_inotify, NULL) )
926 DPRINTF(E_FATAL, L_GENERAL, "ERROR: pthread_create() failed for start_inotify.\n");
929 sudp = OpenAndConfSSDPReceiveSocket(n_lan_addr, lan_addr);
930 if(sudp < 0)
932 DPRINTF(E_FATAL, L_GENERAL, "Failed to open socket for receiving SSDP. EXITING\n");
934 /* open socket for HTTP connections. Listen on the 1st LAN address */
935 shttpl = OpenAndConfHTTPSocket((runtime_vars.port > 0) ? runtime_vars.port : 0);
936 if(shttpl < 0)
938 DPRINTF(E_FATAL, L_GENERAL, "Failed to open socket for HTTP. EXITING\n");
940 if(runtime_vars.port <= 0)
942 struct sockaddr_in sockinfo;
943 socklen_t len = sizeof(struct sockaddr_in);
944 if (getsockname(shttpl, (struct sockaddr *)&sockinfo, &len) < 0)
946 DPRINTF(E_FATAL, L_GENERAL, "getsockname(): %s. EXITING\n", strerror(errno));
948 runtime_vars.port = ntohs(sockinfo.sin_port);
950 DPRINTF(E_WARN, L_GENERAL, "HTTP listening on port %d\n", runtime_vars.port);
952 /* open socket for sending notifications */
953 if(OpenAndConfSSDPNotifySockets(snotify) < 0)
955 DPRINTF(E_FATAL, L_GENERAL, "Failed to open sockets for sending SSDP notify "
956 "messages. EXITING\n");
959 #ifdef TIVO_SUPPORT
960 if( GETFLAG(TIVO_MASK) )
962 DPRINTF(E_WARN, L_GENERAL, "TiVo support is enabled.\n");
963 /* Add TiVo-specific randomize function to sqlite */
964 if( sqlite3_create_function(db, "tivorandom", 1, SQLITE_UTF8, NULL, &TiVoRandomSeedFunc, NULL, NULL) != SQLITE_OK )
966 DPRINTF(E_ERROR, L_TIVO, "ERROR: Failed to add sqlite randomize function for TiVo!\n");
968 /* open socket for sending Tivo notifications */
969 sbeacon = OpenAndConfTivoBeaconSocket();
970 if(sbeacon < 0)
972 DPRINTF(E_FATAL, L_GENERAL, "Failed to open sockets for sending Tivo beacon notify "
973 "messages. EXITING\n");
975 tivo_bcast.sin_family = AF_INET;
976 tivo_bcast.sin_addr.s_addr = htonl(getBcastAddress());
977 tivo_bcast.sin_port = htons(2190);
979 else
981 sbeacon = -1;
983 #endif
985 SendSSDPGoodbye(snotify, n_lan_addr);
987 /* main loop */
988 while(!quitting)
990 /* Check if we need to send SSDP NOTIFY messages and do it if
991 * needed */
992 if(gettimeofday(&timeofday, 0) < 0)
994 DPRINTF(E_ERROR, L_GENERAL, "gettimeofday(): %s\n", strerror(errno));
995 timeout.tv_sec = runtime_vars.notify_interval;
996 timeout.tv_usec = 0;
998 else
1000 /* the comparaison is not very precise but who cares ? */
1001 if(timeofday.tv_sec >= (lastnotifytime.tv_sec + runtime_vars.notify_interval))
1003 SendSSDPNotifies2(snotify,
1004 (unsigned short)runtime_vars.port,
1005 (runtime_vars.notify_interval << 1)+10);
1006 memcpy(&lastnotifytime, &timeofday, sizeof(struct timeval));
1007 timeout.tv_sec = runtime_vars.notify_interval;
1008 timeout.tv_usec = 0;
1010 else
1012 timeout.tv_sec = lastnotifytime.tv_sec + runtime_vars.notify_interval
1013 - timeofday.tv_sec;
1014 if(timeofday.tv_usec > lastnotifytime.tv_usec)
1016 timeout.tv_usec = 1000000 + lastnotifytime.tv_usec
1017 - timeofday.tv_usec;
1018 timeout.tv_sec--;
1020 else
1022 timeout.tv_usec = lastnotifytime.tv_usec - timeofday.tv_usec;
1025 #ifdef TIVO_SUPPORT
1026 if( GETFLAG(TIVO_MASK) )
1028 if(timeofday.tv_sec >= (lastbeacontime.tv_sec + beacon_interval))
1030 sendBeaconMessage(sbeacon, &tivo_bcast, sizeof(struct sockaddr_in), 1);
1031 memcpy(&lastbeacontime, &timeofday, sizeof(struct timeval));
1032 if( timeout.tv_sec > beacon_interval )
1034 timeout.tv_sec = beacon_interval;
1035 timeout.tv_usec = 0;
1037 /* Beacons should be sent every 5 seconds or so for the first minute,
1038 * then every minute or so thereafter. */
1039 if( beacon_interval == 5 && (timeofday.tv_sec - startup_time) > 60 )
1041 beacon_interval = 60;
1044 else if( timeout.tv_sec > (lastbeacontime.tv_sec + beacon_interval + 1 - timeofday.tv_sec) )
1046 timeout.tv_sec = lastbeacontime.tv_sec + beacon_interval - timeofday.tv_sec;
1049 #endif
1052 if( scanning )
1054 if( !scanner_pid || kill(scanner_pid, 0) )
1055 scanning = 0;
1058 /* select open sockets (SSDP, HTTP listen, and all HTTP soap sockets) */
1059 FD_ZERO(&readset);
1061 if (sudp >= 0)
1063 FD_SET(sudp, &readset);
1064 max_fd = MAX( max_fd, sudp);
1067 if (shttpl >= 0)
1069 FD_SET(shttpl, &readset);
1070 max_fd = MAX( max_fd, shttpl);
1072 #ifdef TIVO_SUPPORT
1073 if (sbeacon >= 0)
1075 FD_SET(sbeacon, &readset);
1076 max_fd = MAX(max_fd, sbeacon);
1078 #endif
1079 i = 0; /* active HTTP connections count */
1080 for(e = upnphttphead.lh_first; e != NULL; e = e->entries.le_next)
1082 if((e->socket >= 0) && (e->state <= 2))
1084 FD_SET(e->socket, &readset);
1085 max_fd = MAX( max_fd, e->socket);
1086 i++;
1089 #ifdef DEBUG
1090 /* for debug */
1091 if(i > 1)
1093 DPRINTF(E_DEBUG, L_GENERAL, "%d active incoming HTTP connections\n", i);
1095 #endif
1096 FD_ZERO(&writeset);
1097 upnpevents_selectfds(&readset, &writeset, &max_fd);
1099 if(select(max_fd+1, &readset, &writeset, 0, &timeout) < 0)
1101 if(quitting) goto shutdown;
1102 DPRINTF(E_ERROR, L_GENERAL, "select(all): %s\n", strerror(errno));
1103 DPRINTF(E_FATAL, L_GENERAL, "Failed to select open sockets. EXITING\n");
1105 upnpevents_processfds(&readset, &writeset);
1106 /* process SSDP packets */
1107 if(sudp >= 0 && FD_ISSET(sudp, &readset))
1109 /*DPRINTF(E_DEBUG, L_GENERAL, "Received UDP Packet\n");*/
1110 ProcessSSDPRequest(sudp, (unsigned short)runtime_vars.port);
1112 #ifdef TIVO_SUPPORT
1113 if(sbeacon >= 0 && FD_ISSET(sbeacon, &readset))
1115 /*DPRINTF(E_DEBUG, L_GENERAL, "Received UDP Packet\n");*/
1116 ProcessTiVoBeacon(sbeacon);
1118 #endif
1119 /* increment SystemUpdateID if the content database has changed,
1120 * and if there is an active HTTP connection, at most once every 2 seconds */
1121 if( i && (time(NULL) >= (lastupdatetime.tv_sec + 2)) )
1123 if( sqlite3_total_changes(db) != last_changecnt )
1125 updateID++;
1126 last_changecnt = sqlite3_total_changes(db);
1127 upnp_event_var_change_notify(EContentDirectory);
1128 memcpy(&lastupdatetime, &timeofday, sizeof(struct timeval));
1131 /* process active HTTP connections */
1132 for(e = upnphttphead.lh_first; e != NULL; e = e->entries.le_next)
1134 if( (e->socket >= 0) && (e->state <= 2)
1135 &&(FD_ISSET(e->socket, &readset)) )
1137 Process_upnphttp(e);
1140 /* process incoming HTTP connections */
1141 if(shttpl >= 0 && FD_ISSET(shttpl, &readset))
1143 int shttp;
1144 socklen_t clientnamelen;
1145 struct sockaddr_in clientname;
1146 clientnamelen = sizeof(struct sockaddr_in);
1147 shttp = accept(shttpl, (struct sockaddr *)&clientname, &clientnamelen);
1148 if(shttp<0)
1150 DPRINTF(E_ERROR, L_GENERAL, "accept(http): %s\n", strerror(errno));
1152 else
1154 struct upnphttp * tmp = 0;
1155 DPRINTF(E_DEBUG, L_GENERAL, "HTTP connection from %s:%d\n",
1156 inet_ntoa(clientname.sin_addr),
1157 ntohs(clientname.sin_port) );
1158 /*if (fcntl(shttp, F_SETFL, O_NONBLOCK) < 0) {
1159 DPRINTF(E_ERROR, L_GENERAL, "fcntl F_SETFL, O_NONBLOCK");
1161 /* Create a new upnphttp object and add it to
1162 * the active upnphttp object list */
1163 tmp = New_upnphttp(shttp);
1164 if(tmp)
1166 tmp->clientaddr = clientname.sin_addr;
1167 LIST_INSERT_HEAD(&upnphttphead, tmp, entries);
1169 else
1171 DPRINTF(E_ERROR, L_GENERAL, "New_upnphttp() failed\n");
1172 close(shttp);
1176 /* delete finished HTTP connections */
1177 for(e = upnphttphead.lh_first; e != NULL; )
1179 next = e->entries.le_next;
1180 if(e->state >= 100)
1182 LIST_REMOVE(e, entries);
1183 Delete_upnphttp(e);
1185 e = next;
1189 shutdown:
1190 /* kill the scanner */
1191 if( scanning && scanner_pid )
1193 kill(scanner_pid, 9);
1195 /* close out open sockets */
1196 while(upnphttphead.lh_first != NULL)
1198 e = upnphttphead.lh_first;
1199 LIST_REMOVE(e, entries);
1200 Delete_upnphttp(e);
1203 if (sudp >= 0) close(sudp);
1204 if (shttpl >= 0) close(shttpl);
1205 #ifdef TIVO_SUPPORT
1206 if (sbeacon >= 0) close(sbeacon);
1207 #endif
1209 if(SendSSDPGoodbye(snotify, n_lan_addr) < 0)
1211 DPRINTF(E_ERROR, L_GENERAL, "Failed to broadcast good-bye notifications\n");
1213 for(i=0; i<n_lan_addr; i++)
1214 close(snotify[i]);
1216 if( inotify_thread )
1217 pthread_join(inotify_thread, NULL);
1219 sql_exec(db, "UPDATE SETTINGS set UPDATE_ID = %u", updateID);
1220 sqlite3_close(db);
1222 media_path = media_dirs;
1223 art_names = album_art_names;
1224 while( media_path )
1226 free(media_path->path);
1227 last_path = media_path;
1228 media_path = media_path->next;
1229 free(last_path);
1231 while( art_names )
1233 free(art_names->name);
1234 last_name = art_names;
1235 art_names = art_names->next;
1236 free(last_name);
1239 if(unlink(pidfilename) < 0)
1241 DPRINTF(E_ERROR, L_GENERAL, "Failed to remove pidfile %s: %s\n", pidfilename, strerror(errno));
1244 freeoptions();
1246 exit(EXIT_SUCCESS);