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

« back to all changes in this revision

Viewing changes to .pc/fix_fsf_address.patch/nzbget.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) 2004 Sven Henkel <sidddy@users.sourceforge.net>
5
 
 *  Copyright (C) 2007-2010 Andrei Prygounkov <hugbug@users.sourceforge.net>
6
 
 *
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; either version 2 of the License, or
10
 
 *  (at your option) any later version.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU General Public License
18
 
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
 
 *
21
 
 * $Revision: 380 $
22
 
 * $Date: 2010-01-30 15:43:58 +0100 (Sat, 30 Jan 2010) $
23
 
 *
24
 
 */
25
 
 
26
 
 
27
 
#ifdef HAVE_CONFIG_H
28
 
#include <config.h>
29
 
#endif
30
 
 
31
 
#ifdef WIN32
32
 
#include "win32.h"
33
 
#endif
34
 
 
35
 
#include <stdlib.h>
36
 
#include <string.h>
37
 
#ifdef WIN32
38
 
#include <winsvc.h>
39
 
#else
40
 
#include <unistd.h>
41
 
#include <pwd.h>
42
 
#include <grp.h>
43
 
#include <sys/resource.h>
44
 
#ifdef HAVE_SYS_PRCTL_H
45
 
#include <sys/prctl.h>
46
 
#endif
47
 
#include <signal.h>
48
 
#endif
49
 
#include <sys/types.h>
50
 
#include <sys/stat.h>
51
 
#include <stdio.h>
52
 
#include <fcntl.h>
53
 
#ifndef DISABLE_PARCHECK
54
 
#include <iostream>
55
 
#endif
56
 
#ifdef HAVE_BACKTRACE
57
 
#include <execinfo.h>
58
 
#endif
59
 
 
60
 
#include "nzbget.h"
61
 
#include "ServerPool.h"
62
 
#include "Log.h"
63
 
#include "NZBFile.h"
64
 
#include "Options.h"
65
 
#include "Thread.h"
66
 
#include "ColoredFrontend.h"
67
 
#include "NCursesFrontend.h"
68
 
#include "QueueCoordinator.h"
69
 
#include "RemoteServer.h"
70
 
#include "RemoteClient.h"
71
 
#include "MessageBase.h"
72
 
#include "DiskState.h"
73
 
#include "PrePostProcessor.h"
74
 
#include "ParChecker.h"
75
 
#include "Scheduler.h"
76
 
#include "Util.h"
77
 
#ifdef WIN32
78
 
#include "NTService.h"
79
 
#endif
80
 
 
81
 
// Prototypes
82
 
void Run();
83
 
void Cleanup();
84
 
void ProcessClientRequest();
85
 
#ifndef WIN32
86
 
void InstallSignalHandlers();
87
 
void Daemonize();
88
 
void PrintBacktrace();
89
 
#ifdef HAVE_SYS_PRCTL_H
90
 
void EnableDumpCore();
91
 
#endif
92
 
#ifdef DEBUG
93
 
void MakeSegFault();
94
 
#endif
95
 
#endif
96
 
#ifndef DISABLE_PARCHECK
97
 
void DisableCout();
98
 
#endif
99
 
 
100
 
Thread* g_pFrontend = NULL;
101
 
Options* g_pOptions = NULL;
102
 
ServerPool* g_pServerPool = NULL;
103
 
QueueCoordinator* g_pQueueCoordinator = NULL;
104
 
RemoteServer* g_pRemoteServer = NULL;
105
 
DownloadSpeedMeter* g_pDownloadSpeedMeter = NULL;
106
 
DownloadQueueHolder* g_pDownloadQueueHolder = NULL;
107
 
Log* g_pLog = NULL;
108
 
PrePostProcessor* g_pPrePostProcessor = NULL;
109
 
DiskState* g_pDiskState = NULL;
110
 
Scheduler* g_pScheduler = NULL;
111
 
char* (*szEnvironmentVariables)[] = NULL;
112
 
 
113
 
/*
114
 
 * Main loop
115
 
 */
116
 
int main(int argc, char *argv[], char *argp[])
117
 
{
118
 
#ifdef WIN32
119
 
#ifdef _DEBUG
120
 
        _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
121
 
        _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
122
 
        _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF
123
 
#ifdef DEBUG_CRTMEMLEAKS
124
 
                | _CRTDBG_CHECK_CRT_DF | _CRTDBG_CHECK_ALWAYS_DF
125
 
#endif
126
 
                );
127
 
#endif
128
 
#endif
129
 
 
130
 
        Util::InitVersionRevision();
131
 
        
132
 
#ifdef WIN32
133
 
        InstallUninstallServiceCheck(argc, argv);
134
 
#endif
135
 
 
136
 
#ifndef DISABLE_PARCHECK
137
 
        DisableCout();
138
 
#endif
139
 
 
140
 
        g_pLog = new Log();
141
 
 
142
 
        debug("nzbget %s", Util::VersionRevision());
143
 
 
144
 
        g_pServerPool = new ServerPool();
145
 
        g_pScheduler = new Scheduler();
146
 
        Thread::Init();
147
 
 
148
 
        debug("Reading options");
149
 
        g_pOptions = new Options(argc, argv);
150
 
        szEnvironmentVariables = (char*(*)[])argp;
151
 
 
152
 
#ifndef WIN32
153
 
        if (g_pOptions->GetUMask() < 01000)
154
 
        {
155
 
                /* set newly created file permissions */
156
 
                umask(g_pOptions->GetUMask());
157
 
        }
158
 
#endif
159
 
        
160
 
        if (g_pOptions->GetServerMode() && g_pOptions->GetCreateLog() && g_pOptions->GetResetLog())
161
 
        {
162
 
                debug("Deleting old log-file");
163
 
                g_pLog->ResetLog();
164
 
        }
165
 
 
166
 
        g_pLog->InitOptions();
167
 
 
168
 
        if (g_pOptions->GetDaemonMode())
169
 
        {
170
 
#ifdef WIN32
171
 
                info("nzbget %s service-mode", Util::VersionRevision());
172
 
#else
173
 
                Daemonize();
174
 
                info("nzbget %s daemon-mode", Util::VersionRevision());
175
 
#endif
176
 
        }
177
 
        else if (g_pOptions->GetServerMode())
178
 
        {
179
 
                info("nzbget %s server-mode", Util::VersionRevision());
180
 
        }
181
 
        else if (g_pOptions->GetRemoteClientMode())
182
 
        {
183
 
                info("nzbget %s remote-mode", Util::VersionRevision());
184
 
        }
185
 
 
186
 
        if (!g_pOptions->GetRemoteClientMode())
187
 
        {
188
 
                g_pServerPool->InitConnections();
189
 
#ifdef DEBUG
190
 
                g_pServerPool->LogDebugInfo();
191
 
#endif
192
 
        }
193
 
 
194
 
#ifdef WIN32
195
 
        if (g_pOptions->GetDaemonMode())
196
 
        {
197
 
                StartService(Run);
198
 
                return 0;
199
 
        }
200
 
#else
201
 
#ifdef HAVE_SYS_PRCTL_H
202
 
        if (g_pOptions->GetDumpCore())
203
 
        {
204
 
                EnableDumpCore();
205
 
        }
206
 
#endif
207
 
#endif
208
 
 
209
 
        Run();
210
 
 
211
 
#ifdef WIN32
212
 
#ifdef _DEBUG
213
 
        _CrtDumpMemoryLeaks();
214
 
#endif
215
 
#endif
216
 
 
217
 
        return 0;
218
 
}
219
 
 
220
 
void Run()
221
 
{
222
 
#ifndef WIN32
223
 
        InstallSignalHandlers();
224
 
#ifdef DEBUG
225
 
        if (g_pOptions->GetTestBacktrace())
226
 
        {
227
 
                MakeSegFault();
228
 
        }
229
 
#endif
230
 
#endif
231
 
        
232
 
        Connection::Init(g_pOptions->GetTLS() && !g_pOptions->GetRemoteClientMode() &&
233
 
                (g_pOptions->GetClientOperation() == Options::opClientNoOperation));
234
 
 
235
 
        // client request
236
 
        if (g_pOptions->GetClientOperation() != Options::opClientNoOperation)
237
 
        {
238
 
                ProcessClientRequest();
239
 
                Cleanup();
240
 
                return;
241
 
        }
242
 
 
243
 
        // Create the queue coordinator
244
 
        if (!g_pOptions->GetRemoteClientMode())
245
 
        {                                    
246
 
                g_pQueueCoordinator = new QueueCoordinator();
247
 
                g_pDownloadSpeedMeter = g_pQueueCoordinator;
248
 
                g_pDownloadQueueHolder = g_pQueueCoordinator;
249
 
        }
250
 
 
251
 
        // Setup the network-server
252
 
        if (g_pOptions->GetServerMode())
253
 
        {
254
 
                g_pRemoteServer = new RemoteServer();
255
 
                g_pRemoteServer->Start();
256
 
        }
257
 
 
258
 
        // Creating PrePostProcessor
259
 
        if (!g_pOptions->GetRemoteClientMode())
260
 
        {
261
 
                g_pPrePostProcessor = new PrePostProcessor();
262
 
        }
263
 
 
264
 
        // Create the frontend
265
 
        if (!g_pOptions->GetDaemonMode())
266
 
        {
267
 
                switch (g_pOptions->GetOutputMode())
268
 
                {
269
 
                        case Options::omNCurses:
270
 
#ifndef DISABLE_CURSES
271
 
                                g_pFrontend = new NCursesFrontend();
272
 
                                break;
273
 
#endif
274
 
                        case Options::omColored:
275
 
                                g_pFrontend = new ColoredFrontend();
276
 
                                break;
277
 
                        case Options::omLoggable:
278
 
                                g_pFrontend = new LoggableFrontend();
279
 
                                break;
280
 
                }
281
 
        }
282
 
 
283
 
        // Starting a thread with the frontend
284
 
        if (g_pFrontend)
285
 
        {
286
 
                g_pFrontend->Start();
287
 
        }
288
 
 
289
 
        // Starting QueueCoordinator and PrePostProcessor
290
 
        if (!g_pOptions->GetRemoteClientMode())
291
 
        {
292
 
                // Standalone-mode
293
 
                if (!g_pOptions->GetServerMode())
294
 
                {
295
 
                        NZBFile* pNZBFile = NZBFile::CreateFromFile(g_pOptions->GetArgFilename(), g_pOptions->GetCategory() ? g_pOptions->GetCategory() : "");
296
 
                        if (!pNZBFile)
297
 
                        {
298
 
                                abort("FATAL ERROR: Parsing NZB-document %s failed\n\n", g_pOptions->GetArgFilename() ? g_pOptions->GetArgFilename() : "N/A");
299
 
                                return;
300
 
                        }
301
 
                        g_pQueueCoordinator->AddNZBFileToQueue(pNZBFile, false);
302
 
                        delete pNZBFile;
303
 
                }
304
 
 
305
 
                if (g_pOptions->GetSaveQueue() && g_pOptions->GetServerMode())
306
 
                {
307
 
                        g_pDiskState = new DiskState();
308
 
                }
309
 
 
310
 
                g_pQueueCoordinator->Start();
311
 
                g_pPrePostProcessor->Start();
312
 
 
313
 
                // enter main program-loop
314
 
                while (g_pQueueCoordinator->IsRunning() || g_pPrePostProcessor->IsRunning())
315
 
                {
316
 
                        if (!g_pOptions->GetServerMode() && !g_pQueueCoordinator->HasMoreJobs() && !g_pPrePostProcessor->HasMoreJobs())
317
 
                        {
318
 
                                // Standalone-mode: download completed
319
 
                                if (!g_pQueueCoordinator->IsStopped())
320
 
                                {
321
 
                                        g_pQueueCoordinator->Stop();
322
 
                                }
323
 
                                if (!g_pPrePostProcessor->IsStopped())
324
 
                                {
325
 
                                        g_pPrePostProcessor->Stop();
326
 
                                }
327
 
                        }
328
 
                        usleep(100 * 1000);
329
 
                }
330
 
 
331
 
                // main program-loop is terminated
332
 
                debug("QueueCoordinator stopped");
333
 
                debug("PrePostProcessor stopped");
334
 
        }
335
 
 
336
 
        // Stop network-server
337
 
        if (g_pRemoteServer)
338
 
        {
339
 
                debug("stopping RemoteServer");
340
 
                g_pRemoteServer->Stop();
341
 
                int iMaxWaitMSec = 1000;
342
 
                while (g_pRemoteServer->IsRunning() && iMaxWaitMSec > 0)
343
 
                {
344
 
                        usleep(100 * 1000);
345
 
                        iMaxWaitMSec -= 100;
346
 
                }
347
 
                if (g_pRemoteServer->IsRunning())
348
 
                {
349
 
                        debug("Killing RemoteServer");
350
 
                        g_pRemoteServer->Kill();
351
 
                }
352
 
                debug("RemoteServer stopped");
353
 
        }
354
 
        
355
 
        // Stop Frontend
356
 
        if (g_pFrontend)
357
 
        {
358
 
                if (!g_pOptions->GetRemoteClientMode())
359
 
                {
360
 
                        debug("Stopping Frontend");
361
 
                        g_pFrontend->Stop();
362
 
                }
363
 
                while (g_pFrontend->IsRunning())
364
 
                {
365
 
                        usleep(50 * 1000);
366
 
                }
367
 
                debug("Frontend stopped");
368
 
        }
369
 
 
370
 
        Cleanup();
371
 
}
372
 
 
373
 
void ProcessClientRequest()
374
 
{
375
 
        RemoteClient* Client = new RemoteClient();
376
 
 
377
 
        switch (g_pOptions->GetClientOperation())
378
 
        {
379
 
                case Options::opClientRequestListFiles:
380
 
                        Client->RequestServerList(true, false);
381
 
                        break;
382
 
 
383
 
                case Options::opClientRequestListGroups:
384
 
                        Client->RequestServerList(false, true);
385
 
                        break;
386
 
 
387
 
                case Options::opClientRequestListStatus:
388
 
                        Client->RequestServerList(false, false);
389
 
                        break;
390
 
 
391
 
                case Options::opClientRequestDownloadPause:
392
 
                        Client->RequestServerPauseUnpause(true, eRemotePauseUnpauseActionDownload);
393
 
                        break;
394
 
 
395
 
                case Options::opClientRequestDownloadUnpause:
396
 
                        Client->RequestServerPauseUnpause(false, eRemotePauseUnpauseActionDownload);
397
 
                        break;
398
 
 
399
 
                case Options::opClientRequestDownload2Pause:
400
 
                        Client->RequestServerPauseUnpause(true, eRemotePauseUnpauseActionDownload2);
401
 
                        break;
402
 
 
403
 
                case Options::opClientRequestDownload2Unpause:
404
 
                        Client->RequestServerPauseUnpause(false, eRemotePauseUnpauseActionDownload2);
405
 
                        break;
406
 
 
407
 
                case Options::opClientRequestSetRate:
408
 
                        Client->RequestServerSetDownloadRate(g_pOptions->GetSetRate());
409
 
                        break;
410
 
 
411
 
                case Options::opClientRequestDumpDebug:
412
 
                        Client->RequestServerDumpDebug();
413
 
                        break;
414
 
 
415
 
                case Options::opClientRequestEditQueue:
416
 
                        Client->RequestServerEditQueue((eRemoteEditAction)g_pOptions->GetEditQueueAction(), g_pOptions->GetEditQueueOffset(),
417
 
                                g_pOptions->GetEditQueueText(), g_pOptions->GetEditQueueIDList(), g_pOptions->GetEditQueueIDCount(), true);
418
 
                        break;
419
 
 
420
 
                case Options::opClientRequestLog:
421
 
                        Client->RequestServerLog(g_pOptions->GetLogLines());
422
 
                        break;
423
 
 
424
 
                case Options::opClientRequestShutdown:
425
 
                        Client->RequestServerShutdown();
426
 
                        break;
427
 
 
428
 
                case Options::opClientRequestDownload:
429
 
                        Client->RequestServerDownload(g_pOptions->GetArgFilename(), g_pOptions->GetCategory(), g_pOptions->GetAddTop());
430
 
                        break;
431
 
 
432
 
                case Options::opClientRequestVersion:
433
 
                        Client->RequestServerVersion();
434
 
                        break;
435
 
 
436
 
                case Options::opClientRequestPostQueue:
437
 
                        Client->RequestPostQueue();
438
 
                        break;
439
 
 
440
 
                case Options::opClientRequestWriteLog:
441
 
                        Client->RequestWriteLog(g_pOptions->GetWriteLogKind(), g_pOptions->GetLastArg());
442
 
                        break;
443
 
 
444
 
                case Options::opClientRequestScan:
445
 
                        Client->RequestScan();
446
 
                        break;
447
 
 
448
 
                case Options::opClientRequestPostPause:
449
 
                        Client->RequestServerPauseUnpause(true, eRemotePauseUnpauseActionPostProcess);
450
 
                        break;
451
 
 
452
 
                case Options::opClientRequestPostUnpause:
453
 
                        Client->RequestServerPauseUnpause(false, eRemotePauseUnpauseActionPostProcess);
454
 
                        break;
455
 
 
456
 
                case Options::opClientRequestScanPause:
457
 
                        Client->RequestServerPauseUnpause(true, eRemotePauseUnpauseActionScan);
458
 
                        break;
459
 
 
460
 
                case Options::opClientRequestScanUnpause:
461
 
                        Client->RequestServerPauseUnpause(false, eRemotePauseUnpauseActionScan);
462
 
                        break;
463
 
 
464
 
                case Options::opClientRequestHistory:    
465
 
                        Client->RequestHistory();        
466
 
                        break;
467
 
 
468
 
                case Options::opClientNoOperation:
469
 
                        break;
470
 
        }
471
 
 
472
 
        delete Client;
473
 
}
474
 
 
475
 
void ExitProc()
476
 
{
477
 
        info("Stopping, please wait...");
478
 
        if (g_pOptions->GetRemoteClientMode())
479
 
        {
480
 
                if (g_pFrontend)
481
 
                {
482
 
                        debug("Stopping Frontend");
483
 
                        g_pFrontend->Stop();
484
 
                }
485
 
        }
486
 
        else
487
 
        {
488
 
                if (g_pQueueCoordinator)
489
 
                {
490
 
                        debug("Stopping QueueCoordinator");
491
 
                        g_pQueueCoordinator->Stop();
492
 
                        g_pPrePostProcessor->Stop();
493
 
                }
494
 
        }
495
 
}
496
 
 
497
 
#ifndef WIN32
498
 
#ifdef DEBUG
499
 
typedef void(*sighandler)(int);
500
 
std::vector<sighandler> SignalProcList;
501
 
#endif
502
 
 
503
 
/*
504
 
 * Signal handler
505
 
 */
506
 
void SignalProc(int iSignal)
507
 
{
508
 
        switch (iSignal)
509
 
        {
510
 
                case SIGINT:
511
 
                        signal(SIGINT, SIG_DFL);   // Reset the signal handler
512
 
                        ExitProc();
513
 
                        break;
514
 
 
515
 
                case SIGTERM:
516
 
                        signal(SIGTERM, SIG_DFL);   // Reset the signal handler
517
 
                        ExitProc();
518
 
                        break;
519
 
 
520
 
                case SIGCHLD:
521
 
                        // ignoring
522
 
                        break;
523
 
 
524
 
#ifdef DEBUG
525
 
                case SIGPIPE:
526
 
                        // ignoring
527
 
                        break;
528
 
                        
529
 
                case SIGSEGV:
530
 
                        signal(SIGSEGV, SIG_DFL);   // Reset the signal handler
531
 
                        PrintBacktrace();
532
 
                        break;
533
 
                
534
 
                default:
535
 
                        // printf("Signal %i received\n", iSignal);
536
 
                        if (SignalProcList[iSignal - 1])
537
 
                        {
538
 
                                SignalProcList[iSignal - 1](iSignal);
539
 
                        }
540
 
                        break;
541
 
#endif
542
 
        }
543
 
}
544
 
 
545
 
void InstallSignalHandlers()
546
 
{
547
 
        signal(SIGINT, SignalProc);
548
 
        signal(SIGTERM, SignalProc);
549
 
        signal(SIGCHLD, SignalProc);
550
 
        signal(SIGPIPE, SIG_IGN);
551
 
#ifdef DEBUG
552
 
        SignalProcList.clear();
553
 
        for (int i = 1; i <= 32; i++)
554
 
        {
555
 
                SignalProcList.push_back((sighandler)signal(i, SignalProc));
556
 
        }
557
 
        signal(SIGWINCH, SIG_DFL);
558
 
#endif
559
 
}
560
 
 
561
 
void PrintBacktrace()
562
 
{
563
 
#ifdef HAVE_BACKTRACE
564
 
        printf("Segmentation fault, tracing...\n");
565
 
        
566
 
        void *array[100];
567
 
        size_t size;
568
 
        char **strings;
569
 
        size_t i;
570
 
 
571
 
        size = backtrace(array, 100);
572
 
        strings = backtrace_symbols(array, size);
573
 
 
574
 
        // first trace to screen
575
 
        printf("Obtained %zd stack frames\n", size);
576
 
        for (i = 0; i < size; i++)
577
 
        {
578
 
                printf("%s\n", strings[i]);
579
 
        }
580
 
 
581
 
        // then trace to log
582
 
        error("Segmentation fault, tracing...");
583
 
        error("Obtained %zd stack frames", size);
584
 
        for (i = 0; i < size; i++)
585
 
        {
586
 
                error("%s", strings[i]);
587
 
        }
588
 
 
589
 
        free(strings);
590
 
#else
591
 
        error("Segmentation fault");
592
 
#endif
593
 
}
594
 
 
595
 
#ifdef DEBUG
596
 
void MakeSegFault()
597
 
{
598
 
        char* N = NULL;
599
 
        strcpy(N, "");
600
 
}
601
 
#endif
602
 
 
603
 
#ifdef HAVE_SYS_PRCTL_H
604
 
/**
605
 
* activates the creation of core-files
606
 
*/
607
 
void EnableDumpCore()
608
 
{
609
 
        rlimit rlim;
610
 
        rlim.rlim_cur= RLIM_INFINITY;
611
 
        rlim.rlim_max= RLIM_INFINITY;
612
 
        setrlimit(RLIMIT_CORE, &rlim);
613
 
        prctl(PR_SET_DUMPABLE, 1);
614
 
}
615
 
#endif
616
 
#endif
617
 
 
618
 
void Cleanup()
619
 
{
620
 
        debug("Cleaning up global objects");
621
 
 
622
 
        debug("Deleting QueueCoordinator");
623
 
        if (g_pQueueCoordinator)
624
 
        {
625
 
                delete g_pQueueCoordinator;
626
 
                g_pQueueCoordinator = NULL;
627
 
        }
628
 
        debug("QueueCoordinator deleted");
629
 
 
630
 
        debug("Deleting RemoteServer");
631
 
        if (g_pRemoteServer)
632
 
        {
633
 
                delete g_pRemoteServer;
634
 
                g_pRemoteServer = NULL;
635
 
        }
636
 
        debug("RemoteServer deleted");
637
 
 
638
 
        debug("Deleting PrePostProcessor");
639
 
        if (g_pPrePostProcessor)
640
 
        {
641
 
                delete g_pPrePostProcessor;
642
 
                g_pPrePostProcessor = NULL;
643
 
        }
644
 
        debug("PrePostProcessor deleted");
645
 
 
646
 
        debug("Deleting Frontend");
647
 
        if (g_pFrontend)
648
 
        {
649
 
                delete g_pFrontend;
650
 
                g_pFrontend = NULL;
651
 
        }
652
 
        debug("Frontend deleted");
653
 
 
654
 
        debug("Deleting DiskState");
655
 
        if (g_pDiskState)
656
 
        {
657
 
                delete g_pDiskState;
658
 
                g_pDiskState = NULL;
659
 
        }
660
 
        debug("DiskState deleted");
661
 
 
662
 
        debug("Deleting Options");
663
 
        if (g_pOptions)
664
 
        {
665
 
                if (g_pOptions->GetDaemonMode())
666
 
                {
667
 
                        info("Deleting lock file");
668
 
                        remove(g_pOptions->GetLockFile());
669
 
                }
670
 
                delete g_pOptions;
671
 
                g_pOptions = NULL;
672
 
        }
673
 
        debug("Options deleted");
674
 
 
675
 
        debug("Deleting ServerPool");
676
 
        if (g_pServerPool)
677
 
        {
678
 
                delete g_pServerPool;
679
 
                g_pServerPool = NULL;
680
 
        }
681
 
        debug("ServerPool deleted");
682
 
 
683
 
        debug("Deleting Scheduler");
684
 
        if (g_pScheduler)
685
 
        {
686
 
                delete g_pScheduler;
687
 
                g_pScheduler = NULL;
688
 
        }
689
 
        debug("Scheduler deleted");
690
 
 
691
 
        Connection::Final();
692
 
 
693
 
        Thread::Final();
694
 
 
695
 
        debug("Global objects cleaned up");
696
 
 
697
 
        if (g_pLog)
698
 
        {
699
 
                delete g_pLog;
700
 
                g_pLog = NULL;
701
 
        }
702
 
}
703
 
 
704
 
#ifndef WIN32
705
 
void Daemonize()
706
 
{
707
 
        int i, lfp;
708
 
        char str[10];
709
 
        if (getppid() == 1) return; /* already a daemon */
710
 
        i = fork();
711
 
        if (i < 0) exit(1); /* fork error */
712
 
        if (i > 0) exit(0); /* parent exits */
713
 
        /* child (daemon) continues */
714
 
        setsid(); /* obtain a new process group */
715
 
        for (i = getdtablesize();i >= 0;--i) close(i); /* close all descriptors */
716
 
        i = open("/dev/null", O_RDWR); dup(i); dup(i); /* handle standart I/O */
717
 
        chdir(g_pOptions->GetDestDir()); /* change running directory */
718
 
        lfp = open(g_pOptions->GetLockFile(), O_RDWR | O_CREAT, 0640);
719
 
        if (lfp < 0) exit(1); /* can not open */
720
 
        if (lockf(lfp, F_TLOCK, 0) < 0) exit(0); /* can not lock */
721
 
 
722
 
        /* Drop user if there is one, and we were run as root */
723
 
        if ( getuid() == 0 || geteuid() == 0 )
724
 
        {
725
 
                struct passwd *pw = getpwnam(g_pOptions->GetDaemonUserName());
726
 
                if (pw)
727
 
                {
728
 
                        fchown(lfp, pw->pw_uid, pw->pw_gid); /* change owner of lock file  */
729
 
                        setgroups( 0, (const gid_t*) 0 ); /* Set aux groups to null. */
730
 
                        setgid(pw->pw_gid); /* Set primary group. */
731
 
                        /* Try setting aux groups correctly - not critical if this fails. */
732
 
                        initgroups( g_pOptions->GetDaemonUserName(),pw->pw_gid); 
733
 
                        /* Finally, set uid. */
734
 
                        setuid(pw->pw_uid);
735
 
                }
736
 
        }
737
 
 
738
 
        /* first instance continues */
739
 
        sprintf(str, "%d\n", getpid());
740
 
        write(lfp, str, strlen(str)); /* record pid to lockfile */
741
 
        signal(SIGCHLD, SIG_IGN); /* ignore child */
742
 
        signal(SIGTSTP, SIG_IGN); /* ignore tty signals */
743
 
        signal(SIGTTOU, SIG_IGN);
744
 
        signal(SIGTTIN, SIG_IGN);
745
 
}
746
 
#endif
747
 
 
748
 
#ifndef DISABLE_PARCHECK
749
 
class NullStreamBuf : public std::streambuf
750
 
{
751
 
public:
752
 
        int sputc ( char c ) { return (int) c; }
753
 
} NullStreamBufInstance;
754
 
 
755
 
void DisableCout()
756
 
{
757
 
        // libpar2 prints messages to c++ standard output stream (std::cout).
758
 
        // However we do not want these messages to be printed.
759
 
        // Since we do not use std::cout in nzbget we just disable it.
760
 
        std::cout.rdbuf(&NullStreamBufInstance);
761
 
}
762
 
#endif