35 #include <sys/capability.h>
36 #include <sys/prctl.h>
70 #define MINCHANNELWAIT 10 // seconds to wait between failed channel switchings
71 #define ACTIVITYTIMEOUT 60 // seconds before starting housekeeping
72 #define SHUTDOWNWAIT 300 // seconds to wait in user prompt before automatic shutdown
73 #define SHUTDOWNRETRY 360 // seconds before trying again to shut down
74 #define SHUTDOWNFORCEPROMPT 5 // seconds to wait in user prompt to allow forcing shutdown
75 #define SHUTDOWNCANCELPROMPT 5 // seconds to wait in user prompt to allow canceling shutdown
76 #define RESTARTCANCELPROMPT 5 // seconds to wait in user prompt before restarting on SIGHUP
77 #define MANUALSTART 600 // seconds the next timer must be in the future to assume manual start
78 #define CHANNELSAVEDELTA 600 // seconds before saving channels.conf after automatic modifications
79 #define DEVICEREADYTIMEOUT 30 // seconds to wait until all devices are ready
80 #define MENUTIMEOUT 120 // seconds of user inactivity after which an OSD display is closed
81 #define TIMERCHECKDELTA 10 // seconds between checks for timers that need to see their channel
82 #define TIMERDEVICETIMEOUT 8 // seconds before a device used for timer check may be reused
83 #define TIMERLOOKAHEADTIME 60 // seconds before a non-VPS timer starts and the channel is switched if possible
84 #define VPSLOOKAHEADTIME 24 // hours within which VPS timers will make sure their events are up to date
85 #define VPSUPTODATETIME 3600 // seconds before the event or schedule of a VPS timer needs to be refreshed
87 #define EXIT(v) { ShutdownHandler.Exit(v); goto Exit; }
91 static bool SetUser(
const char *UserName,
bool UserDump)
94 struct passwd *user = getpwnam(UserName);
96 fprintf(stderr,
"vdr: unknown user: '%s'\n", UserName);
99 if (setgid(user->pw_gid) < 0) {
100 fprintf(stderr,
"vdr: cannot set group id %u: %s\n", (
unsigned int)user->pw_gid, strerror(errno));
103 if (initgroups(user->pw_name, user->pw_gid) < 0) {
104 fprintf(stderr,
"vdr: cannot set supplemental group ids for user %s: %s\n", user->pw_name, strerror(errno));
107 if (setuid(user->pw_uid) < 0) {
108 fprintf(stderr,
"vdr: cannot set user id %u: %s\n", (
unsigned int)user->pw_uid, strerror(errno));
111 if (UserDump && prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) < 0)
112 fprintf(stderr,
"vdr: warning - cannot set dumpable: %s\n", strerror(errno));
120 cap_t caps = cap_from_text(
"= cap_sys_nice,cap_sys_time,cap_net_raw=ep");
122 fprintf(stderr,
"vdr: cap_from_text failed: %s\n", strerror(errno));
125 if (cap_set_proc(caps) == -1) {
126 fprintf(stderr,
"vdr: cap_set_proc failed: %s\n", strerror(errno));
137 if (prctl(PR_SET_KEEPCAPS, On ? 1 : 0, 0, 0, 0) != 0) {
138 fprintf(stderr,
"vdr: prctl failed\n");
164 esyslog(
"PANIC: watchdog timer expired - exiting!");
168 int main(
int argc,
char *argv[])
172 struct termios savedTm;
173 bool HasStdin = (tcgetpgrp(STDIN_FILENO) == getpid() || getppid() != (pid_t)1) && tcgetattr(STDIN_FILENO, &savedTm) == 0;
177 setlocale(LC_ALL,
"");
178 setlocale(LC_NUMERIC,
"C");
182 #define dd(a, b) (*a ? a : b)
183 #define DEFAULTSVDRPPORT 6419
184 #define DEFAULTWATCHDOG 0 // seconds
185 #define DEFAULTVIDEODIR VIDEODIR
186 #define DEFAULTCONFDIR dd(CONFDIR, VideoDirectory)
187 #define DEFAULTCACHEDIR dd(CACHEDIR, VideoDirectory)
188 #define DEFAULTRESDIR dd(RESDIR, ConfigDirectory)
189 #define DEFAULTPLUGINDIR PLUGINDIR
190 #define DEFAULTLOCDIR LOCDIR
191 #define DEFAULTEPGDATAFILENAME "epg.data"
193 bool StartedAsRoot =
false;
194 const char *VdrUser = NULL;
195 bool UserDump =
false;
197 const char *AudioCommand = NULL;
199 const char *ConfigDirectory = NULL;
200 const char *CacheDirectory = NULL;
201 const char *ResourceDirectory = NULL;
204 bool DisplayHelp =
false;
205 bool DisplayVersion =
false;
206 bool DaemonMode =
false;
207 int SysLogTarget = LOG_USER;
208 bool MuteAudio =
false;
210 const char *Terminal = NULL;
213 const char *LircDevice = NULL;
214 #if !defined(REMOTE_KBD)
217 #if defined(REMOTE_LIRC)
218 LircDevice = LIRC_DEVICE;
220 #if defined(VDR_USER)
226 static struct option long_options[] = {
227 {
"audio", required_argument, NULL,
'a' },
228 {
"cachedir", required_argument, NULL,
'c' | 0x100 },
229 {
"config", required_argument, NULL,
'c' },
230 {
"daemon", no_argument, NULL,
'd' },
231 {
"device", required_argument, NULL,
'D' },
232 {
"edit", required_argument, NULL,
'e' | 0x100 },
233 {
"epgfile", required_argument, NULL,
'E' },
234 {
"filesize", required_argument, NULL,
'f' | 0x100 },
235 {
"genindex", required_argument, NULL,
'g' | 0x100 },
236 {
"grab", required_argument, NULL,
'g' },
237 {
"help", no_argument, NULL,
'h' },
238 {
"instance", required_argument, NULL,
'i' },
239 {
"lib", required_argument, NULL,
'L' },
240 {
"lirc", optional_argument, NULL,
'l' | 0x100 },
241 {
"localedir",required_argument, NULL,
'l' | 0x200 },
242 {
"log", required_argument, NULL,
'l' },
243 {
"mute", no_argument, NULL,
'm' },
244 {
"no-kbd", no_argument, NULL,
'n' | 0x100 },
245 {
"plugin", required_argument, NULL,
'P' },
246 {
"port", required_argument, NULL,
'p' },
247 {
"record", required_argument, NULL,
'r' },
248 {
"resdir", required_argument, NULL,
'r' | 0x100 },
249 {
"shutdown", required_argument, NULL,
's' },
250 {
"split", no_argument, NULL,
's' | 0x100 },
251 {
"terminal", required_argument, NULL,
't' },
252 {
"user", required_argument, NULL,
'u' },
253 {
"userdump", no_argument, NULL,
'u' | 0x100 },
254 {
"version", no_argument, NULL,
'V' },
255 {
"vfat", no_argument, NULL,
'v' | 0x100 },
256 {
"video", required_argument, NULL,
'v' },
257 {
"watchdog", required_argument, NULL,
'w' },
258 { NULL, no_argument, NULL, 0 }
262 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) {
264 case 'a': AudioCommand = optarg;
267 CacheDirectory = optarg;
269 case 'c': ConfigDirectory = optarg;
271 case 'd': DaemonMode =
true;
break;
273 int n = atoi(optarg);
279 fprintf(stderr,
"vdr: invalid DVB device number: %s\n", optarg);
284 case 'E': EpgDataFileName = (*optarg !=
'-' ? optarg : NULL);
297 case 'h': DisplayHelp =
true;
304 fprintf(stderr,
"vdr: invalid instance id: %s\n", optarg);
307 char *p = strchr(optarg,
'.');
311 int l = atoi(optarg);
312 if (0 <= l && l <= 3) {
318 if (0 <= l && l <= 7) {
319 int targets[] = { LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7 };
320 SysLogTarget = targets[l];
328 fprintf(stderr,
"vdr: invalid log level: %s\n", optarg);
332 case 'L':
if (access(optarg, R_OK | X_OK) == 0)
335 fprintf(stderr,
"vdr: can't access plugin directory: %s\n", optarg);
340 LircDevice = optarg ? optarg : LIRC_DEVICE;
343 if (access(optarg, R_OK | X_OK) == 0)
344 LocaleDirectory = optarg;
346 fprintf(stderr,
"vdr: can't access locale directory: %s\n", optarg);
350 case 'm': MuteAudio =
true;
356 SVDRPport = atoi(optarg);
358 fprintf(stderr,
"vdr: invalid port number: %s\n", optarg);
362 case 'P': PluginManager.
AddPlugin(optarg);
367 ResourceDirectory = optarg;
374 case 't': Terminal = optarg;
375 if (access(Terminal, R_OK | W_OK) < 0) {
376 fprintf(stderr,
"vdr: can't access terminal: %s\n", Terminal);
380 case 'u':
if (*optarg)
386 case 'V': DisplayVersion =
true;
391 case 'v': VideoDirectory = optarg;
392 while (optarg && *optarg && optarg[strlen(optarg) - 1] ==
'/')
393 optarg[strlen(optarg) - 1] = 0;
396 int t = atoi(optarg);
402 fprintf(stderr,
"vdr: invalid watchdog timeout: %s\n", optarg);
411 if (VdrUser && geteuid() == 0) {
412 StartedAsRoot =
true;
413 if (strcmp(VdrUser,
"root")) {
416 if (!
SetUser(VdrUser, UserDump))
427 if (DisplayHelp || DisplayVersion) {
432 printf(
"Usage: vdr [OPTIONS]\n\n"
433 " -a CMD, --audio=CMD send Dolby Digital audio to stdin of command CMD\n"
434 " --cachedir=DIR save cache files in DIR (default: %s)\n"
435 " -c DIR, --config=DIR read config files from DIR (default: %s)\n"
436 " -d, --daemon run in daemon mode\n"
437 " -D NUM, --device=NUM use only the given DVB device (NUM = 0, 1, 2...)\n"
438 " there may be several -D options (default: all DVB\n"
439 " devices will be used)\n"
440 " --edit=REC cut recording REC and exit\n"
441 " -E FILE, --epgfile=FILE write the EPG data into the given FILE (default is\n"
442 " /var/cache/vdr/%s)\n"
443 " '-E-' disables this\n"
444 " if FILE is a directory, the default EPG file will be\n"
445 " created in that directory\n"
446 " --filesize=SIZE limit video files to SIZE bytes (default is %dM)\n"
447 " only useful in conjunction with --edit\n"
448 " --genindex=REC generate index for recording REC and exit\n"
449 " -g DIR, --grab=DIR write images from the SVDRP command GRAB into the\n"
450 " given DIR; DIR must be the full path name of an\n"
451 " existing directory, without any \"..\", double '/'\n"
452 " or symlinks (default: none, same as -g-)\n"
453 " -h, --help print this help and exit\n"
454 " -i ID, --instance=ID use ID as the id of this VDR instance (default: 0)\n"
455 " -l LEVEL, --log=LEVEL set log level (default: 3)\n"
456 " 0 = no logging, 1 = errors only,\n"
457 " 2 = errors and info, 3 = errors, info and debug\n"
458 " if logging should be done to LOG_LOCALn instead of\n"
459 " LOG_USER, add '.n' to LEVEL, as in 3.7 (n=0..7)\n"
460 " -L DIR, --lib=DIR search for plugins in DIR (default is %s)\n"
461 " --lirc[=PATH] use a LIRC remote control device, attached to PATH\n"
463 " --localedir=DIR search for locale files in DIR (default is\n"
465 " -m, --mute mute audio of the primary DVB device at startup\n"
466 " --no-kbd don't use the keyboard as an input device\n"
467 " -p PORT, --port=PORT use PORT for SVDRP (default: %d)\n"
468 " 0 turns off SVDRP\n"
469 " -P OPT, --plugin=OPT load a plugin defined by the given options\n"
470 " -r CMD, --record=CMD call CMD before and after a recording, and after\n"
471 " a recording has been edited or deleted\n"
472 " --resdir=DIR read resource files from DIR (default: %s)\n"
473 " -s CMD, --shutdown=CMD call CMD to shutdown the computer\n"
474 " --split split edited files at the editing marks (only\n"
475 " useful in conjunction with --edit)\n"
476 " -t TTY, --terminal=TTY controlling tty\n"
477 " -u USER, --user=USER run as user USER; only applicable if started as\n"
479 " --userdump allow coredumps if -u is given (debugging)\n"
480 " -v DIR, --video=DIR use DIR as video directory (default: %s)\n"
481 " -V, --version print version information and exit\n"
482 " --vfat encode special characters in recording names to\n"
483 " avoid problems with VFAT file systems\n"
484 " -w SEC, --watchdog=SEC activate the watchdog timer with a timeout of SEC\n"
485 " seconds (default: %d); '0' disables the watchdog\n"
504 printf(
"Plugins: vdr -P\"name [OPTIONS]\"\n\n");
505 for (
int i = 0; ; i++) {
510 if (DisplayHelp && help) {
525 openlog(
"vdr", LOG_CONS, SysLogTarget);
530 fprintf(stderr,
"vdr: can't access video directory %s\n", VideoDirectory);
537 if (daemon(1, 0) == -1) {
538 fprintf(stderr,
"vdr: %m\n");
545 stdin = freopen(Terminal,
"r", stdin);
546 stdout = freopen(Terminal,
"w", stdout);
547 stderr = freopen(Terminal,
"w", stderr);
549 tcgetattr(STDIN_FILENO, &savedTm);
553 if (StartedAsRoot && VdrUser)
554 isyslog(
"switched to user '%s'", VdrUser);
561 char *CodeSet = NULL;
562 if (setlocale(LC_CTYPE,
""))
563 CodeSet = nl_langinfo(CODESET);
565 char *LangEnv = getenv(
"LANG");
567 CodeSet = strchr(LangEnv,
'.');
574 isyslog(
"codeset is '%s' - %s", CodeSet, known ?
"known" :
"unknown");
587 int LastTimerChannel = -1;
588 int PreviousChannel[2] = { 1, 1 };
589 int PreviousChannelIndex = 0;
590 time_t LastChannelChanged = time(NULL);
591 time_t LastInteract = 0;
592 int MaxLatencyTime = 0;
593 bool InhibitEpgScan =
false;
594 bool IsInfoMenu =
false;
595 cSkin *CurrentSkin = NULL;
605 if (!ConfigDirectory)
611 if (!ResourceDirectory)
633 const char *msg =
"no fonts available - OSD will not show any text!";
634 fprintf(stderr,
"vdr: %s\n", msg);
645 if (EpgDataFileName) {
646 const char *EpgDirectory = NULL;
648 EpgDirectory = EpgDataFileName;
651 else if (*EpgDataFileName !=
'/' && *EpgDataFileName !=
'.')
652 EpgDirectory = CacheDirectory;
657 EpgDataReader.
Start();
679 isyslog(
"trying device number %d instead", i + 1);
687 const char *msg =
"no primary device found - using first device!";
688 fprintf(stderr,
"vdr: %s\n", msg);
693 const char *msg =
"no primary device found - giving up!";
694 fprintf(stderr,
"vdr: %s\n", msg);
733 if (!DaemonMode && HasStdin && UseKbd)
764 if (signal(SIGHUP,
SignalHandler) == SIG_IGN) signal(SIGHUP, SIG_IGN);
765 if (signal(SIGINT,
SignalHandler) == SIG_IGN) signal(SIGINT, SIG_IGN);
766 if (signal(SIGTERM,
SignalHandler) == SIG_IGN) signal(SIGTERM, SIG_IGN);
767 if (signal(SIGPIPE,
SignalHandler) == SIG_IGN) signal(SIGPIPE, SIG_IGN);
768 if (WatchdogTimeout > 0)
769 if (signal(SIGALRM,
Watchdog) == SIG_IGN) signal(SIGALRM, SIG_IGN);
773 if (WatchdogTimeout > 0) {
774 dsyslog(
"setting watchdog timer to %d seconds", WatchdogTimeout);
775 alarm(WatchdogTimeout);
780 #define DELETE_MENU ((IsInfoMenu &= (Menu == NULL)), delete Menu, Menu = NULL)
783 #ifdef DEBUGRINGBUFFERS
784 cRingBufferLinear::PrintDebugRBL();
789 time_t Now = time(NULL);
793 static time_t lastTime = 0;
796 if (Channel && (Channel->
Vpid() || Channel->
Apid(0) || Channel->
Dpid(0))) {
799 else if (LastTimerChannel > 0) {
806 LastTimerChannel = -1;
811 static time_t lastOsdSizeUpdate = 0;
812 if (Now != lastOsdSizeUpdate) {
814 lastOsdSizeUpdate = Now;
818 if (WatchdogTimeout > 0) {
819 int LatencyTime = WatchdogTimeout - alarm(WatchdogTimeout);
820 if (LatencyTime > MaxLatencyTime) {
821 MaxLatencyTime = LatencyTime;
822 dsyslog(
"max. latency time %d seconds", MaxLatencyTime);
828 static time_t ChannelSaveTimeout = 0;
829 static int TimerState = 0;
833 ChannelSaveTimeout = 1;
834 else if (modified && !ChannelSaveTimeout)
836 bool timeout = ChannelSaveTimeout == 1 || ChannelSaveTimeout && Now > ChannelSaveTimeout && !
cRecordControls::Active();
837 if ((modified || timeout) &&
Channels.
Lock(
false, 100)) {
841 ChannelSaveTimeout = 0;
849 isyslog(
"retuning due to modification of channel %d", Channel->Number());
864 LastChannelChanged = Now;
866 if (Now - LastChannelChanged >=
Setup.
ZapTimeout && LastChannel != PreviousChannel[PreviousChannelIndex])
867 PreviousChannel[PreviousChannelIndex ^= 1] = LastChannel;
884 static time_t LastTimerCheck = 0;
886 InhibitEpgScan =
false;
888 bool InVpsMargin =
false;
889 bool NeedsTransponder =
false;
896 else if (Timer->
Event()) {
905 InVpsMargin = !Schedule;
909 InhibitEpgScan |= InVpsMargin | NeedsTransponder;
914 if (NeedsTransponder || InVpsMargin) {
917 if (!Device && InVpsMargin)
934 LastTimerCheck = Now;
965 bool WasOpen = Interact != NULL;
966 bool WasMenu = Interact && Interact->
IsMenu();
1004 #define DirectMainFunction(function)\
1006 if (cControl::Control())\
1007 cControl::Control()->Hide();\
1008 Menu = new cMenuMain(function);\
1009 key = kNone; // nobody else needs to see this key
1030 esyslog(
"ERROR: unknown plugin '%s'", PluginName);
1116 isyslog(
"Power button pressed");
1150 if (state ==
osEnd) {
1193 case osEnd:
if (Interact == Menu)
1211 if (PreviousChannel[PreviousChannelIndex ^ 1] == LastChannel || LastChannel != PreviousChannel[0] && LastChannel != PreviousChannel[1])
1212 PreviousChannelIndex ^= 1;
1236 case kOk: LastChannel = -1;
break;
1248 if (!InhibitEpgScan)
1313 esyslog(
"emergency exit requested - shutting down");
1318 signal(SIGHUP, SIG_DFL);
1319 signal(SIGINT, SIG_DFL);
1320 signal(SIGTERM, SIG_DFL);
1321 signal(SIGPIPE, SIG_DFL);
1322 signal(SIGALRM, SIG_DFL);
1346 if (WatchdogTimeout > 0)
1347 dsyslog(
"max. latency time %d seconds", MaxLatencyTime);
1356 tcsetattr(STDIN_FILENO, TCSANOW, &savedTm);