-
Notifications
You must be signed in to change notification settings - Fork 3
/
shutdown.c
293 lines (262 loc) · 10.8 KB
/
shutdown.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "shutdown.h"
#include "common.h"
#include "helper.h"
#include <vdr/cutter.h>
#include <vdr/menu.h>
#include <vdr/plugin.h>
#include <vdr/remote.h>
#include <vdr/shutdown.h>
#include <vdr/timers.h>
class cDBusShutdownHelper
{
public:
static const char *_xmlNodeInfo;
static void SendReply(GDBusMethodInvocation *Invocation, int ReplyCode, const char *ReplyMessage, int ShExitCode, const char *ShParameter)
{
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(isis)"));
g_variant_builder_add(builder, "i", ReplyCode);
g_variant_builder_add(builder, "s", ReplyMessage);
g_variant_builder_add(builder, "i", ShExitCode);
g_variant_builder_add(builder, "s", ShParameter);
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(builder);
};
static void IsUserActive(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
gboolean active = !ShutdownHandler.IsUserInactive();
g_dbus_method_invocation_return_value(Invocation, g_variant_new("(b)", active));
}
static void ConfirmShutdown(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
gboolean ignoreuser = FALSE;
g_variant_get(Parameters, "(b)", &ignoreuser);
// this is nearly a copy of vdr's cShutdownHandler::ConfirmShutdown
// if the original changes take this into account
if (!ignoreuser && !ShutdownHandler.IsUserInactive()) {
SendReply(Invocation, 901, "user is active", 0, "");
return;
}
#if APIVERSNUM > 20101
if (RecordingsHandler.Active()) {
#else
if (cCutter::Active()) {
#endif
SendReply(Invocation, 902, "cutter is active", 0, "");
return;
}
time_t Now = time(NULL);
#if VDRVERSNUM > 20300
LOCK_TIMERS_READ;
const cTimers *timers = Timers;
#else
cTimers *timers = &Timers;
#endif
const cTimer *timer = timers->GetNextActiveTimer();
time_t Next = timer ? timer->StartTime() : 0;
time_t Delta = timer ? Next - Now : 0;
if (cRecordControls::Active() || (Next && Delta <= 0)) {
// VPS recordings in timer end margin may cause Delta <= 0
SendReply(Invocation, 903, "recording is active", 0, "");
return;
}
else if (Next && Delta <= Setup.MinEventTimeout * 60) {
// Timer within Min Event Timeout
SendReply(Invocation, 904, "recording is active in the near future", 0, "");
return;
}
if (cPluginManager::Active(NULL)) {
SendReply(Invocation, 905, "some plugin is active", 0, "");
return;
}
cPlugin *Plugin = cPluginManager::GetNextWakeupPlugin();
time_t NextPlugin = Plugin ? Plugin->WakeupTime() : 0;
Delta = NextPlugin ? NextPlugin - Now : 0;
if (NextPlugin && Delta <= Setup.MinEventTimeout * 60) {
// Plugin wakeup within Min Event Timeout
cString buf = cString::sprintf("plugin %s wakes up in %ld min", Plugin->Name(), Delta / 60);
SendReply(Invocation, 906, *buf, 0, "");
return;
}
// insanity check: ask vdr again, if implementation of ConfirmShutdown has changed...
if (cRemote::Enabled() && !ShutdownHandler.ConfirmShutdown(false)) {
SendReply(Invocation, 550, "vdr is not ready for shutdown", 0, "");
return;
}
if (((*cDBusShutdown::_shutdownHooksDir) != NULL) && (strlen(*cDBusShutdown::_shutdownHooksDir) > 0)
&& ((*cDBusShutdown::_shutdownHooksWrapper) != NULL) && (strlen(*cDBusShutdown::_shutdownHooksWrapper) > 0)) {
if (NextPlugin && (!Next || Next > NextPlugin)) {
Next = NextPlugin;
timer = NULL;
}
Delta = Next ? Next - Now : 0;
char *tmp;
if (Next && timer)
tmp = strdup(*cString::sprintf("%ld %ld %d \"%s\" %d", Next, Delta, timer->Channel()->Number(), *strescape(timer->File(), "\\\"$"), false));
else if (Next && Plugin)
tmp = strdup(*cString::sprintf("%ld %ld %d \"%s\" %d", Next, Delta, 0, Plugin->Name(), false));
else
tmp = strdup(*cString::sprintf("%ld %ld %d \"%s\" %d", Next, Delta, 0, "", false));
cString params = strescape(tmp, "\\\"");
cString shutdowncmd;
cString cmd = cString::sprintf("%s %s \"%s\"", *cDBusShutdown::_shutdownHooksWrapper, *cDBusShutdown::_shutdownHooksDir, *params);
if (access(*cDBusShutdown::_shutdownHooksWrapper, X_OK) != 0)
cmd = cString::sprintf("/bin/sh %s %s \"%s\"", *cDBusShutdown::_shutdownHooksWrapper, *cDBusShutdown::_shutdownHooksDir, *params);
isyslog("dbus2vdr: calling shutdown-hook-wrapper %s", *cmd);
tmp = NULL;
cExitPipe p;
int ret = -1;
if (p.Open(*cmd, "r")) {
int l = 0;
int c;
while ((c = fgetc(p)) != EOF) {
if (l % 20 == 0) {
if (char *NewBuffer = (char *)realloc(tmp, l + 21))
tmp = NewBuffer;
else {
esyslog("dbus2vdr: out of memory");
break;
}
}
tmp[l++] = char(c);
}
if (tmp)
tmp[l] = 0;
ret = p.Close();
}
else
esyslog("dbus2vdr: can't open pipe for command '%s'", *cmd);
cString result(stripspace(tmp), true); // for automatic free
isyslog("dbus2vdr: result(%d) = %s", ret, *result);
if (ret != 0) {
if (*result) {
static const char *message = "ABORT_MESSAGE=\"";
if ((strlen(*result) > strlen(message)) && startswith(*result, message)) {
cString abort_message = tmp + strlen(message);
abort_message.Truncate(-1);
SendReply(Invocation, 992, *abort_message, ret, "");
return;
}
}
SendReply(Invocation, 999, "shutdown-hook returned a non-zero exit code", ret, "");
return;
}
if (*result) {
static const char *message = "TRY_AGAIN=\"";
if ((strlen(*result) > strlen(message)) && startswith(*result, message)) {
cString s_try_again = tmp + strlen(message);
s_try_again.Truncate(-1);
if ((strlen(*s_try_again) > 0) && isnumber(*s_try_again)) {
int try_again = strtol(s_try_again, NULL, 10);
if (try_again > 0) {
SendReply(Invocation, 991, *s_try_again, 0, "");
return;
}
}
}
}
if (*result) {
static const char *message = "SHUTDOWNCMD=\"";
if ((strlen(*result) > strlen(message)) && startswith(*result, message)) {
shutdowncmd = tmp + strlen(message);
shutdowncmd.Truncate(-1);
}
}
if (*shutdowncmd && (strlen(*shutdowncmd) > 0)) {
SendReply(Invocation, 990, *shutdowncmd, 0, *params);
return;
}
}
SendReply(Invocation, 250, "vdr is ready for shutdown", 0, "");
};
static void ManualStart(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
gboolean manual = FALSE;
time_t Delta = Setup.NextWakeupTime ? Setup.NextWakeupTime - cDBusShutdown::StartupTime : 0;
if (!Setup.NextWakeupTime || (abs(Delta) > 600)) // 600 comes from vdr's MANUALSTART constant in vdr.c
manual = TRUE;
g_dbus_method_invocation_return_value(Invocation, g_variant_new("(b)", manual));
};
static void NextWakeupTime(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
time_t nextWakeupTime = 0;
cString info = "";
time_t Now = time(NULL);
#if VDRVERSNUM > 20300
LOCK_TIMERS_READ;
const cTimers *timers = Timers;
#else
cTimers *timers = &Timers;
#endif
const cTimer *timer = timers->GetNextActiveTimer();
time_t Next = timer ? timer->StartTime() : 0;
cPlugin *Plugin = cPluginManager::GetNextWakeupPlugin();
time_t NextPlugin = Plugin ? Plugin->WakeupTime() : 0;
if (Next > Now) {
nextWakeupTime = Next;
info = "timer";
}
if (NextPlugin > Now) {
if (NextPlugin < nextWakeupTime) {
nextWakeupTime = NextPlugin;
info = cString::sprintf("plugin:%s", Plugin->Name());
}
}
g_dbus_method_invocation_return_value(Invocation, g_variant_new("(xs)", (gint64)nextWakeupTime, *info));
};
static void SetUserInactive(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
ShutdownHandler.SetUserInactive();
cDBusHelper::SendReply(Invocation, 250, "vdr is set to non-interactive mode");
};
};
const char *cDBusShutdownHelper::_xmlNodeInfo =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>\n"
" <interface name=\""DBUS_VDR_SHUTDOWN_INTERFACE"\">\n"
" <method name=\"IsUserActive\">\n"
" <arg name=\"active\" type=\"b\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"ConfirmShutdown\">\n"
" <arg name=\"ignoreuser\" type=\"b\" direction=\"in\"/>\n"
" <arg name=\"replycode\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"replymessage\" type=\"s\" direction=\"out\"/>\n"
" <arg name=\"shexitcode\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"shparameter\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"ManualStart\">\n"
" <arg name=\"manual\" type=\"b\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"NextWakeupTime\">\n"
" <arg name=\"timestamp\" type=\"x\" direction=\"out\"/>\n"
" <arg name=\"info\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"SetUserInactive\">\n"
" <arg name=\"replycode\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"replymessage\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" </interface>\n"
"</node>\n";
time_t cDBusShutdown::StartupTime;
cString cDBusShutdown::_shutdownHooksDir;
cString cDBusShutdown::_shutdownHooksWrapper;
void cDBusShutdown::SetShutdownHooksDir(const char *Dir)
{
_shutdownHooksDir = cString( Dir);
}
void cDBusShutdown::SetShutdownHooksWrapper(const char *Wrapper)
{
_shutdownHooksWrapper = cString( Wrapper);
}
cDBusShutdown::cDBusShutdown(void)
:cDBusObject("/Shutdown", cDBusShutdownHelper::_xmlNodeInfo)
{
AddMethod("IsUserActive", cDBusShutdownHelper::IsUserActive);
AddMethod("ConfirmShutdown" , cDBusShutdownHelper::ConfirmShutdown);
AddMethod("ManualStart" , cDBusShutdownHelper::ManualStart);
AddMethod("NextWakeupTime" , cDBusShutdownHelper::NextWakeupTime);
AddMethod("SetUserInactive" , cDBusShutdownHelper::SetUserInactive);
}
cDBusShutdown::~cDBusShutdown(void)
{
}