home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / include / md / _unixos.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  15.7 KB  |  455 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef prunixos_h___
  20. #define prunixos_h___
  21.  
  22. /*
  23.  * If FD_SETSIZE is not defined on the command line, set the default value
  24.  * before include select.h
  25.  */
  26. /*
  27.  * Linux: FD_SETSIZE is defined in /usr/include/sys/select.h and should
  28.  * not be redefined.
  29.  */
  30. #if !defined(LINUX)
  31. #ifndef FD_SETSIZE
  32. #define FD_SETSIZE  4096
  33. #endif
  34. #endif
  35.  
  36. #include <unistd.h>
  37. #include <stddef.h>
  38. #include <sys/stat.h>
  39. #include <dirent.h>
  40.  
  41. #include "prio.h"
  42. #include "prmem.h"
  43. #include "prclist.h"
  44.  
  45. /* To pick up fd_set */
  46. #if defined(HPUX)
  47. #include <sys/time.h>
  48. #elif defined(OSF1) || defined(AIX) || defined(SOLARIS) || defined(IRIX) \
  49.         || defined(UNIXWARE) || defined(NCR) || defined(SNI) || defined(NEC) \
  50.         || defined(BSDI) || defined(SONY)
  51. #include <sys/select.h>
  52. #elif defined(SUNOS4) || defined(SCO) || defined(FREEBSD)
  53. #include <sys/types.h>
  54. #elif defined(LINUX)
  55. #include <sys/time.h>
  56. #include <sys/types.h>
  57. #else
  58. #error Find out what include file defines fd_set on this platform
  59. #endif
  60.  
  61. #define PR_DIRECTORY_SEPARATOR        '/'
  62. #define PR_DIRECTORY_SEPARATOR_STR    "/"
  63. #define PR_PATH_SEPARATOR        ':'
  64. #define PR_PATH_SEPARATOR_STR        ":"
  65. #define GCPTR
  66. typedef int (*FARPROC)();
  67.  
  68. /*
  69.  * intervals at which GLOBAL threads wakeup to check for pending interrupt
  70.  */
  71. #define _PR_INTERRUPT_CHECK_INTERVAL_SECS 5
  72. extern PRIntervalTime intr_timeout_ticks;
  73.  
  74. typedef struct _PRUnixPollDesc {
  75.     PRInt32 osfd;
  76.     PRInt16 in_flags;
  77.     PRInt16 out_flags;
  78. } _PRUnixPollDesc;
  79.  
  80. typedef struct PRPollQueue {
  81.     PRCList links;        /* for linking PRPollQueue's together */
  82.     _PRUnixPollDesc *pds;        /* array of poll descriptors */
  83.     PRUintn npds;            /* length of the array */
  84.     PRPackedBool on_ioq;    /* is this on the async i/o work q? */
  85.     PRIntervalTime timeout;        /* timeout, in ticks */
  86.     struct PRThread *thr;
  87. } PRPollQueue;
  88.  
  89. #define _PR_POLLQUEUE_PTR(_qp) \
  90.     ((PRPollQueue*) ((char*) (_qp) - offsetof(PRPollQueue,links)))
  91.  
  92.  
  93. extern PRInt32 _PR_WaitForFD(PRInt32 osfd, PRUintn how,
  94.                     PRIntervalTime timeout);
  95. extern void _PR_Unblock_IO_Wait(struct PRThread *thr);
  96.  
  97. #if defined(_PR_LOCAL_THREADS_ONLY) || defined(_PR_GLOBAL_THREADS_ONLY)
  98. #define _MD_CHECK_FOR_EXIT()
  99. #endif
  100.  
  101. extern fd_set _pr_md_read_set, _pr_md_write_set, _pr_md_exception_set;
  102. extern PRInt16 _pr_md_read_cnt[], _pr_md_write_cnt[], _pr_md_exception_cnt[];
  103. extern PRInt32 _pr_md_ioq_max_osfd;
  104. extern PRUint32 _pr_md_ioq_timeout;
  105.  
  106. struct _MDFileDesc {
  107.     int osfd;
  108. };
  109.  
  110. struct _MDDir {
  111.     DIR *d;
  112. };
  113.  
  114. /*
  115.  * md-specific cpu structure field, common to all Unix platforms
  116.  */
  117. #define _PR_MD_MAX_OSFD FD_SETSIZE
  118.  
  119. struct _MDCPU_Unix {
  120.     PRCList ioQ;
  121.     fd_set fd_read_set, fd_write_set, fd_exception_set;
  122.     PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
  123.                 fd_exception_cnt[_PR_MD_MAX_OSFD];
  124.     PRUint32 ioq_timeout;
  125.     PRInt32 ioq_max_osfd;
  126. };
  127. struct _PRCPU;
  128. extern void _MD_unix_init_running_cpu(struct _PRCPU *cpu);
  129.  
  130. #define _PR_IOQ(_cpu)            ((_cpu)->md.md_unix.ioQ)
  131. #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
  132. #define _PR_FD_READ_SET(_cpu)        ((_cpu)->md.md_unix.fd_read_set)
  133. #define _PR_FD_READ_CNT(_cpu)        ((_cpu)->md.md_unix.fd_read_cnt)
  134. #define _PR_FD_WRITE_SET(_cpu)        ((_cpu)->md.md_unix.fd_write_set)
  135. #define _PR_FD_WRITE_CNT(_cpu)        ((_cpu)->md.md_unix.fd_write_cnt)
  136. #define _PR_FD_EXCEPTION_SET(_cpu)    ((_cpu)->md.md_unix.fd_exception_set)
  137. #define _PR_FD_EXCEPTION_CNT(_cpu)    ((_cpu)->md.md_unix.fd_exception_cnt)
  138. #define _PR_IOQ_TIMEOUT(_cpu)        ((_cpu)->md.md_unix.ioq_timeout)
  139. #define _PR_IOQ_MAX_OSFD(_cpu)        ((_cpu)->md.md_unix.ioq_max_osfd)
  140.  
  141.  
  142. /*
  143. ** Make a redzone at both ends of the stack segment. Disallow access
  144. ** to those pages of memory. It's ok if the mprotect call's don't
  145. ** work - it just means that we don't really have a functional
  146. ** redzone.
  147. */
  148. #if defined(DEBUG)
  149. #if !defined(SOLARIS)    
  150. #define _MD_INIT_STACK(ts,REDZONE)                    \
  151.     PR_BEGIN_MACRO                                     \
  152.     (void) mprotect((void*)ts->seg->vaddr, REDZONE, PROT_NONE);    \
  153.     (void) mprotect((void*) ((char*)ts->seg->vaddr + REDZONE + ts->stackSize),\
  154.             REDZONE, PROT_NONE);                \
  155.     /*                                    \
  156.     ** Fill stack memory with something that turns into an illegal    \
  157.     ** pointer value. This will sometimes find runtime references to    \
  158.     ** uninitialized pointers. We don't do this for solaris because we    \
  159.     ** can use purify instead.                        \
  160.     */                                    \
  161.     if (_pr_debugStacks) {                        \
  162.     memset(ts->allocBase + REDZONE, 0xf7, ts->stackSize);        \
  163.     }                                    \
  164.     PR_END_MACRO
  165. #else    /* !SOLARIS    */
  166. #define _MD_INIT_STACK(ts,REDZONE)                    \
  167.     PR_BEGIN_MACRO                                     \
  168.     (void) mprotect((void*)ts->seg->vaddr, REDZONE, PROT_NONE);    \
  169.     (void) mprotect((void*) ((char*)ts->seg->vaddr + REDZONE + ts->stackSize),\
  170.             REDZONE, PROT_NONE);                \
  171.     PR_END_MACRO
  172. #endif    /* !SOLARIS    */
  173.  
  174. /*
  175.  * _MD_CLEAR_STACK
  176.  *    Allow access to the redzone pages; the access was turned off in
  177.  *    _MD_INIT_STACK.
  178.  */
  179. #define _MD_CLEAR_STACK(ts)                        \
  180.     PR_BEGIN_MACRO                                     \
  181.     (void) mprotect((void*)ts->seg->vaddr, REDZONE, PROT_READ|PROT_WRITE);\
  182.     (void) mprotect((void*) ((char*)ts->seg->vaddr + REDZONE + ts->stackSize),\
  183.             REDZONE, PROT_READ|PROT_WRITE);            \
  184.     PR_END_MACRO
  185.  
  186. #else    /* DEBUG */
  187.  
  188. #define _MD_INIT_STACK(ts,REDZONE)
  189. #define _MD_CLEAR_STACK(ts)
  190.  
  191. #endif    /* DEBUG */
  192.  
  193. #if !defined(SOLARIS) 
  194.  
  195. #define PR_SET_INTSOFF(newval)
  196.  
  197. #endif
  198.  
  199. /************************************************************************/
  200.  
  201. extern void _PR_UnixInit(void);
  202.  
  203. /************************************************************************/
  204.  
  205. struct _MDProcess {
  206.     pid_t pid;
  207. };
  208.  
  209. struct PRProcess;
  210. struct PRProcessAttr;
  211.  
  212. /* Create a new process (fork() + exec()) */
  213. #define _MD_CREATE_PROCESS _MD_CreateUnixProcess
  214. extern struct PRProcess * _MD_CreateUnixProcess(
  215.     const char *path,
  216.     char *const *argv,
  217.     char *const *envp,
  218.     const struct PRProcessAttr *attr
  219. );
  220.  
  221. #define _MD_DETACH_PROCESS _MD_DetachUnixProcess
  222. extern PRStatus _MD_DetachUnixProcess(struct PRProcess *process);
  223.  
  224. /* Wait for a child process to terminate */
  225. #define _MD_WAIT_PROCESS _MD_WaitUnixProcess
  226. extern PRStatus _MD_WaitUnixProcess(struct PRProcess *process,
  227.     PRInt32 *exitCode);
  228.  
  229. #define _MD_KILL_PROCESS _MD_KillUnixProcess
  230. extern PRStatus _MD_KillUnixProcess(struct PRProcess *process);
  231.  
  232. /************************************************************************/
  233.  
  234. #define _MD_START_INTERRUPTS            _MD_StartInterrupts
  235. #define _MD_STOP_INTERRUPTS                _MD_StopInterrupts
  236. #define _MD_DISABLE_CLOCK_INTERRUPTS    _MD_DisableClockInterrupts
  237. #define _MD_BLOCK_CLOCK_INTERRUPTS        _MD_BlockClockInterrupts
  238. #define _MD_UNBLOCK_CLOCK_INTERRUPTS    _MD_UnblockClockInterrupts
  239.  
  240. /************************************************************************/
  241.  
  242. extern void        _MD_InitCPUS(void);
  243. #define _MD_INIT_CPUS           _MD_InitCPUS
  244.  
  245. extern void        _MD_Wakeup_CPUs(void);
  246. #define _MD_WAKEUP_CPUS _MD_Wakeup_CPUs
  247.  
  248. #define _MD_PAUSE_CPU            _MD_PauseCPU
  249.  
  250. #if defined(_PR_LOCAL_THREADS_ONLY) || defined(_PR_GLOBAL_THREADS_ONLY)
  251. #define _MD_CLEANUP_BEFORE_EXIT()
  252. #endif
  253.  
  254. #ifndef IRIX
  255. #define _MD_EXIT(status)        _exit(status)
  256. #endif
  257.  
  258. /************************************************************************/
  259.  
  260. #define _MD_GET_ENV                getenv
  261. #define _MD_PUT_ENV                putenv
  262.  
  263. /************************************************************************/
  264.  
  265. extern void        _MD_MakeNonblock(PRFileDesc *fd);
  266. #define _MD_MAKE_NONBLOCK            _MD_MakeNonblock        
  267.  
  268. /************************************************************************/
  269.  
  270. extern PRStatus    _MD_AllocSegment(PRSegment *seg, PRUint32 size,
  271.                 void *vaddr);
  272. extern void        _MD_FreeSegment(PRSegment *seg);
  273.  
  274. #define _MD_INIT_SEGS            _MD_InitSegs
  275. #define _MD_ALLOC_SEGMENT        _MD_AllocSegment
  276. #define _MD_FREE_SEGMENT        _MD_FreeSegment
  277.  
  278. /************************************************************************/
  279.  
  280. #define _MD_INTERVAL_INIT()
  281. #define _MD_INTERVAL_PER_MILLISEC()    (_PR_MD_INTERVAL_PER_SEC() / 1000)
  282. #define _MD_INTERVAL_PER_MICROSEC()    (_PR_MD_INTERVAL_PER_SEC() / 1000000)
  283.  
  284. /************************************************************************/
  285.  
  286. #define _MD_ERRNO()                 (errno)
  287. #define _MD_GET_SOCKET_ERROR()        (errno)
  288.  
  289. /************************************************************************/
  290.  
  291. extern PRInt32 _MD_AvailableSocket(PRInt32 osfd);
  292.  
  293. #include <errno.h>
  294. #include <sys/types.h>
  295. #include <sys/time.h>
  296. #include <sys/stat.h>
  297.  
  298. extern void _MD_InitSegs(void);
  299. extern void _MD_StartInterrupts(void);
  300. extern void _MD_StopInterrupts(void);
  301. extern void _MD_DisableClockInterrupts(void);
  302. extern void _MD_BlockClockInterrupts(void);
  303. extern void _MD_UnblockClockInterrupts(void);
  304. extern void _MD_PauseCPU(PRIntervalTime timeout);
  305.  
  306. extern PRStatus _MD_open_dir(struct _MDDir *, const char *);
  307. extern PRInt32  _MD_close_dir(struct _MDDir *);
  308. extern char *   _MD_read_dir(struct _MDDir *, PRIntn);
  309. extern PRInt32  _MD_open(const char *name, PRIntn osflags, PRIntn mode);
  310. extern PRInt32    _MD_delete(const char *name);
  311. extern PRInt32    _MD_getfileinfo(const char *fn, PRFileInfo *info);
  312. extern PRInt32  _MD_getopenfileinfo(const PRFileDesc *fd, PRFileInfo *info);
  313. extern PRInt32  _MD_getopenfileinfo64(const PRFileDesc *fd, PRFileInfo64 *info);
  314. extern PRInt32    _MD_rename(const char *from, const char *to);
  315. extern PRInt32    _MD_access(const char *name, PRIntn how);
  316. extern PRInt32    _MD_mkdir(const char *name, PRIntn mode);
  317. extern PRInt32    _MD_rmdir(const char *name);
  318. extern PRInt32    _MD_accept_read(PRInt32 sock, PRInt32 *newSock,
  319.                 PRNetAddr **raddr, void *buf, PRInt32 amount);
  320. extern PRInt32     _PR_UnixTransmitFile(PRFileDesc *sd, PRFileDesc *fd,
  321.             const void *headers, PRInt32 hlen,
  322.             PRTransmitFileFlags flags, PRIntervalTime timeout);
  323.  
  324. extern PRStatus _MD_LockFile(PRInt32 osfd);
  325. extern PRStatus _MD_TLockFile(PRInt32 osfd);
  326. extern PRStatus _MD_UnlockFile(PRInt32 osfd);
  327.  
  328. #define _MD_OPEN_DIR(dir, name)            _MD_open_dir(dir, name)
  329. #define _MD_CLOSE_DIR(dir)                _MD_close_dir(dir)
  330. #define _MD_READ_DIR(dir, flags)        _MD_read_dir(dir, flags)
  331. #define _MD_OPEN(name, osflags, mode    )    _MD_open(name, osflags, mode)
  332. extern PRInt32 _MD_read(PRFileDesc *fd, void *buf, PRInt32 amount);
  333. #define _MD_READ(fd,buf,amount)            _MD_read(fd,buf,amount)
  334. extern PRInt32 _MD_write(PRFileDesc *fd, const void *buf, PRInt32 amount);
  335. #define _MD_WRITE(fd,buf,amount)        _MD_write(fd,buf,amount)
  336. #define _MD_DELETE(name)                _MD_delete(name)
  337. #define _MD_GETFILEINFO(fn, info)        _MD_getfileinfo(fn, info)
  338. #define _MD_GETFILEINFO64(fn, info)        _MD_getfileinfo64(fn, info)
  339. #define _MD_GETOPENFILEINFO(fd, info)    _MD_getopenfileinfo(fd, info)
  340. #define _MD_GETOPENFILEINFO64(fd, info)    _MD_getopenfileinfo64(fd, info)
  341. #define _MD_RENAME(from, to)            _MD_rename(from, to)
  342. #define _MD_ACCESS(name, how)            _MD_access(name, how)
  343. #define _MD_MKDIR(name, mode)            _MD_mkdir(name, mode)
  344. #define _MD_RMDIR(name)                    _MD_rmdir(name)
  345. #define _MD_ACCEPT_READ(sock, newSock, raddr, buf, amount)    _MD_accept_read(sock, newSock, raddr, buf, amount)
  346.  
  347. #define _MD_LOCKFILE _MD_LockFile
  348. #define _MD_TLOCKFILE _MD_TLockFile
  349. #define _MD_UNLOCKFILE _MD_UnlockFile
  350.  
  351.  
  352. extern PRInt32        _MD_socket(int af, int type, int flags);
  353. #define _MD_SOCKET    _MD_socket
  354. extern PRInt32        _MD_connect(PRFileDesc *fd, const PRNetAddr *addr,
  355.                                 PRUint32 addrlen, PRIntervalTime timeout);
  356. #define _MD_CONNECT    _MD_connect
  357. extern PRInt32        _MD_accept(PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen,
  358.                                                     PRIntervalTime timeout);
  359. #define _MD_ACCEPT    _MD_accept
  360. extern PRInt32        _MD_bind(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen);
  361. #define _MD_BIND    _MD_bind
  362. extern PRInt32        _MD_listen(PRFileDesc *fd, PRIntn backlog);
  363. #define _MD_LISTEN    _MD_listen
  364. extern PRInt32        _MD_shutdown(PRFileDesc *fd, PRIntn how);
  365. #define _MD_SHUTDOWN    _MD_shutdown
  366.  
  367. extern PRInt32        _MD_recv(PRFileDesc *fd, void *buf, PRInt32 amount, 
  368.                                PRIntn flags, PRIntervalTime timeout);
  369. #define _MD_RECV    _MD_recv
  370. extern PRInt32        _MD_send(PRFileDesc *fd, const void *buf, PRInt32 amount,
  371.                                     PRIntn flags, PRIntervalTime timeout);
  372. #define _MD_SEND    _MD_send
  373. extern PRInt32        _MD_recvfrom(PRFileDesc *fd, void *buf, PRInt32 amount,
  374.                         PRIntn flags, PRNetAddr *addr, PRUint32 *addrlen,
  375.                                             PRIntervalTime timeout);
  376. #define _MD_RECVFROM    _MD_recvfrom
  377. extern PRInt32 _MD_sendto(PRFileDesc *fd, const void *buf, PRInt32 amount,
  378.                             PRIntn flags, const PRNetAddr *addr, PRUint32 addrlen,
  379.                                                 PRIntervalTime timeout);
  380. #define _MD_SENDTO    _MD_sendto
  381. extern PRInt32        _MD_writev(PRFileDesc *fd, struct PRIOVec *iov,
  382.                                 PRInt32 iov_size, PRIntervalTime timeout);
  383. #define _MD_WRITEV    _MD_writev
  384.  
  385. extern PRInt32        _MD_socketavailable(PRFileDesc *fd);
  386. #define    _MD_SOCKETAVAILABLE        _MD_socketavailable
  387. extern PRInt64        _MD_socketavailable64(PRFileDesc *fd);
  388. #define    _MD_SOCKETAVAILABLE64        _MD_socketavailable64
  389.  
  390. extern PRInt32 _MD_pr_poll(PRPollDesc *pds, PRIntn npds,
  391.                                                 PRIntervalTime timeout);
  392. #define _MD_PR_POLL        _MD_pr_poll
  393.  
  394. extern PRInt32        _MD_close(PRInt32 osfd);
  395. #define _MD_CLOSE_FILE    _MD_close
  396. extern PRInt32        _MD_lseek(PRFileDesc*, PRInt32, PRSeekWhence);
  397. #define _MD_LSEEK    _MD_lseek
  398. extern PRInt64        _MD_lseek64(PRFileDesc*, PRInt64, PRSeekWhence);
  399. #define _MD_LSEEK64    _MD_lseek64
  400. extern PRInt32        _MD_fsync(PRFileDesc *fd);
  401. #define _MD_FSYNC    _MD_fsync
  402.  
  403. extern PRInt32 _MD_socketpair(int af, int type, int flags, PRInt32 *osfd);
  404. #define _MD_SOCKETPAIR        _MD_socketpair
  405.  
  406. #define _MD_CLOSE_SOCKET    _MD_close
  407.  
  408. #ifndef NO_NSPR_10_SUPPORT
  409. #define _MD_STAT    stat
  410. #endif
  411.  
  412. extern PRStatus _MD_getpeername(PRFileDesc *fd, PRNetAddr *addr,
  413.                                             PRUint32 *addrlen);
  414. #define _MD_GETPEERNAME _MD_getpeername
  415. extern PRStatus _MD_getsockname(PRFileDesc *fd, PRNetAddr *addr,
  416.                                             PRUint32 *addrlen);
  417. #define _MD_GETSOCKNAME _MD_getsockname
  418.  
  419. extern PRStatus _MD_getsockopt(PRFileDesc *fd, PRInt32 level,
  420.                         PRInt32 optname, char* optval, PRInt32* optlen);
  421. #define _MD_GETSOCKOPT        _MD_getsockopt
  422. extern PRStatus _MD_setsockopt(PRFileDesc *fd, PRInt32 level,
  423.                     PRInt32 optname, const char* optval, PRInt32 optlen);
  424. #define _MD_SETSOCKOPT        _MD_setsockopt
  425.  
  426. extern PRStatus _MD_gethostname(char *name, PRUint32 namelen);
  427. #define _MD_GETHOSTNAME        _MD_gethostname
  428.  
  429. extern int _MD_unix_get_nonblocking_connect_error(int osfd);
  430.  
  431. #define HAVE_SOCKET_REUSEADDR
  432. #define HAVE_SOCKET_KEEPALIVE
  433.  
  434. /* Memory-mapped files */
  435.  
  436. struct _MDFileMap {
  437.     PRIntn prot;
  438.     PRIntn flags;
  439. };
  440.  
  441. extern PRStatus _MD_CreateFileMap(struct PRFileMap *fmap, PRInt64 size);
  442. #define _MD_CREATE_FILE_MAP _MD_CreateFileMap
  443.  
  444. extern void * _MD_MemMap(struct PRFileMap *fmap, PRInt64 offset,
  445.         PRUint32 len);
  446. #define _MD_MEM_MAP _MD_MemMap
  447.  
  448. extern PRStatus _MD_MemUnmap(void *addr, PRUint32 size);
  449. #define _MD_MEM_UNMAP _MD_MemUnmap
  450.  
  451. extern PRStatus _MD_CloseFileMap(struct PRFileMap *fmap);
  452. #define _MD_CLOSE_FILE_MAP _MD_CloseFileMap
  453.  
  454. #endif /* prunixos_h___ */
  455.