LibCSymTable.h 3.7 KB

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