apr_portable.h 21 KB

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