00001
00002
00003
00004
00005
00006
00007
00008 #ifndef FDLIB_H
00009 #define FDLIB_H
00010
00011 #include "global.h"
00012 #include "pike_macros.h"
00013
00014 #include "pike_netlib.h"
00015
00016 #ifdef HAVE_ERRNO_H
00017 #include <errno.h>
00018 #endif
00019
00020 #ifdef HAVE_SYS_SELECT_H
00021 #include <sys/select.h>
00022 #endif
00023
00024 #ifdef HAVE_FCNTL_H
00025 #include <fcntl.h>
00026 #endif
00027
00028 #ifdef HAVE_SYS_STAT_H
00029 #include <sys/stat.h>
00030 #endif
00031
00032 #ifdef HAVE_SYS_FILE_H
00033 #include <sys/file.h>
00034 #endif
00035
00036 #ifdef HAVE_SOCKET_H
00037 #include <socket.h>
00038 #endif
00039
00040 #include "pike_netlib.h"
00041
00042 #define fd_INTERPROCESSABLE 1
00043 #define fd_CAN_NONBLOCK 2
00044 #define fd_CAN_SHUTDOWN 4
00045 #define fd_BUFFERED 8
00046 #define fd_BIDIRECTIONAL 16
00047 #define fd_REVERSE 32
00048
00049
00050 #if defined(HAVE_WINSOCK_H)
00051
00052 #define HAVE_FD_FLOCK
00053
00054
00055 #define FILE_CAPABILITIES (fd_INTERPROCESSABLE)
00056 #define PIPE_CAPABILITIES (fd_INTERPROCESSABLE | fd_BUFFERED)
00057 #define SOCKET_CAPABILITIES (fd_BIDIRECTIONAL | fd_CAN_NONBLOCK | fd_CAN_SHUTDOWN)
00058
00059 #ifndef FD_SETSIZE
00060 #define FD_SETSIZE MAX_OPEN_FILEDESCRIPTORS
00061 #endif
00062
00063 #include <winbase.h>
00064
00065 typedef int FD;
00066
00067 #if _INTEGRAL_MAX_BITS >= 64
00068 typedef struct _stati64 PIKE_STAT_T;
00069 typedef __int64 PIKE_OFF_T;
00070 #else
00071 typedef struct stat PIKE_STAT_T;
00072 typedef off_t PIKE_OFF_T;
00073 #endif
00074
00075 #define SOCKFUN1(NAME,T1) PMOD_EXPORT int PIKE_CONCAT(debug_fd_,NAME) (FD,T1);
00076 #define SOCKFUN2(NAME,T1,T2) PMOD_EXPORT int PIKE_CONCAT(debug_fd_,NAME) (FD,T1,T2);
00077 #define SOCKFUN3(NAME,T1,T2,T3) PMOD_EXPORT int PIKE_CONCAT(debug_fd_,NAME) (FD,T1,T2,T3);
00078 #define SOCKFUN4(NAME,T1,T2,T3,T4) PMOD_EXPORT int PIKE_CONCAT(debug_fd_,NAME) (FD,T1,T2,T3,T4);
00079 #define SOCKFUN5(NAME,T1,T2,T3,T4,T5) PMOD_EXPORT int PIKE_CONCAT(debug_fd_,NAME) (FD,T1,T2,T3,T4,T5);
00080
00081
00082 #define fd_info(fd) debug_fd_info(dmalloc_touch_fd(fd))
00083 #define fd_query_properties(fd,Y) \
00084 debug_fd_query_properties(dmalloc_touch_fd(fd),(Y))
00085 #define fd_stat(F,BUF) debug_fd_stat(F,BUF)
00086 #define fd_lstat(F,BUF) debug_fd_stat(F,BUF)
00087 #define fd_open(X,Y,Z) dmalloc_register_fd(debug_fd_open((X),(Y),(Z)))
00088 #define fd_socket(X,Y,Z) dmalloc_register_fd(debug_fd_socket((X),(Y),(Z)))
00089 #define fd_pipe(X) debug_fd_pipe( (X) DMALLOC_POS )
00090 #define fd_accept(X,Y,Z) dmalloc_register_fd(debug_fd_accept((X),(Y),(Z)))
00091
00092 #define fd_bind(fd,X,Y) debug_fd_bind(dmalloc_touch_fd(fd), (X), (Y))
00093 #define fd_getsockopt(fd,X,Y,Z,Q) debug_fd_getsockopt(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q))
00094 #define fd_setsockopt(fd,X,Y,Z,Q) debug_fd_setsockopt(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q))
00095 #define fd_recv(fd,X,Y,Z) debug_fd_recv(dmalloc_touch_fd(fd), (X), (Y),(Z))
00096 #define fd_getsockname(fd,X,Y) debug_fd_getsockname(dmalloc_touch_fd(fd), (X), (Y))
00097 #define fd_getpeername(fd,X,Y) debug_fd_getpeername(dmalloc_touch_fd(fd), (X), (Y))
00098 #define fd_recvfrom(fd,X,Y,Z,Q,P) debug_fd_recvfrom(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q),(P))
00099 #define fd_send(fd,X,Y,Z) debug_fd_send(dmalloc_touch_fd(fd), (X), (Y),(Z))
00100 #define fd_sendto(fd,X,Y,Z,Q,P) debug_fd_sendto(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q),(P))
00101 #define fd_shutdown(fd,X) debug_fd_shutdown(dmalloc_touch_fd(fd), (X))
00102 #define fd_listen(fd,X) debug_fd_listen(dmalloc_touch_fd(fd), (X))
00103 #define fd_close(fd) debug_fd_close(dmalloc_close_fd(fd))
00104 #define fd_write(fd,X,Y) debug_fd_write(dmalloc_touch_fd(fd),(X),(Y))
00105 #define fd_read(fd,X,Y) debug_fd_read(dmalloc_touch_fd(fd),(X),(Y))
00106 #define fd_lseek(fd,X,Y) debug_fd_lseek(dmalloc_touch_fd(fd),(X),(Y))
00107 #define fd_ftruncate(fd,X) debug_fd_ftruncate(dmalloc_touch_fd(fd),(X))
00108 #define fd_flock(fd,X) debug_fd_flock(dmalloc_touch_fd(fd),(X))
00109 #define fd_fstat(fd,X) debug_fd_fstat(dmalloc_touch_fd(fd),(X))
00110 #define fd_select debug_fd_select
00111 #define fd_ioctl(fd,X,Y) debug_fd_ioctl(dmalloc_touch_fd(fd),(X),(Y))
00112 #define fd_dup(fd) dmalloc_register_fd(debug_fd_dup(dmalloc_touch_fd(fd)))
00113 #define fd_dup2(fd,to) dmalloc_register_fd(debug_fd_dup2(dmalloc_touch_fd(fd),dmalloc_close_fd(to)))
00114 #define fd_connect(fd,X,Z) debug_fd_connect(dmalloc_touch_fd(fd),(X),(Z))
00115
00116
00117
00118 PMOD_EXPORT char *debug_fd_info(int fd);
00119 PMOD_EXPORT int debug_fd_query_properties(int fd, int guess);
00120 void fd_init();
00121 void fd_exit();
00122 PMOD_EXPORT int debug_fd_stat(const char *file, PIKE_STAT_T *buf);
00123 PMOD_EXPORT FD debug_fd_open(const char *file, int open_mode, int create_mode);
00124 PMOD_EXPORT FD debug_fd_socket(int domain, int type, int proto);
00125 PMOD_EXPORT int debug_fd_pipe(int fds[2] DMALLOC_LINE_ARGS);
00126 PMOD_EXPORT FD debug_fd_accept(FD fd, struct sockaddr *addr, ACCEPT_SIZE_T *addrlen);
00127 SOCKFUN2(bind, struct sockaddr *, int)
00128 PMOD_EXPORT int debug_fd_connect (FD fd, struct sockaddr *a, int len);
00129 SOCKFUN4(getsockopt,int,int,void*,ACCEPT_SIZE_T *)
00130 SOCKFUN4(setsockopt,int,int,void*,int)
00131 SOCKFUN3(recv,void *,int,int)
00132 SOCKFUN2(getsockname,struct sockaddr *,ACCEPT_SIZE_T *)
00133 SOCKFUN2(getpeername,struct sockaddr *,ACCEPT_SIZE_T *)
00134 SOCKFUN5(recvfrom,void *,int,int,struct sockaddr *,ACCEPT_SIZE_T *)
00135 SOCKFUN3(send,void *,int,int)
00136 SOCKFUN5(sendto,void *,int,int,struct sockaddr *,unsigned int)
00137 SOCKFUN1(shutdown, int)
00138 SOCKFUN1(listen, int)
00139 PMOD_EXPORT int debug_fd_close(FD fd);
00140 PMOD_EXPORT ptrdiff_t debug_fd_write(FD fd, void *buf, ptrdiff_t len);
00141 PMOD_EXPORT ptrdiff_t debug_fd_read(FD fd, void *to, ptrdiff_t len);
00142 PMOD_EXPORT PIKE_OFF_T debug_fd_lseek(FD fd, PIKE_OFF_T pos, int where);
00143 PMOD_EXPORT int debug_fd_ftruncate(FD fd, PIKE_OFF_T len);
00144 PMOD_EXPORT int debug_fd_flock(FD fd, int oper);
00145 PMOD_EXPORT int debug_fd_fstat(FD fd, PIKE_STAT_T *s);
00146 PMOD_EXPORT int debug_fd_select(int fds, FD_SET *a, FD_SET *b, FD_SET *c, struct timeval *t);
00147 PMOD_EXPORT int debug_fd_ioctl(FD fd, int cmd, void *data);
00148 PMOD_EXPORT FD debug_fd_dup(FD from);
00149 PMOD_EXPORT FD debug_fd_dup2(FD from, FD to);
00150
00151
00152 #undef SOCKFUN1
00153 #undef SOCKFUN2
00154 #undef SOCKFUN3
00155 #undef SOCKFUN4
00156 #undef SOCKFUN5
00157
00158 #ifndef EWOULDBLOCK
00159 #define EWOULDBLOCK WSAEWOULDBLOCK
00160 #endif
00161
00162 #ifndef EADDRINUSE
00163 #define EADDRINUSE WSAEADDRINUSE
00164 #endif
00165
00166 #ifndef ENOTSUPP
00167 #define ENOTSUPP WSAEOPNOTSUPP
00168 #endif
00169
00170 #define fd_RDONLY 1
00171 #define fd_WRONLY 2
00172 #define fd_RDWR 3
00173 #define fd_APPEND 4
00174 #define fd_CREAT 8
00175 #define fd_TRUNC 16
00176 #define fd_EXCL 32
00177 #define fd_BINARY 0
00178 #define fd_LARGEFILE 0
00179
00180 #define fd_shutdown_read SD_RECEIVE
00181 #define fd_shutdown_write SD_SEND
00182 #define fd_shutdown_both SD_BOTH
00183
00184 #define FD_PIPE -5
00185 #define FD_SOCKET -4
00186 #define FD_CONSOLE -3
00187 #define FD_FILE -2
00188 #define FD_NO_MORE_FREE -1
00189
00190
00191 #define fd_LOCK_SH 1
00192 #define fd_LOCK_EX 2
00193 #define fd_LOCK_UN 4
00194 #define fd_LOCK_NB 8
00195
00196 struct my_fd_set_s
00197 {
00198 char bits[MAX_OPEN_FILEDESCRIPTORS/8];
00199 };
00200
00201 typedef struct my_fd_set_s my_fd_set;
00202
00203 #ifdef PIKE_DEBUG
00204 #define fd_check_fd(X) do { if(fd_type[X]>=0) Pike_fatal("FD_SET on closed fd %d (%d) %s:%d.\n",X,da_handle[X],__FILE__,__LINE__); }while(0)
00205 #else
00206 #define fd_check_fd(X)
00207 #endif
00208 #define my_FD_CLR(FD,S) ((S)->bits[(FD)>>3]&=~ (1<<(FD&7)))
00209 #define my_FD_SET(FD,S) do{ fd_check_fd(FD); ((S)->bits[(FD)>>3]|= (1<<(FD&7))); }while(0)
00210 #define my_FD_ISSET(FD,S) ((S)->bits[(FD)>>3]&(1<<(FD&7)))
00211 #define my_FD_ZERO(S) MEMSET(& (S)->bits, 0, sizeof(my_fd_set))
00212
00213 #define fd_copy_my_fd_set_to_fd_set(TO,FROM,max) do { \
00214 int e_,d_,max_=MINIMUM(MAX_OPEN_FILEDESCRIPTORS>>3,(max+7)>>3); \
00215 (TO)->fd_count=0; \
00216 for(e_=0;e_<max_;e_++) \
00217 { \
00218 int b_=(FROM)->bits[e_]; \
00219 if(b_) \
00220 { \
00221 for(d_=0;d_<8;d_++) \
00222 { \
00223 if(b_ & (1<<d_)) \
00224 { \
00225 int fd_=(e_<<3)+d_; \
00226 fd_check_fd(fd_); \
00227 (TO)->fd_array[(TO)->fd_count++]=(SOCKET)da_handle[fd_]; \
00228 } \
00229 } \
00230 } \
00231 } \
00232 }while(0)
00233
00234 extern HANDLE da_handle[MAX_OPEN_FILEDESCRIPTORS];
00235 extern int fd_type[MAX_OPEN_FILEDESCRIPTORS];
00236
00237 #define fd_FD_CLR(X,Y) FD_CLR((SOCKET)da_handle[X],Y)
00238 #define fd_FD_SET(X,Y) \
00239 do { fd_check_fd(X); FD_SET((SOCKET)da_handle[X],Y); }while(0)
00240 #define fd_FD_ISSET(X,Y) FD_ISSET((SOCKET)da_handle[X],Y)
00241 #define fd_FD_ZERO(X) FD_ZERO(X)
00242
00243 #ifndef S_IFSOCK
00244 #define S_IFSOCK 0140000
00245 #endif
00246
00247 #ifndef S_IFIFO
00248 #define S_IFIFO 0010000
00249 #endif
00250
00251
00252
00253 #if defined(__NT__) && !defined(__MINGW32__)
00254 #define EMULATE_DIRECT
00255 #endif
00256
00257 #ifdef EMULATE_DIRECT
00258
00259 #define d_name cFileName
00260 #define direct _WIN32_FIND_DATAA
00261 #define dirent direct
00262 #define MAXPATHLEN MAX_PATH
00263 #define NAMLEN(dirent) strlen((dirent)->d_name)
00264
00265 typedef struct DIR_s
00266 {
00267 int first;
00268 WIN32_FIND_DATA find_data;
00269 HANDLE h;
00270 } DIR;
00271
00272 PMOD_EXPORT DIR *opendir(char *dir);
00273 PMOD_EXPORT int readdir_r(DIR *dir, struct direct *tmp ,struct direct **d);
00274 PMOD_EXPORT void closedir(DIR *dir);
00275
00276 #define HAVE_POSIX_READDIR_R
00277
00278
00279 #undef HAVE_DIRECT_H
00280 #undef HAVE_NDIR_H
00281 #undef HAVE_SYS_NDIR_H
00282 #undef HAVE_DIRENT_H
00283
00284 #endif
00285
00286
00287
00288 #else
00289
00290
00291 typedef int FD;
00292 typedef struct stat PIKE_STAT_T;
00293 typedef off_t PIKE_OFF_T;
00294
00295 #define fd_info(X) ""
00296 #define fd_init()
00297 #define fd_exit()
00298
00299 #define fd_RDONLY O_RDONLY
00300 #define fd_WRONLY O_WRONLY
00301 #define fd_RDWR O_RDWR
00302 #define fd_APPEND O_APPEND
00303 #define fd_CREAT O_CREAT
00304 #define fd_TRUNC O_TRUNC
00305 #define fd_EXCL O_EXCL
00306 #define fd_BINARY 0
00307 #ifdef O_LARGEFILE
00308 #define fd_LARGEFILE O_LARGEFILE
00309 #else
00310 #define fd_LARGEFILE 0
00311 #endif
00312
00313 #define fd_query_properties(X,Y) ( fd_INTERPROCESSABLE | (Y))
00314
00315 #define fd_stat(F,BUF) stat(F,BUF)
00316 #define fd_lstat(F,BUF) lstat(F,BUF)
00317 #define fd_open(X,Y,Z) dmalloc_register_fd(open((X),(Y),(Z)))
00318 #define fd_socket(X,Y,Z) dmalloc_register_fd(socket((X),(Y),(Z)))
00319 #define fd_pipe pipe
00320 #define fd_accept(X,Y,Z) dmalloc_register_fd(accept((X),(Y),(Z)))
00321
00322 #define fd_bind(fd,X,Y) bind(dmalloc_touch_fd(fd), (X), (Y))
00323 #define fd_getsockopt(fd,X,Y,Z,Q) getsockopt(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q))
00324 #define fd_setsockopt(fd,X,Y,Z,Q) setsockopt(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q))
00325 #define fd_recv(fd,X,Y,Z) recv(dmalloc_touch_fd(fd), (X), (Y),(Z))
00326 #define fd_getsockname(fd,X,Y) getsockname(dmalloc_touch_fd(fd), (X), (Y))
00327 #define fd_getpeername(fd,X,Y) getpeername(dmalloc_touch_fd(fd), (X), (Y))
00328 #define fd_recvfrom(fd,X,Y,Z,Q,P) recvfrom(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q),(P))
00329 #define fd_send(fd,X,Y,Z) send(dmalloc_touch_fd(fd), (X), (Y),(Z))
00330 #define fd_sendto(fd,X,Y,Z,Q,P) sendto(dmalloc_touch_fd(fd), (X),(Y),(Z),(Q),(P))
00331 #define fd_shutdown(fd,X) shutdown(dmalloc_touch_fd(fd), (X))
00332 #define fd_listen(fd,X) listen(dmalloc_touch_fd(fd), (X))
00333
00334 #ifdef HAVE_BROKEN_F_SETFD
00335 #define fd_close(fd) (set_close_on_exec(fd,0),close(dmalloc_close_fd(fd)))
00336 #else
00337 #define fd_close(fd) close(dmalloc_close_fd(fd))
00338 #endif
00339
00340 #define fd_write(fd,X,Y) write(dmalloc_touch_fd(fd),(X),(Y))
00341 #define fd_read(fd,X,Y) read(dmalloc_touch_fd(fd),(X),(Y))
00342 #define fd_lseek(fd,X,Y) lseek(dmalloc_touch_fd(fd),(X),(Y))
00343 #define fd_ftruncate(fd,X) ftruncate(dmalloc_touch_fd(fd),(X))
00344 #define fd_fstat(fd,X) fstat(dmalloc_touch_fd(fd),(X))
00345 #define fd_select select
00346 #define fd_ioctl(fd,X,Y) ioctl(dmalloc_touch_fd(fd),(X),(Y))
00347 #define fd_dup(fd) dmalloc_register_fd(dup(dmalloc_touch_fd(fd)))
00348 #define fd_connect(fd,X,Z) connect(dmalloc_touch_fd(fd),(X),(Z))
00349
00350 #ifdef HAVE_BROKEN_F_SETFD
00351 #define fd_dup2(fd,to) (set_close_on_exec(to,0), dmalloc_register_fd(dup2(dmalloc_touch_fd(fd),dmalloc_close_fd(to))))
00352 #else
00353 #define fd_dup2(fd,to) dmalloc_register_fd(dup2(dmalloc_touch_fd(fd),dmalloc_close_fd(to)))
00354 #endif
00355
00356 #define fd_socketpair socketpair
00357
00358 #define fd_fd_set fd_set
00359 #define fd_FD_CLR FD_CLR
00360 #define fd_FD_SET FD_SET
00361 #define fd_FD_ISSET FD_ISSET
00362 #define fd_FD_ZERO FD_ZERO
00363
00364 #ifdef HAVE_FLOCK
00365 #define HAVE_FD_FLOCK
00366 #define fd_flock(fd,X) flock(dmalloc_touch_fd(fd),(X))
00367 #define fd_LOCK_SH LOCK_SH
00368 #define fd_LOCK_EX LOCK_EX
00369 #define fd_LOCK_UN LOCK_UN
00370 #define fd_LOCK_NB LOCK_NB
00371 #else
00372 #ifdef HAVE_LOCKF
00373 #define HAVE_FD_LOCKF
00374 #define fd_LOCK_EX F_LOCK
00375 #define fd_LOCK_UN F_ULOCK
00376 #define fd_LOCK_NB F_TLOCK
00377
00378 #define fd_lockf(fd,mode) lockf(dmalloc_touch_fd(fd),mode,0)
00379 #endif
00380 #endif
00381
00382
00383 #define fd_shutdown_read 0
00384 #define fd_shutdown_write 1
00385 #define fd_shutdown_both 2
00386
00387 struct my_fd_set_s
00388 {
00389 fd_set tmp;
00390 };
00391
00392 typedef struct my_fd_set_s my_fd_set;
00393
00394 #define my_FD_CLR(FD,S) FD_CLR((FD), & (S)->tmp)
00395 #define my_FD_SET(FD,S) FD_SET((FD), & (S)->tmp)
00396 #define my_FD_ISSET(FD,S) FD_ISSET((FD), & (S)->tmp)
00397 #define my_FD_ZERO(S) FD_ZERO(& (S)->tmp)
00398
00399 #define fd_copy_my_fd_set_to_fd_set(TO,FROM,max) \
00400 MEMCPY((TO),&(FROM)->tmp,sizeof(*(TO)))
00401
00402 #define FILE_CAPABILITIES (fd_INTERPROCESSABLE | fd_CAN_NONBLOCK)
00403 #ifndef __amigaos__
00404 #define PIPE_CAPABILITIES (fd_INTERPROCESSABLE | fd_BUFFERED | fd_CAN_NONBLOCK)
00405 #endif
00406 #define UNIX_SOCKET_CAPABILITIES (fd_INTERPROCESSABLE | fd_BIDIRECTIONAL | fd_CAN_NONBLOCK)
00407 #define SOCKET_CAPABILITIES (fd_INTERPROCESSABLE | fd_BIDIRECTIONAL | fd_CAN_NONBLOCK | fd_CAN_SHUTDOWN)
00408
00409 #endif
00410
00411 #ifndef SEEK_SET
00412 #define SEEK_SET 0
00413 #endif
00414
00415 #ifndef SEEK_CUR
00416 #define SEEK_CUR 1
00417 #endif
00418
00419 #ifndef SEEK_END
00420 #define SEEK_END 2
00421 #endif
00422
00423 #ifndef S_ISREG
00424 #ifdef S_IFREG
00425 #define S_ISREG(mode) (((mode) & (S_IFMT)) == (S_IFREG))
00426 #else
00427 #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
00428 #endif
00429 #endif
00430
00431 PMOD_PROTO extern int pike_make_pipe(int *fds);
00432 PMOD_PROTO extern int fd_from_object(struct object *o);
00433 PMOD_PROTO extern void create_proxy_pipe(struct object *o, int for_reading);
00434 PMOD_PROTO struct object *file_make_object_from_fd(int fd, int mode, int guess);
00435
00436 #endif