~ubuntu-branches/debian/experimental/nzbget/experimental

« back to all changes in this revision

Viewing changes to .pc/fix_fsf_address.patch/ScriptController.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Moog
  • Date: 2013-07-18 14:50:28 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130718145028-qhxse81w1sj5w424
Tags: 11.0+dfsg-1
* New upstream release (Closes: #701896)
* Repackage original tarball to remove copies of jquery and twitter-
  bootstrap
* debian/watch: Update for new versioning scheme
* debian/patches: Remove all old patches, add one patch:
  - dont-embed-libraries.patch: Don't install embedded jquery and bootstrap 
    libraries
* debian/combat: Upgrade to debhelper combat 9
* debian/control:
  - Fix Vcs-Git field
  - Adjust debhelper version for combat level 9
  - Add jquery and bootstrap to depends for integrated webserver
  - Add python to recommends for post-processing scripts
  - Bump build-depends on libpar2-dev to support the cancel function
* debian/links:
  - Use the system jquery and bootstrap libraries
* debian/rules:
  - Add get-orig-source target to build modified upstream tarball
* Adjust sample nzbget.conf:
  - Only listen to 127.0.0.1 instead of 0.0.0.0
  - Use nzbget.conf as template for webui configuration
* Adjust sample nzbgetd init file:
  - Point to correct location of nzbget binary

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  This file is part of nzbget
3
 
 *
4
 
 *  Copyright (C) 2007-2010 Andrei Prygounkov <hugbug@users.sourceforge.net>
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 
 *
20
 
 * $Revision: 381 $
21
 
 * $Date: 2010-02-08 17:08:10 +0100 (Mon, 08 Feb 2010) $
22
 
 *
23
 
 */
24
 
 
25
 
 
26
 
#ifdef HAVE_CONFIG_H
27
 
#include <config.h>
28
 
#endif
29
 
 
30
 
#ifdef WIN32
31
 
#include "win32.h"
32
 
#endif
33
 
 
34
 
#include <stdlib.h>
35
 
#include <string.h>
36
 
#include <ctype.h>
37
 
#ifndef WIN32
38
 
#include <unistd.h>
39
 
#include <sys/wait.h>
40
 
#include <signal.h>
41
 
#endif
42
 
#include <sys/stat.h>
43
 
#include <errno.h>
44
 
#include <fcntl.h>
45
 
#include <stdio.h>
46
 
 
47
 
#include "nzbget.h"
48
 
#include "ScriptController.h"
49
 
#include "Log.h"
50
 
#include "Util.h"
51
 
 
52
 
extern Options* g_pOptions;
53
 
extern char* (*szEnvironmentVariables)[];
54
 
extern DownloadQueueHolder* g_pDownloadQueueHolder;
55
 
 
56
 
static const int POSTPROCESS_PARCHECK_CURRENT = 91;
57
 
static const int POSTPROCESS_PARCHECK_ALL = 92;
58
 
static const int POSTPROCESS_SUCCESS = 93;
59
 
static const int POSTPROCESS_ERROR = 94;
60
 
static const int POSTPROCESS_NONE = 95;
61
 
 
62
 
#ifndef WIN32
63
 
#define CHILD_WATCHDOG 1
64
 
#endif
65
 
 
66
 
#ifdef CHILD_WATCHDOG
67
 
/**
68
 
 * Sometimes the forked child process doesn't start properly and hangs
69
 
 * just during the starting. I didn't find any explanation about what
70
 
 * could cause that problem except of a general advice, that
71
 
 * "a forking in a multithread application is not recommended".
72
 
 *
73
 
 * Workaround:
74
 
 * 1) child process prints a line into stdout directly after the start;
75
 
 * 2) parent process waits for a line for 60 seconds. If it didn't receive it
76
 
 *    the cild process assumed to hang and will be killed. Another attempt
77
 
 *    will be made.
78
 
 */
79
 
 
80
 
class ChildWatchDog : public Thread
81
 
{
82
 
private:
83
 
        pid_t                   m_hProcessID;
84
 
protected:
85
 
        virtual void    Run();
86
 
public:
87
 
        void                    SetProcessID(pid_t hProcessID) { m_hProcessID = hProcessID; }
88
 
};
89
 
 
90
 
void ChildWatchDog::Run()
91
 
{
92
 
        static const int WAIT_SECONDS = 60;
93
 
        time_t tStart = time(NULL);
94
 
        while (!IsStopped() && (time(NULL) - tStart) < WAIT_SECONDS)
95
 
        {
96
 
                usleep(10 * 1000);
97
 
        }
98
 
 
99
 
        if (!IsStopped())
100
 
        {
101
 
                info("Restarting hanging child process");
102
 
                kill(m_hProcessID, SIGKILL);
103
 
        }
104
 
}
105
 
#endif
106
 
 
107
 
 
108
 
EnvironmentStrings::EnvironmentStrings()
109
 
{
110
 
}
111
 
 
112
 
EnvironmentStrings::~EnvironmentStrings()
113
 
{
114
 
        for (Strings::iterator it = m_strings.begin(); it != m_strings.end(); it++)
115
 
        {
116
 
                free(*it);
117
 
        }
118
 
        m_strings.clear();
119
 
}
120
 
 
121
 
void EnvironmentStrings::InitFromCurrentProcess()
122
 
{
123
 
        for (int i = 0; (*szEnvironmentVariables)[i]; i++)
124
 
        {
125
 
                char* szVar = (*szEnvironmentVariables)[i];
126
 
                Append(strdup(szVar));
127
 
        }
128
 
}
129
 
 
130
 
void EnvironmentStrings::Append(char* szString)
131
 
{
132
 
        m_strings.push_back(szString);
133
 
}
134
 
 
135
 
#ifdef WIN32
136
 
/*
137
 
 * Returns environment block in format suitable for using with CreateProcess. 
138
 
 * The allocated memory must be freed by caller using "free()".
139
 
 */
140
 
char* EnvironmentStrings::GetStrings()
141
 
{
142
 
        int iSize = 1;
143
 
        for (Strings::iterator it = m_strings.begin(); it != m_strings.end(); it++)
144
 
        {
145
 
                char* szVar = *it;
146
 
                iSize += strlen(szVar) + 1;
147
 
        }
148
 
 
149
 
        char* szStrings = (char*)malloc(iSize);
150
 
        char* szPtr = szStrings;
151
 
        for (Strings::iterator it = m_strings.begin(); it != m_strings.end(); it++)
152
 
        {
153
 
                char* szVar = *it;
154
 
                strcpy(szPtr, szVar);
155
 
                szPtr += strlen(szVar) + 1;
156
 
        }
157
 
        *szPtr = '\0';
158
 
 
159
 
        return szStrings;
160
 
}
161
 
 
162
 
#else
163
 
 
164
 
/*
165
 
 * Returns environment block in format suitable for using with execve 
166
 
 * The allocated memory must be freed by caller using "free()".
167
 
 */
168
 
char** EnvironmentStrings::GetStrings()
169
 
{
170
 
        char** pStrings = (char**)malloc((m_strings.size() + 1) * sizeof(char*));
171
 
        char** pPtr = pStrings;
172
 
        for (Strings::iterator it = m_strings.begin(); it != m_strings.end(); it++)
173
 
        {
174
 
                char* szVar = *it;
175
 
                *pPtr = szVar;
176
 
                pPtr++;
177
 
        }
178
 
        *pPtr = NULL;
179
 
 
180
 
        return pStrings;
181
 
}
182
 
#endif
183
 
 
184
 
 
185
 
ScriptController::ScriptController()
186
 
{
187
 
        m_szScript = NULL;
188
 
        m_szWorkingDir = NULL;
189
 
        m_szArgs = NULL;
190
 
        m_bFreeArgs = false;
191
 
        m_szInfoName = NULL;
192
 
        m_szDefaultKindPrefix = NULL;
193
 
        m_bTerminated = false;
194
 
        m_environmentStrings.InitFromCurrentProcess();
195
 
}
196
 
 
197
 
ScriptController::~ScriptController()
198
 
{
199
 
        if (m_bFreeArgs)
200
 
        {
201
 
                for (const char** szArgPtr = m_szArgs; *szArgPtr; szArgPtr++)
202
 
                {
203
 
                        free((char*)*szArgPtr);
204
 
                }
205
 
                free(m_szArgs);
206
 
        }
207
 
}
208
 
 
209
 
void ScriptController::SetEnvVar(const char* szName, const char* szValue)
210
 
{
211
 
        int iLen = strlen(szName) + strlen(szValue) + 2;
212
 
        char* szVar = (char*)malloc(iLen);
213
 
        snprintf(szVar, iLen, "%s=%s", szName, szValue);
214
 
        m_environmentStrings.Append(szVar);
215
 
}
216
 
 
217
 
void ScriptController::PrepareEnvironmentStrings()
218
 
{
219
 
        Options::OptEntries* pOptEntries = g_pOptions->LockOptEntries();
220
 
 
221
 
        for (Options::OptEntries::iterator it = pOptEntries->begin(); it != pOptEntries->end(); it++)
222
 
        {
223
 
                Options::OptEntry* pOptEntry = *it;
224
 
                char szVarname[1024];
225
 
                snprintf(szVarname, sizeof(szVarname), "NZBOP_%s", pOptEntry->GetName());
226
 
 
227
 
                // convert to upper case; replace "." with "_".
228
 
                for (char* szPtr = szVarname; *szPtr; szPtr++)
229
 
                {
230
 
                        if (*szPtr == '.')
231
 
                        {
232
 
                                *szPtr = '_';
233
 
                        }
234
 
                        *szPtr = toupper(*szPtr);
235
 
                }
236
 
 
237
 
                szVarname[1024-1] = '\0';
238
 
                SetEnvVar(szVarname, pOptEntry->GetValue());
239
 
        }
240
 
 
241
 
        g_pOptions->UnlockOptEntries();
242
 
}
243
 
 
244
 
int ScriptController::Execute()
245
 
{
246
 
        if (!Util::FileExists(m_szScript))
247
 
        {
248
 
                error("Could not start %s: could not find file %s", m_szInfoName, m_szScript);
249
 
                return -1;
250
 
        }
251
 
 
252
 
        PrepareEnvironmentStrings();
253
 
 
254
 
        int iExitCode = 0;
255
 
        int pipein;
256
 
 
257
 
#ifdef CHILD_WATCHDOG
258
 
        bool bChildConfirmed = false;
259
 
        while (!bChildConfirmed && !m_bTerminated)
260
 
        {
261
 
#endif
262
 
 
263
 
#ifdef WIN32
264
 
        // build command line
265
 
        char szCmdLine[2048];
266
 
        int iUsedLen = 0;
267
 
        for (const char** szArgPtr = m_szArgs; *szArgPtr; szArgPtr++)
268
 
        {
269
 
                snprintf(szCmdLine + iUsedLen, 2048 - iUsedLen, "\"%s\" ", *szArgPtr);
270
 
                iUsedLen += strlen(*szArgPtr) + 3;
271
 
        }
272
 
        szCmdLine[iUsedLen < 2048 ? iUsedLen - 1 : 2048 - 1] = '\0';
273
 
        
274
 
        // create pipes to write and read data
275
 
        HANDLE hReadPipe, hWritePipe;
276
 
        SECURITY_ATTRIBUTES SecurityAttributes;
277
 
        memset(&SecurityAttributes, 0, sizeof(SecurityAttributes));
278
 
        SecurityAttributes.nLength = sizeof(SecurityAttributes);
279
 
        SecurityAttributes.bInheritHandle = TRUE;
280
 
 
281
 
        CreatePipe(&hReadPipe, &hWritePipe, &SecurityAttributes, 0);
282
 
 
283
 
        STARTUPINFO StartupInfo;
284
 
        memset(&StartupInfo, 0, sizeof(StartupInfo));
285
 
        StartupInfo.cb = sizeof(StartupInfo);
286
 
        StartupInfo.dwFlags = STARTF_USESTDHANDLES;
287
 
        StartupInfo.hStdInput = 0;
288
 
        StartupInfo.hStdOutput = hWritePipe;
289
 
        StartupInfo.hStdError = hWritePipe;
290
 
 
291
 
        PROCESS_INFORMATION ProcessInfo;
292
 
        memset(&ProcessInfo, 0, sizeof(ProcessInfo));
293
 
 
294
 
        char* szEnvironmentStrings = m_environmentStrings.GetStrings();
295
 
 
296
 
        BOOL bOK = CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, szEnvironmentStrings, m_szWorkingDir, &StartupInfo, &ProcessInfo);
297
 
        if (!bOK)
298
 
        {
299
 
                DWORD dwErrCode = GetLastError();
300
 
                char szErrMsg[255];
301
 
                szErrMsg[255-1] = '\0';
302
 
                if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM || FORMAT_MESSAGE_IGNORE_INSERTS || FORMAT_MESSAGE_ARGUMENT_ARRAY, 
303
 
                        NULL, dwErrCode, 0, szErrMsg, 255, NULL))
304
 
                {
305
 
                        error("Could not start %s: %s", m_szInfoName, szErrMsg);
306
 
                }
307
 
                else
308
 
                {
309
 
                        error("Could not start %s: error %i", m_szInfoName, dwErrCode);
310
 
                }
311
 
                free(szEnvironmentStrings);
312
 
                return -1;
313
 
        }
314
 
 
315
 
        free(szEnvironmentStrings);
316
 
 
317
 
        debug("Child Process-ID: %i", (int)ProcessInfo.dwProcessId);
318
 
 
319
 
        m_hProcess = ProcessInfo.hProcess;
320
 
 
321
 
        // close unused "write" end
322
 
        CloseHandle(hWritePipe);
323
 
 
324
 
        pipein = _open_osfhandle((intptr_t)hReadPipe, _O_RDONLY);
325
 
 
326
 
#else
327
 
 
328
 
        int p[2];
329
 
        int pipeout;
330
 
 
331
 
        // create the pipe
332
 
        if (pipe(p))
333
 
        {
334
 
                error("Could not open pipe: errno %i", errno);
335
 
                return -1;
336
 
        }
337
 
 
338
 
        char** pEnvironmentStrings = m_environmentStrings.GetStrings();
339
 
 
340
 
        pipein = p[0];
341
 
        pipeout = p[1];
342
 
 
343
 
        debug("forking");
344
 
        pid_t pid = fork();
345
 
 
346
 
        if (pid == -1)
347
 
        {
348
 
                error("Could not start %s: errno %i", m_szInfoName, errno);
349
 
                free(pEnvironmentStrings);
350
 
                return -1;
351
 
        }
352
 
        else if (pid == 0)
353
 
        {
354
 
                // here goes the second instance
355
 
 
356
 
                // create new process group (see Terminate() where it is used)
357
 
                setsid();
358
 
                        
359
 
                // close up the "read" end
360
 
                close(pipein);
361
 
                        
362
 
                // make the pipeout to be the same as stdout and stderr
363
 
                dup2(pipeout, 1);
364
 
                dup2(pipeout, 2);
365
 
                
366
 
                close(pipeout);
367
 
 
368
 
#ifdef CHILD_WATCHDOG
369
 
                fwrite("\n", 1, 1, stdout);
370
 
                fflush(stdout);
371
 
#endif
372
 
 
373
 
                execve(m_szScript, (char* const*)m_szArgs, (char* const*)pEnvironmentStrings);
374
 
                fprintf(stdout, "[ERROR] Could not start script: %s", strerror(errno));
375
 
                fflush(stdout);
376
 
                _exit(-1);
377
 
        }
378
 
 
379
 
        // continue the first instance
380
 
        debug("forked");
381
 
        debug("Child Process-ID: %i", (int)pid);
382
 
 
383
 
        free(pEnvironmentStrings);
384
 
 
385
 
        m_hProcess = pid;
386
 
 
387
 
        // close unused "write" end
388
 
        close(pipeout);
389
 
#endif
390
 
 
391
 
        // open the read end
392
 
        FILE* readpipe = fdopen(pipein, "r");
393
 
        if (!readpipe)
394
 
        {
395
 
                error("Could not open pipe to %s", m_szInfoName);
396
 
                return -1;
397
 
        }
398
 
        
399
 
#ifdef CHILD_WATCHDOG
400
 
        debug("Creating child watchdog");
401
 
        ChildWatchDog* pWatchDog = new ChildWatchDog();
402
 
        pWatchDog->SetAutoDestroy(false);
403
 
        pWatchDog->SetProcessID(pid);
404
 
        pWatchDog->Start();
405
 
#endif
406
 
        
407
 
        char* buf = (char*)malloc(10240);
408
 
 
409
 
        debug("Entering pipe-loop");
410
 
        while (!feof(readpipe) && !m_bTerminated)
411
 
        {
412
 
                if (fgets(buf, 10240, readpipe))
413
 
                {
414
 
#ifdef CHILD_WATCHDOG
415
 
                        if (!bChildConfirmed)
416
 
                        {
417
 
                                bChildConfirmed = true;
418
 
                                pWatchDog->Stop();
419
 
                                debug("Child confirmed");
420
 
                        }
421
 
#endif
422
 
                        ProcessOutput(buf);
423
 
                }
424
 
        }
425
 
        debug("Exited pipe-loop");
426
 
 
427
 
#ifdef CHILD_WATCHDOG
428
 
        debug("Destroying WatchDog");
429
 
        if (!bChildConfirmed)
430
 
        {
431
 
                pWatchDog->Stop();
432
 
        }
433
 
        while (pWatchDog->IsRunning())
434
 
        {
435
 
                usleep(1 * 1000);
436
 
        }
437
 
        delete pWatchDog;
438
 
#endif
439
 
        
440
 
        free(buf);
441
 
        fclose(readpipe);
442
 
 
443
 
        if (m_bTerminated)
444
 
        {
445
 
                warn("Interrupted %s", m_szInfoName);
446
 
        }
447
 
 
448
 
        iExitCode = 0;
449
 
 
450
 
#ifdef WIN32
451
 
        WaitForSingleObject(m_hProcess, INFINITE);
452
 
        DWORD dExitCode = 0;
453
 
        GetExitCodeProcess(m_hProcess, &dExitCode);
454
 
        iExitCode = dExitCode;
455
 
#else
456
 
        int iStatus = 0;
457
 
        waitpid(m_hProcess, &iStatus, 0);
458
 
        if (WIFEXITED(iStatus))
459
 
        {
460
 
                iExitCode = WEXITSTATUS(iStatus);
461
 
        }
462
 
#endif
463
 
        
464
 
#ifdef CHILD_WATCHDOG
465
 
        }       // while (!bChildConfirmed && !m_bTerminated)
466
 
#endif
467
 
        
468
 
        if (!m_bTerminated)
469
 
        {
470
 
                info("Completed %s", m_szInfoName);
471
 
                debug("Exit code %i", iExitCode);
472
 
        }
473
 
 
474
 
        return iExitCode;
475
 
}
476
 
 
477
 
void ScriptController::Terminate()
478
 
{
479
 
        debug("Stopping %s", m_szInfoName);
480
 
        m_bTerminated = true;
481
 
 
482
 
#ifdef WIN32
483
 
        BOOL bOK = TerminateProcess(m_hProcess, -1);
484
 
#else
485
 
        pid_t hKillProcess = m_hProcess;
486
 
        if (getpgid(hKillProcess) == hKillProcess)
487
 
        {
488
 
                // if the child process has its own group (setsid() was successful), kill the whole group
489
 
                hKillProcess = -hKillProcess;
490
 
        }
491
 
        bool bOK = kill(hKillProcess, SIGKILL) == 0;
492
 
#endif
493
 
 
494
 
        if (bOK)
495
 
        {
496
 
                debug("Terminated %s", m_szInfoName);
497
 
        }
498
 
        else
499
 
        {
500
 
                error("Could not terminate %s", m_szInfoName);
501
 
        }
502
 
 
503
 
        debug("Stopped %s", m_szInfoName);
504
 
}
505
 
 
506
 
void ScriptController::ProcessOutput(char* szText)
507
 
{
508
 
        debug("Processing output received from script");
509
 
 
510
 
        for (char* pend = szText + strlen(szText) - 1; pend >= szText && (*pend == '\n' || *pend == '\r' || *pend == ' '); pend--) *pend = '\0';
511
 
 
512
 
        if (strlen(szText) == 0)
513
 
        {
514
 
                // skip empty lines
515
 
                return;
516
 
        }
517
 
 
518
 
        if (!strncmp(szText, "[INFO] ", 7))
519
 
        {
520
 
                AddMessage(Message::mkInfo, false, g_pOptions->GetInfoTarget(), szText + 7);
521
 
        }
522
 
        else if (!strncmp(szText, "[WARNING] ", 10))
523
 
        {
524
 
                AddMessage(Message::mkWarning, false, g_pOptions->GetWarningTarget(), szText + 10);
525
 
        }
526
 
        else if (!strncmp(szText, "[ERROR] ", 8))
527
 
        {
528
 
                AddMessage(Message::mkError, false, g_pOptions->GetErrorTarget(), szText + 8);
529
 
        }
530
 
        else if (!strncmp(szText, "[DETAIL] ", 9))
531
 
        {
532
 
                AddMessage(Message::mkDetail, false, g_pOptions->GetDetailTarget(), szText + 9);
533
 
        }
534
 
        else if (!strncmp(szText, "[DEBUG] ", 8))
535
 
        {
536
 
                AddMessage(Message::mkDebug, false, g_pOptions->GetDebugTarget(), szText + 8);
537
 
        }
538
 
        else 
539
 
        {       
540
 
                switch (m_eDefaultLogKind)
541
 
                {
542
 
                        case Options::slNone:
543
 
                                break;
544
 
 
545
 
                        case Options::slDetail:
546
 
                                AddMessage(Message::mkDetail, true, g_pOptions->GetDetailTarget(), szText);
547
 
                                break;
548
 
 
549
 
                        case Options::slInfo:
550
 
                                AddMessage(Message::mkInfo, true, g_pOptions->GetInfoTarget(), szText);
551
 
                                break;
552
 
 
553
 
                        case Options::slWarning:
554
 
                                AddMessage(Message::mkWarning, true, g_pOptions->GetWarningTarget(), szText);
555
 
                                break;
556
 
 
557
 
                        case Options::slError:
558
 
                                AddMessage(Message::mkError, true, g_pOptions->GetErrorTarget(), szText);
559
 
                                break;
560
 
 
561
 
                        case Options::slDebug:
562
 
                                AddMessage(Message::mkDebug, true, g_pOptions->GetDebugTarget(), szText);
563
 
                                break;
564
 
                }
565
 
        }
566
 
 
567
 
        debug("Processing output received from script - completed");
568
 
}
569
 
 
570
 
void ScriptController::AddMessage(Message::EKind eKind, bool bDefaultKind, Options::EMessageTarget eMessageTarget, const char* szText)
571
 
{
572
 
        switch (eKind)
573
 
        {
574
 
                case Message::mkDetail:
575
 
                        detail("%s%s", (bDefaultKind && m_szDefaultKindPrefix ? m_szDefaultKindPrefix : ""), szText);
576
 
                        break;
577
 
 
578
 
                case Message::mkInfo:
579
 
                        info("%s%s", (bDefaultKind && m_szDefaultKindPrefix ? m_szDefaultKindPrefix : ""), szText);
580
 
                        break;
581
 
 
582
 
                case Message::mkWarning:
583
 
                        warn("%s%s", (bDefaultKind && m_szDefaultKindPrefix ? m_szDefaultKindPrefix : ""), szText);
584
 
                        break;
585
 
 
586
 
                case Message::mkError:
587
 
                        error("%s%s", (bDefaultKind && m_szDefaultKindPrefix ? m_szDefaultKindPrefix : ""), szText);
588
 
                        break;
589
 
 
590
 
                case Message::mkDebug:
591
 
                        debug("%s%s", (bDefaultKind && m_szDefaultKindPrefix ? m_szDefaultKindPrefix : ""), szText);
592
 
                        break;
593
 
        }
594
 
}
595
 
 
596
 
void PostScriptController::StartScriptJob(PostInfo* pPostInfo, const char* szScript, bool bNZBFileCompleted, bool bHasFailedParJobs)
597
 
{
598
 
        info("Executing post-process-script for %s", pPostInfo->GetInfoName());
599
 
 
600
 
        PostScriptController* pScriptController = new PostScriptController();
601
 
        pScriptController->m_pPostInfo = pPostInfo;
602
 
        pScriptController->SetScript(szScript);
603
 
        pScriptController->SetWorkingDir(g_pOptions->GetDestDir());
604
 
        pScriptController->m_bNZBFileCompleted = bNZBFileCompleted;
605
 
        pScriptController->m_bHasFailedParJobs = bHasFailedParJobs;
606
 
        pScriptController->SetAutoDestroy(false);
607
 
 
608
 
        pPostInfo->SetScriptThread(pScriptController);
609
 
 
610
 
        pScriptController->Start();
611
 
}
612
 
 
613
 
void PostScriptController::Run()
614
 
{
615
 
        // the locking is needed for accessing the memebers of NZBInfo
616
 
        g_pDownloadQueueHolder->LockQueue();
617
 
 
618
 
        char szParStatus[10];
619
 
        snprintf(szParStatus, 10, "%i", m_pPostInfo->GetParStatus());
620
 
        szParStatus[10-1] = '\0';
621
 
 
622
 
        char szCollectionCompleted[10];
623
 
        snprintf(szCollectionCompleted, 10, "%i", (int)m_bNZBFileCompleted);
624
 
        szCollectionCompleted[10-1] = '\0';
625
 
 
626
 
        char szHasFailedParJobs[10];
627
 
        snprintf(szHasFailedParJobs, 10, "%i", (int)m_bHasFailedParJobs);
628
 
        szHasFailedParJobs[10-1] = '\0';
629
 
 
630
 
        char szDestDir[1024];
631
 
        strncpy(szDestDir, m_pPostInfo->GetNZBInfo()->GetDestDir(), 1024);
632
 
        szDestDir[1024-1] = '\0';
633
 
        
634
 
        char szNZBFilename[1024];
635
 
        strncpy(szNZBFilename, m_pPostInfo->GetNZBInfo()->GetFilename(), 1024);
636
 
        szNZBFilename[1024-1] = '\0';
637
 
        
638
 
        char szParFilename[1024];
639
 
        strncpy(szParFilename, m_pPostInfo->GetParFilename(), 1024);
640
 
        szParFilename[1024-1] = '\0';
641
 
 
642
 
        char szCategory[1024];
643
 
        strncpy(szCategory, m_pPostInfo->GetNZBInfo()->GetCategory(), 1024);
644
 
        szCategory[1024-1] = '\0';
645
 
 
646
 
        char szInfoName[1024];
647
 
        snprintf(szInfoName, 1024, "post-process-script for %s", m_pPostInfo->GetInfoName());
648
 
        szInfoName[1024-1] = '\0';
649
 
        SetInfoName(szInfoName);
650
 
 
651
 
        SetDefaultKindPrefix("Post-Process: ");
652
 
        SetDefaultLogKind(g_pOptions->GetProcessLogKind());
653
 
 
654
 
        const char* szArgs[9];
655
 
        szArgs[0] = GetScript();
656
 
        szArgs[1] = szDestDir;
657
 
        szArgs[2] = szNZBFilename;
658
 
        szArgs[3] = szParFilename;
659
 
        szArgs[4] = szParStatus;
660
 
        szArgs[5] = szCollectionCompleted;
661
 
        szArgs[6] = szHasFailedParJobs;
662
 
        szArgs[7] = szCategory;
663
 
        szArgs[8] = NULL;
664
 
        SetArgs(szArgs, false);
665
 
 
666
 
        SetEnvVar("NZBPP_DIRECTORY", szDestDir);
667
 
        SetEnvVar("NZBPP_NZBFILENAME", szNZBFilename);
668
 
        SetEnvVar("NZBPP_PARFILENAME", szParFilename);
669
 
        SetEnvVar("NZBPP_PARSTATUS", szParStatus);
670
 
        SetEnvVar("NZBPP_NZBCOMPLETED", szCollectionCompleted);
671
 
        SetEnvVar("NZBPP_PARFAILED", szHasFailedParJobs);
672
 
        SetEnvVar("NZBPP_CATEGORY", szCategory);
673
 
 
674
 
        for (NZBParameterList::iterator it = m_pPostInfo->GetNZBInfo()->GetParameters()->begin(); it != m_pPostInfo->GetNZBInfo()->GetParameters()->end(); it++)
675
 
        {
676
 
                NZBParameter* pParameter = *it;
677
 
                char szVarname[1024];
678
 
                snprintf(szVarname, sizeof(szVarname), "NZBPR_%s", pParameter->GetName());
679
 
                szVarname[1024-1] = '\0';
680
 
                SetEnvVar(szVarname, pParameter->GetValue());
681
 
        }
682
 
 
683
 
        g_pDownloadQueueHolder->UnlockQueue();
684
 
 
685
 
        int iResult = Execute();
686
 
 
687
 
        switch (iResult)
688
 
        {
689
 
                case POSTPROCESS_SUCCESS:
690
 
                        info("%s sucessful", szInfoName);
691
 
                        m_pPostInfo->SetScriptStatus(PostInfo::srSuccess);
692
 
                        break;
693
 
 
694
 
                case POSTPROCESS_ERROR:
695
 
                        info("%s failed", szInfoName);
696
 
                        m_pPostInfo->SetScriptStatus(PostInfo::srFailure);
697
 
                        break;
698
 
 
699
 
                case POSTPROCESS_NONE:
700
 
                        info("%s skipped", szInfoName);
701
 
                        m_pPostInfo->SetScriptStatus(PostInfo::srNone);
702
 
                        break;
703
 
 
704
 
#ifndef DISABLE_PARCHECK
705
 
                case POSTPROCESS_PARCHECK_ALL:
706
 
                        if (m_pPostInfo->GetParCheck())
707
 
                        {
708
 
                                error("%s requested par-check/repair for all collections, but they were already checked", szInfoName);
709
 
                                m_pPostInfo->SetScriptStatus(PostInfo::srFailure);
710
 
                        }
711
 
                        else if (!m_bNZBFileCompleted)
712
 
                        {
713
 
                                error("%s requested par-check/repair for all collections, but it was not the call for the last collection", szInfoName);
714
 
                                m_pPostInfo->SetScriptStatus(PostInfo::srFailure);
715
 
                        }
716
 
                        else
717
 
                        {
718
 
                                info("%s requested par-check/repair for all collections", szInfoName);
719
 
                                m_pPostInfo->SetRequestParCheck(PostInfo::rpAll);
720
 
                                m_pPostInfo->SetScriptStatus(PostInfo::srSuccess);
721
 
                        }
722
 
                        break;
723
 
 
724
 
                case POSTPROCESS_PARCHECK_CURRENT:
725
 
                        if (m_pPostInfo->GetParCheck())
726
 
                        {
727
 
                                error("%s requested par-check/repair for current collection, but it was already checked", szInfoName);
728
 
                                m_pPostInfo->SetScriptStatus(PostInfo::srFailure);
729
 
                        }
730
 
                        else if (strlen(m_pPostInfo->GetParFilename()) == 0)
731
 
                        {
732
 
                                error("%s requested par-check/repair for current collection, but it doesn't have any par-files", szInfoName);
733
 
                                m_pPostInfo->SetScriptStatus(PostInfo::srFailure);
734
 
                        }
735
 
                        else
736
 
                        {
737
 
                                info("%s requested par-check/repair for current collection", szInfoName);
738
 
                                m_pPostInfo->SetRequestParCheck(PostInfo::rpCurrent);
739
 
                                m_pPostInfo->SetScriptStatus(PostInfo::srSuccess);
740
 
                        }
741
 
                        break;
742
 
#endif
743
 
 
744
 
                default:
745
 
                        info("%s terminated with unknown status", szInfoName);
746
 
                        m_pPostInfo->SetScriptStatus(PostInfo::srUnknown);
747
 
        }
748
 
 
749
 
        m_pPostInfo->SetStage(PostInfo::ptFinished);
750
 
        m_pPostInfo->SetWorking(false);
751
 
}
752
 
 
753
 
void PostScriptController::AddMessage(Message::EKind eKind, bool bDefaultKind, Options::EMessageTarget eMessageTarget, const char* szText)
754
 
{
755
 
        if (!strncmp(szText, "[HISTORY] ", 10))
756
 
        {
757
 
                if (eMessageTarget == Options::mtScreen || eMessageTarget == Options::mtBoth)
758
 
                {
759
 
                        m_pPostInfo->GetNZBInfo()->AppendMessage(eKind, 0, szText + 10);
760
 
                }
761
 
        }
762
 
        else
763
 
        {
764
 
                ScriptController::AddMessage(eKind, bDefaultKind, eMessageTarget, szText);
765
 
 
766
 
                if (eMessageTarget == Options::mtScreen || eMessageTarget == Options::mtBoth)
767
 
                {
768
 
                        m_pPostInfo->AppendMessage(eKind, szText);
769
 
                }
770
 
        }
771
 
 
772
 
 
773
 
        if (g_pOptions->GetPausePostProcess())
774
 
        {
775
 
                time_t tStageTime = m_pPostInfo->GetStageTime();
776
 
                time_t tStartTime = m_pPostInfo->GetStartTime();
777
 
                time_t tWaitTime = time(NULL);
778
 
 
779
 
                // wait until Post-processor is unpaused
780
 
                while (g_pOptions->GetPausePostProcess() && !IsStopped())
781
 
                {
782
 
                        usleep(100 * 1000);
783
 
 
784
 
                        // update time stamps
785
 
 
786
 
                        time_t tDelta = time(NULL) - tWaitTime;
787
 
 
788
 
                        if (tStageTime > 0)
789
 
                        {
790
 
                                m_pPostInfo->SetStageTime(tStageTime + tDelta);
791
 
                        }
792
 
 
793
 
                        if (tStartTime > 0)
794
 
                        {
795
 
                                m_pPostInfo->SetStartTime(tStartTime + tDelta);
796
 
                        }
797
 
                }
798
 
        }
799
 
}
800
 
 
801
 
void PostScriptController::Stop()
802
 
{
803
 
        debug("Stopping post-process-script");
804
 
        Thread::Stop();
805
 
        Terminate();
806
 
}
807
 
 
808
 
void NZBScriptController::ExecuteScript(const char* szScript, const char* szNZBFilename, 
809
 
        const char* szDirectory, char** pCategory, NZBParameterList* pParameterList)
810
 
{
811
 
        info("Executing nzb-process-script for %s", Util::BaseFileName(szNZBFilename));
812
 
 
813
 
        NZBScriptController* pScriptController = new NZBScriptController();
814
 
        pScriptController->SetScript(szScript);
815
 
        pScriptController->m_pCategory = pCategory;
816
 
        pScriptController->m_pParameterList = pParameterList;
817
 
 
818
 
        char szInfoName[1024];
819
 
        snprintf(szInfoName, 1024, "nzb-process-script for %s", Util::BaseFileName(szNZBFilename));
820
 
        szInfoName[1024-1] = '\0';
821
 
        pScriptController->SetInfoName(szInfoName);
822
 
 
823
 
        // remove trailing slash
824
 
        char szDir[1024];
825
 
        strncpy(szDir, szDirectory, 1024);
826
 
        szDir[1024-1] = '\0';
827
 
        int iLen = strlen(szDir);
828
 
        if (szDir[iLen-1] == PATH_SEPARATOR)
829
 
        {
830
 
                szDir[iLen-1] = '\0';
831
 
        }
832
 
 
833
 
        pScriptController->SetDefaultKindPrefix("NZB-Process: ");
834
 
        pScriptController->SetDefaultLogKind(g_pOptions->GetProcessLogKind());
835
 
 
836
 
        const char* szArgs[4];
837
 
        szArgs[0] = szScript;
838
 
        szArgs[1] = szDir;
839
 
        szArgs[2] = szNZBFilename;
840
 
        szArgs[3] = NULL;
841
 
        pScriptController->SetArgs(szArgs, false);
842
 
 
843
 
        pScriptController->SetEnvVar("NZBNP_DIRECTORY", szDir);
844
 
        pScriptController->SetEnvVar("NZBNP_FILENAME", szNZBFilename);
845
 
 
846
 
        pScriptController->Execute();
847
 
 
848
 
        delete pScriptController;
849
 
}
850
 
 
851
 
void NZBScriptController::AddMessage(Message::EKind eKind, bool bDefaultKind, Options::EMessageTarget eMessageTarget, const char* szText)
852
 
{
853
 
        if (!strncmp(szText, "[NZB] ", 6))
854
 
        {
855
 
                debug("Command %s detected", szText + 6);
856
 
                if (!strncmp(szText + 6, "CATEGORY=", 9))
857
 
                {
858
 
                        free(*m_pCategory);
859
 
                        *m_pCategory = strdup(szText + 6 + 9);
860
 
                }
861
 
                else if (!strncmp(szText + 6, "NZBPR_", 6))
862
 
                {
863
 
                        char* szParam = strdup(szText + 6 + 6);
864
 
                        char* szValue = strchr(szParam, '=');
865
 
                        if (szValue)
866
 
                        {
867
 
                                *szValue = '\0';
868
 
                                m_pParameterList->SetParameter(szParam, szValue + 1);
869
 
                        }
870
 
                        else
871
 
                        {
872
 
                                error("Invalid command \"%s\" received from %s", szText, GetInfoName());
873
 
                        }
874
 
                        free(szParam);
875
 
                }
876
 
                else
877
 
                {
878
 
                        error("Invalid command \"%s\" received from %s", szText, GetInfoName());
879
 
                }
880
 
        }
881
 
        else
882
 
        {
883
 
                ScriptController::AddMessage(eKind, bDefaultKind, eMessageTarget, szText);
884
 
        }
885
 
}
886
 
 
887
 
void SchedulerScriptController::StartScript(const char* szCommandLine)
888
 
{
889
 
        char** argv = NULL;
890
 
        if (!Util::SplitCommandLine(szCommandLine, &argv))
891
 
        {
892
 
                error("Could not execute scheduled process-script, failed to parse command line: %s", szCommandLine);
893
 
                return;
894
 
        }
895
 
 
896
 
        info("Executing scheduled process-script %s", Util::BaseFileName(argv[0]));
897
 
 
898
 
        SchedulerScriptController* pScriptController = new SchedulerScriptController();
899
 
        pScriptController->SetScript(argv[0]);
900
 
        pScriptController->SetArgs((const char**)argv, true);
901
 
        pScriptController->SetAutoDestroy(true);
902
 
 
903
 
        pScriptController->Start();
904
 
}
905
 
 
906
 
void SchedulerScriptController::Run()
907
 
{
908
 
        char szInfoName[1024];
909
 
        snprintf(szInfoName, 1024, "scheduled process-script %s", Util::BaseFileName(GetScript()));
910
 
        szInfoName[1024-1] = '\0';
911
 
        SetInfoName(szInfoName);
912
 
 
913
 
        SetDefaultKindPrefix("Scheduled Process: ");
914
 
        SetDefaultLogKind(g_pOptions->GetProcessLogKind());
915
 
 
916
 
        Execute();
917
 
}