tvheadend/main.c

419 lines
6.7 KiB
C
Raw Normal View History

2007-08-09 15:42:01 +00:00
/*
* TV headend
* Copyright (C) 2007 Andreas <EFBFBD>man
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iconv.h>
2007-08-09 15:42:01 +00:00
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
2008-08-25 16:22:50 +00:00
#include <syslog.h>
2007-08-09 15:42:01 +00:00
#include <pwd.h>
#include <grp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
2008-02-25 09:40:52 +00:00
#include <libavformat/avformat.h>
2007-08-09 15:42:01 +00:00
#include "tvhead.h"
#include "tcp.h"
#include "access.h"
#include "http.h"
#include "webui/webui.h"
#include "dvb/dvb.h"
2008-09-05 16:02:41 +00:00
#include "xmltv.h"
#include "spawn.h"
2008-09-05 22:02:22 +00:00
#include "subscriptions.h"
#include "serviceprobe.h"
2008-02-16 20:42:13 +00:00
#include <libhts/htsparachute.h>
2008-08-25 16:22:50 +00:00
#include <libhts/htssettings.h>
2008-02-16 20:42:13 +00:00
2007-08-09 15:42:01 +00:00
int running;
2008-06-23 15:00:54 +00:00
extern const char *htsversion;
time_t dispatch_clock;
static LIST_HEAD(, gtimer) gtimers;
pthread_mutex_t global_lock;
2007-08-09 15:42:01 +00:00
static void
handle_sigpipe(int x)
2007-08-09 15:42:01 +00:00
{
return;
2007-08-09 15:42:01 +00:00
}
static void
doexit(int x)
2007-08-09 15:42:01 +00:00
{
running = 0;
2007-08-09 15:42:01 +00:00
}
2008-02-16 20:42:13 +00:00
static void
pull_chute (int sig)
{
char pwd[PATH_MAX];
getcwd(pwd, sizeof(pwd));
syslog(LOG_ERR, "HTS Tvheadend crashed on signal %i (pwd \"%s\")",
2008-02-16 20:42:13 +00:00
sig, pwd);
}
2007-08-09 15:42:01 +00:00
/**
*
*/
static int
gtimercmp(gtimer_t *a, gtimer_t *b)
{
if(a->gti_expire < b->gti_expire)
return -1;
else if(a->gti_expire > b->gti_expire)
return 1;
return 0;
}
/**
*
*/
void
gtimer_arm(gtimer_t *gti, gti_callback_t *callback, void *opaque, int delta)
{
time_t now;
time(&now);
lock_assert(&global_lock);
if(gti->gti_callback != NULL)
LIST_REMOVE(gti, gti_link);
gti->gti_callback = callback;
gti->gti_opaque = opaque;
gti->gti_expire = now + delta;
LIST_INSERT_SORTED(&gtimers, gti, gti_link, gtimercmp);
}
/**
*
*/
void
gtimer_disarm(gtimer_t *gti)
{
if(gti->gti_callback) {
LIST_REMOVE(gti, gti_link);
gti->gti_callback = NULL;
}
}
/**
*
*/
static void
mainloop(void)
{
gtimer_t *gti;
gti_callback_t *cb;
while(running) {
sleep(1);
2008-09-05 16:02:41 +00:00
spawn_reaper();
time(&dispatch_clock);
comet_flush(); /* Flush idle comet mailboxes */
pthread_mutex_lock(&global_lock);
while((gti = LIST_FIRST(&gtimers)) != NULL) {
if(gti->gti_expire > dispatch_clock)
break;
cb = gti->gti_callback;
LIST_REMOVE(gti, gti_link);
gti->gti_callback = NULL;
cb(gti->gti_opaque);
}
pthread_mutex_unlock(&global_lock);
}
}
/**
*
*/
2007-08-09 15:42:01 +00:00
int
main(int argc, char **argv)
{
int c;
int forkaway = 0;
FILE *pidfile;
struct group *grp;
struct passwd *pw;
const char *usernam = NULL;
const char *groupnam = NULL;
char *cfgfile = NULL;
2007-08-09 15:42:01 +00:00
int logfacility = LOG_DAEMON;
2007-11-02 22:21:48 +00:00
int disable_dvb = 0;
sigset_t set;
2007-08-09 15:42:01 +00:00
signal(SIGPIPE, handle_sigpipe);
while((c = getopt(argc, argv, "c:fu:g:d")) != -1) {
2007-08-09 15:42:01 +00:00
switch(c) {
2007-11-02 22:21:48 +00:00
case 'd':
disable_dvb = 1;
break;
2007-08-09 15:42:01 +00:00
case 'c':
cfgfile = optarg;
break;
case 'f':
forkaway = 1;
break;
case 'u':
usernam = optarg;
break;
case 'g':
groupnam = optarg;
break;
}
}
if(forkaway) {
if(daemon(0, 0)) {
exit(2);
}
pidfile = fopen("/var/run/tvhead.pid", "w+");
if(pidfile != NULL) {
fprintf(pidfile, "%d\n", getpid());
fclose(pidfile);
}
2007-08-09 15:42:01 +00:00
grp = getgrnam(groupnam ?: "video");
if(grp != NULL) {
setgid(grp->gr_gid);
} else {
setgid(1);
}
pw = usernam ? getpwnam(usernam) : NULL;
if(pw != NULL) {
setuid(pw->pw_uid);
} else {
setuid(1);
}
umask(0);
}
sigfillset(&set);
sigprocmask(SIG_BLOCK, &set, NULL);
2007-08-09 15:42:01 +00:00
openlog("tvheadend", LOG_PID, logfacility);
2008-08-25 16:22:50 +00:00
hts_settings_init("tvheadend2");
pthread_mutex_init(&global_lock, NULL);
2008-08-25 16:22:50 +00:00
pthread_mutex_lock(&global_lock);
2008-02-16 20:42:13 +00:00
htsparachute_init(pull_chute);
2008-09-05 16:09:02 +00:00
/**
* Initialize subsystems
*/
2008-09-05 22:02:22 +00:00
av_register_all();
2008-09-05 16:02:41 +00:00
xmltv_init(); /* Must be initialized before channels */
channels_init();
access_init();
2007-08-09 15:42:01 +00:00
tcp_server_init();
2007-08-09 15:42:01 +00:00
dvb_init();
http_server_init();
webui_init();
2008-09-05 22:02:22 +00:00
subscriptions_init();
serviceprobe_init();
pthread_mutex_unlock(&global_lock);
2007-11-27 16:56:28 +00:00
/**
* Wait for SIGTERM / SIGINT, but only in this thread
*/
2007-12-02 13:49:03 +00:00
2007-08-09 15:42:01 +00:00
running = 1;
sigemptyset(&set);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGINT);
signal(SIGTERM, doexit);
signal(SIGINT, doexit);
pthread_sigmask(SIG_UNBLOCK, &set, NULL);
2008-02-25 21:05:35 +00:00
mainloop();
2007-08-09 15:42:01 +00:00
syslog(LOG_NOTICE, "Exiting HTS Tvheadend");
2007-08-09 15:42:01 +00:00
if(forkaway)
unlink("/var/run/tvhead.pid");
return 0;
}
static char *
convert_to(const char *in, const char *target_encoding)
{
iconv_t ic;
size_t inlen, outlen, alloced, pos;
char *out, *start;
int r;
inlen = strlen(in);
alloced = 100;
outlen = alloced;
ic = iconv_open(target_encoding, "UTF8");
if(ic == NULL)
return NULL;
out = start = malloc(alloced + 1);
while(inlen > 0) {
r = iconv(ic, (char **)&in, &inlen, &out, &outlen);
if(r == (size_t) -1) {
if(errno == EILSEQ) {
in++;
inlen--;
continue;
}
if(errno == E2BIG) {
pos = alloced - outlen;
alloced *= 2;
start = realloc(start, alloced + 1);
out = start + pos;
outlen = alloced - pos;
continue;
}
break;
}
}
iconv_close(ic);
pos = alloced - outlen;
start[pos] = 0;
return start;
}
char *
utf8toprintable(const char *in)
{
return convert_to(in, "ISO_8859-1");
}
char *
utf8tofilename(const char *in)
{
return convert_to(in, "LATIN1");
}
2008-08-25 16:22:50 +00:00
/**
*
*/
void
tvhlog(int severity, const char *subsys, const char *fmt, ...)
{
va_list ap;
htsmsg_t *m;
char buf[512];
2008-08-25 17:11:58 +00:00
char buf2[512];
char t[50];
2008-08-25 16:22:50 +00:00
int l;
2008-08-25 17:11:58 +00:00
struct tm tm;
time_t now;
2008-08-25 16:22:50 +00:00
l = snprintf(buf, sizeof(buf), "%s: ", subsys);
va_start(ap, fmt);
vsnprintf(buf + l, sizeof(buf) - l, fmt, ap);
va_end(ap);
syslog(severity, "%s", buf);
/**
*
*/
2008-08-25 17:11:58 +00:00
time(&now);
localtime_r(&now, &tm);
strftime(t, sizeof(t), "%b %d %H:%M:%S", &tm);
snprintf(buf2, sizeof(buf2), "%s %s", t, buf);
2008-08-25 16:22:50 +00:00
m = htsmsg_create();
htsmsg_add_str(m, "notificationClass", "logmessage");
2008-08-25 17:11:58 +00:00
htsmsg_add_str(m, "logtxt", buf2);
comet_mailbox_add_message(m);
2008-08-25 16:22:50 +00:00
htsmsg_destroy(m);
}
2008-09-05 16:02:41 +00:00
/**
*
*/
void
tvh_str_set(char **strp, const char *src)
{
free(*strp);
*strp = src ? strdup(src) : NULL;
}