213int main(
int argc,
char *argv[])
217 struct termios savedTm;
218 bool HasStdin = (tcgetpgrp(STDIN_FILENO) == getpid() || getppid() != (pid_t)1) && tcgetattr(STDIN_FILENO, &savedTm) == 0;
222 setlocale(LC_ALL,
"");
226#define dd(a, b) (*a ? a : b)
227#define DEFAULTSVDRPPORT 6419
228#define DEFAULTWATCHDOG 0
229#define DEFAULTVIDEODIR VIDEODIR
230#define DEFAULTCONFDIR dd(CONFDIR, VideoDirectory)
231#define DEFAULTARGSDIR dd(ARGSDIR, "/etc/vdr/conf.d")
232#define DEFAULTCACHEDIR dd(CACHEDIR, VideoDirectory)
233#define DEFAULTRESDIR dd(RESDIR, ConfigDirectory)
234#define DEFAULTPLUGINDIR PLUGINDIR
235#define DEFAULTLOCDIR LOCDIR
236#define DEFAULTEPGDATAFILENAME "epg.data"
238 bool StartedAsRoot =
false;
239 const char *VdrUser = NULL;
240 bool UserDump =
false;
242 const char *AudioCommand = NULL;
244 const char *ConfigDirectory = NULL;
245 const char *CacheDirectory = NULL;
246 const char *ResourceDirectory = NULL;
249 bool DisplayHelp =
false;
250 bool DisplayVersion =
false;
251 bool DaemonMode =
false;
252 int SysLogTarget = LOG_USER;
253 bool MuteAudio =
false;
255 const char *Terminal = NULL;
256 const char *OverrideCharacterTable = NULL;
259 const char *LircDevice = NULL;
260#if !defined(REMOTE_KBD)
263#if defined(REMOTE_LIRC)
264 LircDevice = LIRC_DEVICE;
270 time_t SdWatchdog = 0;
271 int SdWatchdogTimeout = 0;
276 Args =
new cArgs(argv[0]);
286 static struct option long_options[] = {
287 {
"audio", required_argument, NULL,
'a' },
288 {
"cachedir", required_argument, NULL,
'c' | 0x100 },
289 {
"chartab", required_argument, NULL,
'c' | 0x200 },
290 {
"config", required_argument, NULL,
'c' },
291 {
"daemon", no_argument, NULL,
'd' },
292 {
"device", required_argument, NULL,
'D' },
293 {
"dirnames", required_argument, NULL,
'd' | 0x100 },
294 {
"edit", required_argument, NULL,
'e' | 0x100 },
295 {
"epgfile", required_argument, NULL,
'E' },
296 {
"filesize", required_argument, NULL,
'f' | 0x100 },
297 {
"genindex", required_argument, NULL,
'g' | 0x100 },
298 {
"grab", required_argument, NULL,
'g' },
299 {
"help", no_argument, NULL,
'h' },
300 {
"instance", required_argument, NULL,
'i' },
301 {
"lib", required_argument, NULL,
'L' },
302 {
"lirc", optional_argument, NULL,
'l' | 0x100 },
303 {
"localedir",required_argument, NULL,
'l' | 0x200 },
304 {
"log", required_argument, NULL,
'l' },
305 {
"mute", no_argument, NULL,
'm' },
306 {
"no-kbd", no_argument, NULL,
'n' | 0x100 },
307 {
"plugin", required_argument, NULL,
'P' },
308 {
"port", required_argument, NULL,
'p' },
309 {
"record", required_argument, NULL,
'r' },
310 {
"resdir", required_argument, NULL,
'r' | 0x100 },
311 {
"showargs", optional_argument, NULL,
's' | 0x200 },
312 {
"shutdown", required_argument, NULL,
's' },
313 {
"split", no_argument, NULL,
's' | 0x100 },
314 {
"terminal", required_argument, NULL,
't' },
315 {
"updindex", required_argument, NULL,
'u' | 0x200 },
316 {
"user", required_argument, NULL,
'u' },
317 {
"userdump", no_argument, NULL,
'u' | 0x100 },
318 {
"version", no_argument, NULL,
'V' },
319 {
"vfat", no_argument, NULL,
'v' | 0x100 },
320 {
"video", required_argument, NULL,
'v' },
321 {
"watchdog", required_argument, NULL,
'w' },
322 { NULL, no_argument, NULL, 0 }
326 while ((c = getopt_long(argc, argv,
"a:c:dD:e:E:g:hi:l:L:mp:P:r:s:t:u:v:Vw:", long_options, NULL)) != -1) {
328 case 'a': AudioCommand = optarg;
331 CacheDirectory = optarg;
334 OverrideCharacterTable = optarg;
336 case 'c': ConfigDirectory = optarg;
338 case 'd': DaemonMode =
true;
340 case 'D':
if (*optarg ==
'-') {
345 int n = atoi(optarg);
351 fprintf(stderr,
"vdr: invalid DVB device number: %s\n", optarg);
356 int n = strtol(s, &s, 10);
357 if (n <= 0 || n >= PATH_MAX) {
358 fprintf(stderr,
"vdr: invalid directory path length: %s\n", optarg);
365 fprintf(stderr,
"vdr: invalid delimiter: %s\n", optarg);
373 int n = strtol(s, &s, 10);
374 if (n <= 0 || n > NAME_MAX) {
375 fprintf(stderr,
"vdr: invalid directory name length: %s\n", optarg);
382 fprintf(stderr,
"vdr: invalid delimiter: %s\n", optarg);
389 int n = strtol(s, &s, 10);
390 if (n != 0 && n != 1) {
391 fprintf(stderr,
"vdr: invalid directory encoding: %s\n", optarg);
396 fprintf(stderr,
"vdr: unexpected data: %s\n", optarg);
403 case 'E': EpgDataFileName = (*optarg !=
'-' ? optarg : NULL);
416 case 'h': DisplayHelp =
true;
423 fprintf(stderr,
"vdr: invalid instance id: %s\n", optarg);
426 char *p = strchr(optarg,
'.');
430 int l = atoi(optarg);
431 if (0 <= l && l <= 3) {
438 if (0 <= l && l <= 7) {
439 int targets[] = { LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
440 SysLogTarget = targets[l];
448 fprintf(stderr,
"vdr: invalid log level: %s\n", optarg);
451 case 'L':
if (access(optarg, R_OK | X_OK) == 0)
454 fprintf(stderr,
"vdr: can't access plugin directory: %s\n", optarg);
459 LircDevice = optarg ? optarg : LIRC_DEVICE;
462 if (access(optarg, R_OK | X_OK) == 0)
463 LocaleDirectory = optarg;
465 fprintf(stderr,
"vdr: can't access locale directory: %s\n", optarg);
469 case 'm': MuteAudio =
true;
475 SVDRPport = atoi(optarg);
477 fprintf(stderr,
"vdr: invalid port number: %s\n", optarg);
481 case 'P': PluginManager.
AddPlugin(optarg);
486 ResourceDirectory = optarg;
491 Setup.SplitEditedFiles = 1;
497 fprintf(stderr,
"vdr: can't read arguments from directory: %s\n", ArgsDir);
502 for (
int i = 1; i < c; i++)
503 printf(
"%s\n", v[i]);
506 case 't': Terminal = optarg;
507 if (access(Terminal, R_OK | W_OK) < 0) {
508 fprintf(stderr,
"vdr: can't access terminal: %s\n", Terminal);
512 case 'u':
if (*optarg)
519 fprintf(stderr,
"vdr: option '--updindex' is deprecated, using '--genindex' instead\n");
521 case 'V': DisplayVersion =
true;
528 case 'v': VideoDirectory = optarg;
529 while (optarg && *optarg && optarg[strlen(optarg) - 1] ==
'/')
530 optarg[strlen(optarg) - 1] = 0;
534 int t = atoi(optarg);
540 fprintf(stderr,
"vdr: invalid watchdog timeout: %s\n", optarg);
548 if (DisplayHelp || DisplayVersion) {
553 printf(
"Usage: vdr [OPTIONS]\n\n"
554 " -a CMD, --audio=CMD send Dolby Digital audio to stdin of command CMD\n"
555 " --cachedir=DIR save cache files in DIR (default: %s)\n"
556 " --chartab=CHARACTER_TABLE\n"
557 " set the character table to use for strings in the\n"
558 " DVB data stream that don't begin with a character\n"
559 " table indicator, but don't use the standard default\n"
560 " character table (for instance ISO-8859-9)\n"
561 " -c DIR, --config=DIR read config files from DIR (default: %s)\n"
562 " -d, --daemon run in daemon mode\n"
563 " -D NUM, --device=NUM use only the given DVB device (NUM = 0, 1, 2...)\n"
564 " there may be several -D options (default: all DVB\n"
565 " devices will be used); if -D- is given, no DVB\n"
566 " devices will be used at all, independent of any\n"
567 " other -D options\n"
568 " --dirnames=PATH[,NAME[,ENC]]\n"
569 " set the maximum directory path length to PATH\n"
570 " (default: %d); if NAME is also given, it defines\n"
571 " the maximum directory name length (default: %d);\n"
572 " the optional ENC can be 0 or 1, and controls whether\n"
573 " special characters in directory names are encoded as\n"
574 " hex values (default: 0); if PATH or NAME are left\n"
575 " empty (as in \",,1\" to only set ENC), the defaults\n"
577 " --edit=REC cut recording REC and exit\n"
578 " -E FILE, --epgfile=FILE write the EPG data into the given FILE (default is\n"
579 " '%s' in the cache directory)\n"
580 " '-E-' disables this\n"
581 " if FILE is a directory, the default EPG file will be\n"
582 " created in that directory\n"
583 " --filesize=SIZE limit video files to SIZE bytes (default is %dM)\n"
584 " only useful in conjunction with --edit\n"
585 " --genindex=REC generate index for recording REC and exit\n"
586 " -g DIR, --grab=DIR write images from the SVDRP command GRAB into the\n"
587 " given DIR; DIR must be the full path name of an\n"
588 " existing directory, without any \"..\", double '/'\n"
589 " or symlinks (default: none, same as -g-)\n"
590 " -h, --help print this help and exit\n"
591 " -i ID, --instance=ID use ID as the id of this VDR instance (default: 0)\n"
592 " -l LEVEL, --log=LEVEL set log level (default: 3)\n"
593 " 0 = no logging, 1 = errors only,\n"
594 " 2 = errors and info, 3 = errors, info and debug\n"
595 " if logging should be done to LOG_LOCALn instead of\n"
596 " LOG_USER, add '.n' to LEVEL, as in 3.7 (n=0..7)\n"
597 " -L DIR, --lib=DIR search for plugins in DIR (default is %s)\n"
598 " --lirc[=PATH] use a LIRC remote control device, attached to PATH\n"
600 " --localedir=DIR search for locale files in DIR (default is\n"
602 " -m, --mute mute audio of the primary DVB device at startup\n"
603 " --no-kbd don't use the keyboard as an input device\n"
604 " -p PORT, --port=PORT use PORT for SVDRP (default: %d)\n"
605 " 0 turns off SVDRP\n"
606 " -P OPT, --plugin=OPT load a plugin defined by the given options\n"
607 " -r CMD, --record=CMD call CMD before and after a recording, and after\n"
608 " a recording has been edited or deleted\n"
609 " --resdir=DIR read resource files from DIR (default: %s)\n"
610 " -s CMD, --shutdown=CMD call CMD to shutdown the computer\n"
611 " --split split edited files at the editing marks (only\n"
612 " useful in conjunction with --edit)\n"
613 " --showargs[=DIR] print the arguments read from DIR and exit\n"
615 " -t TTY, --terminal=TTY controlling tty\n"
616 " -u USER, --user=USER run as user USER; only applicable if started as\n"
617 " root; USER can be a user name or a numerical id\n"
618 " --userdump allow coredumps if -u is given (debugging)\n"
619 " -v DIR, --video=DIR use DIR as video directory (default: %s)\n"
620 " -V, --version print version information and exit\n"
621 " --vfat for backwards compatibility (same as\n"
622 " --dirnames=250,40,1)\n"
623 " -w SEC, --watchdog=SEC activate the watchdog timer with a timeout of SEC\n"
624 " seconds (default: %d); '0' disables the watchdog\n"
646 printf(
"Plugins: vdr -P\"name [OPTIONS]\"\n\n");
647 for (
int i = 0; ; i++) {
652 if (DisplayHelp && help) {
667 openlog(
"vdr", LOG_CONS, SysLogTarget);
672 if (daemon(1, 0) == -1) {
673 fprintf(stderr,
"vdr: %m\n");
680 stdin = freopen(Terminal,
"r", stdin);
681 stdout = freopen(Terminal,
"w", stdout);
682 stderr = freopen(Terminal,
"w", stderr);
684 tcgetattr(STDIN_FILENO, &savedTm);
689 if (VdrUser && geteuid() == 0) {
690 StartedAsRoot =
true;
691 if (strcmp(VdrUser,
"root") && strcmp(VdrUser,
"0")) {
694 if (!
SetUser(VdrUser, UserDump))
706 fprintf(stderr,
"vdr: can't access video directory %s\n", VideoDirectory);
711 if (StartedAsRoot && VdrUser)
712 isyslog(
"switched to user '%s'", VdrUser);
719 char *CodeSet = NULL;
720 if (setlocale(LC_CTYPE,
""))
721 CodeSet = nl_langinfo(CODESET);
723 char *LangEnv = getenv(
"LANG");
725 CodeSet = strchr(LangEnv,
'.');
732 isyslog(
"codeset is '%s' - %s", CodeSet, known ?
"known" :
"unknown");
735 if (OverrideCharacterTable) {
737 isyslog(
"override character table is '%s' - %s", OverrideCharacterTable, known ?
"known" :
"unknown");
749 int LastTimerChannel = -1;
750 int PreviousChannel[2] = { 1, 1 };
751 int PreviousChannelIndex = 0;
752 time_t LastChannelChanged = time(NULL);
753 time_t LastInteract = 0;
754 int MaxLatencyTime = 0;
755 bool IsInfoMenu =
false;
756 cSkin *CurrentSkin = NULL;
757 int OldPrimaryDVB = 0;
766 if (!ConfigDirectory)
772 if (!ResourceDirectory)
795 const char *msg =
"no fonts available - OSD will not show any text!";
796 fprintf(stderr,
"vdr: %s\n", msg);
806 if (EpgDataFileName) {
807 const char *EpgDirectory = NULL;
809 EpgDirectory = EpgDataFileName;
812 else if (*EpgDataFileName !=
'/' && *EpgDataFileName !=
'.')
813 EpgDirectory = CacheDirectory;
818 EpgDataReader.
Start();
840 isyslog(
"trying device number %d instead", i + 1);
842 Setup.PrimaryDVB = i + 1;
848 const char *msg =
"no primary device found - using first device!";
849 fprintf(stderr,
"vdr: %s\n", msg);
854 const char *msg =
"no primary device found - giving up!";
855 fprintf(stderr,
"vdr: %s\n", msg);
861 OldPrimaryDVB =
Setup.PrimaryDVB;
878 CurrentSkin =
Skins.Current();
887 if (!CurrentSkin || CurrentSkin ==
Skins.Current() && strcmp(
Skins.Current()->Name(),
Setup.OSDSkin) != 0) {
895 if (!DaemonMode && HasStdin && UseKbd)
919 if (*
Setup.InitialChannel) {
922 if (
const cChannel *Channel = Channels->GetByNumber(atoi(
Setup.InitialChannel)))
923 Setup.InitialChannel = Channel->GetChannelID().ToString();
926 Setup.CurrentChannel = Channel->Number();
928 if (
Setup.InitialVolume >= 0)
932 Channels->SwitchTo(
Setup.CurrentChannel);
943 if (signal(SIGHUP,
SignalHandler) == SIG_IGN) signal(SIGHUP, SIG_IGN);
944 if (signal(SIGINT,
SignalHandler) == SIG_IGN) signal(SIGINT, SIG_IGN);
945 if (signal(SIGTERM,
SignalHandler) == SIG_IGN) signal(SIGTERM, SIG_IGN);
946 if (signal(SIGPIPE,
SignalHandler) == SIG_IGN) signal(SIGPIPE, SIG_IGN);
947 if (WatchdogTimeout > 0)
948 if (signal(SIGALRM,
Watchdog) == SIG_IGN) signal(SIGALRM, SIG_IGN);
952 if (WatchdogTimeout > 0) {
953 dsyslog(
"setting watchdog timer to %d seconds", WatchdogTimeout);
954 alarm(WatchdogTimeout);
958 if (sd_watchdog_enabled(0, NULL) > 0) {
960 SdWatchdog = time(NULL);
961 sd_watchdog_enabled(0, &timeout);
962 SdWatchdogTimeout = (int)timeout/1000000;
963 dsyslog(
"SD_WATCHDOG enabled with timeout set to %d seconds", SdWatchdogTimeout);
968 sd_notify(0,
"READY=1\nSTATUS=Ready");
978#define DELETE_MENU ((IsInfoMenu &= (Menu == NULL)), delete Menu, Menu = NULL)
981#ifdef DEBUGRINGBUFFERS
982 cRingBufferLinear::PrintDebugRBL();
987 time_t Now = time(NULL);
991 static time_t lastTime = 0;
996 if (Channel && (Channel->
Vpid() || Channel->
Apid(0) || Channel->
Dpid(0))) {
999 else if (LastTimerChannel > 0) {
1000 Channel = Channels->GetByNumber(LastTimerChannel);
1006 LastTimerChannel = -1;
1014 static time_t lastOsdSizeUpdate = 0;
1015 if (Now != lastOsdSizeUpdate) {
1017 static int OsdState = 0;
1022 lastOsdSizeUpdate = Now;
1026 if (WatchdogTimeout > 0) {
1027 int LatencyTime = WatchdogTimeout - alarm(WatchdogTimeout);
1028 if (LatencyTime > MaxLatencyTime) {
1029 MaxLatencyTime = LatencyTime;
1030 dsyslog(
"max. latency time %d seconds", MaxLatencyTime);
1035 if (SdWatchdogTimeout && (Now - SdWatchdog) * 2 > SdWatchdogTimeout) {
1036 sd_notify(0,
"WATCHDOG=1");
1045 static time_t ChannelSaveTimeout = 0;
1047 static cStateKey ChannelsStateKey(
true);
1048 static int ChannelsModifiedByUser = 0;
1051 if (ChannelSaveTimeout != 1) {
1054 ChannelSaveTimeout = 1;
1055 else if (!ChannelSaveTimeout)
1059 ChannelSaveTimeout = 1;
1062 ChannelSaveTimeout = 1;
1063 if (Timers && Channels) {
1066 ChannelSaveTimeout = 0;
1069 for (
const cChannel *Channel = Channels->
First(); Channel; Channel = Channels->
Next(Channel)) {
1075 isyslog(
"retuning due to modification of channel %d (%s)", Channel->Number(), Channel->Name());
1076 Channels->
SwitchTo(Channel->Number());
1086 ChannelsStateKey.
Remove();
1089 if (ChannelSaveTimeout == 1) {
1091 ChannelsStateKey.
Reset();
1092 TimersStateKey.
Reset();
1100 LastChannelChanged = Now;
1102 if (Now - LastChannelChanged >=
Setup.ZapTimeout && LastChannel != PreviousChannel[PreviousChannelIndex])
1103 PreviousChannel[PreviousChannelIndex ^= 1] = LastChannel;
1113 SchedulesStateKey.
Reset();
1114 bool TimersModified =
false;
1119 TimersModified =
true;
1122 TimersModified =
true;
1125 TimersModified =
true;
1127 SchedulesStateKey.
Remove();
1129 TimersStateKey.
Remove(TimersModified);
1134 bool TimersModified =
false;
1136 TimersModified =
true;
1140 Timer->SetPending(
true);
1142 LastTimerChannel = Timer->Channel()->Number();
1143 TimersModified =
true;
1146 static time_t LastTimerCheck = 0;
1148 for (
cTimer *Timer = Timers->
First(); Timer; Timer = Timers->
Next(Timer)) {
1149 if (Timer->Remote() || Timer->IsPatternTimer())
1151 bool InVpsMargin =
false;
1152 bool NeedsTransponder =
false;
1153 if (Timer->HasFlags(
tfActive) && !Timer->Recording()) {
1154 if (Timer->HasFlags(
tfVps)) {
1155 if (Timer->Matches(Now,
Setup.VpsMargin))
1157 else if (Timer->Event()) {
1159 InVpsMargin = Timer->Event()->StartTime() <= Now && Now < Timer->Event()->EndTime();
1164 const cSchedule *Schedule = Schedules->GetSchedule(Timer->Channel());
1165 InVpsMargin = !Schedule;
1172 if (!Timer->Recording())
1173 Timer->SetInVpsMargin(InVpsMargin);
1174 if (NeedsTransponder || InVpsMargin) {
1189 dsyslog(
"switching device %d to channel %d %s (%s)", Device->
DeviceNumber() + 1, Timer->Channel()->Number(), *Timer->Channel()->GetChannelID().ToString(), Timer->Channel()->Name());
1194 LastTimerChannel = Timer->Channel()->Number();
1200 LastTimerCheck = Now;
1204 TimersModified =
true;
1207 if (MaxPriority >= 0)
1209 TimersStateKey.
Remove(TimersModified);
1220 Skins.ProcessQueuedMessages();
1223 if (!NeedsFastResponse) {
1248 bool WasOpen = Interact != NULL;
1249 bool WasMenu = Interact && Interact->
IsMenu();
1258 if (!WasOpen || !WasMenu && !
Setup.MenuKeyCloses)
1287 #define DirectMainFunction(function...)\
1291 Menu = new cMenuMain(function);\
1315 esyslog(
"ERROR: unknown plugin '%s'", PluginName);
1384 if (
Setup.PauseKeyHandling) {
1385 if (
Setup.PauseKeyHandling > 1 ||
Interface->Confirm(
tr(
"Pause live video?"))) {
1387 Skins.QueueMessage(
mtError,
tr(
"No free DVB device to record!"));
1396 if (
Setup.RecordKeyHandling) {
1397 if (
Setup.RecordKeyHandling > 1 ||
Interface->Confirm(
tr(
"Start recording?"))) {
1407 isyslog(
"Power button pressed");
1434 Interact = Menu ? Menu : Control;
1438 if (state ==
osUnknown && Interact != Control) {
1441 if (state ==
osEnd) {
1454 Skins.QueueMessage(
mtError,
tr(
"No free DVB device to record!"));
1482 case osEnd:
if (Interact == Menu)
1502 if (PreviousChannel[PreviousChannelIndex ^ 1] == LastChannel || LastChannel != PreviousChannel[0] && LastChannel != PreviousChannel[1])
1503 PreviousChannelIndex ^= 1;
1505 Channels->SwitchTo(PreviousChannel[PreviousChannelIndex ^= 1]);
1528 case kOk: LastChannel = -1;
break;
1557 int NewPrimaryDVB =
Setup.PrimaryDVB;
1558 if (NewPrimaryDVB != OldPrimaryDVB) {
1565 OldPrimaryDVB = NewPrimaryDVB;
1609 static time_t LastMemoryCleanup = 0;
1612 LastMemoryCleanup = Now;
1626 esyslog(
"emergency exit requested - shutting down");
1631 signal(SIGHUP, SIG_DFL);
1632 signal(SIGINT, SIG_DFL);
1633 signal(SIGTERM, SIG_DFL);
1634 signal(SIGPIPE, SIG_DFL);
1635 signal(SIGALRM, SIG_DFL);
1664 if (WatchdogTimeout > 0)
1665 dsyslog(
"max. latency time %d seconds", MaxLatencyTime);
1674 tcsetattr(STDIN_FILENO, TCSANOW, &savedTm);
1677 sd_notify(0,
"STOPPING=1\nSTATUS=Startup failed, exiting");
1679 sd_notify(0,
"STOPPING=1\nSTATUS=Exiting");