vdr  2.2.0
shutdown.c
Go to the documentation of this file.
1 /*
2  * shutdown.c: Handling of shutdown and inactivity
3  *
4  * See the main source file 'vdr.c' for copyright information and
5  * how to reach the author.
6  *
7  * Original version written by Udo Richter <udo_richter@gmx.de>.
8  *
9  * $Id: shutdown.c 3.1 2013/10/02 09:02:01 kls Exp $
10  */
11 
12 #include "shutdown.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include "channels.h"
18 #include "config.h"
19 #include "i18n.h"
20 #include "interface.h"
21 #include "menu.h"
22 #include "plugin.h"
23 #include "recording.h"
24 #include "timers.h"
25 #include "tools.h"
26 
28 
30 {
31  timeout = 0;
32  counter = 0;
33  timedOut = false;
34  message = NULL;
35 }
36 
37 void cCountdown::Start(const char *Message, int Seconds)
38 {
39  timeout = time(NULL) + Seconds;
40  counter = -1;
41  timedOut = false;
42  message = Message;
43  Update();
44 }
45 
47 {
48  if (timeout) {
49  timeout = 0;
50  timedOut = false;
51  Skins.Message(mtStatus, NULL);
52  }
53 }
54 
55 bool cCountdown::Done(void)
56 {
57  if (timedOut) {
58  Cancel();
59  return true;
60  }
61  return false;
62 }
63 
65 {
66  if (timeout) {
67  int NewCounter = (timeout - time(NULL) + 9) / 10;
68  if (NewCounter <= 0)
69  timedOut = true;
70  if (counter != NewCounter) {
71  counter = NewCounter;
72  char time[10];
73  snprintf(time, sizeof(time), "%d:%d0", counter > 0 ? counter / 6 : 0, counter > 0 ? counter % 6 : 0);
74  cString Message = cString::sprintf(message, time);
75  Skins.Message(mtStatus, Message);
76  return true;
77  }
78  }
79  return false;
80 }
81 
83 {
84  activeTimeout = 0;
85  retry = 0;
86  shutdownCommand = NULL;
87  exitCode = -1;
88  emergencyExitRequested = false;
89 }
90 
92 {
93  free(shutdownCommand);
94 }
95 
97 {
98  if (Setup.EmergencyExit) {
99  esyslog("initiating emergency exit");
100  emergencyExitRequested = true;
101  Exit(1);
102  }
103  else
104  dsyslog("emergency exit request ignored according to setup");
105 }
106 
108 {
109  time_t Delta = Setup.NextWakeupTime ? Setup.NextWakeupTime - time(NULL) : 0;
110 
111  if (!Setup.NextWakeupTime || abs(Delta) > ManualStart) {
112  // Apparently the user started VDR manually
113  dsyslog("assuming manual start of VDR");
114  // Set inactive after MinUserInactivity
115  SetUserInactiveTimeout();
116  }
117  else {
118  // Set inactive from now on
119  dsyslog("scheduled wakeup time in %ld minutes, assuming automatic start of VDR", Delta / 60);
120  SetUserInactiveTimeout(-3, true);
121  }
122 }
123 
124 void cShutdownHandler::SetShutdownCommand(const char *ShutdownCommand)
125 {
126  free(shutdownCommand);
127  shutdownCommand = ShutdownCommand ? strdup(ShutdownCommand) : NULL;
128 }
129 
130 void cShutdownHandler::CallShutdownCommand(time_t WakeupTime, int Channel, const char *File, bool UserShutdown)
131 {
132  time_t Delta = WakeupTime ? WakeupTime - time(NULL) : 0;
133  cString cmd = cString::sprintf("%s %ld %ld %d \"%s\" %d", shutdownCommand, WakeupTime, Delta, Channel, *strescape(File, "\\\"$"), UserShutdown);
134  isyslog("executing '%s'", *cmd);
135  int Status = SystemExec(cmd, true);
136  if (!WIFEXITED(Status) || WEXITSTATUS(Status))
137  esyslog("SystemExec() failed with status %d", Status);
138  else {
139  Setup.NextWakeupTime = WakeupTime; // Remember this wakeup time for comparison on reboot
140  Setup.Save();
141  }
142 }
143 
144 void cShutdownHandler::SetUserInactiveTimeout(int Seconds, bool Force)
145 {
146  if (!Setup.MinUserInactivity && !Force) {
147  activeTimeout = 0;
148  return;
149  }
150  if (Seconds >= 0)
151  activeTimeout = time(NULL) + Seconds;
152  else if (Seconds == -1)
153  activeTimeout = time(NULL) + Setup.MinUserInactivity * 60;
154  else if (Seconds == -2)
155  activeTimeout = 0;
156  else if (Seconds == -3)
157  activeTimeout = 1;
158 }
159 
160 bool cShutdownHandler::ConfirmShutdown(bool Interactive)
161 {
162  if (!Interactive && !cRemote::Enabled())
163  return false;
164 
165  if (!shutdownCommand) {
166  if (Interactive)
167  Skins.Message(mtError, tr("Can't shutdown - option '-s' not given!"));
168  return false;
169  }
170  if (RecordingsHandler.Active()) {
171  if (!Interactive || !Interface->Confirm(tr("Editing - shut down anyway?")))
172  return false;
173  }
174 
175  cTimer *timer = Timers.GetNextActiveTimer();
176  time_t Next = timer ? timer->StartTime() : 0;
177  time_t Delta = timer ? Next - time(NULL) : 0;
178 
179  if (cRecordControls::Active() || (Next && Delta <= 0)) {
180  // VPS recordings in timer end margin may cause Delta <= 0
181  if (!Interactive || !Interface->Confirm(tr("Recording - shut down anyway?")))
182  return false;
183  }
184  else if (Next && Delta <= Setup.MinEventTimeout * 60) {
185  // Timer within Min Event Timeout
186  if (!Interactive)
187  return false;
188  cString buf = cString::sprintf(tr("Recording in %ld minutes, shut down anyway?"), Delta / 60);
189  if (!Interface->Confirm(buf))
190  return false;
191  }
192 
193  if (cPluginManager::Active(Interactive ? tr("shut down anyway?") : NULL))
194  return false;
195 
197  Next = Plugin ? Plugin->WakeupTime() : 0;
198  Delta = Next ? Next - time(NULL) : 0;
199  if (Next && Delta <= Setup.MinEventTimeout * 60) {
200  // Plugin wakeup within Min Event Timeout
201  if (!Interactive)
202  return false;
203  cString buf = cString::sprintf(tr("Plugin %s wakes up in %ld min, continue?"), Plugin->Name(), Delta / 60);
204  if (!Interface->Confirm(buf))
205  return false;
206  }
207 
208  return true;
209 }
210 
211 bool cShutdownHandler::ConfirmRestart(bool Interactive)
212 {
213  if (RecordingsHandler.Active()) {
214  if (!Interactive || !Interface->Confirm(tr("Editing - restart anyway?")))
215  return false;
216  }
217 
218  cTimer *timer = Timers.GetNextActiveTimer();
219  time_t Next = timer ? timer->StartTime() : 0;
220  time_t Delta = timer ? Next - time(NULL) : 0;
221 
222  if (cRecordControls::Active() || (Next && Delta <= 0)) {
223  // VPS recordings in timer end margin may cause Delta <= 0
224  if (!Interactive || !Interface->Confirm(tr("Recording - restart anyway?")))
225  return false;
226  }
227 
228  if (cPluginManager::Active(Interactive ? tr("restart anyway?") : NULL))
229  return false;
230 
231  return true;
232 }
233 
235 {
236  time_t Now = time(NULL);
237  cTimer *timer = Timers.GetNextActiveTimer();
239 
240  time_t Next = timer ? timer->StartTime() : 0;
241  time_t NextPlugin = Plugin ? Plugin->WakeupTime() : 0;
242  if (NextPlugin && (!Next || Next > NextPlugin)) {
243  Next = NextPlugin;
244  timer = NULL;
245  }
246  time_t Delta = Next ? Next - Now : 0;
247 
248  if (Next && Delta < Setup.MinEventTimeout * 60) {
249  if (!Force)
250  return false;
251  Delta = Setup.MinEventTimeout * 60;
252  Next = Now + Delta;
253  timer = NULL;
254  dsyslog("reboot at %s", *TimeToString(Next));
255  }
256 
257  if (Next && timer) {
258  dsyslog("next timer event at %s", *TimeToString(Next));
259  CallShutdownCommand(Next, timer->Channel()->Number(), timer->File(), Force);
260  }
261  else if (Next && Plugin) {
262  CallShutdownCommand(Next, 0, Plugin->Name(), Force);
263  dsyslog("next plugin wakeup at %s", *TimeToString(Next));
264  }
265  else
266  CallShutdownCommand(Next, 0, "", Force); // Next should always be 0 here. Just for safety, pass it.
267 
268  return true;
269 }
void CallShutdownCommand(time_t WakeupTime, int Channel, const char *File, bool UserShutdown)
Call the shutdown command with the given parameters.
Definition: shutdown.c:130
int Number(void) const
Definition: channels.h:197
void CheckManualStart(int ManualStart)
Check whether the next timer is in ManualStart time window.
Definition: shutdown.c:107
#define dsyslog(a...)
Definition: tools.h:36
bool Confirm(const char *s, int Seconds=10, bool WaitForTimeout=false)
Definition: interface.c:67
const char * Name(void)
Definition: plugin.h:34
int counter
last shown time in 10s units
Definition: shutdown.h:20
cTimers Timers
Definition: timers.c:694
static cString sprintf(const char *fmt,...) __attribute__((format(printf
Definition: tools.c:1080
Definition: plugin.h:20
bool timedOut
countdown did run down to 0 and was not canceled
Definition: shutdown.h:21
#define esyslog(a...)
Definition: tools.h:34
int MinUserInactivity
Definition: config.h:337
bool Save(void)
Definition: config.c:727
bool Update(void)
Update status display of the countdown.
Definition: shutdown.c:64
bool DoShutdown(bool Force)
Call the shutdown script with data of the next pending timer.
Definition: shutdown.c:234
time_t StartTime(void) const
Definition: timers.c:497
cTimer * GetNextActiveTimer(void)
Definition: timers.c:757
const cChannel * Channel(void) const
Definition: timers.h:56
static bool Active(const char *Prompt=NULL)
Definition: plugin.c:415
Definition: timers.h:27
int EmergencyExit
Definition: config.h:362
cString TimeToString(time_t t)
Converts the given time to a string of the form "www mmm dd hh:mm:ss yyyy".
Definition: tools.c:1156
void Start(const char *Message, int Seconds)
Start the 5 minute shutdown warning countdown.
Definition: shutdown.c:37
cShutdownHandler(void)
Definition: shutdown.c:82
const char * message
message to display, s is placeholder for time
Definition: shutdown.h:22
static bool Active(void)
Definition: menu.c:5321
bool ConfirmShutdown(bool Ask)
Check for background activity that blocks shutdown.
Definition: shutdown.c:160
cSetup Setup
Definition: config.c:373
cShutdownHandler ShutdownHandler
Definition: shutdown.c:27
cRecordingsHandler RecordingsHandler
Definition: recording.c:1910
int SystemExec(const char *Command, bool Detached)
Definition: thread.c:559
int MinEventTimeout
Definition: config.h:337
eKeys Message(eMessageType Type, const char *s, int Seconds=0)
Displays the given message, either through a currently visible display object that is capable of doin...
Definition: skins.c:250
Definition: skins.h:24
static bool Enabled(void)
Definition: remote.h:49
Definition: skins.h:24
static cPlugin * GetNextWakeupPlugin(void)
Definition: plugin.c:432
void SetUserInactiveTimeout(int Seconds=-1, bool Force=false)
Set the time in the future when VDR will switch into non-interactive mode or power down...
Definition: shutdown.c:144
time_t NextWakeupTime
Definition: config.h:338
cCountdown(void)
Definition: shutdown.c:29
#define tr(s)
Definition: i18n.h:85
const char * File(void) const
Definition: timers.h:63
bool ConfirmRestart(bool Ask)
Check for background activity that blocks restart.
Definition: shutdown.c:211
#define isyslog(a...)
Definition: tools.h:35
bool Active(void)
Checks whether there is currently any operation running and starts the next one form the list if the ...
Definition: recording.c:1989
cString strescape(const char *s, const char *chars)
Definition: tools.c:254
cInterface * Interface
Definition: interface.c:20
virtual time_t WakeupTime(void)
Definition: plugin.c:85
void SetShutdownCommand(const char *ShutdownCommand)
Set the command string for shutdown command.
Definition: shutdown.c:124
time_t timeout
5-minute countdown timer
Definition: shutdown.h:19
void RequestEmergencyExit(void)
Requests an emergency exit of the VDR main loop.
Definition: shutdown.c:96
Definition: tools.h:168
void Cancel(void)
Cancel the 5 minute shutdown warning countdown.
Definition: shutdown.c:46
bool Done(void)
Check if countdown timer has run out without canceling.
Definition: shutdown.c:55
cSkins Skins
Definition: skins.c:219