LibCSymTable.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIBCSYMTABLE_H_
  19. #define LIBCSYMTABLE_H_
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netdb.h>
  23. #include <stddef.h>
  24. #include <dlfcn.h>
  25. #include <cassert>
  26. #include <poll.h>
  27. #include <time.h>
  28. #include <unistd.h> // needed for _POSIX_MONOTONIC_CLOCK
  29. #ifdef THREADED
  30. #include <pthread.h>
  31. #endif
  32. #include "config.h"
  33. // TODO: move all these macros to config.h (generated by autoconf)
  34. #ifdef __CYGWIN__
  35. #if (CYGWIN_VERSION_DLL_MAJOR < 1007)
  36. #define RANDOM_RET_TYPE int
  37. #else
  38. #define RANDOM_RET_TYPE long int
  39. #endif
  40. #define GETTIMEOFDAY_ARG2_TYPE void*
  41. #else
  42. #define RANDOM_RET_TYPE long int
  43. #define GETTIMEOFDAY_ARG2_TYPE struct timezone*
  44. #endif
  45. #define DECLARE_SYM(ret,sym,sig) \
  46. typedef ret (*sym##_sig)sig; \
  47. static sym##_sig preload_##sym () { \
  48. static sym##_sig ptr=0;\
  49. if(!ptr){ void* h=getHandle(); ptr=(sym##_sig)dlsym(h,#sym); } \
  50. assert("Unable to load "#sym" from libc"&&ptr); \
  51. return ptr; \
  52. } \
  53. sym##_sig sym
  54. #define LIBC_SYMBOLS LibCSymTable::instance()
  55. //******************************************************************************
  56. // preload original libc symbols
  57. struct LibCSymTable
  58. {
  59. DECLARE_SYM(hostent*,gethostbyname,(const char*));
  60. DECLARE_SYM(void*,calloc,(size_t, size_t));
  61. DECLARE_SYM(void*,realloc,(void*, size_t));
  62. DECLARE_SYM(void,free,(void*));
  63. DECLARE_SYM(RANDOM_RET_TYPE,random,(void));
  64. DECLARE_SYM(void,srandom,(unsigned long));
  65. DECLARE_SYM(int,printf,(const char*, ...));
  66. DECLARE_SYM(int,socket,(int,int,int));
  67. DECLARE_SYM(int,close,(int));
  68. DECLARE_SYM(int,getsockopt,(int,int,int,void*,socklen_t*));
  69. DECLARE_SYM(int,setsockopt,(int,int,int,const void*,socklen_t));
  70. DECLARE_SYM(int,fcntl,(int,int,...));
  71. DECLARE_SYM(int,connect,(int,const struct sockaddr*,socklen_t));
  72. DECLARE_SYM(ssize_t,send,(int,const void*,size_t,int));
  73. DECLARE_SYM(ssize_t,recv,(int,const void*,size_t,int));
  74. DECLARE_SYM(int,select,(int,fd_set*,fd_set*,fd_set*,struct timeval*));
  75. DECLARE_SYM(int,poll,(struct pollfd*,POLL_NFDS_TYPE,int));
  76. DECLARE_SYM(int,gettimeofday,(struct timeval*,GETTIMEOFDAY_ARG2_TYPE));
  77. #ifdef _POSIX_MONOTONIC_CLOCK
  78. DECLARE_SYM(int,clock_gettime,(clockid_t clk_id, struct timespec*));
  79. #endif
  80. #ifdef THREADED
  81. DECLARE_SYM(int,pthread_create,(pthread_t *, const pthread_attr_t *,
  82. void *(*)(void *), void *));
  83. DECLARE_SYM(int,pthread_detach,(pthread_t));
  84. DECLARE_SYM(int,pthread_cond_broadcast,(pthread_cond_t *));
  85. DECLARE_SYM(int,pthread_cond_destroy,(pthread_cond_t *));
  86. DECLARE_SYM(int,pthread_cond_init,(pthread_cond_t *, const pthread_condattr_t *));
  87. DECLARE_SYM(int,pthread_cond_signal,(pthread_cond_t *));
  88. DECLARE_SYM(int,pthread_cond_timedwait,(pthread_cond_t *,
  89. pthread_mutex_t *, const struct timespec *));
  90. DECLARE_SYM(int,pthread_cond_wait,(pthread_cond_t *, pthread_mutex_t *));
  91. DECLARE_SYM(int,pthread_join,(pthread_t, void **));
  92. DECLARE_SYM(int,pthread_mutex_destroy,(pthread_mutex_t *));
  93. DECLARE_SYM(int,pthread_mutex_init,(pthread_mutex_t *, const pthread_mutexattr_t *));
  94. DECLARE_SYM(int,pthread_mutex_lock,(pthread_mutex_t *));
  95. DECLARE_SYM(int,pthread_mutex_trylock,(pthread_mutex_t *));
  96. DECLARE_SYM(int,pthread_mutex_unlock,(pthread_mutex_t *));
  97. #endif
  98. LibCSymTable();
  99. static void* getHandle();
  100. static LibCSymTable& instance();
  101. };
  102. #endif /*LIBCSYMTABLE_H_*/