1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the Netscape Portable Runtime (NSPR).
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998-2000
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
44
#if defined(AIX) || defined(QNX)
45
/* To pick up sysconf */
48
/* To pick up getrlimit, setrlimit */
50
#include <sys/resource.h>
54
extern PRLock *_pr_flock_lock;
55
extern PRCondVar *_pr_flock_cv;
57
static PRInt32 PR_CALLBACK FileRead(PRFileDesc *fd, void *buf, PRInt32 amount)
60
PRThread *me = _PR_MD_CURRENT_THREAD();
62
if (_PR_PENDING_INTERRUPT(me)) {
63
me->flags &= ~_PR_INTERRUPT;
64
PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
67
if (_PR_IO_PENDING(me)) {
68
PR_SetError(PR_IO_PENDING_ERROR, 0);
74
PR_LOG(_pr_io_lm, PR_LOG_MAX,
75
("read: fd=%p osfd=%d buf=%p amount=%d",
76
fd, fd ? fd->secret->md.osfd : 0, buf, amount));
78
rv = _PR_MD_READ(fd, buf, amount);
82
PR_LOG(_pr_io_lm, PR_LOG_MAX, ("read -> %d", rv));
86
static PRInt32 PR_CALLBACK FileWrite(PRFileDesc *fd, const void *buf, PRInt32 amount)
90
PRThread *me = _PR_MD_CURRENT_THREAD();
92
if (_PR_PENDING_INTERRUPT(me)) {
93
me->flags &= ~_PR_INTERRUPT;
94
PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
97
if (_PR_IO_PENDING(me)) {
98
PR_SetError(PR_IO_PENDING_ERROR, 0);
105
#if !defined(XP_UNIX) /* BugZilla: 4090 */
106
if ( PR_TRUE == fd->secret->appendMode ) {
107
rv = PR_Seek(fd, 0, PR_SEEK_END );
111
} /* if (fd->secret->appendMode...) */
114
PR_LOG(_pr_io_lm, PR_LOG_MAX,
115
("write: fd=%p osfd=%d buf=%p amount=%d",
116
fd, fd ? fd->secret->md.osfd : 0, buf, amount));
117
temp = _PR_MD_WRITE(fd, buf, amount);
123
if (fd->secret->nonblocking) {
126
buf = (const void*) ((const char*)buf + temp);
129
PR_LOG(_pr_io_lm, PR_LOG_MAX, ("write -> %d", count));
133
static PROffset32 PR_CALLBACK FileSeek(PRFileDesc *fd, PROffset32 offset, PRSeekWhence whence)
137
result = _PR_MD_LSEEK(fd, offset, whence);
141
static PROffset64 PR_CALLBACK FileSeek64(PRFileDesc *fd, PROffset64 offset, PRSeekWhence whence)
144
#pragma unused( fd, offset, whence )
148
result = _PR_MD_LSEEK64(fd, offset, whence);
152
static PRInt32 PR_CALLBACK FileAvailable(PRFileDesc *fd)
154
PRInt32 result, cur, end;
156
cur = _PR_MD_LSEEK(fd, 0, PR_SEEK_CUR);
159
end = _PR_MD_LSEEK(fd, 0, PR_SEEK_END);
161
if ((cur < 0) || (end < 0)) {
166
_PR_MD_LSEEK(fd, cur, PR_SEEK_SET);
171
static PRInt64 PR_CALLBACK FileAvailable64(PRFileDesc *fd)
176
PRInt64 result, cur, end;
179
LL_I2L(minus_one, -1);
180
cur = _PR_MD_LSEEK64(fd, LL_ZERO, PR_SEEK_CUR);
183
end = _PR_MD_LSEEK64(fd, LL_ZERO, PR_SEEK_END);
185
if (!LL_GE_ZERO(cur) || !LL_GE_ZERO(end)) return minus_one;
187
LL_SUB(result, end, cur);
188
(void)_PR_MD_LSEEK64(fd, cur, PR_SEEK_SET);
193
static PRInt32 PR_CALLBACK PipeAvailable(PRFileDesc *fd)
196
rv = _PR_MD_PIPEAVAILABLE(fd);
200
static PRInt64 PR_CALLBACK PipeAvailable64(PRFileDesc *fd)
203
LL_I2L(rv, _PR_MD_PIPEAVAILABLE(fd));
207
static PRStatus PR_CALLBACK PipeSync(PRFileDesc *fd)
216
static PRStatus PR_CALLBACK FileGetInfo(PRFileDesc *fd, PRFileInfo *info)
220
rv = _PR_MD_GETOPENFILEINFO(fd, info);
227
static PRStatus PR_CALLBACK FileGetInfo64(PRFileDesc *fd, PRFileInfo64 *info)
230
#pragma unused( fd, info )
232
/* $$$$ NOT YET IMPLEMENTED */
235
rv = _PR_MD_GETOPENFILEINFO64(fd, info);
236
if (rv < 0) return PR_FAILURE;
237
else return PR_SUCCESS;
240
static PRStatus PR_CALLBACK FileSync(PRFileDesc *fd)
243
result = _PR_MD_FSYNC(fd);
250
static PRStatus PR_CALLBACK FileClose(PRFileDesc *fd)
252
if (!fd || !fd->secret
253
|| (fd->secret->state != _PR_FILEDESC_OPEN
254
&& fd->secret->state != _PR_FILEDESC_CLOSED)) {
255
PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0);
259
if (fd->secret->state == _PR_FILEDESC_OPEN) {
260
if (_PR_MD_CLOSE_FILE(fd->secret->md.osfd) < 0) {
263
fd->secret->state = _PR_FILEDESC_CLOSED;
269
static PRInt16 PR_CALLBACK FilePoll(
270
PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags)
273
#pragma unused( fd, in_flags )
279
static PRIOMethods _pr_fileMethods = {
291
(PRWritevFN)_PR_InvalidInt,
292
(PRConnectFN)_PR_InvalidStatus,
293
(PRAcceptFN)_PR_InvalidDesc,
294
(PRBindFN)_PR_InvalidStatus,
295
(PRListenFN)_PR_InvalidStatus,
296
(PRShutdownFN)_PR_InvalidStatus,
297
(PRRecvFN)_PR_InvalidInt,
298
(PRSendFN)_PR_InvalidInt,
299
(PRRecvfromFN)_PR_InvalidInt,
300
(PRSendtoFN)_PR_InvalidInt,
302
(PRAcceptreadFN)_PR_InvalidInt,
303
(PRTransmitfileFN)_PR_InvalidInt,
304
(PRGetsocknameFN)_PR_InvalidStatus,
305
(PRGetpeernameFN)_PR_InvalidStatus,
306
(PRReservedFN)_PR_InvalidInt,
307
(PRReservedFN)_PR_InvalidInt,
308
(PRGetsocketoptionFN)_PR_InvalidStatus,
309
(PRSetsocketoptionFN)_PR_InvalidStatus,
310
(PRSendfileFN)_PR_InvalidInt,
311
(PRConnectcontinueFN)_PR_InvalidStatus,
312
(PRReservedFN)_PR_InvalidInt,
313
(PRReservedFN)_PR_InvalidInt,
314
(PRReservedFN)_PR_InvalidInt,
315
(PRReservedFN)_PR_InvalidInt
318
PR_IMPLEMENT(const PRIOMethods*) PR_GetFileMethods(void)
320
return &_pr_fileMethods;
323
static PRIOMethods _pr_pipeMethods = {
331
(PRSeekFN)_PR_InvalidInt,
332
(PRSeek64FN)_PR_InvalidInt64,
333
(PRFileInfoFN)_PR_InvalidStatus,
334
(PRFileInfo64FN)_PR_InvalidStatus,
335
(PRWritevFN)_PR_InvalidInt,
336
(PRConnectFN)_PR_InvalidStatus,
337
(PRAcceptFN)_PR_InvalidDesc,
338
(PRBindFN)_PR_InvalidStatus,
339
(PRListenFN)_PR_InvalidStatus,
340
(PRShutdownFN)_PR_InvalidStatus,
341
(PRRecvFN)_PR_InvalidInt,
342
(PRSendFN)_PR_InvalidInt,
343
(PRRecvfromFN)_PR_InvalidInt,
344
(PRSendtoFN)_PR_InvalidInt,
346
(PRAcceptreadFN)_PR_InvalidInt,
347
(PRTransmitfileFN)_PR_InvalidInt,
348
(PRGetsocknameFN)_PR_InvalidStatus,
349
(PRGetpeernameFN)_PR_InvalidStatus,
350
(PRReservedFN)_PR_InvalidInt,
351
(PRReservedFN)_PR_InvalidInt,
352
(PRGetsocketoptionFN)_PR_InvalidStatus,
353
(PRSetsocketoptionFN)_PR_InvalidStatus,
354
(PRSendfileFN)_PR_InvalidInt,
355
(PRConnectcontinueFN)_PR_InvalidStatus,
356
(PRReservedFN)_PR_InvalidInt,
357
(PRReservedFN)_PR_InvalidInt,
358
(PRReservedFN)_PR_InvalidInt,
359
(PRReservedFN)_PR_InvalidInt
362
PR_IMPLEMENT(const PRIOMethods*) PR_GetPipeMethods(void)
364
return &_pr_pipeMethods;
367
PR_IMPLEMENT(PRFileDesc*) PR_Open(const char *name, PRIntn flags, PRIntn mode)
371
#if !defined(XP_UNIX) /* BugZilla: 4090 */
372
PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE;
375
if (!_pr_initialized) _PR_ImplicitInitialization();
377
/* Map pr open flags and mode to os specific flags */
379
osfd = _PR_MD_OPEN(name, flags, mode);
381
fd = PR_AllocFileDesc(osfd, &_pr_fileMethods);
383
(void) _PR_MD_CLOSE_FILE(osfd);
385
#if !defined(XP_UNIX) /* BugZilla: 4090 */
386
fd->secret->appendMode = appendMode;
388
_PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE);
394
PR_IMPLEMENT(PRFileDesc*) PR_OpenFile(
395
const char *name, PRIntn flags, PRIntn mode)
399
#if !defined(XP_UNIX) /* BugZilla: 4090 */
400
PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE;
403
if (!_pr_initialized) _PR_ImplicitInitialization();
405
/* Map pr open flags and mode to os specific flags */
407
osfd = _PR_MD_OPEN_FILE(name, flags, mode);
409
fd = PR_AllocFileDesc(osfd, &_pr_fileMethods);
411
(void) _PR_MD_CLOSE_FILE(osfd);
413
#if !defined(XP_UNIX) /* BugZilla: 4090 */
414
fd->secret->appendMode = appendMode;
416
_PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE);
422
PRInt32 PR_GetSysfdTableMax(void)
424
#if defined(XP_UNIX) && !defined(AIX) && !defined(NEXTSTEP) && !defined(QNX)
427
if ( getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
428
/* XXX need to call PR_SetError() */
432
return rlim.rlim_max;
433
#elif defined(AIX) || defined(NEXTSTEP) || defined(QNX)
434
return sysconf(_SC_OPEN_MAX);
437
* There is a systemwide limit of 65536 user handles.
440
#elif defined (WIN16)
442
#elif defined(XP_OS2)
443
ULONG ulReqCount = 0;
444
ULONG ulCurMaxFH = 0;
445
DosSetRelMaxFH(&ulReqCount, &ulCurMaxFH);
447
#elif defined (XP_MAC) || defined(XP_BEOS)
448
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
455
PRInt32 PR_SetSysfdTableSize(int table_size)
457
#if defined(XP_UNIX) && !defined(AIX) && !defined(NEXTSTEP) && !defined(QNX)
459
PRInt32 tableMax = PR_GetSysfdTableMax();
464
if (tableMax > FD_SETSIZE)
465
tableMax = FD_SETSIZE;
467
rlim.rlim_max = tableMax;
469
/* Grow as much as we can; even if too big */
470
if ( rlim.rlim_max < table_size )
471
rlim.rlim_cur = rlim.rlim_max;
473
rlim.rlim_cur = table_size;
475
if ( setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
476
/* XXX need to call PR_SetError() */
480
return rlim.rlim_cur;
481
#elif defined(XP_OS2)
482
PRInt32 tableMax = PR_GetSysfdTableMax();
483
if (table_size > tableMax) {
484
APIRET rc = NO_ERROR;
485
rc = DosSetMaxFH(table_size);
492
#elif defined(AIX) || defined(NEXTSTEP) || defined(QNX) \
493
|| defined(WIN32) || defined(WIN16) || defined(XP_BEOS)
494
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
496
#elif defined (XP_MAC)
497
#pragma unused (table_size)
498
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
505
PR_IMPLEMENT(PRStatus) PR_Delete(const char *name)
509
rv = _PR_MD_DELETE(name);
516
PR_IMPLEMENT(PRStatus) PR_GetFileInfo(const char *fn, PRFileInfo *info)
520
rv = _PR_MD_GETFILEINFO(fn, info);
527
PR_IMPLEMENT(PRStatus) PR_GetFileInfo64(const char *fn, PRFileInfo64 *info)
530
#pragma unused (fn, info)
534
if (!_pr_initialized) _PR_ImplicitInitialization();
535
rv = _PR_MD_GETFILEINFO64(fn, info);
543
PR_IMPLEMENT(PRStatus) PR_Rename(const char *from, const char *to)
547
rv = _PR_MD_RENAME(from, to);
554
PR_IMPLEMENT(PRStatus) PR_Access(const char *name, PRAccessHow how)
558
rv = _PR_MD_ACCESS(name, how);
566
** Import an existing OS file to NSPR
568
PR_IMPLEMENT(PRFileDesc*) PR_ImportFile(PRInt32 osfd)
570
PRFileDesc *fd = NULL;
572
if (!_pr_initialized) _PR_ImplicitInitialization();
574
fd = PR_AllocFileDesc(osfd, &_pr_fileMethods);
576
(void) _PR_MD_CLOSE_FILE(osfd);
578
_PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE);
585
** Import an existing OS pipe to NSPR
587
PR_IMPLEMENT(PRFileDesc*) PR_ImportPipe(PRInt32 osfd)
589
PRFileDesc *fd = NULL;
591
if (!_pr_initialized) _PR_ImplicitInitialization();
593
fd = PR_AllocFileDesc(osfd, &_pr_pipeMethods);
595
(void) _PR_MD_CLOSE_FILE(osfd);
597
_PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE);
599
fd->secret->md.sync_file_io = PR_TRUE;
606
#ifndef NO_NSPR_10_SUPPORT
608
** PR_Stat() for Win16 is defined in w16io.c
609
** it is a hack to circumvent problems in Gromit and Java
610
** See also: BugSplat: 98516.
614
* This function is supposed to be for backward compatibility with
615
* nspr 1.0. Therefore, it still uses the nspr 1.0 error-reporting
616
* mechanism -- returns a PRInt32, which is the error code when the call
619
* If we need this function in nspr 2.0, it should be changed to
620
* return PRStatus, as follows:
622
* PR_IMPLEMENT(PRStatus) PR_Stat(const char *name, struct stat *buf)
626
* rv = _PR_MD_STAT(name, buf);
635
PR_IMPLEMENT(PRInt32) PR_Stat(const char *name, struct stat *buf)
639
rv = _PR_MD_STAT(name, buf);
643
#endif /* !defined(WIN16) */
644
#endif /* ! NO_NSPR_10_SUPPORT */
646
PR_IMPLEMENT(PRStatus) PR_LockFile(PRFileDesc *fd)
648
PRStatus status = PR_SUCCESS;
651
if (!fd->secret->md.io_model_committed) {
653
rv = _md_Associate((HANDLE)fd->secret->md.osfd);
655
fd->secret->md.io_model_committed = PR_TRUE;
659
PR_Lock(_pr_flock_lock);
660
while (fd->secret->lockCount == -1)
661
PR_WaitCondVar(_pr_flock_cv, PR_INTERVAL_NO_TIMEOUT);
662
if (fd->secret->lockCount == 0) {
663
fd->secret->lockCount = -1;
664
PR_Unlock(_pr_flock_lock);
665
status = _PR_MD_LOCKFILE(fd->secret->md.osfd);
666
PR_Lock(_pr_flock_lock);
667
fd->secret->lockCount = (status == PR_SUCCESS) ? 1 : 0;
668
PR_NotifyAllCondVar(_pr_flock_cv);
670
fd->secret->lockCount++;
672
PR_Unlock(_pr_flock_lock);
677
PR_IMPLEMENT(PRStatus) PR_TLockFile(PRFileDesc *fd)
679
PRStatus status = PR_SUCCESS;
682
if (!fd->secret->md.io_model_committed) {
684
rv = _md_Associate((HANDLE)fd->secret->md.osfd);
686
fd->secret->md.io_model_committed = PR_TRUE;
690
PR_Lock(_pr_flock_lock);
691
if (fd->secret->lockCount == 0) {
692
status = _PR_MD_TLOCKFILE(fd->secret->md.osfd);
693
PR_ASSERT(status == PR_SUCCESS || fd->secret->lockCount == 0);
694
if (status == PR_SUCCESS)
695
fd->secret->lockCount = 1;
697
fd->secret->lockCount++;
699
PR_Unlock(_pr_flock_lock);
704
PR_IMPLEMENT(PRStatus) PR_UnlockFile(PRFileDesc *fd)
706
PRStatus rv = PR_SUCCESS;
708
PR_Lock(_pr_flock_lock);
709
if (fd->secret->lockCount == 1) {
710
rv = _PR_MD_UNLOCKFILE(fd->secret->md.osfd);
711
if (rv == PR_SUCCESS)
712
fd->secret->lockCount = 0;
714
fd->secret->lockCount--;
716
PR_Unlock(_pr_flock_lock);
721
PR_IMPLEMENT(PRStatus) PR_CreatePipe(
722
PRFileDesc **readPipe,
723
PRFileDesc **writePipe
727
#pragma unused (readPipe, writePipe)
731
HANDLE readEnd, writeEnd;
732
SECURITY_ATTRIBUTES pipeAttributes;
734
if (!_pr_initialized) _PR_ImplicitInitialization();
736
ZeroMemory(&pipeAttributes, sizeof(pipeAttributes));
737
pipeAttributes.nLength = sizeof(pipeAttributes);
738
pipeAttributes.bInheritHandle = TRUE;
739
if (CreatePipe(&readEnd, &writeEnd, &pipeAttributes, 0) == 0) {
740
PR_SetError(PR_UNKNOWN_ERROR, GetLastError());
743
*readPipe = PR_AllocFileDesc((PRInt32)readEnd, &_pr_pipeMethods);
744
if (NULL == *readPipe) {
745
CloseHandle(readEnd);
746
CloseHandle(writeEnd);
749
*writePipe = PR_AllocFileDesc((PRInt32)writeEnd, &_pr_pipeMethods);
750
if (NULL == *writePipe) {
752
CloseHandle(writeEnd);
756
(*readPipe)->secret->md.sync_file_io = PR_TRUE;
757
(*writePipe)->secret->md.sync_file_io = PR_TRUE;
759
(*readPipe)->secret->inheritable = _PR_TRI_TRUE;
760
(*writePipe)->secret->inheritable = _PR_TRI_TRUE;
762
#elif defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
769
if (!_pr_initialized) _PR_ImplicitInitialization();
772
if (DosCreatePipe(&pipefd[0], &pipefd[1], 4096) != 0) {
774
if (pipe(pipefd) == -1) {
776
/* XXX map pipe error */
777
PR_SetError(PR_UNKNOWN_ERROR, errno);
780
*readPipe = PR_AllocFileDesc(pipefd[0], &_pr_pipeMethods);
781
if (NULL == *readPipe) {
786
*writePipe = PR_AllocFileDesc(pipefd[1], &_pr_pipeMethods);
787
if (NULL == *writePipe) {
792
#ifndef XP_BEOS /* Pipes are nonblocking on BeOS */
793
_PR_MD_MAKE_NONBLOCK(*readPipe);
795
_PR_MD_INIT_FD_INHERITABLE(*readPipe, PR_FALSE);
796
#ifndef XP_BEOS /* Pipes are nonblocking on BeOS */
797
_PR_MD_MAKE_NONBLOCK(*writePipe);
799
_PR_MD_INIT_FD_INHERITABLE(*writePipe, PR_FALSE);
802
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
808
/* ================ UTF16 Interfaces ================================ */
809
PR_IMPLEMENT(PRFileDesc*) PR_OpenFileUTF16(
810
const PRUnichar *name, PRIntn flags, PRIntn mode)
814
#if !defined(XP_UNIX) /* BugZilla: 4090 */
815
PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE;
818
if (!_pr_initialized) _PR_ImplicitInitialization();
820
/* Map pr open flags and mode to os specific flags */
821
osfd = _PR_MD_OPEN_FILE_UTF16(name, flags, mode);
823
fd = PR_AllocFileDesc(osfd, &_pr_fileMethods);
825
(void) _PR_MD_CLOSE_FILE(osfd);
827
#if !defined(XP_UNIX) /* BugZilla: 4090 */
828
fd->secret->appendMode = appendMode;
830
_PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE);
836
PR_IMPLEMENT(PRStatus) PR_GetFileInfo64UTF16(const PRUnichar *fn, PRFileInfo64 *info)
839
#pragma unused (fn, info)
843
if (!_pr_initialized) _PR_ImplicitInitialization();
844
rv = _PR_MD_GETFILEINFO64_UTF16(fn, info);
852
/* ================ UTF16 Interfaces ================================ */
853
#endif /* MOZ_UNICODE */