apr_portable.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. 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. /* This header file is where you should put ANY platform specific information.
  17. * This should be the only header file that programs need to include that
  18. * actually has platform dependent code which refers to the .
  19. */
  20. #ifndef APR_PORTABLE_H
  21. #define APR_PORTABLE_H
  22. /**
  23. * @file apr_portable.h
  24. * @brief APR Portability Routines
  25. */
  26. #include "apr.h"
  27. #include "apr_pools.h"
  28. #include "apr_thread_proc.h"
  29. #include "apr_file_io.h"
  30. #include "apr_network_io.h"
  31. #include "apr_errno.h"
  32. #include "apr_global_mutex.h"
  33. #include "apr_proc_mutex.h"
  34. #include "apr_time.h"
  35. #include "apr_dso.h"
  36. #include "apr_shm.h"
  37. #if APR_HAVE_DIRENT_H
  38. #include <dirent.h>
  39. #endif
  40. #if APR_HAVE_FCNTL_H
  41. #include <fcntl.h>
  42. #endif
  43. #if APR_HAVE_PTHREAD_H
  44. #include <pthread.h>
  45. #endif
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif /* __cplusplus */
  49. /**
  50. * @defgroup apr_portabile Portability Routines
  51. * @ingroup APR
  52. * @{
  53. */
  54. #ifdef WIN32
  55. /* The primitives for Windows types */
  56. typedef HANDLE apr_os_file_t;
  57. typedef HANDLE apr_os_dir_t;
  58. typedef SOCKET apr_os_sock_t;
  59. typedef HANDLE apr_os_proc_mutex_t;
  60. typedef HANDLE apr_os_thread_t;
  61. typedef HANDLE apr_os_proc_t;
  62. typedef DWORD apr_os_threadkey_t;
  63. typedef FILETIME apr_os_imp_time_t;
  64. typedef SYSTEMTIME apr_os_exp_time_t;
  65. typedef HANDLE apr_os_dso_handle_t;
  66. typedef HANDLE apr_os_shm_t;
  67. #elif defined(OS2)
  68. typedef HFILE apr_os_file_t;
  69. typedef HDIR apr_os_dir_t;
  70. typedef int apr_os_sock_t;
  71. typedef HMTX apr_os_proc_mutex_t;
  72. typedef TID apr_os_thread_t;
  73. typedef PID apr_os_proc_t;
  74. typedef PULONG apr_os_threadkey_t;
  75. typedef struct timeval apr_os_imp_time_t;
  76. typedef struct tm apr_os_exp_time_t;
  77. typedef HMODULE apr_os_dso_handle_t;
  78. typedef void* apr_os_shm_t;
  79. #elif defined(__BEOS__)
  80. #include <kernel/OS.h>
  81. #include <kernel/image.h>
  82. struct apr_os_proc_mutex_t {
  83. sem_id sem;
  84. int32 ben;
  85. };
  86. typedef int apr_os_file_t;
  87. typedef DIR apr_os_dir_t;
  88. typedef int apr_os_sock_t;
  89. typedef struct apr_os_proc_mutex_t apr_os_proc_mutex_t;
  90. typedef thread_id apr_os_thread_t;
  91. typedef thread_id apr_os_proc_t;
  92. typedef int apr_os_threadkey_t;
  93. typedef struct timeval apr_os_imp_time_t;
  94. typedef struct tm apr_os_exp_time_t;
  95. typedef image_id apr_os_dso_handle_t;
  96. typedef void* apr_os_shm_t;
  97. #elif defined(NETWARE)
  98. typedef int apr_os_file_t;
  99. typedef DIR apr_os_dir_t;
  100. typedef int apr_os_sock_t;
  101. typedef NXMutex_t apr_os_proc_mutex_t;
  102. typedef NXThreadId_t apr_os_thread_t;
  103. typedef long apr_os_proc_t;
  104. typedef NXKey_t apr_os_threadkey_t;
  105. typedef struct timeval apr_os_imp_time_t;
  106. typedef struct tm apr_os_exp_time_t;
  107. typedef void * apr_os_dso_handle_t;
  108. typedef void* apr_os_shm_t;
  109. #else
  110. /* Any other OS should go above this one. This is the lowest common
  111. * denominator typedefs for all UNIX-like systems. :)
  112. */
  113. /** Basic OS process mutex structure. */
  114. struct apr_os_proc_mutex_t {
  115. #if APR_HAS_SYSVSEM_SERIALIZE || APR_HAS_FCNTL_SERIALIZE || APR_HAS_FLOCK_SERIALIZE
  116. /** Value used for SYS V Semaphore, FCNTL and FLOCK serialization */
  117. int crossproc;
  118. #endif
  119. #if APR_HAS_PROC_PTHREAD_SERIALIZE
  120. /** Value used for PTHREAD serialization */
  121. pthread_mutex_t *pthread_interproc;
  122. #endif
  123. #if APR_HAS_THREADS
  124. /* If no threads, no need for thread locks */
  125. #if APR_USE_PTHREAD_SERIALIZE
  126. /** This value is currently unused within APR and Apache */
  127. pthread_mutex_t *intraproc;
  128. #endif
  129. #endif
  130. };
  131. typedef int apr_os_file_t; /**< native file */
  132. typedef DIR apr_os_dir_t; /**< native dir */
  133. typedef int apr_os_sock_t; /**< native dir */
  134. typedef struct apr_os_proc_mutex_t apr_os_proc_mutex_t; /**< native process
  135. * mutex
  136. */
  137. #if APR_HAS_THREADS && APR_HAVE_PTHREAD_H
  138. typedef pthread_t apr_os_thread_t; /**< native thread */
  139. typedef pthread_key_t apr_os_threadkey_t; /**< native thread address
  140. * space */
  141. #endif
  142. typedef pid_t apr_os_proc_t; /**< native pid */
  143. typedef struct timeval apr_os_imp_time_t; /**< native timeval */
  144. typedef struct tm apr_os_exp_time_t; /**< native tm */
  145. /** @var apr_os_dso_handle_t
  146. * native dso types
  147. */
  148. #if defined(HPUX) || defined(HPUX10) || defined(HPUX11)
  149. #include <dl.h>
  150. typedef shl_t apr_os_dso_handle_t;
  151. #elif defined(DARWIN)
  152. #include <mach-o/dyld.h>
  153. typedef NSModule apr_os_dso_handle_t;
  154. #else
  155. typedef void * apr_os_dso_handle_t;
  156. #endif
  157. typedef void* apr_os_shm_t; /**< native SHM */
  158. #endif
  159. /**
  160. * @typedef apr_os_sock_info_t
  161. * @brief alias for local OS socket
  162. */
  163. /**
  164. * everything APR needs to know about an active socket to construct
  165. * an APR socket from it; currently, this is platform-independent
  166. */
  167. struct apr_os_sock_info_t {
  168. apr_os_sock_t *os_sock; /**< always required */
  169. struct sockaddr *local; /**< NULL if not yet bound */
  170. struct sockaddr *remote; /**< NULL if not connected */
  171. int family; /**< always required (APR_INET, APR_INET6, etc.) */
  172. int type; /**< always required (SOCK_STREAM, SOCK_DGRAM, etc.) */
  173. int protocol; /**< 0 or actual protocol (APR_PROTO_SCTP, APR_PROTO_TCP, etc.) */
  174. };
  175. typedef struct apr_os_sock_info_t apr_os_sock_info_t;
  176. #if APR_PROC_MUTEX_IS_GLOBAL || defined(DOXYGEN)
  177. /** Opaque global mutex type */
  178. #define apr_os_global_mutex_t apr_os_proc_mutex_t
  179. /** @return apr_os_global_mutex */
  180. #define apr_os_global_mutex_get apr_os_proc_mutex_get
  181. #else
  182. /** Thread and process mutex for those platforms where process mutexes
  183. * are not held in threads.
  184. */
  185. struct apr_os_global_mutex_t {
  186. apr_pool_t *pool;
  187. apr_proc_mutex_t *proc_mutex;
  188. #if APR_HAS_THREADS
  189. apr_thread_mutex_t *thread_mutex;
  190. #endif /* APR_HAS_THREADS */
  191. };
  192. typedef struct apr_os_global_mutex_t apr_os_global_mutex_t;
  193. APR_DECLARE(apr_status_t) apr_os_global_mutex_get(apr_os_global_mutex_t *ospmutex,
  194. apr_global_mutex_t *pmutex);
  195. #endif
  196. /**
  197. * convert the file from apr type to os specific type.
  198. * @param thefile The os specific file we are converting to
  199. * @param file The apr file to convert.
  200. * @remark On Unix, it is only possible to get a file descriptor from
  201. * an apr file type.
  202. */
  203. APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile,
  204. apr_file_t *file);
  205. /**
  206. * convert the dir from apr type to os specific type.
  207. * @param thedir The os specific dir we are converting to
  208. * @param dir The apr dir to convert.
  209. */
  210. APR_DECLARE(apr_status_t) apr_os_dir_get(apr_os_dir_t **thedir,
  211. apr_dir_t *dir);
  212. /**
  213. * Convert the socket from an apr type to an OS specific socket
  214. * @param thesock The socket to convert.
  215. * @param sock The os specific equivalent of the apr socket..
  216. */
  217. APR_DECLARE(apr_status_t) apr_os_sock_get(apr_os_sock_t *thesock,
  218. apr_socket_t *sock);
  219. /**
  220. * Convert the proc mutex from os specific type to apr type
  221. * @param ospmutex The os specific proc mutex we are converting to.
  222. * @param pmutex The apr proc mutex to convert.
  223. */
  224. APR_DECLARE(apr_status_t) apr_os_proc_mutex_get(apr_os_proc_mutex_t *ospmutex,
  225. apr_proc_mutex_t *pmutex);
  226. /**
  227. * Get the exploded time in the platforms native format.
  228. * @param ostime the native time format
  229. * @param aprtime the time to convert
  230. */
  231. APR_DECLARE(apr_status_t) apr_os_exp_time_get(apr_os_exp_time_t **ostime,
  232. apr_time_exp_t *aprtime);
  233. /**
  234. * Get the imploded time in the platforms native format.
  235. * @param ostime the native time format
  236. * @param aprtime the time to convert
  237. */
  238. APR_DECLARE(apr_status_t) apr_os_imp_time_get(apr_os_imp_time_t **ostime,
  239. apr_time_t *aprtime);
  240. /**
  241. * convert the shm from apr type to os specific type.
  242. * @param osshm The os specific shm representation
  243. * @param shm The apr shm to convert.
  244. */
  245. APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
  246. apr_shm_t *shm);
  247. #if APR_HAS_THREADS || defined(DOXYGEN)
  248. /**
  249. * @defgroup apr_os_thread Thread portability Routines
  250. * @{
  251. */
  252. /**
  253. * convert the thread to os specific type from apr type.
  254. * @param thethd The apr thread to convert
  255. * @param thd The os specific thread we are converting to
  256. */
  257. APR_DECLARE(apr_status_t) apr_os_thread_get(apr_os_thread_t **thethd,
  258. apr_thread_t *thd);
  259. /**
  260. * convert the thread private memory key to os specific type from an apr type.
  261. * @param thekey The apr handle we are converting from.
  262. * @param key The os specific handle we are converting to.
  263. */
  264. APR_DECLARE(apr_status_t) apr_os_threadkey_get(apr_os_threadkey_t *thekey,
  265. apr_threadkey_t *key);
  266. /**
  267. * convert the thread from os specific type to apr type.
  268. * @param thd The apr thread we are converting to.
  269. * @param thethd The os specific thread to convert
  270. * @param cont The pool to use if it is needed.
  271. */
  272. APR_DECLARE(apr_status_t) apr_os_thread_put(apr_thread_t **thd,
  273. apr_os_thread_t *thethd,
  274. apr_pool_t *cont);
  275. /**
  276. * convert the thread private memory key from os specific type to apr type.
  277. * @param key The apr handle we are converting to.
  278. * @param thekey The os specific handle to convert
  279. * @param cont The pool to use if it is needed.
  280. */
  281. APR_DECLARE(apr_status_t) apr_os_threadkey_put(apr_threadkey_t **key,
  282. apr_os_threadkey_t *thekey,
  283. apr_pool_t *cont);
  284. /**
  285. * Get the thread ID
  286. */
  287. APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void);
  288. /**
  289. * Compare two thread id's
  290. * @param tid1 1st Thread ID to compare
  291. * @param tid2 2nd Thread ID to compare
  292. * @return non-zero if the two threads are equal, zero otherwise
  293. */
  294. APR_DECLARE(int) apr_os_thread_equal(apr_os_thread_t tid1,
  295. apr_os_thread_t tid2);
  296. /** @} */
  297. #endif /* APR_HAS_THREADS */
  298. /**
  299. * convert the file from os specific type to apr type.
  300. * @param file The apr file we are converting to.
  301. * @param thefile The os specific file to convert
  302. * @param flags The flags that were used to open this file.
  303. * @param cont The pool to use if it is needed.
  304. * @remark On Unix, it is only possible to put a file descriptor into
  305. * an apr file type.
  306. */
  307. APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file,
  308. apr_os_file_t *thefile,
  309. apr_int32_t flags, apr_pool_t *cont);
  310. /**
  311. * convert the file from os specific type to apr type.
  312. * @param file The apr file we are converting to.
  313. * @param thefile The os specific pipe to convert
  314. * @param cont The pool to use if it is needed.
  315. * @remark On Unix, it is only possible to put a file descriptor into
  316. * an apr file type.
  317. */
  318. APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
  319. apr_os_file_t *thefile,
  320. apr_pool_t *cont);
  321. /**
  322. * convert the file from os specific type to apr type.
  323. * @param file The apr file we are converting to.
  324. * @param thefile The os specific pipe to convert
  325. * @param register_cleanup A cleanup will be registered on the apr_file_t
  326. * to issue apr_file_close().
  327. * @param cont The pool to use if it is needed.
  328. * @remark On Unix, it is only possible to put a file descriptor into
  329. * an apr file type.
  330. */
  331. APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file,
  332. apr_os_file_t *thefile,
  333. int register_cleanup,
  334. apr_pool_t *cont);
  335. /**
  336. * convert the dir from os specific type to apr type.
  337. * @param dir The apr dir we are converting to.
  338. * @param thedir The os specific dir to convert
  339. * @param cont The pool to use when creating to apr directory.
  340. */
  341. APR_DECLARE(apr_status_t) apr_os_dir_put(apr_dir_t **dir,
  342. apr_os_dir_t *thedir,
  343. apr_pool_t *cont);
  344. /**
  345. * Convert a socket from the os specific type to the apr type
  346. * @param sock The pool to use.
  347. * @param thesock The socket to convert to.
  348. * @param cont The socket we are converting to an apr type.
  349. * @remark If it is a true socket, it is best to call apr_os_sock_make()
  350. * and provide APR with more information about the socket.
  351. */
  352. APR_DECLARE(apr_status_t) apr_os_sock_put(apr_socket_t **sock,
  353. apr_os_sock_t *thesock,
  354. apr_pool_t *cont);
  355. /**
  356. * Create a socket from an existing descriptor and local and remote
  357. * socket addresses.
  358. * @param apr_sock The new socket that has been set up
  359. * @param os_sock_info The os representation of the socket handle and
  360. * other characteristics of the socket
  361. * @param cont The pool to use
  362. * @remark If you only know the descriptor/handle or if it isn't really
  363. * a true socket, use apr_os_sock_put() instead.
  364. */
  365. APR_DECLARE(apr_status_t) apr_os_sock_make(apr_socket_t **apr_sock,
  366. apr_os_sock_info_t *os_sock_info,
  367. apr_pool_t *cont);
  368. /**
  369. * Convert the proc mutex from os specific type to apr type
  370. * @param pmutex The apr proc mutex we are converting to.
  371. * @param ospmutex The os specific proc mutex to convert.
  372. * @param cont The pool to use if it is needed.
  373. */
  374. APR_DECLARE(apr_status_t) apr_os_proc_mutex_put(apr_proc_mutex_t **pmutex,
  375. apr_os_proc_mutex_t *ospmutex,
  376. apr_pool_t *cont);
  377. /**
  378. * Put the imploded time in the APR format.
  379. * @param aprtime the APR time format
  380. * @param ostime the time to convert
  381. * @param cont the pool to use if necessary
  382. */
  383. APR_DECLARE(apr_status_t) apr_os_imp_time_put(apr_time_t *aprtime,
  384. apr_os_imp_time_t **ostime,
  385. apr_pool_t *cont);
  386. /**
  387. * Put the exploded time in the APR format.
  388. * @param aprtime the APR time format
  389. * @param ostime the time to convert
  390. * @param cont the pool to use if necessary
  391. */
  392. APR_DECLARE(apr_status_t) apr_os_exp_time_put(apr_time_exp_t *aprtime,
  393. apr_os_exp_time_t **ostime,
  394. apr_pool_t *cont);
  395. /**
  396. * convert the shared memory from os specific type to apr type.
  397. * @param shm The apr shm representation of osshm
  398. * @param osshm The os specific shm identity
  399. * @param cont The pool to use if it is needed.
  400. * @remark On fork()ed architectures, this is typically nothing more than
  401. * the memory block mapped. On non-fork architectures, this is typically
  402. * some internal handle to pass the mapping from process to process.
  403. */
  404. APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **shm,
  405. apr_os_shm_t *osshm,
  406. apr_pool_t *cont);
  407. #if APR_HAS_DSO || defined(DOXYGEN)
  408. /**
  409. * @defgroup apr_os_dso DSO (Dynamic Loading) Portability Routines
  410. * @{
  411. */
  412. /**
  413. * convert the dso handle from os specific to apr
  414. * @param dso The apr handle we are converting to
  415. * @param thedso the os specific handle to convert
  416. * @param pool the pool to use if it is needed
  417. */
  418. APR_DECLARE(apr_status_t) apr_os_dso_handle_put(apr_dso_handle_t **dso,
  419. apr_os_dso_handle_t thedso,
  420. apr_pool_t *pool);
  421. /**
  422. * convert the apr dso handle into an os specific one
  423. * @param aprdso The apr dso handle to convert
  424. * @param dso The os specific dso to return
  425. */
  426. APR_DECLARE(apr_status_t) apr_os_dso_handle_get(apr_os_dso_handle_t *dso,
  427. apr_dso_handle_t *aprdso);
  428. /** @} */
  429. #endif /* APR_HAS_DSO */
  430. #if APR_HAS_OS_UUID
  431. /**
  432. * Private: apr-util's apr_uuid module when supported by the platform
  433. */
  434. APR_DECLARE(apr_status_t) apr_os_uuid_get(unsigned char *uuid_data);
  435. #endif
  436. /**
  437. * Get the name of the system default character set.
  438. * @param pool the pool to allocate the name from, if needed
  439. */
  440. APR_DECLARE(const char*) apr_os_default_encoding(apr_pool_t *pool);
  441. /**
  442. * Get the name of the current locale character set.
  443. * @param pool the pool to allocate the name from, if needed
  444. * @remark Defers to apr_os_default_encoding if the current locale's
  445. * data can't be retrieved on this system.
  446. */
  447. APR_DECLARE(const char*) apr_os_locale_encoding(apr_pool_t *pool);
  448. /** @} */
  449. #ifdef __cplusplus
  450. }
  451. #endif
  452. #endif /* ! APR_PORTABLE_H */