coroipc_ipc.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef COROIPC_IPC_H_DEFINED
  35. #define COROIPC_IPC_H_DEFINED
  36. #include <unistd.h>
  37. #include <poll.h>
  38. #include <time.h>
  39. #include "corotypes.h"
  40. #include "config.h"
  41. /*
  42. * Darwin claims to support process shared synchronization
  43. * but it really does not. The unistd.h header file is wrong.
  44. */
  45. #if defined(COROSYNC_DARWIN) || defined(__UCLIBC__)
  46. #undef _POSIX_THREAD_PROCESS_SHARED
  47. #define _POSIX_THREAD_PROCESS_SHARED -1
  48. #endif
  49. #ifndef _POSIX_THREAD_PROCESS_SHARED
  50. #define _POSIX_THREAD_PROCESS_SHARED -1
  51. #endif
  52. #if _POSIX_THREAD_PROCESS_SHARED > 0
  53. #include <semaphore.h>
  54. #else
  55. #include <sys/sem.h>
  56. #endif
  57. /*
  58. * Define sem_wait timeout (real timeout will be (n-1;n) )
  59. */
  60. #define IPC_SEMWAIT_TIMEOUT 2
  61. #define IPC_SEMWAIT_NOFILE 0
  62. enum req_init_types {
  63. MESSAGE_REQ_RESPONSE_INIT = 0,
  64. MESSAGE_REQ_DISPATCH_INIT = 1
  65. };
  66. #define MESSAGE_REQ_CHANGE_EUID 1
  67. enum ipc_semaphore_identifiers {
  68. SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT = 0,
  69. SEMAPHORE_REQUEST = 1,
  70. SEMAPHORE_RESPONSE = 2,
  71. SEMAPHORE_DISPATCH = 3
  72. };
  73. struct control_buffer {
  74. unsigned int read;
  75. unsigned int write;
  76. int flow_control_enabled;
  77. #if _POSIX_THREAD_PROCESS_SHARED > 0
  78. sem_t sem_request_or_flush_or_exit;
  79. sem_t sem_response;
  80. sem_t sem_dispatch;
  81. sem_t sem_request;
  82. #else
  83. int semid;
  84. #endif
  85. };
  86. enum res_init_types {
  87. MESSAGE_RES_INIT
  88. };
  89. typedef struct {
  90. int service __attribute__((aligned(8)));
  91. unsigned long long semkey __attribute__((aligned(8)));
  92. char control_file[PATH_MAX] __attribute__((aligned(8)));
  93. char request_file[PATH_MAX] __attribute__((aligned(8)));
  94. char response_file[PATH_MAX] __attribute__((aligned(8)));
  95. char dispatch_file[PATH_MAX] __attribute__((aligned(8)));
  96. size_t control_size __attribute__((aligned(8)));
  97. size_t request_size __attribute__((aligned(8)));
  98. size_t response_size __attribute__((aligned(8)));
  99. size_t dispatch_size __attribute__((aligned(8)));
  100. } mar_req_setup_t __attribute__((aligned(8)));
  101. typedef struct {
  102. int error __attribute__((aligned(8)));
  103. } mar_res_setup_t __attribute__((aligned(8)));
  104. typedef struct {
  105. uid_t euid __attribute__((aligned(8)));
  106. gid_t egid __attribute__((aligned(8)));
  107. } mar_req_priv_change __attribute__((aligned(8)));
  108. typedef struct {
  109. coroipc_response_header_t header __attribute__((aligned(8)));
  110. uint64_t conn_info __attribute__((aligned(8)));
  111. } mar_res_lib_response_init_t __attribute__((aligned(8)));
  112. typedef struct {
  113. coroipc_response_header_t header __attribute__((aligned(8)));
  114. } mar_res_lib_dispatch_init_t __attribute__((aligned(8)));
  115. typedef struct {
  116. uint32_t nodeid __attribute__((aligned(8)));
  117. void *conn __attribute__((aligned(8)));
  118. } mar_message_source_t __attribute__((aligned(8)));
  119. typedef struct {
  120. coroipc_request_header_t header __attribute__((aligned(8)));
  121. size_t map_size __attribute__((aligned(8)));
  122. char path_to_file[128] __attribute__((aligned(8)));
  123. } mar_req_coroipcc_zc_alloc_t __attribute__((aligned(8)));
  124. typedef struct {
  125. coroipc_request_header_t header __attribute__((aligned(8)));
  126. size_t map_size __attribute__((aligned(8)));
  127. uint64_t server_address __attribute__((aligned(8)));
  128. } mar_req_coroipcc_zc_free_t __attribute__((aligned(8)));
  129. typedef struct {
  130. coroipc_request_header_t header __attribute__((aligned(8)));
  131. uint64_t server_address __attribute__((aligned(8)));
  132. } mar_req_coroipcc_zc_execute_t __attribute__((aligned(8)));
  133. struct coroipcs_zc_header {
  134. int map_size;
  135. uint64_t server_address;
  136. };
  137. #define SOCKET_SERVICE_INIT 0xFFFFFFFF
  138. #define ZC_ALLOC_HEADER 0xFFFFFFFF
  139. #define ZC_FREE_HEADER 0xFFFFFFFE
  140. #define ZC_EXECUTE_HEADER 0xFFFFFFFD
  141. static inline cs_error_t
  142. ipc_sem_wait (
  143. struct control_buffer *control_buffer,
  144. enum ipc_semaphore_identifiers sem_id,
  145. int fd)
  146. {
  147. #if _POSIX_THREAD_PROCESS_SHARED < 1
  148. struct sembuf sop;
  149. #else
  150. struct timespec timeout;
  151. struct pollfd pfd;
  152. sem_t *sem = NULL;
  153. #endif
  154. int res;
  155. #if _POSIX_THREAD_PROCESS_SHARED > 0
  156. switch (sem_id) {
  157. case SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT:
  158. sem = &control_buffer->sem_request_or_flush_or_exit;
  159. break;
  160. case SEMAPHORE_RESPONSE:
  161. sem = &control_buffer->sem_request;
  162. break;
  163. case SEMAPHORE_DISPATCH:
  164. sem = &control_buffer->sem_response;
  165. break;
  166. case SEMAPHORE_REQUEST:
  167. sem = &control_buffer->sem_dispatch;
  168. break;
  169. }
  170. if (fd == IPC_SEMWAIT_NOFILE) {
  171. retry_sem_wait:
  172. res = sem_wait (sem);
  173. if (res == -1 && errno == EINTR) {
  174. goto retry_sem_wait;
  175. } else
  176. if (res == -1) {
  177. return (CS_ERR_LIBRARY);
  178. }
  179. } else {
  180. timeout.tv_sec = time(NULL) + IPC_SEMWAIT_TIMEOUT;
  181. timeout.tv_nsec = 0;
  182. retry_sem_timedwait:
  183. res = sem_timedwait (sem, &timeout);
  184. if (res == -1 && errno == ETIMEDOUT) {
  185. pfd.fd = fd;
  186. pfd.events = 0;
  187. /*
  188. * Determine if server has failed (ERR_LIBRARY) or
  189. * is just performing slowly or in configuration change
  190. * (retry sem op)
  191. */
  192. retry_poll:
  193. res = poll (&pfd, 1, 0);
  194. if (res == -1 && errno == EINTR) {
  195. goto retry_poll;
  196. } else
  197. if (res == -1) {
  198. return (CS_ERR_LIBRARY);
  199. }
  200. if (res == 1) {
  201. if (pfd.revents == POLLERR ||
  202. pfd.revents == POLLHUP ||
  203. pfd.revents == POLLNVAL) {
  204. return (CS_ERR_LIBRARY);
  205. }
  206. }
  207. goto retry_sem_timedwait;
  208. } else
  209. if (res == -1 && errno == EINTR) {
  210. goto retry_sem_timedwait;
  211. } else
  212. if (res == -1) {
  213. return (CS_ERR_LIBRARY);
  214. }
  215. }
  216. #else
  217. sop.sem_num = sem_id;
  218. sop.sem_op = -1;
  219. sop.sem_flg = 0;
  220. retry_semop:
  221. res = semop (control_buffer->semid, &sop, 1);
  222. if (res == -1 && errno == EINTR) {
  223. return (CS_ERR_TRY_AGAIN);
  224. goto retry_semop;
  225. } else
  226. if (res == -1 && errno == EACCES) {
  227. return (CS_ERR_TRY_AGAIN);
  228. } else
  229. if (res == -1) {
  230. return (CS_ERR_LIBRARY);
  231. }
  232. #endif
  233. return (CS_OK);
  234. }
  235. static inline cs_error_t
  236. ipc_sem_post (
  237. struct control_buffer *control_buffer,
  238. enum ipc_semaphore_identifiers sem_id)
  239. {
  240. #if _POSIX_THREAD_PROCESS_SHARED < 1
  241. struct sembuf sop;
  242. #else
  243. sem_t *sem = NULL;
  244. #endif
  245. int res;
  246. #if _POSIX_THREAD_PROCESS_SHARED > 0
  247. switch (sem_id) {
  248. case SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT:
  249. sem = &control_buffer->sem_request_or_flush_or_exit;
  250. break;
  251. case SEMAPHORE_RESPONSE:
  252. sem = &control_buffer->sem_request;
  253. break;
  254. case SEMAPHORE_DISPATCH:
  255. sem = &control_buffer->sem_response;
  256. break;
  257. case SEMAPHORE_REQUEST:
  258. sem = &control_buffer->sem_dispatch;
  259. break;
  260. }
  261. res = sem_post (sem);
  262. if (res == -1) {
  263. return (CS_ERR_LIBRARY);
  264. }
  265. #else
  266. sop.sem_num = sem_id;
  267. sop.sem_op = 1;
  268. sop.sem_flg = 0;
  269. retry_semop:
  270. res = semop (control_buffer->semid, &sop, 1);
  271. if (res == -1 && errno == EINTR) {
  272. goto retry_semop;
  273. } else
  274. if (res == -1) {
  275. return (CS_ERR_LIBRARY);
  276. }
  277. #endif
  278. return (CS_OK);
  279. }
  280. static inline cs_error_t
  281. ipc_sem_getvalue (
  282. struct control_buffer *control_buffer,
  283. enum ipc_semaphore_identifiers sem_id,
  284. int *sem_value)
  285. {
  286. #if _POSIX_THREAD_PROCESS_SHARED < 1
  287. struct sembuf sop;
  288. int sem_value_hold;
  289. #else
  290. sem_t *sem = NULL;
  291. int res;
  292. #endif
  293. #if _POSIX_THREAD_PROCESS_SHARED > 0
  294. switch (sem_id) {
  295. case SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT:
  296. sem = &control_buffer->sem_request_or_flush_or_exit;
  297. break;
  298. case SEMAPHORE_RESPONSE:
  299. sem = &control_buffer->sem_request;
  300. break;
  301. case SEMAPHORE_DISPATCH:
  302. sem = &control_buffer->sem_response;
  303. break;
  304. case SEMAPHORE_REQUEST:
  305. sem = &control_buffer->sem_dispatch;
  306. break;
  307. }
  308. res = sem_getvalue (sem, sem_value);
  309. if (res == -1) {
  310. return (CS_ERR_LIBRARY);
  311. }
  312. #else
  313. sop.sem_num = sem_id;
  314. sop.sem_op = 1;
  315. sop.sem_flg = 0;
  316. retry_semctl:
  317. sem_value_hold = semctl (control_buffer->semid, sem_id, GETVAL);
  318. if (sem_value_hold == -1 && errno == EINTR) {
  319. goto retry_semctl;
  320. } else
  321. if (sem_value_hold == -1) {
  322. return (CS_ERR_LIBRARY);
  323. }
  324. *sem_value = sem_value_hold;
  325. #endif
  326. return (CS_OK);
  327. }
  328. #endif /* COROIPC_IPC_H_DEFINED */