-
Notifications
You must be signed in to change notification settings - Fork 12
/
ScriptController.cpp
1182 lines (991 loc) · 30 KB
/
ScriptController.cpp
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of nzbget
*
* Copyright (C) 2007-2013 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $Revision$
* $Date$
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef WIN32
#include "win32.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef WIN32
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#endif
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include "nzbget.h"
#include "ScriptController.h"
#include "Log.h"
#include "Util.h"
// System global variable holding environments variables
extern char** environ;
extern Options* g_pOptions;
extern char* (*g_szEnvironmentVariables)[];
extern DownloadQueueHolder* g_pDownloadQueueHolder;
static const int POSTPROCESS_PARCHECK = 92;
static const int POSTPROCESS_SUCCESS = 93;
static const int POSTPROCESS_ERROR = 94;
static const int POSTPROCESS_NONE = 95;
#ifndef WIN32
#define CHILD_WATCHDOG 1
#endif
#ifdef CHILD_WATCHDOG
/**
* Sometimes the forked child process doesn't start properly and hangs
* just during the starting. I didn't find any explanation about what
* could cause that problem except of a general advice, that
* "a forking in a multithread application is not recommended".
*
* Workaround:
* 1) child process prints a line into stdout directly after the start;
* 2) parent process waits for a line for 60 seconds. If it didn't receive it
* the cild process assumed to hang and will be killed. Another attempt
* will be made.
*/
class ChildWatchDog : public Thread
{
private:
pid_t m_hProcessID;
protected:
virtual void Run();
public:
void SetProcessID(pid_t hProcessID) { m_hProcessID = hProcessID; }
};
void ChildWatchDog::Run()
{
static const int WAIT_SECONDS = 60;
time_t tStart = time(NULL);
while (!IsStopped() && (time(NULL) - tStart) < WAIT_SECONDS)
{
usleep(10 * 1000);
}
if (!IsStopped())
{
info("Restarting hanging child process");
kill(m_hProcessID, SIGKILL);
}
}
#endif
EnvironmentStrings::EnvironmentStrings()
{
}
EnvironmentStrings::~EnvironmentStrings()
{
Clear();
}
void EnvironmentStrings::Clear()
{
for (Strings::iterator it = m_strings.begin(); it != m_strings.end(); it++)
{
free(*it);
}
m_strings.clear();
}
void EnvironmentStrings::InitFromCurrentProcess()
{
for (int i = 0; (*g_szEnvironmentVariables)[i]; i++)
{
char* szVar = (*g_szEnvironmentVariables)[i];
Append(strdup(szVar));
}
}
void EnvironmentStrings::Append(char* szString)
{
m_strings.push_back(szString);
}
#ifdef WIN32
/*
* Returns environment block in format suitable for using with CreateProcess.
* The allocated memory must be freed by caller using "free()".
*/
char* EnvironmentStrings::GetStrings()
{
int iSize = 1;
for (Strings::iterator it = m_strings.begin(); it != m_strings.end(); it++)
{
char* szVar = *it;
iSize += strlen(szVar) + 1;
}
char* szStrings = (char*)malloc(iSize);
char* szPtr = szStrings;
for (Strings::iterator it = m_strings.begin(); it != m_strings.end(); it++)
{
char* szVar = *it;
strcpy(szPtr, szVar);
szPtr += strlen(szVar) + 1;
}
*szPtr = '\0';
return szStrings;
}
#else
/*
* Returns environment block in format suitable for using with execve
* The allocated memory must be freed by caller using "free()".
*/
char** EnvironmentStrings::GetStrings()
{
char** pStrings = (char**)malloc((m_strings.size() + 1) * sizeof(char*));
char** pPtr = pStrings;
for (Strings::iterator it = m_strings.begin(); it != m_strings.end(); it++)
{
char* szVar = *it;
*pPtr = szVar;
pPtr++;
}
*pPtr = NULL;
return pStrings;
}
#endif
ScriptController::ScriptController()
{
m_szScript = NULL;
m_szWorkingDir = NULL;
m_szArgs = NULL;
m_bFreeArgs = false;
m_szInfoName = NULL;
m_szLogPrefix = NULL;
m_bTerminated = false;
m_environmentStrings.InitFromCurrentProcess();
}
ScriptController::~ScriptController()
{
if (m_bFreeArgs)
{
for (const char** szArgPtr = m_szArgs; *szArgPtr; szArgPtr++)
{
free((char*)*szArgPtr);
}
free(m_szArgs);
}
}
void ScriptController::ResetEnv()
{
m_environmentStrings.Clear();
m_environmentStrings.InitFromCurrentProcess();
}
void ScriptController::SetEnvVar(const char* szName, const char* szValue)
{
int iLen = strlen(szName) + strlen(szValue) + 2;
char* szVar = (char*)malloc(iLen);
snprintf(szVar, iLen, "%s=%s", szName, szValue);
m_environmentStrings.Append(szVar);
}
/**
* If szStripPrefix is not NULL, only options, whose names start with the prefix
* are processed. The prefix is then stripped from the names.
* If szStripPrefix is NULL, all options are processed; without stripping.
*/
void ScriptController::PrepareEnvOptions(const char* szStripPrefix)
{
int iPrefixLen = szStripPrefix ? strlen(szStripPrefix) : 0;
Options::OptEntries* pOptEntries = g_pOptions->LockOptEntries();
for (Options::OptEntries::iterator it = pOptEntries->begin(); it != pOptEntries->end(); it++)
{
Options::OptEntry* pOptEntry = *it;
if (szStripPrefix && !strncmp(pOptEntry->GetName(), szStripPrefix, iPrefixLen) && (int)strlen(pOptEntry->GetName()) > iPrefixLen)
{
SetEnvVarSpecial("NZBPO", pOptEntry->GetName() + iPrefixLen, pOptEntry->GetValue());
}
else if (!szStripPrefix)
{
SetEnvVarSpecial("NZBOP", pOptEntry->GetName(), pOptEntry->GetValue());
}
}
g_pOptions->UnlockOptEntries();
}
/**
* If szStripPrefix is not NULL, only pp-parameters, whose names start with the prefix
* are processed. The prefix is then stripped from the names.
* If szStripPrefix is NULL, all pp-parameters are processed; without stripping.
*/
void ScriptController::PrepareEnvParameters(NZBInfo* pNZBInfo, const char* szStripPrefix)
{
int iPrefixLen = szStripPrefix ? strlen(szStripPrefix) : 0;
for (NZBParameterList::iterator it = pNZBInfo->GetParameters()->begin(); it != pNZBInfo->GetParameters()->end(); it++)
{
NZBParameter* pParameter = *it;
if (szStripPrefix && !strncmp(pParameter->GetName(), szStripPrefix, iPrefixLen) && (int)strlen(pParameter->GetName()) > iPrefixLen)
{
SetEnvVarSpecial("NZBPR", pParameter->GetName() + iPrefixLen, pParameter->GetValue());
}
else if (!szStripPrefix)
{
SetEnvVarSpecial("NZBPR", pParameter->GetName(), pParameter->GetValue());
}
}
}
void ScriptController::SetEnvVarSpecial(const char* szPrefix, const char* szName, const char* szValue)
{
char szVarname[1024];
snprintf(szVarname, sizeof(szVarname), "%s_%s", szPrefix, szName);
szVarname[1024-1] = '\0';
// Original name
SetEnvVar(szVarname, szValue);
char szNormVarname[1024];
strncpy(szNormVarname, szVarname, sizeof(szVarname));
szNormVarname[1024-1] = '\0';
// Replace special characters with "_" and convert to upper case
for (char* szPtr = szNormVarname; *szPtr; szPtr++)
{
if (strchr(".:*!\"$%&/()=`+~#'{}[]@- ", *szPtr)) *szPtr = '_';
*szPtr = toupper(*szPtr);
}
// Another env var with normalized name (replaced special chars and converted to upper case)
if (strcmp(szVarname, szNormVarname))
{
SetEnvVar(szNormVarname, szValue);
}
}
void ScriptController::PrepareArgs()
{
#ifdef WIN32
if (!m_szArgs)
{
// Special support for script languages:
// automatically find the app registered for this extension and run it
const char* szExtension = strrchr(GetScript(), '.');
if (szExtension && strcasecmp(szExtension, ".exe") && strcasecmp(szExtension, ".bat") && strcasecmp(szExtension, ".cmd"))
{
debug("Looking for associated program for %s", szExtension);
char szCommand[512];
int iBufLen = 512-1;
if (Util::RegReadStr(HKEY_CLASSES_ROOT, szExtension, NULL, szCommand, &iBufLen))
{
szCommand[iBufLen] = '\0';
debug("Extension: %s", szCommand);
char szRegPath[512];
snprintf(szRegPath, 512, "%s\\shell\\open\\command", szCommand);
szRegPath[512-1] = '\0';
iBufLen = 512-1;
if (Util::RegReadStr(HKEY_CLASSES_ROOT, szRegPath, NULL, szCommand, &iBufLen))
{
szCommand[iBufLen] = '\0';
debug("Command: %s", szCommand);
DWORD_PTR pArgs[] = { (DWORD_PTR)GetScript(), (DWORD_PTR)0 };
if (FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, szCommand, 0, 0,
m_szCmdLine, sizeof(m_szCmdLine), (va_list*)pArgs))
{
debug("CmdLine: %s", m_szCmdLine);
return;
}
}
}
warn("Could not found associated program for %s. Trying to execute %s directly", szExtension, Util::BaseFileName(GetScript()));
}
}
#endif
if (!m_szArgs)
{
m_szStdArgs[0] = GetScript();
m_szStdArgs[1] = NULL;
SetArgs(m_szStdArgs, false);
}
}
int ScriptController::Execute()
{
PrepareEnvOptions(NULL);
PrepareArgs();
int iExitCode = 0;
int pipein;
#ifdef CHILD_WATCHDOG
bool bChildConfirmed = false;
while (!bChildConfirmed && !m_bTerminated)
{
#endif
#ifdef WIN32
// build command line
char* szCmdLine = NULL;
if (m_szArgs)
{
char szCmdLineBuf[2048];
int iUsedLen = 0;
for (const char** szArgPtr = m_szArgs; *szArgPtr; szArgPtr++)
{
snprintf(szCmdLineBuf + iUsedLen, 2048 - iUsedLen, "\"%s\" ", *szArgPtr);
iUsedLen += strlen(*szArgPtr) + 3;
}
szCmdLineBuf[iUsedLen < 2048 ? iUsedLen - 1 : 2048 - 1] = '\0';
szCmdLine = szCmdLineBuf;
}
else
{
szCmdLine = m_szCmdLine;
}
// create pipes to write and read data
HANDLE hReadPipe, hWritePipe;
SECURITY_ATTRIBUTES SecurityAttributes;
memset(&SecurityAttributes, 0, sizeof(SecurityAttributes));
SecurityAttributes.nLength = sizeof(SecurityAttributes);
SecurityAttributes.bInheritHandle = TRUE;
CreatePipe(&hReadPipe, &hWritePipe, &SecurityAttributes, 0);
STARTUPINFO StartupInfo;
memset(&StartupInfo, 0, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
StartupInfo.dwFlags = STARTF_USESTDHANDLES;
StartupInfo.hStdInput = 0;
StartupInfo.hStdOutput = hWritePipe;
StartupInfo.hStdError = hWritePipe;
PROCESS_INFORMATION ProcessInfo;
memset(&ProcessInfo, 0, sizeof(ProcessInfo));
char* szEnvironmentStrings = m_environmentStrings.GetStrings();
BOOL bOK = CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, szEnvironmentStrings, m_szWorkingDir, &StartupInfo, &ProcessInfo);
if (!bOK)
{
DWORD dwErrCode = GetLastError();
char szErrMsg[255];
szErrMsg[255-1] = '\0';
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwErrCode, 0, szErrMsg, 255, NULL))
{
error("Could not start %s: %s", m_szInfoName, szErrMsg);
}
else
{
error("Could not start %s: error %i", m_szInfoName, dwErrCode);
}
if (!Util::FileExists(m_szScript))
{
error("Could not find file %s", m_szScript);
}
free(szEnvironmentStrings);
return -1;
}
free(szEnvironmentStrings);
debug("Child Process-ID: %i", (int)ProcessInfo.dwProcessId);
m_hProcess = ProcessInfo.hProcess;
// close unused "write" end
CloseHandle(hWritePipe);
pipein = _open_osfhandle((intptr_t)hReadPipe, _O_RDONLY);
#else
int p[2];
int pipeout;
// create the pipe
if (pipe(p))
{
error("Could not open pipe: errno %i", errno);
return -1;
}
char** pEnvironmentStrings = m_environmentStrings.GetStrings();
pipein = p[0];
pipeout = p[1];
debug("forking");
pid_t pid = fork();
if (pid == -1)
{
error("Could not start %s: errno %i", m_szInfoName, errno);
free(pEnvironmentStrings);
return -1;
}
else if (pid == 0)
{
// here goes the second instance
// create new process group (see Terminate() where it is used)
setsid();
// close up the "read" end
close(pipein);
// make the pipeout to be the same as stdout and stderr
dup2(pipeout, 1);
dup2(pipeout, 2);
close(pipeout);
#ifdef CHILD_WATCHDOG
fwrite("\n", 1, 1, stdout);
fflush(stdout);
#endif
chdir(m_szWorkingDir);
environ = pEnvironmentStrings;
execvp(m_szScript, (char* const*)m_szArgs);
// NOTE: the text "[ERROR] Could not start " is checked later,
// by changing adjust the dependent code below.
fprintf(stdout, "[ERROR] Could not start %s: %s", m_szScript, strerror(errno));
fflush(stdout);
_exit(254);
}
// continue the first instance
debug("forked");
debug("Child Process-ID: %i", (int)pid);
free(pEnvironmentStrings);
m_hProcess = pid;
// close unused "write" end
close(pipeout);
#endif
// open the read end
FILE* readpipe = fdopen(pipein, "r");
if (!readpipe)
{
error("Could not open pipe to %s", m_szInfoName);
return -1;
}
#ifdef CHILD_WATCHDOG
debug("Creating child watchdog");
ChildWatchDog* pWatchDog = new ChildWatchDog();
pWatchDog->SetAutoDestroy(false);
pWatchDog->SetProcessID(pid);
pWatchDog->Start();
#endif
char* buf = (char*)malloc(10240);
debug("Entering pipe-loop");
bool bFirstLine = true;
bool bStartError = false;
while (!feof(readpipe) && !m_bTerminated)
{
if (ReadLine(buf, 10240, readpipe))
{
#ifdef CHILD_WATCHDOG
if (!bChildConfirmed)
{
bChildConfirmed = true;
pWatchDog->Stop();
debug("Child confirmed");
continue;
}
#endif
if (bFirstLine && !strncmp(buf, "[ERROR] Could not start ", 24))
{
bStartError = true;
}
ProcessOutput(buf);
bFirstLine = false;
}
}
debug("Exited pipe-loop");
#ifdef CHILD_WATCHDOG
debug("Destroying WatchDog");
if (!bChildConfirmed)
{
pWatchDog->Stop();
}
while (pWatchDog->IsRunning())
{
usleep(1 * 1000);
}
delete pWatchDog;
#endif
free(buf);
fclose(readpipe);
if (m_bTerminated)
{
warn("Interrupted %s", m_szInfoName);
}
iExitCode = 0;
#ifdef WIN32
WaitForSingleObject(m_hProcess, INFINITE);
DWORD dExitCode = 0;
GetExitCodeProcess(m_hProcess, &dExitCode);
iExitCode = dExitCode;
#else
int iStatus = 0;
waitpid(m_hProcess, &iStatus, 0);
if (WIFEXITED(iStatus))
{
iExitCode = WEXITSTATUS(iStatus);
if (iExitCode == 254 && bStartError)
{
iExitCode = -1;
}
}
#endif
#ifdef CHILD_WATCHDOG
} // while (!bChildConfirmed && !m_bTerminated)
#endif
debug("Exit code %i", iExitCode);
return iExitCode;
}
void ScriptController::Terminate()
{
debug("Stopping %s", m_szInfoName);
m_bTerminated = true;
#ifdef WIN32
BOOL bOK = TerminateProcess(m_hProcess, -1);
#else
pid_t hKillProcess = m_hProcess;
if (getpgid(hKillProcess) == hKillProcess)
{
// if the child process has its own group (setsid() was successful), kill the whole group
hKillProcess = -hKillProcess;
}
bool bOK = kill(hKillProcess, SIGKILL) == 0;
#endif
if (bOK)
{
debug("Terminated %s", m_szInfoName);
}
else
{
error("Could not terminate %s", m_szInfoName);
}
debug("Stopped %s", m_szInfoName);
}
bool ScriptController::ReadLine(char* szBuf, int iBufSize, FILE* pStream)
{
return fgets(szBuf, iBufSize, pStream);
}
void ScriptController::ProcessOutput(char* szText)
{
debug("Processing output received from script");
for (char* pend = szText + strlen(szText) - 1; pend >= szText && (*pend == '\n' || *pend == '\r' || *pend == ' '); pend--) *pend = '\0';
if (szText[0] == '\0')
{
// skip empty lines
return;
}
if (!strncmp(szText, "[INFO] ", 7))
{
PrintMessage(Message::mkInfo, "%s", szText + 7);
}
else if (!strncmp(szText, "[WARNING] ", 10))
{
PrintMessage(Message::mkWarning, "%s", szText + 10);
}
else if (!strncmp(szText, "[ERROR] ", 8))
{
PrintMessage(Message::mkError, "%s", szText + 8);
}
else if (!strncmp(szText, "[DETAIL] ", 9))
{
PrintMessage(Message::mkDetail, "%s", szText + 9);
}
else if (!strncmp(szText, "[DEBUG] ", 8))
{
PrintMessage(Message::mkDebug, "%s", szText + 8);
}
else
{
PrintMessage(Message::mkInfo, "%s", szText);
}
debug("Processing output received from script - completed");
}
void ScriptController::AddMessage(Message::EKind eKind, const char* szText)
{
switch (eKind)
{
case Message::mkDetail:
detail("%s", szText);
break;
case Message::mkInfo:
info("%s", szText);
break;
case Message::mkWarning:
warn("%s", szText);
break;
case Message::mkError:
error("%s", szText);
break;
case Message::mkDebug:
debug("%s", szText);
break;
}
}
void ScriptController::PrintMessage(Message::EKind eKind, const char* szFormat, ...)
{
char tmp2[1024];
va_list ap;
va_start(ap, szFormat);
vsnprintf(tmp2, 1024, szFormat, ap);
tmp2[1024-1] = '\0';
va_end(ap);
char tmp3[1024];
if (m_szLogPrefix)
{
snprintf(tmp3, 1024, "%s: %s", m_szLogPrefix, tmp2);
}
else
{
strncpy(tmp3, tmp2, 1024);
}
tmp3[1024-1] = '\0';
AddMessage(eKind, tmp3);
}
void PostScriptController::StartJob(PostInfo* pPostInfo)
{
PostScriptController* pScriptController = new PostScriptController();
pScriptController->m_pPostInfo = pPostInfo;
pScriptController->SetWorkingDir(g_pOptions->GetDestDir());
pScriptController->SetAutoDestroy(false);
pPostInfo->SetPostThread(pScriptController);
pScriptController->Start();
}
void PostScriptController::Run()
{
FileList activeList;
// the locking is needed for accessing the members of NZBInfo
g_pDownloadQueueHolder->LockQueue();
for (NZBParameterList::iterator it = m_pPostInfo->GetNZBInfo()->GetParameters()->begin(); it != m_pPostInfo->GetNZBInfo()->GetParameters()->end(); it++)
{
NZBParameter* pParameter = *it;
const char* szVarname = pParameter->GetName();
if (strlen(szVarname) > 0 && szVarname[0] != '*' && szVarname[strlen(szVarname)-1] == ':' &&
(!strcasecmp(pParameter->GetValue(), "yes") || !strcasecmp(pParameter->GetValue(), "on") || !strcasecmp(pParameter->GetValue(), "1")))
{
char* szScriptName = strdup(szVarname);
szScriptName[strlen(szScriptName)-1] = '\0'; // remove trailing ':'
activeList.push_back(szScriptName);
}
}
m_pPostInfo->GetNZBInfo()->GetScriptStatuses()->Clear();
g_pDownloadQueueHolder->UnlockQueue();
Options::ScriptList scriptList;
g_pOptions->LoadScriptList(&scriptList);
for (Options::ScriptList::iterator it = scriptList.begin(); it != scriptList.end(); it++)
{
Options::Script* pScript = *it;
for (FileList::iterator it2 = activeList.begin(); it2 != activeList.end(); it2++)
{
char* szActiveName = *it2;
// if any script has requested par-check, do not execute other scripts
if (Util::SameFilename(pScript->GetName(), szActiveName) && !m_pPostInfo->GetRequestParCheck())
{
ExecuteScript(pScript->GetName(), pScript->GetDisplayName(), pScript->GetLocation());
}
}
}
for (FileList::iterator it = activeList.begin(); it != activeList.end(); it++)
{
free(*it);
}
m_pPostInfo->SetStage(PostInfo::ptFinished);
m_pPostInfo->SetWorking(false);
}
void PostScriptController::ExecuteScript(const char* szScriptName, const char* szDisplayName, const char* szLocation)
{
PrintMessage(Message::mkInfo, "Executing post-process-script %s for %s", szScriptName, m_pPostInfo->GetInfoName());
SetScript(szLocation);
SetArgs(NULL, false);
char szInfoName[1024];
snprintf(szInfoName, 1024, "post-process-script %s for %s", szScriptName, m_pPostInfo->GetInfoName());
szInfoName[1024-1] = '\0';
SetInfoName(szInfoName);
SetLogPrefix(szDisplayName);
PrepareParams(szScriptName);
int iExitCode = Execute();
szInfoName[0] = 'P'; // uppercase
SetLogPrefix(NULL);
ScriptStatus::EStatus eStatus = AnalyseExitCode(iExitCode);
// the locking is needed for accessing the members of NZBInfo
g_pDownloadQueueHolder->LockQueue();
m_pPostInfo->GetNZBInfo()->GetScriptStatuses()->Add(szScriptName, eStatus);
g_pDownloadQueueHolder->UnlockQueue();
}
void PostScriptController::PrepareParams(const char* szScriptName)
{
// the locking is needed for accessing the members of NZBInfo
g_pDownloadQueueHolder->LockQueue();
char szNZBName[1024];
strncpy(szNZBName, m_pPostInfo->GetNZBInfo()->GetName(), 1024);
szNZBName[1024-1] = '\0';
int iParStatus[] = { 0, 0, 1, 2, 3, 4 };
char szParStatus[10];
snprintf(szParStatus, 10, "%i", iParStatus[m_pPostInfo->GetNZBInfo()->GetParStatus()]);
szParStatus[10-1] = '\0';
int iUnpackStatus[] = { 0, 0, 1, 2 };
char szUnpackStatus[10];
snprintf(szUnpackStatus, 10, "%i", iUnpackStatus[m_pPostInfo->GetNZBInfo()->GetUnpackStatus()]);
szUnpackStatus[10-1] = '\0';
char szDestDir[1024];
strncpy(szDestDir, m_pPostInfo->GetNZBInfo()->GetDestDir(), 1024);
szDestDir[1024-1] = '\0';
char szNZBID[10];
snprintf(szNZBID, 10, "%i", m_pPostInfo->GetNZBInfo()->GetID());
szNZBID[10-1] = '\0';
char szNZBFilename[1024];
strncpy(szNZBFilename, m_pPostInfo->GetNZBInfo()->GetFilename(), 1024);
szNZBFilename[1024-1] = '\0';
char szCategory[1024];
strncpy(szCategory, m_pPostInfo->GetNZBInfo()->GetCategory(), 1024);
szCategory[1024-1] = '\0';
// Reset
ResetEnv();
SetEnvVar("NZBPP_NZBNAME", szNZBName);
SetEnvVar("NZBPP_NZBID", szNZBID);
SetEnvVar("NZBPP_DIRECTORY", szDestDir);
SetEnvVar("NZBPP_NZBFILENAME", szNZBFilename);
SetEnvVar("NZBPP_PARSTATUS", szParStatus);
SetEnvVar("NZBPP_UNPACKSTATUS", szUnpackStatus);
SetEnvVar("NZBPP_CATEGORY", szCategory);
PrepareEnvParameters(m_pPostInfo->GetNZBInfo(), NULL);
char szParamPrefix[1024];
snprintf(szParamPrefix, 1024, "%s:", szScriptName);
szParamPrefix[1024-1] = '\0';
PrepareEnvParameters(m_pPostInfo->GetNZBInfo(), szParamPrefix);
PrepareEnvOptions(szParamPrefix);
g_pDownloadQueueHolder->UnlockQueue();
}
ScriptStatus::EStatus PostScriptController::AnalyseExitCode(int iExitCode)
{
// The ScriptStatus is accumulated for all scripts:
// If any script has failed the status is "failure", etc.
switch (iExitCode)
{
case POSTPROCESS_SUCCESS:
PrintMessage(Message::mkInfo, "%s successful", GetInfoName());
return ScriptStatus::srSuccess;
case POSTPROCESS_ERROR:
case -1: // Execute() returns -1 if the process could not be started (file not found or other problem)
PrintMessage(Message::mkError, "%s failed", GetInfoName());
return ScriptStatus::srFailure;
case POSTPROCESS_NONE:
PrintMessage(Message::mkInfo, "%s skipped", GetInfoName());
return ScriptStatus::srNone;
#ifndef DISABLE_PARCHECK
case POSTPROCESS_PARCHECK:
if (m_pPostInfo->GetNZBInfo()->GetParStatus() > NZBInfo::psSkipped)
{
PrintMessage(Message::mkError, "%s requested par-check/repair, but the collection was already checked", GetInfoName());
return ScriptStatus::srFailure;
}
else
{
PrintMessage(Message::mkInfo, "%s requested par-check/repair", GetInfoName());
m_pPostInfo->SetRequestParCheck(true);
return ScriptStatus::srSuccess;
}
break;
#endif
default:
PrintMessage(Message::mkError, "%s failed (terminated with unknown status)", GetInfoName());
return ScriptStatus::srFailure;
}
}
void PostScriptController::AddMessage(Message::EKind eKind, const char* szText)
{
if (!strncmp(szText, "[HISTORY] ", 10))
{
m_pPostInfo->GetNZBInfo()->AppendMessage(eKind, 0, szText);
}
else
{
ScriptController::AddMessage(eKind, szText);
m_pPostInfo->AppendMessage(eKind, szText);
}
if (g_pOptions->GetPausePostProcess())
{
time_t tStageTime = m_pPostInfo->GetStageTime();
time_t tStartTime = m_pPostInfo->GetStartTime();
time_t tWaitTime = time(NULL);
// wait until Post-processor is unpaused
while (g_pOptions->GetPausePostProcess() && !IsStopped())
{
usleep(100 * 1000);
// update time stamps
time_t tDelta = time(NULL) - tWaitTime;
if (tStageTime > 0)
{
m_pPostInfo->SetStageTime(tStageTime + tDelta);
}
if (tStartTime > 0)
{
m_pPostInfo->SetStartTime(tStartTime + tDelta);
}
}
}
}
void PostScriptController::Stop()
{
debug("Stopping post-process-script");
Thread::Stop();
Terminate();
}
void NZBScriptController::ExecuteScript(const char* szScript, const char* szNZBFilename, const char* szDirectory,
char** pNZBName, char** pCategory, int* iPriority, NZBParameterList* pParameters, bool* bAddTop, bool* bAddPaused)
{
info("Executing nzb-process-script for %s", Util::BaseFileName(szNZBFilename));
NZBScriptController* pScriptController = new NZBScriptController();
pScriptController->SetScript(szScript);
pScriptController->m_pNZBName = pNZBName;
pScriptController->m_pCategory = pCategory;
pScriptController->m_pParameters = pParameters;
pScriptController->m_iPriority = iPriority;
pScriptController->m_bAddTop = bAddTop;
pScriptController->m_bAddPaused = bAddPaused;
char szInfoName[1024];
snprintf(szInfoName, 1024, "nzb-process-script for %s", Util::BaseFileName(szNZBFilename));
szInfoName[1024-1] = '\0';
pScriptController->SetInfoName(szInfoName);
// remove trailing slash
char szDir[1024];
strncpy(szDir, szDirectory, 1024);
szDir[1024-1] = '\0';
int iLen = strlen(szDir);
if (szDir[iLen-1] == PATH_SEPARATOR)
{
szDir[iLen-1] = '\0';
}
char szPriority[20];