1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public
5
* License Version 1.1 (the "MPL"); you may not use this file
6
* except in compliance with the MPL. You may obtain a copy of
7
* the MPL at http://www.mozilla.org/MPL/
9
* Software distributed under the MPL is distributed on an "AS
10
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
* implied. See the MPL for the specific language governing
12
* rights and limitations under the MPL.
14
* The Original Code is protoZilla.
16
* The Initial Developer of the Original Code is Ramalingam Saravanan.
17
* Portions created by Ramalingam Saravanan <sarava@sarava.net> are
18
* Copyright (C) 2000 Ramalingam Saravanan. All Rights Reserved.
21
* Patrick Brunschwig <patrick@mozilla-enigmail.org>
23
* Alternatively, the contents of this file may be used under the terms of
24
* either the GNU General Public License Version 2 or later (the "GPL"), or
25
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26
* in which case the provisions of the GPL or the LGPL are applicable instead
27
* of those above. If you wish to allow use of your version of this file only
28
* under the terms of either the GPL or the LGPL, and not to allow others to
29
* use your version of this file under the terms of the MPL, indicate your
30
* decision by deleting the provisions above and replace them with the notice
31
* and other provisions required by the GPL or the LGPL. If you do not delete
32
* the provisions above, a recipient may use your version of this file under
33
* the terms of any one of the MPL, the GPL or the LGPL.
34
* ***** END LICENSE BLOCK ***** */
37
// Logging of debug output
38
// The following define statement should occur before any include statements
39
#define FORCE_PR_LOG /* Allow logging even in release build */
44
#include "mozilla/Mutex.h"
45
#include "nsIInputStream.h"
46
#include "nsIThread.h"
47
#include "nsIHttpChannel.h"
49
#include "nsNetUtil.h"
50
#include "nsStringGlue.h"
57
#include "nsPipeConsole.h"
60
PRLogModuleInfo* gPipeConsoleLog = NULL;
63
#define ERROR_LOG(args) PR_LOG(gPipeConsoleLog,PR_LOG_ERROR,args)
64
#define WARNING_LOG(args) PR_LOG(gPipeConsoleLog,PR_LOG_WARNING,args)
65
#define DEBUG_LOG(args) PR_LOG(gPipeConsoleLog,PR_LOG_DEBUG,args)
67
#define NS_PIPE_CONSOLE_BUFFER_SIZE (1024)
69
static const PRUint32 kCharMax = NS_PIPE_CONSOLE_BUFFER_SIZE;
71
///////////////////////////////////////////////////////////////////////////////
73
using namespace mozilla;
75
// nsPipeConsole implementation
77
// nsISupports implementation
78
NS_IMPL_THREADSAFE_ISUPPORTS4(nsPipeConsole,
85
// nsPipeConsole implementation
86
nsPipeConsole::nsPipeConsole()
87
: mFinalized(PR_FALSE),
89
mThreadJoined(PR_FALSE),
90
mOverflowed(PR_FALSE),
92
mLock("nsPipeConsole.lock"),
102
mPipeWrite(IPC_NULL_HANDLE),
103
mPipeRead(IPC_NULL_HANDLE),
110
if (gPipeConsoleLog == nsnull) {
111
gPipeConsoleLog = PR_NewLogModule("nsPipeConsole");
117
nsCOMPtr<nsIThread> myThread;
118
rv = ENIG_GET_THREAD(myThread);
119
DEBUG_LOG(("nsPipeConsole:: <<<<<<<<< CTOR(%p): myThread=%p\n",
120
this, myThread.get()));
125
nsPipeConsole::~nsPipeConsole()
129
nsCOMPtr<nsIThread> myThread;
130
rv = ENIG_GET_THREAD(myThread);
131
DEBUG_LOG(("nsPipeConsole:: >>>>>>>>> DTOR(%p): myThread=%p\n",
132
this, myThread.get()));
136
DEBUG_LOG(("nsPipeConsole::destructor: terminating mPipeTread\n"));
137
mPipeThread->Shutdown(); // ignore result, we need to shutdown anyway
138
DEBUG_LOG(("nsPipeConsole::destructor: done\n"));
139
mPipeThread = nsnull;
146
///////////////////////////////////////////////////////////////////////////////
147
// static functions used here
148
///////////////////////////////////////////////////////////////////////////////
150
PRStatus CreateInheritablePipe(IPCFileDesc* *readPipe,
151
IPCFileDesc* *writePipe,
158
//status = PR_NewTCPSocketPair(fd);
159
status = PR_CreatePipe(readPipe, writePipe);
160
if (status != PR_SUCCESS)
163
status = PR_SetFDInheritable(*readPipe, readInherit);
164
if (status != PR_SUCCESS)
167
status = PR_SetFDInheritable(*writePipe, writeInherit);
168
if (status != PR_SUCCESS)
175
// Security attributes for inheritable handles
176
SECURITY_ATTRIBUTES securityAttr;
177
securityAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
178
securityAttr.lpSecurityDescriptor = NULL;
179
securityAttr.bInheritHandle = TRUE;
182
HANDLE hReadPipe, hWritePipe;
183
bRetVal = CreatePipe( &hReadPipe, &hWritePipe,
191
// Make read handle uninheritable
192
bRetVal = DuplicateHandle( GetCurrentProcess(),
198
DUPLICATE_SAME_ACCESS);
199
CloseHandle(hReadPipe);
202
CloseHandle(hWritePipe);
205
hReadPipe = hPipeTem;
209
// Make write handle uninheritable
210
bRetVal = DuplicateHandle( GetCurrentProcess(),
216
DUPLICATE_SAME_ACCESS);
217
CloseHandle(hWritePipe);
220
CloseHandle(hReadPipe);
223
hWritePipe = hPipeTem;
226
*readPipe = (void*) hReadPipe;
227
*writePipe = (void*) hWritePipe;
234
#define EnigRead PR_Read
236
PRInt32 EnigRead(IPCFileDesc* fd, void *buf, PRInt32 amount)
240
if (ReadFile((HANDLE) fd,
248
DWORD dwLastError = GetLastError();
250
if (dwLastError == ERROR_BROKEN_PIPE)
258
#define EnigClose PR_Close
260
PRStatus EnigClose(IPCFileDesc* fd)
262
return (CloseHandle((HANDLE) fd)) ? PR_SUCCESS : PR_FAILURE;
266
///////////////////////////////////////////////////////////////////////////////
267
// nsPipeConsole methods:
268
///////////////////////////////////////////////////////////////////////////////
271
nsPipeConsole::Finalize(EMBool destructor)
273
DEBUG_LOG(("nsPipeConsole::Finalize: \n"));
278
mFinalized = PR_TRUE;
280
nsCOMPtr<nsIPipeConsole> self;
282
// Hold a reference to ourselves to prevent our DTOR from being called
283
// while finalizing. Automatically released upon returning.
289
EnigClose(mPipeWrite);
290
mPipeWrite = IPC_NULL_HANDLE;
293
// Release owning refs
295
mObserverContext = nsnull;
298
mConsoleBuf.Assign("");
301
mConsoleNewChars = 0;
303
mConsoleMaxLines = 0;
310
nsPipeConsole::Init()
312
DEBUG_LOG(("nsPipeConsole::Init: \n"));
314
// add shutdown observer
316
nsCOMPtr<nsIObserverService> observ(do_GetService("@mozilla.org/observer-service;1"));
318
observ->AddObserver((nsIObserver*)(this),
319
NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
324
///////////////////////////////////////////////////////////////////////////////
325
// nsIPipeConsole methods (thread-safe)
326
///////////////////////////////////////////////////////////////////////////////
329
nsPipeConsole::Open(PRInt32 maxRows, PRInt32 maxCols, EMBool joinable)
333
DEBUG_LOG(("nsPipeConsole::Open: %d, %d, %d\n", maxRows, maxCols,
336
NS_ENSURE_SUCCESS(rv, rv);
338
mJoinable = joinable;
340
if ((maxRows < 0) || (maxCols < 0))
341
return NS_ERROR_FAILURE;
343
mConsoleMaxLines = maxRows;
344
mConsoleMaxCols = ((maxCols > 0) && (maxCols < 3)) ? 3: maxCols;
347
PRStatus status = CreateInheritablePipe(&mPipeRead, &mPipeWrite,
349
if (status != PR_SUCCESS) {
350
ERROR_LOG(("nsPipeConsole::Open: CreateInheritablePipe failed\n"));
351
return NS_ERROR_FAILURE;
354
// Spin up a new thread to handle STDOUT polling
355
rv = NS_NewThread(getter_AddRefs(mPipeThread), this);
356
DEBUG_LOG(("nsPipeConsole::Open: created new thread: %d", rv));
357
NS_ENSURE_SUCCESS(rv, rv);
365
nsPipeConsole::HasNewData(EMBool *_retval)
367
MutexAutoLock lock(mLock);
369
//DEBUG_LOG(("nsPipeConsole::HasNewData:\n"));
371
*_retval = (mConsoleNewChars > 0);
378
nsPipeConsole::GetData(char** _retval)
380
DEBUG_LOG(("nsPipeConsole::GetData:\n"));
382
mConsoleNewChars = mConsoleBuf.Length();
384
return GetNewData(_retval);
389
nsPipeConsole::GetNewData(char** _retval)
391
MutexAutoLock lock(mLock);
393
DEBUG_LOG(("nsPipeConsole::GetNewData:\n"));
396
return NS_ERROR_NULL_POINTER;
398
// Compute offset of "new" portion of string
399
PRInt32 consoleLen = mConsoleBuf.Length();
400
PRInt32 offset = consoleLen - mConsoleNewChars;
402
if ((offset < 0) || (offset > consoleLen)) {
403
ERROR_LOG(("nsPipeConsole::GetData: Internal error - Invalid console offset"));
404
return NS_ERROR_FAILURE;
407
// Copy portion of console data to be returned
408
nsCAutoString consoleCopy (mConsoleBuf);
410
consoleCopy.Cut(0,offset);
412
// Replace any NULs with '0'
413
PRInt32 nulIndex = 0;
414
while (nulIndex != -1) {
415
nulIndex = consoleCopy.FindChar(char(0));
416
if (nulIndex != -1) {
417
consoleCopy.Replace(nulIndex, 1, "0", 1);
421
// Duplicate new C string
422
*_retval = ToNewCString(consoleCopy);
424
return NS_ERROR_OUT_OF_MEMORY;
426
mConsoleNewChars = 0;
431
///////////////////////////////////////////////////////////////////////////////
432
// nsIPipeListener methods (thread-safe)
433
///////////////////////////////////////////////////////////////////////////////
436
nsPipeConsole::Observe(nsIRequestObserver* observer, nsISupports* context)
438
MutexAutoLock lock(mLock);
439
DEBUG_LOG(("nsPipeConsole::Observe: %p, %p\n", observer, context));
441
mObserver = observer;
442
mObserverContext = context;
449
nsPipeConsole::GetJoinable(EMBool *_retval)
451
DEBUG_LOG(("nsPipeConsole::GetJoinable: %d\n", (int) mJoinable));
453
*_retval = mJoinable;
460
nsPipeConsole::Join()
465
return NS_ERROR_FAILURE;
468
// Nested lock to avoid deadlock while waiting for Join
469
MutexAutoLock lock(mLock);
470
DEBUG_LOG(("nsPipeConsole::Join:\n"));
472
if (mThreadJoined || !mPipeThread)
476
// Close write pipe before joining
477
EnigClose(mPipeWrite);
478
mPipeWrite = IPC_NULL_HANDLE;
481
// Assume that this join will succeed to prevent double joining
482
mThreadJoined = PR_TRUE;
485
DEBUG_LOG(("nsPipeConsole::terminating thread\n"));
486
rv = mPipeThread->Shutdown();
487
NS_ENSURE_SUCCESS(rv, rv);
489
if (rv == NS_OK) mPipeThread=nsnull;
496
nsPipeConsole::Shutdown()
498
MutexAutoLock lock(mLock);
499
DEBUG_LOG(("nsPipeConsole::Shutdown:\n"));
503
nsCOMPtr<nsIObserverService> observerSvc =
504
do_GetService("@mozilla.org/observer-service;1");
507
observerSvc->RemoveObserver((nsIObserver*)(this),
508
NS_XPCOM_SHUTDOWN_OBSERVER_ID);
516
nsPipeConsole::GetFileDesc(IPCFileDesc* *_retval)
518
MutexAutoLock lock(mLock);
520
DEBUG_LOG(("nsPipeConsole::GetFileDesc:\n"));
523
return NS_ERROR_NULL_POINTER;
525
if (mPipeWrite == IPC_NULL_HANDLE)
526
return NS_ERROR_FAILURE;
528
*_retval = mPipeWrite;
534
nsPipeConsole::GetOverflowed(EMBool *_retval)
536
MutexAutoLock lock(mLock);
538
DEBUG_LOG(("nsPipeConsole::GetOverflowed: %d\n", (int) mOverflowed));
540
*_retval = mOverflowed;
547
nsPipeConsole::GetByteData(PRUint32 *count, char **data)
549
MutexAutoLock lock(mLock);
551
DEBUG_LOG(("nsPipeConsole::GetByteData:\n"));
554
return NS_ERROR_NULL_POINTER;
557
*count = mConsoleBuf.Length();
558
*data = reinterpret_cast<char*>(nsMemory::Alloc((*count)+1));
560
return NS_ERROR_OUT_OF_MEMORY;
562
memcpy(*data, mConsoleBuf.get(), *count);
564
// NUL terminate byte array(just to be safe!)
565
(*data)[*count] = '\0';
567
mConsoleNewChars = 0;
574
nsPipeConsole::Write(const char* str)
576
// Note: Locking occurs in WriteBuf
578
DEBUG_LOG(("nsPipeConsole::Write: %s\n", str));
580
PRUint32 len = strlen(str);
584
return WriteBuf(str, len);
588
nsPipeConsole::WriteBuf(const char* buf, PRUint32 count)
590
MutexAutoLock lock(mLock);
592
DEBUG_LOG(("nsPipeConsole::WriteBuf: %d\n", count));
596
if ((count <= 0) || !mConsoleMaxLines)
599
PRInt32 consoleOldLen = mConsoleBuf.Length();
601
PRInt32 appendOffset = 0;
605
// Count and append new lines (folding extra-long lines)
606
for (j=0; j<(PRInt32)count; j++) {
607
if (buf[j] == '\n') {
612
} else if (mConsoleMaxCols &&
613
((int)mConsoleLineLen >= mConsoleMaxCols)) {
618
// Append characters upto this point
619
if (j > appendOffset)
620
mConsoleBuf.Append(buf+appendOffset, j-appendOffset);
623
mConsoleBuf.Append('\n');
633
// Append all remaining characters
634
mConsoleBuf.Append(buf+appendOffset, count-appendOffset);
636
PRInt32 deleteLines = mConsoleLines - mConsoleMaxLines;
638
PRInt32 consoleLen = mConsoleBuf.Length();
639
mConsoleNewChars += consoleLen - consoleOldLen;
641
if (deleteLines > 0) {
643
PRInt32 linesLocated = 0;
646
mOverflowed = PR_TRUE;
648
while ((offset < consoleLen) && (linesLocated < deleteLines)) {
649
newOffset = mConsoleBuf.FindChar('\n', offset);
650
if (newOffset == -1) break;
651
offset = newOffset + 1;
655
if (linesLocated != deleteLines) {
657
ERROR_LOG(("nsPipeConsole::WriteBuf: linesLocated(%d) != deleteLines(%d)\n", linesLocated, deleteLines));
659
return NS_ERROR_FAILURE;
662
mConsoleBuf.Cut(0,offset);
663
mConsoleLines -= deleteLines;
666
if (mConsoleNewChars > mConsoleBuf.Length())
667
mConsoleNewChars = mConsoleBuf.Length();
672
///////////////////////////////////////////////////////////////////////////////
673
// nsIRequestObserver methods
674
///////////////////////////////////////////////////////////////////////////////
677
nsPipeConsole::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
679
DEBUG_LOG(("nsPipeConsole::OnStartRequest:\n"));
681
nsCOMPtr<nsIRequestObserver> observer;
682
nsCOMPtr<nsISupports> observerContext;
684
MutexAutoLock lock(mLock);
689
observer = mObserver;
690
observerContext = mObserverContext;
693
return observer->OnStartRequest(aRequest, observerContext);
697
nsPipeConsole::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
700
DEBUG_LOG(("nsPipeConsole::OnStopRequest:\n"));
702
nsCOMPtr<nsIRequestObserver> observer;
703
nsCOMPtr<nsISupports> observerContext;
705
MutexAutoLock lock(mLock);
710
observer = mObserver;
711
observerContext = mObserverContext;
714
return observer->OnStopRequest(aRequest, observerContext, aStatus);
717
///////////////////////////////////////////////////////////////////////////////
718
// nsIStreamListener method
719
///////////////////////////////////////////////////////////////////////////////
722
nsPipeConsole::OnDataAvailable(nsIRequest* aRequest, nsISupports* aContext,
723
nsIInputStream *aInputStream,
724
PRUint32 aSourceOffset,
729
DEBUG_LOG(("nsPipeConsole::OnDataAVailable: %d\n", aLength));
732
PRUint32 readCount, readMax;
734
while (aLength > 0) {
735
readMax = (aLength < kCharMax) ? aLength : kCharMax;
736
rv = aInputStream->Read((char *) buf, readMax, &readCount);
738
ERROR_LOG(("nsPipeConsole::OnDataAvailable: Error in reading from input stream, %x\n", rv));
742
if (readCount <= 0) return NS_OK;
744
rv = WriteBuf(buf, readCount);
745
NS_ENSURE_SUCCESS(rv, rv);
747
aLength -= readCount;
753
///////////////////////////////////////////////////////////////////////////////
754
// nsIRunnable methods:
755
// (runs as a new thread)
756
///////////////////////////////////////////////////////////////////////////////
764
nsCOMPtr<nsIThread> myThread;
765
rv = ENIG_GET_THREAD(myThread);
766
DEBUG_LOG(("nsPipeConsole::Run: myThread=%p\n", myThread.get()));
774
// Read data from pipe (blocking)
775
readCount = EnigRead(mPipeRead, (char *) buf, kCharMax);
777
DEBUG_LOG(("nsPipeConsole::Run: Read %d chars\n", readCount));
782
// Append data read to console
783
WriteBuf(buf, readCount);
786
// Clear any NSPR interrupt
790
EnigClose(mPipeRead);
791
mPipeRead = IPC_NULL_HANDLE;
796
//-----------------------------------------------------------------------------
798
//-----------------------------------------------------------------------------
801
nsPipeConsole::Observe(nsISupports *subject, const char *aTopic, const PRUnichar *data)
803
DEBUG_LOG(("nsPipeConsole::Observe: topic=%s\n", aTopic));
805
if (!PL_strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {