211int main(
int argc,
char *argv[])
215 struct termios savedTm;
216 bool HasStdin = (tcgetpgrp(STDIN_FILENO) == getpid() || getppid() != (pid_t)1) && tcgetattr(STDIN_FILENO, &savedTm) == 0;
220 setlocale(LC_ALL,
"");
224#define dd(a, b) (*a ? a : b)
225#define DEFAULTSVDRPPORT 6419
226#define DEFAULTWATCHDOG 0
227#define DEFAULTVIDEODIR VIDEODIR
228#define DEFAULTCONFDIR dd(CONFDIR, VideoDirectory)
229#define DEFAULTARGSDIR dd(ARGSDIR, "/etc/vdr/conf.d")
230#define DEFAULTCACHEDIR dd(CACHEDIR, VideoDirectory)
231#define DEFAULTRESDIR dd(RESDIR, ConfigDirectory)
232#define DEFAULTPLUGINDIR PLUGINDIR
233#define DEFAULTLOCDIR LOCDIR
234#define DEFAULTEPGDATAFILENAME "epg.data"
236 bool StartedAsRoot =
false;
237 const char *VdrUser = NULL;
238 bool UserDump =
false;
240 const char *AudioCommand = NULL;
242 const char *ConfigDirectory = NULL;
243 const char *CacheDirectory = NULL;
244 const char *ResourceDirectory = NULL;
247 bool DisplayHelp =
false;
248 bool DisplayVersion =
false;
249 bool DaemonMode =
false;
250 int SysLogTarget = LOG_USER;
251 bool MuteAudio =
false;
253 const char *Terminal = NULL;
254 const char *OverrideCharacterTable = NULL;
257 const char *LircDevice = NULL;
258#if !defined(REMOTE_KBD)
261#if defined(REMOTE_LIRC)
262 LircDevice = LIRC_DEVICE;
268 time_t SdWatchdog = 0;
269 int SdWatchdogTimeout = 0;
274 Args =
new cArgs(argv[0]);
284 static struct option long_options[] = {
285 {
"audio", required_argument, NULL,
'a' },
286 {
"cachedir", required_argument, NULL,
'c' | 0x100 },
287 {
"chartab", required_argument, NULL,
'c' | 0x200 },
288 {
"config", required_argument, NULL,
'c' },
289 {
"daemon", no_argument, NULL,
'd' },
290 {
"device", required_argument, NULL,
'D' },
291 {
"dirnames", required_argument, NULL,
'd' | 0x100 },
292 {
"edit", required_argument, NULL,
'e' | 0x100 },
293 {
"epgfile", required_argument, NULL,
'E' },
294 {
"filesize", required_argument, NULL,
'f' | 0x100 },
295 {
"genindex", required_argument, NULL,
'g' | 0x100 },
296 {
"grab", required_argument, NULL,
'g' },
297 {
"help", no_argument, NULL,
'h' },
298 {
"instance", required_argument, NULL,
'i' },
299 {
"lib", required_argument, NULL,
'L' },
300 {
"lirc", optional_argument, NULL,
'l' | 0x100 },
301 {
"localedir",required_argument, NULL,
'l' | 0x200 },
302 {
"log", required_argument, NULL,
'l' },
303 {
"mute", no_argument, NULL,
'm' },
304 {
"no-kbd", no_argument, NULL,
'n' | 0x100 },
305 {
"plugin", required_argument, NULL,
'P' },
306 {
"port", required_argument, NULL,
'p' },
307 {
"record", required_argument, NULL,
'r' },
308 {
"resdir", required_argument, NULL,
'r' | 0x100 },
309 {
"showargs", optional_argument, NULL,
's' | 0x200 },
310 {
"shutdown", required_argument, NULL,
's' },
311 {
"split", no_argument, NULL,
's' | 0x100 },
312 {
"terminal", required_argument, NULL,
't' },
313 {
"updindex", required_argument, NULL,
'u' | 0x200 },
314 {
"user", required_argument, NULL,
'u' },
315 {
"userdump", no_argument, NULL,
'u' | 0x100 },
316 {
"version", no_argument, NULL,
'V' },
317 {
"vfat", no_argument, NULL,
'v' | 0x100 },
318 {
"video", required_argument, NULL,
'v' },
319 {
"watchdog", required_argument, NULL,
'w' },
320 { NULL, no_argument, NULL, 0 }
324 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) {
326 case 'a': AudioCommand = optarg;
329 CacheDirectory = optarg;
332 OverrideCharacterTable = optarg;
334 case 'c': ConfigDirectory = optarg;
336 case 'd': DaemonMode =
true;
338 case 'D':
if (*optarg ==
'-') {
343 int n = atoi(optarg);
349 fprintf(stderr,
"vdr: invalid DVB device number: %s\n", optarg);
354 int n = strtol(s, &s, 10);
355 if (n <= 0 || n >= PATH_MAX) {
356 fprintf(stderr,
"vdr: invalid directory path length: %s\n", optarg);
363 fprintf(stderr,
"vdr: invalid delimiter: %s\n", optarg);
371 int n = strtol(s, &s, 10);
372 if (n <= 0 || n > NAME_MAX) {
373 fprintf(stderr,
"vdr: invalid directory name length: %s\n", optarg);
380 fprintf(stderr,
"vdr: invalid delimiter: %s\n", optarg);
387 int n = strtol(s, &s, 10);
388 if (n != 0 && n != 1) {
389 fprintf(stderr,
"vdr: invalid directory encoding: %s\n", optarg);
394 fprintf(stderr,
"vdr: unexpected data: %s\n", optarg);
401 case 'E': EpgDataFileName = (*optarg !=
'-' ? optarg : NULL);
414 case 'h': DisplayHelp =
true;
421 fprintf(stderr,
"vdr: invalid instance id: %s\n", optarg);
424 char *p = strchr(optarg,
'.');
428 int l = atoi(optarg);
429 if (0 <= l && l <= 3) {
436 if (0 <= l && l <= 7) {
437 int targets[] = { LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
438 SysLogTarget = targets[l];
446 fprintf(stderr,
"vdr: invalid log level: %s\n", optarg);
449 case 'L':
if (access(optarg, R_OK | X_OK) == 0)
452 fprintf(stderr,
"vdr: can't access plugin directory: %s\n", optarg);
457 LircDevice = optarg ? optarg : LIRC_DEVICE;
460 if (access(optarg, R_OK | X_OK) == 0)
461 LocaleDirectory = optarg;
463 fprintf(stderr,
"vdr: can't access locale directory: %s\n", optarg);
467 case 'm': MuteAudio =
true;
473 SVDRPport = atoi(optarg);
475 fprintf(stderr,
"vdr: invalid port number: %s\n", optarg);
479 case 'P': PluginManager.
AddPlugin(optarg);
484 ResourceDirectory = optarg;
489 Setup.SplitEditedFiles = 1;
495 fprintf(stderr,
"vdr: can't read arguments from directory: %s\n", ArgsDir);
500 for (
int i = 1; i < c; i++)
501 printf(
"%s\n", v[i]);
504 case 't': Terminal = optarg;
505 if (access(Terminal, R_OK | W_OK) < 0) {
506 fprintf(stderr,
"vdr: can't access terminal: %s\n", Terminal);
510 case 'u':
if (*optarg)
517 fprintf(stderr,
"vdr: option '--updindex' is deprecated, using '--genindex' instead\n");
519 case 'V': DisplayVersion =
true;
526 case 'v': VideoDirectory = optarg;
527 while (optarg && *optarg && optarg[strlen(optarg) - 1] ==
'/')
528 optarg[strlen(optarg) - 1] = 0;
532 int t = atoi(optarg);
538 fprintf(stderr,
"vdr: invalid watchdog timeout: %s\n", optarg);
546 if (DisplayHelp || DisplayVersion) {
551 printf(
"Usage: vdr [OPTIONS]\n\n"
552 " -a CMD, --audio=CMD send Dolby Digital audio to stdin of command CMD\n"
553 " --cachedir=DIR save cache files in DIR (default: %s)\n"
554 " --chartab=CHARACTER_TABLE\n"
555 " set the character table to use for strings in the\n"
556 " DVB data stream that don't begin with a character\n"
557 " table indicator, but don't use the standard default\n"
558 " character table (for instance ISO-8859-9)\n"
559 " -c DIR, --config=DIR read config files from DIR (default: %s)\n"
560 " -d, --daemon run in daemon mode\n"
561 " -D NUM, --device=NUM use only the given DVB device (NUM = 0, 1, 2...)\n"
562 " there may be several -D options (default: all DVB\n"
563 " devices will be used); if -D- is given, no DVB\n"
564 " devices will be used at all, independent of any\n"
565 " other -D options\n"
566 " --dirnames=PATH[,NAME[,ENC]]\n"
567 " set the maximum directory path length to PATH\n"
568 " (default: %d); if NAME is also given, it defines\n"
569 " the maximum directory name length (default: %d);\n"
570 " the optional ENC can be 0 or 1, and controls whether\n"
571 " special characters in directory names are encoded as\n"
572 " hex values (default: 0); if PATH or NAME are left\n"
573 " empty (as in \",,1\" to only set ENC), the defaults\n"
575 " --edit=REC cut recording REC and exit\n"
576 " -E FILE, --epgfile=FILE write the EPG data into the given FILE (default is\n"
577 " '%s' in the cache directory)\n"
578 " '-E-' disables this\n"
579 " if FILE is a directory, the default EPG file will be\n"
580 " created in that directory\n"
581 " --filesize=SIZE limit video files to SIZE bytes (default is %dM)\n"
582 " only useful in conjunction with --edit\n"
583 " --genindex=REC generate index for recording REC and exit\n"
584 " -g DIR, --grab=DIR write images from the SVDRP command GRAB into the\n"
585 " given DIR; DIR must be the full path name of an\n"
586 " existing directory, without any \"..\", double '/'\n"
587 " or symlinks (default: none, same as -g-)\n"
588 " -h, --help print this help and exit\n"
589 " -i ID, --instance=ID use ID as the id of this VDR instance (default: 0)\n"
590 " -l LEVEL, --log=LEVEL set log level (default: 3)\n"
591 " 0 = no logging, 1 = errors only,\n"
592 " 2 = errors and info, 3 = errors, info and debug\n"
593 " if logging should be done to LOG_LOCALn instead of\n"
594 " LOG_USER, add '.n' to LEVEL, as in 3.7 (n=0..7)\n"
595 " -L DIR, --lib=DIR search for plugins in DIR (default is %s)\n"
596 " --lirc[=PATH] use a LIRC remote control device, attached to PATH\n"
598 " --localedir=DIR search for locale files in DIR (default is\n"
600 " -m, --mute mute audio of the primary DVB device at startup\n"
601 " --no-kbd don't use the keyboard as an input device\n"
602 " -p PORT, --port=PORT use PORT for SVDRP (default: %d)\n"
603 " 0 turns off SVDRP\n"
604 " -P OPT, --plugin=OPT load a plugin defined by the given options\n"
605 " -r CMD, --record=CMD call CMD before and after a recording, and after\n"
606 " a recording has been edited or deleted\n"
607 " --resdir=DIR read resource files from DIR (default: %s)\n"
608 " -s CMD, --shutdown=CMD call CMD to shutdown the computer\n"
609 " --split split edited files at the editing marks (only\n"
610 " useful in conjunction with --edit)\n"
611 " --showargs[=DIR] print the arguments read from DIR and exit\n"
613 " -t TTY, --terminal=TTY controlling tty\n"
614 " -u USER, --user=USER run as user USER; only applicable if started as\n"
615 " root; USER can be a user name or a numerical id\n"
616 " --userdump allow coredumps if -u is given (debugging)\n"
617 " -v DIR, --video=DIR use DIR as video directory (default: %s)\n"
618 " -V, --version print version information and exit\n"
619 " --vfat for backwards compatibility (same as\n"
620 " --dirnames=250,40,1)\n"
621 " -w SEC, --watchdog=SEC activate the watchdog timer with a timeout of SEC\n"
622 " seconds (default: %d); '0' disables the watchdog\n"
644 printf(
"Plugins: vdr -P\"name [OPTIONS]\"\n\n");
645 for (
int i = 0; ; i++) {
650 if (DisplayHelp && help) {
665 openlog(
"vdr", LOG_CONS, SysLogTarget);
670 if (daemon(1, 0) == -1) {
671 fprintf(stderr,
"vdr: %m\n");
678 stdin = freopen(Terminal,
"r", stdin);
679 stdout = freopen(Terminal,
"w", stdout);
680 stderr = freopen(Terminal,
"w", stderr);
682 tcgetattr(STDIN_FILENO, &savedTm);
687 if (VdrUser && geteuid() == 0) {
688 StartedAsRoot =
true;
689 if (strcmp(VdrUser,
"root") && strcmp(VdrUser,
"0")) {
692 if (!
SetUser(VdrUser, UserDump))
704 fprintf(stderr,
"vdr: can't access video directory %s\n", VideoDirectory);
709 if (StartedAsRoot && VdrUser)
710 isyslog(
"switched to user '%s'", VdrUser);
717 char *CodeSet = NULL;
718 if (setlocale(LC_CTYPE,
""))
719 CodeSet = nl_langinfo(CODESET);
721 char *LangEnv = getenv(
"LANG");
723 CodeSet = strchr(LangEnv,
'.');
730 isyslog(
"codeset is '%s' - %s", CodeSet, known ?
"known" :
"unknown");
733 if (OverrideCharacterTable) {
735 isyslog(
"override character table is '%s' - %s", OverrideCharacterTable, known ?
"known" :
"unknown");
747 int LastTimerChannel = -1;
748 int PreviousChannel[2] = { 1, 1 };
749 int PreviousChannelIndex = 0;
750 time_t LastChannelChanged = time(NULL);
751 time_t LastInteract = 0;
752 int MaxLatencyTime = 0;
753 bool IsInfoMenu =
false;
754 cSkin *CurrentSkin = NULL;
755 int OldPrimaryDVB = 0;
764 if (!ConfigDirectory)
770 if (!ResourceDirectory)
793 const char *msg =
"no fonts available - OSD will not show any text!";
794 fprintf(stderr,
"vdr: %s\n", msg);
804 if (EpgDataFileName) {
805 const char *EpgDirectory = NULL;
807 EpgDirectory = EpgDataFileName;
810 else if (*EpgDataFileName !=
'/' && *EpgDataFileName !=
'.')
811 EpgDirectory = CacheDirectory;
816 EpgDataReader.
Start();
838 isyslog(
"trying device number %d instead", i + 1);
840 Setup.PrimaryDVB = i + 1;
846 const char *msg =
"no primary device found - using first device!";
847 fprintf(stderr,
"vdr: %s\n", msg);
852 const char *msg =
"no primary device found - giving up!";
853 fprintf(stderr,
"vdr: %s\n", msg);
859 OldPrimaryDVB =
Setup.PrimaryDVB;
876 CurrentSkin =
Skins.Current();
885 if (!CurrentSkin || CurrentSkin ==
Skins.Current() && strcmp(
Skins.Current()->Name(),
Setup.OSDSkin) != 0) {
893 if (!DaemonMode && HasStdin && UseKbd)
917 if (*
Setup.InitialChannel) {
920 if (
const cChannel *Channel = Channels->GetByNumber(atoi(
Setup.InitialChannel)))
921 Setup.InitialChannel = Channel->GetChannelID().ToString();
924 Setup.CurrentChannel = Channel->Number();
926 if (
Setup.InitialVolume >= 0)
930 Channels->SwitchTo(
Setup.CurrentChannel);
941 if (signal(SIGHUP,
SignalHandler) == SIG_IGN) signal(SIGHUP, SIG_IGN);
942 if (signal(SIGINT,
SignalHandler) == SIG_IGN) signal(SIGINT, SIG_IGN);
943 if (signal(SIGTERM,
SignalHandler) == SIG_IGN) signal(SIGTERM, SIG_IGN);
944 if (signal(SIGPIPE,
SignalHandler) == SIG_IGN) signal(SIGPIPE, SIG_IGN);
945 if (WatchdogTimeout > 0)
946 if (signal(SIGALRM,
Watchdog) == SIG_IGN) signal(SIGALRM, SIG_IGN);
950 if (WatchdogTimeout > 0) {
951 dsyslog(
"setting watchdog timer to %d seconds", WatchdogTimeout);
952 alarm(WatchdogTimeout);
956 if (sd_watchdog_enabled(0, NULL) > 0) {
958 SdWatchdog = time(NULL);
959 sd_watchdog_enabled(0, &timeout);
960 SdWatchdogTimeout = (int)timeout/1000000;
961 dsyslog(
"SD_WATCHDOG enabled with timeout set to %d seconds", SdWatchdogTimeout);
966 sd_notify(0,
"READY=1\nSTATUS=Ready");
976#define DELETE_MENU ((IsInfoMenu &= (Menu == NULL)), delete Menu, Menu = NULL)
979#ifdef DEBUGRINGBUFFERS
980 cRingBufferLinear::PrintDebugRBL();
985 time_t Now = time(NULL);
989 static time_t lastTime = 0;
994 if (Channel && (Channel->
Vpid() || Channel->
Apid(0) || Channel->
Dpid(0))) {
997 else if (LastTimerChannel > 0) {
998 Channel = Channels->GetByNumber(LastTimerChannel);
1004 LastTimerChannel = -1;
1012 static time_t lastOsdSizeUpdate = 0;
1013 if (Now != lastOsdSizeUpdate) {
1015 static int OsdState = 0;
1020 lastOsdSizeUpdate = Now;
1024 if (WatchdogTimeout > 0) {
1025 int LatencyTime = WatchdogTimeout - alarm(WatchdogTimeout);
1026 if (LatencyTime > MaxLatencyTime) {
1027 MaxLatencyTime = LatencyTime;
1028 dsyslog(
"max. latency time %d seconds", MaxLatencyTime);
1033 if (SdWatchdogTimeout && (Now - SdWatchdog) * 2 > SdWatchdogTimeout) {
1034 sd_notify(0,
"WATCHDOG=1");
1043 static time_t ChannelSaveTimeout = 0;
1045 static cStateKey ChannelsStateKey(
true);
1046 static int ChannelsModifiedByUser = 0;
1049 if (ChannelSaveTimeout != 1) {
1052 ChannelSaveTimeout = 1;
1053 else if (!ChannelSaveTimeout)
1057 ChannelSaveTimeout = 1;
1060 ChannelSaveTimeout = 1;
1061 if (Timers && Channels) {
1064 ChannelSaveTimeout = 0;
1067 for (
const cChannel *Channel = Channels->
First(); Channel; Channel = Channels->
Next(Channel)) {
1073 isyslog(
"retuning due to modification of channel %d (%s)", Channel->Number(), Channel->Name());
1074 Channels->
SwitchTo(Channel->Number());
1084 ChannelsStateKey.
Remove();
1087 if (ChannelSaveTimeout == 1) {
1089 ChannelsStateKey.
Reset();
1090 TimersStateKey.
Reset();
1098 LastChannelChanged = Now;
1100 if (Now - LastChannelChanged >=
Setup.ZapTimeout && LastChannel != PreviousChannel[PreviousChannelIndex])
1101 PreviousChannel[PreviousChannelIndex ^= 1] = LastChannel;
1111 SchedulesStateKey.
Reset();
1112 bool TimersModified =
false;
1117 TimersModified =
true;
1120 TimersModified =
true;
1123 TimersModified =
true;
1125 SchedulesStateKey.
Remove();
1127 TimersStateKey.
Remove(TimersModified);
1132 bool TimersModified =
false;
1134 TimersModified =
true;
1138 Timer->SetPending(
true);
1140 LastTimerChannel = Timer->Channel()->Number();
1141 TimersModified =
true;
1144 static time_t LastTimerCheck = 0;
1146 for (
cTimer *Timer = Timers->
First(); Timer; Timer = Timers->
Next(Timer)) {
1147 if (Timer->Remote() || Timer->IsPatternTimer())
1149 bool InVpsMargin =
false;
1150 bool NeedsTransponder =
false;
1151 if (Timer->HasFlags(
tfActive) && !Timer->Recording()) {
1152 if (Timer->HasFlags(
tfVps)) {
1153 if (Timer->Matches(Now,
Setup.VpsMargin))
1155 else if (Timer->Event()) {
1157 InVpsMargin = Timer->Event()->StartTime() <= Now && Now < Timer->Event()->EndTime();
1162 const cSchedule *Schedule = Schedules->GetSchedule(Timer->Channel());
1163 InVpsMargin = !Schedule;
1170 if (!Timer->Recording())
1171 Timer->SetInVpsMargin(InVpsMargin);
1172 if (NeedsTransponder || InVpsMargin) {
1187 dsyslog(
"switching device %d to channel %d %s (%s)", Device->
DeviceNumber() + 1, Timer->Channel()->Number(), *Timer->Channel()->GetChannelID().ToString(), Timer->Channel()->Name());
1192 LastTimerChannel = Timer->Channel()->Number();
1198 LastTimerCheck = Now;
1202 TimersModified =
true;
1205 if (MaxPriority >= 0)
1207 TimersStateKey.
Remove(TimersModified);
1218 Skins.ProcessQueuedMessages();
1221 if (!NeedsFastResponse) {
1246 bool WasOpen = Interact != NULL;
1247 bool WasMenu = Interact && Interact->
IsMenu();
1256 if (!WasOpen || !WasMenu && !
Setup.MenuKeyCloses)
1285 #define DirectMainFunction(function...)\
1289 Menu = new cMenuMain(function);\
1313 esyslog(
"ERROR: unknown plugin '%s'", PluginName);
1382 if (
Setup.PauseKeyHandling) {
1383 if (
Setup.PauseKeyHandling > 1 ||
Interface->Confirm(
tr(
"Pause live video?"))) {
1385 Skins.QueueMessage(
mtError,
tr(
"No free DVB device to record!"));
1394 if (
Setup.RecordKeyHandling) {
1395 if (
Setup.RecordKeyHandling > 1 ||
Interface->Confirm(
tr(
"Start recording?"))) {
1405 isyslog(
"Power button pressed");
1432 Interact = Menu ? Menu : Control;
1436 if (state ==
osUnknown && Interact != Control) {
1439 if (state ==
osEnd) {
1452 Skins.QueueMessage(
mtError,
tr(
"No free DVB device to record!"));
1480 case osEnd:
if (Interact == Menu)
1500 if (PreviousChannel[PreviousChannelIndex ^ 1] == LastChannel || LastChannel != PreviousChannel[0] && LastChannel != PreviousChannel[1])
1501 PreviousChannelIndex ^= 1;
1503 Channels->SwitchTo(PreviousChannel[PreviousChannelIndex ^= 1]);
1526 case kOk: LastChannel = -1;
break;
1555 int NewPrimaryDVB =
Setup.PrimaryDVB;
1556 if (NewPrimaryDVB != OldPrimaryDVB) {
1563 OldPrimaryDVB = NewPrimaryDVB;
1607 static time_t LastMemoryCleanup = 0;
1610 LastMemoryCleanup = Now;
1624 esyslog(
"emergency exit requested - shutting down");
1629 signal(SIGHUP, SIG_DFL);
1630 signal(SIGINT, SIG_DFL);
1631 signal(SIGTERM, SIG_DFL);
1632 signal(SIGPIPE, SIG_DFL);
1633 signal(SIGALRM, SIG_DFL);
1662 if (WatchdogTimeout > 0)
1663 dsyslog(
"max. latency time %d seconds", MaxLatencyTime);
1672 tcsetattr(STDIN_FILENO, TCSANOW, &savedTm);
1675 sd_notify(0,
"STOPPING=1\nSTATUS=Startup failed, exiting");
1677 sd_notify(0,
"STOPPING=1\nSTATUS=Exiting");