Set the IO manager pipe descriptors to FD_CLOEXEC
[packages/base.git] / include / HsBase.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The University of Glasgow 2001-2004
4 *
5 * Definitions for package `base' which are visible in Haskell land.
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #ifndef __HSBASE_H__
10 #define __HSBASE_H__
11
12 #ifdef __NHC__
13 # include "Nhc98BaseConfig.h"
14 #else
15 #include "HsBaseConfig.h"
16 #endif
17
18 /* ultra-evil... */
19 #undef PACKAGE_BUGREPORT
20 #undef PACKAGE_NAME
21 #undef PACKAGE_STRING
22 #undef PACKAGE_TARNAME
23 #undef PACKAGE_VERSION
24
25 /* Needed to get the macro version of errno on some OSs (eg. Solaris).
26 We must do this, because these libs are only compiled once, but
27 must work in both single-threaded and multi-threaded programs. */
28 #define _REENTRANT 1
29
30 #include "HsFFI.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <math.h>
35
36 #if HAVE_SYS_TYPES_H
37 #include <sys/types.h>
38 #endif
39 #if HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #if HAVE_SYS_STAT_H
43 #include <sys/stat.h>
44 #endif
45 #if HAVE_FCNTL_H
46 # include <fcntl.h>
47 #endif
48 #if HAVE_TERMIOS_H
49 #include <termios.h>
50 #endif
51 #if HAVE_SIGNAL_H
52 #include <signal.h>
53 /* Ultra-ugly: OpenBSD uses broken macros for sigemptyset and sigfillset (missing casts) */
54 #if __OpenBSD__
55 #undef sigemptyset
56 #undef sigfillset
57 #endif
58 #endif
59 #if HAVE_ERRNO_H
60 #include <errno.h>
61 #endif
62 #if HAVE_STRING_H
63 #include <string.h>
64 #endif
65 #if HAVE_DIRENT_H
66 #include <dirent.h>
67 #endif
68 #if HAVE_UTIME_H
69 #include <utime.h>
70 #endif
71 #if HAVE_SYS_UTSNAME_H
72 #include <sys/utsname.h>
73 #endif
74 #if HAVE_GETTIMEOFDAY
75 # if HAVE_SYS_TIME_H
76 # include <sys/time.h>
77 # endif
78 #elif HAVE_GETCLOCK
79 # if HAVE_SYS_TIMERS_H
80 # define POSIX_4D9 1
81 # include <sys/timers.h>
82 # endif
83 #endif
84 #if HAVE_TIME_H
85 #include <time.h>
86 #endif
87 #if HAVE_SYS_TIMEB_H
88 #include <sys/timeb.h>
89 #endif
90 #if HAVE_WINDOWS_H
91 #include <windows.h>
92 #endif
93 #if HAVE_SYS_TIMES_H
94 #include <sys/times.h>
95 #endif
96 #if HAVE_WINSOCK_H && defined(__MINGW32__)
97 #include <winsock.h>
98 #endif
99 #if HAVE_LIMITS_H
100 #include <limits.h>
101 #endif
102 #if HAVE_WCTYPE_H
103 #include <wctype.h>
104 #endif
105 #if HAVE_INTTYPES_H
106 # include <inttypes.h>
107 #elif HAVE_STDINT_H
108 # include <stdint.h>
109 #endif
110
111 #if !defined(__MINGW32__) && !defined(irix_HOST_OS)
112 # if HAVE_SYS_RESOURCE_H
113 # include <sys/resource.h>
114 # endif
115 #endif
116
117 #if !HAVE_GETRUSAGE && HAVE_SYS_SYSCALL_H
118 # include <sys/syscall.h>
119 # if defined(SYS_GETRUSAGE) /* hpux_HOST_OS */
120 # define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
121 # define HAVE_GETRUSAGE 1
122 # endif
123 #endif
124
125 /* For System */
126 #if HAVE_SYS_WAIT_H
127 #include <sys/wait.h>
128 #endif
129 #if HAVE_VFORK_H
130 #include <vfork.h>
131 #endif
132 #include "dirUtils.h"
133 #include "WCsubst.h"
134
135 #if defined(__MINGW32__)
136 /* in Win32Utils.c */
137 extern void maperrno (void);
138 extern HsWord64 getUSecOfDay(void);
139 #endif
140
141 #if defined(__MINGW32__)
142 #include <io.h>
143 #include <fcntl.h>
144 #include <shlobj.h>
145 #include <share.h>
146 #endif
147
148 #if HAVE_SYS_SELECT_H
149 #include <sys/select.h>
150 #endif
151
152 /* in inputReady.c */
153 extern int fdReady(int fd, int write, int msecs, int isSock);
154
155 /* in Signals.c */
156 extern HsInt nocldstop;
157
158 /* -----------------------------------------------------------------------------
159 64-bit operations, defined in longlong.c
160 -------------------------------------------------------------------------- */
161
162 #ifdef SUPPORT_LONG_LONGS
163
164 HsBool hs_gtWord64 (HsWord64, HsWord64);
165 HsBool hs_geWord64 (HsWord64, HsWord64);
166 HsBool hs_eqWord64 (HsWord64, HsWord64);
167 HsBool hs_neWord64 (HsWord64, HsWord64);
168 HsBool hs_ltWord64 (HsWord64, HsWord64);
169 HsBool hs_leWord64 (HsWord64, HsWord64);
170
171 HsBool hs_gtInt64 (HsInt64, HsInt64);
172 HsBool hs_geInt64 (HsInt64, HsInt64);
173 HsBool hs_eqInt64 (HsInt64, HsInt64);
174 HsBool hs_neInt64 (HsInt64, HsInt64);
175 HsBool hs_ltInt64 (HsInt64, HsInt64);
176 HsBool hs_leInt64 (HsInt64, HsInt64);
177
178 HsWord64 hs_remWord64 (HsWord64, HsWord64);
179 HsWord64 hs_quotWord64 (HsWord64, HsWord64);
180
181 HsInt64 hs_remInt64 (HsInt64, HsInt64);
182 HsInt64 hs_quotInt64 (HsInt64, HsInt64);
183 HsInt64 hs_negateInt64 (HsInt64);
184 HsInt64 hs_plusInt64 (HsInt64, HsInt64);
185 HsInt64 hs_minusInt64 (HsInt64, HsInt64);
186 HsInt64 hs_timesInt64 (HsInt64, HsInt64);
187
188 HsWord64 hs_and64 (HsWord64, HsWord64);
189 HsWord64 hs_or64 (HsWord64, HsWord64);
190 HsWord64 hs_xor64 (HsWord64, HsWord64);
191 HsWord64 hs_not64 (HsWord64);
192
193 HsWord64 hs_uncheckedShiftL64 (HsWord64, HsInt);
194 HsWord64 hs_uncheckedShiftRL64 (HsWord64, HsInt);
195 HsInt64 hs_uncheckedIShiftL64 (HsInt64, HsInt);
196 HsInt64 hs_uncheckedIShiftRA64 (HsInt64, HsInt);
197 HsInt64 hs_uncheckedIShiftRL64 (HsInt64, HsInt);
198
199 HsInt64 hs_intToInt64 (HsInt);
200 HsInt hs_int64ToInt (HsInt64);
201 HsWord64 hs_int64ToWord64 (HsInt64);
202 HsWord64 hs_wordToWord64 (HsWord);
203 HsWord hs_word64ToWord (HsWord64);
204 HsInt64 hs_word64ToInt64 (HsWord64);
205
206 HsWord64 hs_integerToWord64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
207 HsInt64 hs_integerToInt64 (HsInt sa, StgByteArray /* Really: mp_limb_t* */ da);
208
209 #endif /* SUPPORT_LONG_LONGS */
210
211 /* -----------------------------------------------------------------------------
212 INLINE functions.
213
214 These functions are given as inlines here for when compiling via C,
215 but we also generate static versions into the cbits library for
216 when compiling to native code.
217 -------------------------------------------------------------------------- */
218
219 #ifndef INLINE
220 # if defined(_MSC_VER)
221 # define INLINE extern __inline
222 # else
223 # define INLINE static inline
224 # endif
225 #endif
226
227 INLINE int __hscore_get_errno(void) { return errno; }
228 INLINE void __hscore_set_errno(int e) { errno = e; }
229
230 #if !defined(_MSC_VER)
231 INLINE int __hscore_s_isreg(mode_t m) { return S_ISREG(m); }
232 INLINE int __hscore_s_isdir(mode_t m) { return S_ISDIR(m); }
233 INLINE int __hscore_s_isfifo(mode_t m) { return S_ISFIFO(m); }
234 INLINE int __hscore_s_isblk(mode_t m) { return S_ISBLK(m); }
235 INLINE int __hscore_s_ischr(mode_t m) { return S_ISCHR(m); }
236 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
237 INLINE int __hscore_s_issock(mode_t m) { return S_ISSOCK(m); }
238 #endif
239 #endif
240
241 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
242 INLINE int
243 __hscore_sigemptyset( sigset_t *set )
244 { return sigemptyset(set); }
245
246 INLINE int
247 __hscore_sigfillset( sigset_t *set )
248 { return sigfillset(set); }
249
250 INLINE int
251 __hscore_sigaddset( sigset_t * set, int s )
252 { return sigaddset(set,s); }
253
254 INLINE int
255 __hscore_sigdelset( sigset_t * set, int s )
256 { return sigdelset(set,s); }
257
258 INLINE int
259 __hscore_sigismember( sigset_t * set, int s )
260 { return sigismember(set,s); }
261 #endif
262
263 INLINE void *
264 __hscore_memcpy_dst_off( char *dst, int dst_off, char *src, size_t sz )
265 { return memcpy(dst+dst_off, src, sz); }
266
267 INLINE void *
268 __hscore_memcpy_src_off( char *dst, char *src, int src_off, size_t sz )
269 { return memcpy(dst, src+src_off, sz); }
270
271 INLINE HsBool
272 __hscore_supportsTextMode()
273 {
274 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
275 return HS_BOOL_FALSE;
276 #else
277 return HS_BOOL_TRUE;
278 #endif
279 }
280
281 INLINE HsInt
282 __hscore_bufsiz()
283 {
284 return BUFSIZ;
285 }
286
287 INLINE int
288 __hscore_seek_cur()
289 {
290 return SEEK_CUR;
291 }
292
293 INLINE int
294 __hscore_o_binary()
295 {
296 #if defined(_MSC_VER)
297 return O_BINARY;
298 #else
299 return CONST_O_BINARY;
300 #endif
301 }
302
303 INLINE int
304 __hscore_o_rdonly()
305 {
306 #ifdef O_RDONLY
307 return O_RDONLY;
308 #else
309 return 0;
310 #endif
311 }
312
313 INLINE int
314 __hscore_o_wronly( void )
315 {
316 #ifdef O_WRONLY
317 return O_WRONLY;
318 #else
319 return 0;
320 #endif
321 }
322
323 INLINE int
324 __hscore_o_rdwr( void )
325 {
326 #ifdef O_RDWR
327 return O_RDWR;
328 #else
329 return 0;
330 #endif
331 }
332
333 INLINE int
334 __hscore_o_append( void )
335 {
336 #ifdef O_APPEND
337 return O_APPEND;
338 #else
339 return 0;
340 #endif
341 }
342
343 INLINE int
344 __hscore_o_creat( void )
345 {
346 #ifdef O_CREAT
347 return O_CREAT;
348 #else
349 return 0;
350 #endif
351 }
352
353 INLINE int
354 __hscore_o_excl( void )
355 {
356 #ifdef O_EXCL
357 return O_EXCL;
358 #else
359 return 0;
360 #endif
361 }
362
363 INLINE int
364 __hscore_o_trunc( void )
365 {
366 #ifdef O_TRUNC
367 return O_TRUNC;
368 #else
369 return 0;
370 #endif
371 }
372
373 INLINE int
374 __hscore_o_noctty( void )
375 {
376 #ifdef O_NOCTTY
377 return O_NOCTTY;
378 #else
379 return 0;
380 #endif
381 }
382
383 INLINE int
384 __hscore_o_nonblock( void )
385 {
386 #ifdef O_NONBLOCK
387 return O_NONBLOCK;
388 #else
389 return 0;
390 #endif
391 }
392
393 INLINE int
394 __hscore_seek_set( void )
395 {
396 return SEEK_SET;
397 }
398
399 INLINE int
400 __hscore_seek_end( void )
401 {
402 return SEEK_END;
403 }
404
405 INLINE int
406 __hscore_ftruncate( int fd, off_t where )
407 {
408 #if defined(HAVE_FTRUNCATE)
409 return ftruncate(fd,where);
410 #elif defined(HAVE__CHSIZE)
411 return _chsize(fd,where);
412 #else
413 // ToDo: we should use _chsize_s() on Windows which allows a 64-bit
414 // offset, but it doesn't seem to be available from mingw at this time
415 // --SDM (01/2008)
416 #error at least ftruncate or _chsize functions are required to build
417 #endif
418 }
419
420 INLINE int
421 __hscore_setmode( int fd, HsBool toBin )
422 {
423 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
424 return setmode(fd,(toBin == HS_BOOL_TRUE) ? _O_BINARY : _O_TEXT);
425 #else
426 return 0;
427 #endif
428 }
429
430 #if __GLASGOW_HASKELL__
431
432 INLINE int
433 __hscore_PrelHandle_write( int fd, void *ptr, HsInt off, int sz )
434 {
435 return write(fd,(char *)ptr + off, sz);
436 }
437
438 INLINE int
439 __hscore_PrelHandle_read( int fd, void *ptr, HsInt off, int sz )
440 {
441 return read(fd,(char *)ptr + off, sz);
442
443 }
444
445 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
446 INLINE int
447 __hscore_PrelHandle_send( int fd, void *ptr, HsInt off, int sz )
448 {
449 return send(fd,(char *)ptr + off, sz, 0);
450 }
451
452 INLINE int
453 __hscore_PrelHandle_recv( int fd, void *ptr, HsInt off, int sz )
454 {
455 return recv(fd,(char *)ptr + off, sz, 0);
456 }
457 #endif
458
459 #endif /* __GLASGOW_HASKELL__ */
460
461 INLINE int
462 __hscore_mkdir( char *pathName, int mode )
463 {
464 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
465 return mkdir(pathName);
466 #else
467 return mkdir(pathName,mode);
468 #endif
469 }
470
471 INLINE int
472 __hscore_lstat( const char *fname, struct stat *st )
473 {
474 #if HAVE_LSTAT
475 return lstat(fname, st);
476 #else
477 return stat(fname, st);
478 #endif
479 }
480
481 INLINE char *
482 __hscore_d_name( struct dirent* d )
483 {
484 return (d->d_name);
485 }
486
487 INLINE int
488 __hscore_end_of_dir( void )
489 {
490 return READDIR_ERRNO_EOF;
491 }
492
493 INLINE void
494 __hscore_free_dirent(struct dirent *dEnt)
495 {
496 #if HAVE_READDIR_R
497 free(dEnt);
498 #endif
499 }
500
501 #if defined(__MINGW32__)
502 // We want the versions of stat/fstat/lseek that use 64-bit offsets,
503 // and you have to ask for those explicitly. Unfortunately there
504 // doesn't seem to be a 64-bit version of truncate/ftruncate, so while
505 // hFileSize and hSeek will work with large files, hSetFileSize will not.
506 #define stat(file,buf) _stati64(file,buf)
507 #define fstat(fd,buf) _fstati64(fd,buf)
508 typedef struct _stati64 struct_stat;
509 typedef off64_t stsize_t;
510 #else
511 typedef struct stat struct_stat;
512 typedef off_t stsize_t;
513 #endif
514
515 INLINE HsInt
516 __hscore_sizeof_stat( void )
517 {
518 return sizeof(struct_stat);
519 }
520
521 INLINE time_t __hscore_st_mtime ( struct_stat* st ) { return st->st_mtime; }
522 INLINE stsize_t __hscore_st_size ( struct_stat* st ) { return st->st_size; }
523 #if !defined(_MSC_VER)
524 INLINE mode_t __hscore_st_mode ( struct_stat* st ) { return st->st_mode; }
525 INLINE dev_t __hscore_st_dev ( struct_stat* st ) { return st->st_dev; }
526 INLINE ino_t __hscore_st_ino ( struct_stat* st ) { return st->st_ino; }
527 #endif
528
529 #if HAVE_TERMIOS_H
530 INLINE tcflag_t __hscore_lflag( struct termios* ts ) { return ts->c_lflag; }
531
532 INLINE void
533 __hscore_poke_lflag( struct termios* ts, tcflag_t t ) { ts->c_lflag = t; }
534
535 INLINE unsigned char*
536 __hscore_ptr_c_cc( struct termios* ts )
537 { return (unsigned char*) &ts->c_cc; }
538
539 INLINE HsInt
540 __hscore_sizeof_termios( void )
541 {
542 #ifndef __MINGW32__
543 return sizeof(struct termios);
544 #else
545 return 0;
546 #endif
547 }
548 #endif
549
550 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
551 INLINE HsInt
552 __hscore_sizeof_sigset_t( void )
553 {
554 return sizeof(sigset_t);
555 }
556 #endif
557
558 INLINE int
559 __hscore_echo( void )
560 {
561 #ifdef ECHO
562 return ECHO;
563 #else
564 return 0;
565 #endif
566
567 }
568
569 INLINE int
570 __hscore_tcsanow( void )
571 {
572 #ifdef TCSANOW
573 return TCSANOW;
574 #else
575 return 0;
576 #endif
577
578 }
579
580 INLINE int
581 __hscore_icanon( void )
582 {
583 #ifdef ICANON
584 return ICANON;
585 #else
586 return 0;
587 #endif
588 }
589
590 INLINE int __hscore_vmin( void )
591 {
592 #ifdef VMIN
593 return VMIN;
594 #else
595 return 0;
596 #endif
597 }
598
599 INLINE int __hscore_vtime( void )
600 {
601 #ifdef VTIME
602 return VTIME;
603 #else
604 return 0;
605 #endif
606 }
607
608 INLINE int __hscore_sigttou( void )
609 {
610 #ifdef SIGTTOU
611 return SIGTTOU;
612 #else
613 return 0;
614 #endif
615 }
616
617 INLINE int __hscore_sig_block( void )
618 {
619 #ifdef SIG_BLOCK
620 return SIG_BLOCK;
621 #else
622 return 0;
623 #endif
624 }
625
626 INLINE int __hscore_sig_setmask( void )
627 {
628 #ifdef SIG_SETMASK
629 return SIG_SETMASK;
630 #else
631 return 0;
632 #endif
633 }
634
635 #ifndef __MINGW32__
636 INLINE size_t __hscore_sizeof_siginfo_t (void)
637 {
638 return sizeof(siginfo_t);
639 }
640 #endif
641
642 INLINE int
643 __hscore_f_getfl( void )
644 {
645 #ifdef F_GETFL
646 return F_GETFL;
647 #else
648 return 0;
649 #endif
650 }
651
652 INLINE int
653 __hscore_f_setfl( void )
654 {
655 #ifdef F_SETFL
656 return F_SETFL;
657 #else
658 return 0;
659 #endif
660 }
661
662 INLINE int
663 __hscore_f_setfd( void )
664 {
665 #ifdef F_SETFD
666 return F_SETFD;
667 #else
668 return 0;
669 #endif
670 }
671
672 INLINE long
673 __hscore_fd_cloexec( void )
674 {
675 #ifdef FD_CLOEXEC
676 return FD_CLOEXEC;
677 #else
678 return 0;
679 #endif
680 }
681
682 // defined in rts/RtsStartup.c.
683 extern void* __hscore_get_saved_termios(int fd);
684 extern void __hscore_set_saved_termios(int fd, void* ts);
685
686 INLINE int __hscore_hs_fileno (FILE *f) { return fileno (f); }
687
688 INLINE int __hscore_open(char *file, int how, mode_t mode) {
689 #ifdef __MINGW32__
690 if ((how & O_WRONLY) || (how & O_RDWR) || (how & O_APPEND))
691 return _sopen(file,how,_SH_DENYRW,mode);
692 else
693 return _sopen(file,how,_SH_DENYWR,mode);
694 #else
695 return open(file,how,mode);
696 #endif
697 }
698
699 // These are wrapped because on some OSs (eg. Linux) they are
700 // macros which redirect to the 64-bit-off_t versions when large file
701 // support is enabled.
702 //
703 #if defined(__MINGW32__)
704 INLINE off64_t __hscore_lseek(int fd, off64_t off, int whence) {
705 return (_lseeki64(fd,off,whence));
706 }
707 #else
708 INLINE off_t __hscore_lseek(int fd, off_t off, int whence) {
709 return (lseek(fd,off,whence));
710 }
711 #endif
712
713 INLINE int __hscore_stat(char *file, struct_stat *buf) {
714 return (stat(file,buf));
715 }
716
717 INLINE int __hscore_fstat(int fd, struct_stat *buf) {
718 return (fstat(fd,buf));
719 }
720
721 // select-related stuff
722
723 #if !defined(__MINGW32__)
724 INLINE int hsFD_SETSIZE(void) { return FD_SETSIZE; }
725 INLINE int hsFD_ISSET(int fd, fd_set *fds) { return FD_ISSET(fd, fds); }
726 INLINE void hsFD_SET(int fd, fd_set *fds) { FD_SET(fd, fds); }
727 INLINE HsInt sizeof_fd_set(void) { return sizeof(fd_set); }
728 extern void hsFD_ZERO(fd_set *fds);
729 #endif
730
731 // gettimeofday()-related
732
733 #if !defined(__MINGW32__)
734
735 INLINE HsInt sizeofTimeVal(void) { return sizeof(struct timeval); }
736
737 INLINE HsWord64 getUSecOfDay(void)
738 {
739 struct timeval tv;
740 gettimeofday(&tv, (struct timezone *) NULL);
741 // Don't forget to cast *before* doing the arithmetic, otherwise
742 // the arithmetic happens at the type of tv_sec, which is probably
743 // only 'int'.
744 return ((HsWord64)tv.tv_sec * 1000000 + (HsWord64)tv.tv_usec);
745 }
746
747 INLINE void setTimevalTicks(struct timeval *p, HsWord64 usecs)
748 {
749 p->tv_sec = usecs / 1000000;
750 p->tv_usec = usecs % 1000000;
751 }
752 #endif /* !defined(__MINGW32__) */
753
754 /* ToDo: write a feature test that doesn't assume 'environ' to
755 * be in scope at link-time. */
756 extern char** environ;
757 INLINE char **__hscore_environ() { return environ; }
758
759 /* lossless conversions between pointers and integral types */
760 INLINE void * __hscore_from_uintptr(uintptr_t n) { return (void *)n; }
761 INLINE void * __hscore_from_intptr (intptr_t n) { return (void *)n; }
762 INLINE uintptr_t __hscore_to_uintptr (void *p) { return (uintptr_t)p; }
763 INLINE intptr_t __hscore_to_intptr (void *p) { return (intptr_t)p; }
764
765 void errorBelch2(const char*s, char *t);
766 void debugBelch2(const char*s, char *t);
767
768 #endif /* __HSBASE_H__ */
769