vdr  1.7.31
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 2.0 2008/02/24 10:29:00 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 "cutter.h"
20 #include "filetransfer.h"
21 #include "i18n.h"
22 #include "interface.h"
23 #include "menu.h"
24 #include "plugin.h"
25 #include "timers.h"
26 #include "tools.h"
27 
29 
31 {
32  timeout = 0;
33  counter = 0;
34  timedOut = false;
35  message = NULL;
36 }
37 
38 void cCountdown::Start(const char *Message, int Seconds)
39 {
40  timeout = time(NULL) + Seconds;
41  counter = -1;
42  timedOut = false;
43  message = Message;
44  Update();
45 }
46 
48 {
49  if (timeout) {
50  timeout = 0;
51  timedOut = false;
52  Skins.Message(mtStatus, NULL);
53  }
54 }
55 
56 bool cCountdown::Done(void)
57 {
58  if (timedOut) {
59  Cancel();
60  return true;
61  }
62  return false;
63 }
64 
66 {
67  if (timeout) {
68  int NewCounter = (timeout - time(NULL) + 9) / 10;
69  if (NewCounter <= 0)
70  timedOut = true;
71  if (counter != NewCounter) {
72  counter = NewCounter;
73  char time[10];
74  snprintf(time, sizeof(time), "%d:%d0", counter > 0 ? counter / 6 : 0, counter > 0 ? counter % 6 : 0);
75  cString Message = cString::sprintf(message, time);
76  Skins.Message(mtStatus, Message);
77  return true;
78  }
79  }
80  return false;
81 }
82 
84 {
85  activeTimeout = 0;
86  retry = 0;
87  shutdownCommand = NULL;
88  exitCode = -1;
89  emergencyExitRequested = false;
90 }
91 
93 {
94  free(shutdownCommand);
95 }
96 
98 {
99  if (Setup.EmergencyExit) {
100  esyslog("initiating emergency exit");
101  emergencyExitRequested = true;
102  Exit(1);
103  }
104  else
105  dsyslog("emergency exit request ignored according to setup");
106 }
107 
109 {
110  time_t Delta = Setup.NextWakeupTime ? Setup.NextWakeupTime - time(NULL) : 0;
111 
112  if (!Setup.NextWakeupTime || abs(Delta) > ManualStart) {
113  // Apparently the user started VDR manually
114  dsyslog("assuming manual start of VDR");
115  // Set inactive after MinUserInactivity
117  }
118  else {
119  // Set inactive from now on
120  dsyslog("scheduled wakeup time in %ld minutes, assuming automatic start of VDR", Delta / 60);
121  SetUserInactive();
122  }
123 }
124 
125 void cShutdownHandler::SetShutdownCommand(const char *ShutdownCommand)
126 {
127  free(shutdownCommand);
128  shutdownCommand = ShutdownCommand ? strdup(ShutdownCommand) : NULL;
129 }
130 
131 void cShutdownHandler::CallShutdownCommand(time_t WakeupTime, int Channel, const char *File, bool UserShutdown)
132 {
133  time_t Delta = WakeupTime ? WakeupTime - time(NULL) : 0;
134  cString cmd = cString::sprintf("%s %ld %ld %d \"%s\" %d", shutdownCommand, WakeupTime, Delta, Channel, *strescape(File, "\\\"$"), UserShutdown);
135  isyslog("executing '%s'", *cmd);
136  int Status = SystemExec(cmd, true);
137  if (!WIFEXITED(Status) || WEXITSTATUS(Status))
138  esyslog("SystemExec() failed with status %d", Status);
139  else {
140  Setup.NextWakeupTime = WakeupTime; // Remember this wakeup time for comparison on reboot
141  Setup.Save();
142  }
143 }
144 
145 void cShutdownHandler::SetUserInactiveTimeout(int Seconds, bool Force)
146 {
147  if (!Setup.MinUserInactivity && !Force) {
148  activeTimeout = 0;
149  return;
150  }
151  if (Seconds < 0)
152  Seconds = Setup.MinUserInactivity * 60;
153  activeTimeout = time(NULL) + Seconds;
154 }
155 
156 bool cShutdownHandler::ConfirmShutdown(bool Interactive)
157 {
158  if (!Interactive && !cRemote::Enabled())
159  return false;
160 
161  if (!shutdownCommand) {
162  if (Interactive)
163  Skins.Message(mtError, tr("Can't shutdown - option '-s' not given!"));
164  return false;
165  }
166  if (cCutter::Active()) {
167  if (!Interactive || !Interface->Confirm(tr("Editing - shut down anyway?")))
168  return false;
169  }
170  if (cFileTransfer::Active()) {
171  if (!Interactive || !Interface->Confirm(tr("Transfering file - 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 (cCutter::Active()) {
214  if (!Interactive || !Interface->Confirm(tr("Editing - restart anyway?")))
215  return false;
216  }
217  if (cFileTransfer::Active()) {
218  if (!Interactive || !Interface->Confirm(tr("Transfering file - restart anyway?")))
219  return false;
220  }
221 
222  cTimer *timer = Timers.GetNextActiveTimer();
223  time_t Next = timer ? timer->StartTime() : 0;
224  time_t Delta = timer ? Next - time(NULL) : 0;
225 
226  if (cRecordControls::Active() || (Next && Delta <= 0)) {
227  // VPS recordings in timer end margin may cause Delta <= 0
228  if (!Interactive || !Interface->Confirm(tr("Recording - restart anyway?")))
229  return false;
230  }
231 
232  if (cPluginManager::Active(Interactive ? tr("restart anyway?") : NULL))
233  return false;
234 
235  return true;
236 }
237 
239 {
240  time_t Now = time(NULL);
241  cTimer *timer = Timers.GetNextActiveTimer();
243 
244  time_t Next = timer ? timer->StartTime() : 0;
245  time_t NextPlugin = Plugin ? Plugin->WakeupTime() : 0;
246  if (NextPlugin && (!Next || Next > NextPlugin)) {
247  Next = NextPlugin;
248  timer = NULL;
249  }
250  time_t Delta = Next ? Next - Now : 0;
251 
252  if (Next && Delta < Setup.MinEventTimeout * 60) {
253  if (!Force)
254  return false;
255  Delta = Setup.MinEventTimeout * 60;
256  Next = Now + Delta;
257  timer = NULL;
258  dsyslog("reboot at %s", *TimeToString(Next));
259  }
260 
261  if (Next && timer) {
262  dsyslog("next timer event at %s", *TimeToString(Next));
263  CallShutdownCommand(Next, timer->Channel()->Number(), timer->File(), Force);
264  }
265  else if (Next && Plugin) {
266  CallShutdownCommand(Next, 0, Plugin->Name(), Force);
267  dsyslog("next plugin wakeup at %s", *TimeToString(Next));
268  }
269  else
270  CallShutdownCommand(Next, 0, "", Force); // Next should always be 0 here. Just for safety, pass it.
271 
272  return true;
273 }