LibCSymTable.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifdef THREADED
  28. #include <pthread.h>
  29. #endif
  30. #include "../config.h"
  31. // TODO: move all these macros to config.h (generated by autoconf)
  32. #ifdef __CYGWIN__
  33. #define RANDOM_RET_TYPE int
  34. #define GETTIMEOFDAY_ARG2_TYPE void*
  35. #else
  36. #define RANDOM_RET_TYPE long int
  37. #define GETTIMEOFDAY_ARG2_TYPE struct timezone*
  38. #endif
  39. #define DECLARE_SYM(ret,sym,sig) \
  40. typedef ret (*sym##_sig)sig; \
  41. static sym##_sig preload_##sym () { \
  42. static sym##_sig ptr=0;\
  43. if(!ptr){ void* h=getHandle(); ptr=(sym##_sig)dlsym(h,#sym); } \
  44. assert("Unable to load "#sym" from libc"&&ptr); \
  45. return ptr; \
  46. } \
  47. sym##_sig sym
  48. #define LIBC_SYMBOLS LibCSymTable::instance()
  49. //******************************************************************************
  50. // preload original libc symbols
  51. struct LibCSymTable
  52. {
  53. DECLARE_SYM(hostent*,gethostbyname,(const char*));
  54. DECLARE_SYM(void*,calloc,(size_t, size_t));
  55. DECLARE_SYM(void*,realloc,(void*, size_t));
  56. DECLARE_SYM(void,free,(void*));
  57. DECLARE_SYM(RANDOM_RET_TYPE,random,(void));
  58. DECLARE_SYM(void,srandom,(unsigned long));
  59. DECLARE_SYM(int,printf,(const char*, ...));
  60. DECLARE_SYM(int,socket,(int,int,int));
  61. DECLARE_SYM(int,close,(int));
  62. DECLARE_SYM(int,getsockopt,(int,int,int,void*,socklen_t*));
  63. DECLARE_SYM(int,setsockopt,(int,int,int,const void*,socklen_t));
  64. DECLARE_SYM(int,fcntl,(int,int,...));
  65. DECLARE_SYM(int,connect,(int,const struct sockaddr*,socklen_t));
  66. DECLARE_SYM(ssize_t,send,(int,const void*,size_t,int));
  67. DECLARE_SYM(ssize_t,recv,(int,const void*,size_t,int));
  68. DECLARE_SYM(int,select,(int,fd_set*,fd_set*,fd_set*,struct timeval*));
  69. DECLARE_SYM(int,poll,(struct pollfd*,POLL_NFDS_TYPE,int));
  70. DECLARE_SYM(int,gettimeofday,(struct timeval*,GETTIMEOFDAY_ARG2_TYPE));
  71. #ifdef THREADED
  72. DECLARE_SYM(int,pthread_create,(pthread_t *, const pthread_attr_t *,
  73. void *(*)(void *), void *));
  74. DECLARE_SYM(int,pthread_detach,(pthread_t));
  75. DECLARE_SYM(int,pthread_cond_broadcast,(pthread_cond_t *));
  76. DECLARE_SYM(int,pthread_cond_destroy,(pthread_cond_t *));
  77. DECLARE_SYM(int,pthread_cond_init,(pthread_cond_t *, const pthread_condattr_t *));
  78. DECLARE_SYM(int,pthread_cond_signal,(pthread_cond_t *));
  79. DECLARE_SYM(int,pthread_cond_timedwait,(pthread_cond_t *,
  80. pthread_mutex_t *, const struct timespec *));
  81. DECLARE_SYM(int,pthread_cond_wait,(pthread_cond_t *, pthread_mutex_t *));
  82. DECLARE_SYM(int,pthread_join,(pthread_t, void **));
  83. DECLARE_SYM(int,pthread_mutex_destroy,(pthread_mutex_t *));
  84. DECLARE_SYM(int,pthread_mutex_init,(pthread_mutex_t *, const pthread_mutexattr_t *));
  85. DECLARE_SYM(int,pthread_mutex_lock,(pthread_mutex_t *));
  86. DECLARE_SYM(int,pthread_mutex_trylock,(pthread_mutex_t *));
  87. DECLARE_SYM(int,pthread_mutex_unlock,(pthread_mutex_t *));
  88. #endif
  89. LibCSymTable();
  90. static void* getHandle();
  91. static LibCSymTable& instance();
  92. };
  93. #endif /*LIBCSYMTABLE_H_*/