coroipc_ipc.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. int ipc_closed;
  86. };
  87. enum res_init_types {
  88. MESSAGE_RES_INIT
  89. };
  90. typedef struct {
  91. int service __attribute__((aligned(8)));
  92. unsigned long long semkey __attribute__((aligned(8)));
  93. char control_file[PATH_MAX] __attribute__((aligned(8)));
  94. char request_file[PATH_MAX] __attribute__((aligned(8)));
  95. char response_file[PATH_MAX] __attribute__((aligned(8)));
  96. char dispatch_file[PATH_MAX] __attribute__((aligned(8)));
  97. size_t control_size __attribute__((aligned(8)));
  98. size_t request_size __attribute__((aligned(8)));
  99. size_t response_size __attribute__((aligned(8)));
  100. size_t dispatch_size __attribute__((aligned(8)));
  101. } mar_req_setup_t __attribute__((aligned(8)));
  102. typedef struct {
  103. int error __attribute__((aligned(8)));
  104. } mar_res_setup_t __attribute__((aligned(8)));
  105. typedef struct {
  106. uid_t euid __attribute__((aligned(8)));
  107. gid_t egid __attribute__((aligned(8)));
  108. } mar_req_priv_change __attribute__((aligned(8)));
  109. typedef struct {
  110. coroipc_response_header_t header __attribute__((aligned(8)));
  111. uint64_t conn_info __attribute__((aligned(8)));
  112. } mar_res_lib_response_init_t __attribute__((aligned(8)));
  113. typedef struct {
  114. coroipc_response_header_t header __attribute__((aligned(8)));
  115. } mar_res_lib_dispatch_init_t __attribute__((aligned(8)));
  116. typedef struct {
  117. uint32_t nodeid __attribute__((aligned(8)));
  118. void *conn __attribute__((aligned(8)));
  119. } mar_message_source_t __attribute__((aligned(8)));
  120. typedef struct {
  121. coroipc_request_header_t header __attribute__((aligned(8)));
  122. size_t map_size __attribute__((aligned(8)));
  123. char path_to_file[128] __attribute__((aligned(8)));
  124. } mar_req_coroipcc_zc_alloc_t __attribute__((aligned(8)));
  125. typedef struct {
  126. coroipc_request_header_t header __attribute__((aligned(8)));
  127. size_t map_size __attribute__((aligned(8)));
  128. uint64_t server_address __attribute__((aligned(8)));
  129. } mar_req_coroipcc_zc_free_t __attribute__((aligned(8)));
  130. typedef struct {
  131. coroipc_request_header_t header __attribute__((aligned(8)));
  132. uint64_t server_address __attribute__((aligned(8)));
  133. } mar_req_coroipcc_zc_execute_t __attribute__((aligned(8)));
  134. struct coroipcs_zc_header {
  135. int map_size;
  136. uint64_t server_address;
  137. };
  138. #define SOCKET_SERVICE_INIT 0xFFFFFFFF
  139. #define ZC_ALLOC_HEADER 0xFFFFFFFF
  140. #define ZC_FREE_HEADER 0xFFFFFFFE
  141. #define ZC_EXECUTE_HEADER 0xFFFFFFFD
  142. static inline cs_error_t
  143. ipc_sem_wait (
  144. struct control_buffer *control_buffer,
  145. enum ipc_semaphore_identifiers sem_id,
  146. int fd)
  147. {
  148. #if _POSIX_THREAD_PROCESS_SHARED < 1
  149. struct sembuf sop;
  150. #else
  151. struct timespec timeout;
  152. struct pollfd pfd;
  153. sem_t *sem = NULL;
  154. #endif
  155. int res;
  156. #if _POSIX_THREAD_PROCESS_SHARED > 0
  157. switch (sem_id) {
  158. case SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT:
  159. sem = &control_buffer->sem_request_or_flush_or_exit;
  160. break;
  161. case SEMAPHORE_RESPONSE:
  162. sem = &control_buffer->sem_request;
  163. break;
  164. case SEMAPHORE_DISPATCH:
  165. sem = &control_buffer->sem_response;
  166. break;
  167. case SEMAPHORE_REQUEST:
  168. sem = &control_buffer->sem_dispatch;
  169. break;
  170. }
  171. if (fd == IPC_SEMWAIT_NOFILE) {
  172. retry_sem_wait:
  173. res = sem_wait (sem);
  174. if (res == -1 && errno == EINTR) {
  175. goto retry_sem_wait;
  176. } else
  177. if (res == -1) {
  178. return (CS_ERR_LIBRARY);
  179. }
  180. } else {
  181. if (control_buffer->ipc_closed) {
  182. return (CS_ERR_LIBRARY);
  183. }
  184. timeout.tv_sec = time(NULL) + IPC_SEMWAIT_TIMEOUT;
  185. timeout.tv_nsec = 0;
  186. retry_sem_timedwait:
  187. res = sem_timedwait (sem, &timeout);
  188. if (res == -1 && errno == ETIMEDOUT) {
  189. pfd.fd = fd;
  190. pfd.events = 0;
  191. /*
  192. * Determine if server has failed (ERR_LIBRARY) or
  193. * is just performing slowly or in configuration change
  194. * (retry sem op)
  195. */
  196. retry_poll:
  197. res = poll (&pfd, 1, 0);
  198. if (res == -1 && errno == EINTR) {
  199. goto retry_poll;
  200. } else
  201. if (res == -1) {
  202. return (CS_ERR_LIBRARY);
  203. }
  204. if (res == 1) {
  205. if (pfd.revents == POLLERR ||
  206. pfd.revents == POLLHUP ||
  207. pfd.revents == POLLNVAL) {
  208. return (CS_ERR_LIBRARY);
  209. }
  210. }
  211. goto retry_sem_timedwait;
  212. } else
  213. if (res == -1 && errno == EINTR) {
  214. goto retry_sem_timedwait;
  215. } else
  216. if (res == -1) {
  217. return (CS_ERR_LIBRARY);
  218. }
  219. if (res == 0 && control_buffer->ipc_closed) {
  220. return (CS_ERR_LIBRARY);
  221. }
  222. }
  223. #else
  224. sop.sem_num = sem_id;
  225. sop.sem_op = -1;
  226. sop.sem_flg = 0;
  227. retry_semop:
  228. res = semop (control_buffer->semid, &sop, 1);
  229. if (res == -1 && errno == EINTR) {
  230. return (CS_ERR_TRY_AGAIN);
  231. goto retry_semop;
  232. } else
  233. if (res == -1 && errno == EACCES) {
  234. return (CS_ERR_TRY_AGAIN);
  235. } else
  236. if (res == -1) {
  237. return (CS_ERR_LIBRARY);
  238. }
  239. #endif
  240. return (CS_OK);
  241. }
  242. static inline cs_error_t
  243. ipc_sem_post (
  244. struct control_buffer *control_buffer,
  245. enum ipc_semaphore_identifiers sem_id)
  246. {
  247. #if _POSIX_THREAD_PROCESS_SHARED < 1
  248. struct sembuf sop;
  249. #else
  250. sem_t *sem = NULL;
  251. #endif
  252. int res;
  253. #if _POSIX_THREAD_PROCESS_SHARED > 0
  254. switch (sem_id) {
  255. case SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT:
  256. sem = &control_buffer->sem_request_or_flush_or_exit;
  257. break;
  258. case SEMAPHORE_RESPONSE:
  259. sem = &control_buffer->sem_request;
  260. break;
  261. case SEMAPHORE_DISPATCH:
  262. sem = &control_buffer->sem_response;
  263. break;
  264. case SEMAPHORE_REQUEST:
  265. sem = &control_buffer->sem_dispatch;
  266. break;
  267. }
  268. res = sem_post (sem);
  269. if (res == -1) {
  270. return (CS_ERR_LIBRARY);
  271. }
  272. #else
  273. sop.sem_num = sem_id;
  274. sop.sem_op = 1;
  275. sop.sem_flg = 0;
  276. retry_semop:
  277. res = semop (control_buffer->semid, &sop, 1);
  278. if (res == -1 && errno == EINTR) {
  279. goto retry_semop;
  280. } else
  281. if (res == -1) {
  282. return (CS_ERR_LIBRARY);
  283. }
  284. #endif
  285. return (CS_OK);
  286. }
  287. static inline cs_error_t
  288. ipc_sem_getvalue (
  289. struct control_buffer *control_buffer,
  290. enum ipc_semaphore_identifiers sem_id,
  291. int *sem_value)
  292. {
  293. #if _POSIX_THREAD_PROCESS_SHARED < 1
  294. struct sembuf sop;
  295. int sem_value_hold;
  296. #else
  297. sem_t *sem = NULL;
  298. int res;
  299. #endif
  300. #if _POSIX_THREAD_PROCESS_SHARED > 0
  301. switch (sem_id) {
  302. case SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT:
  303. sem = &control_buffer->sem_request_or_flush_or_exit;
  304. break;
  305. case SEMAPHORE_RESPONSE:
  306. sem = &control_buffer->sem_request;
  307. break;
  308. case SEMAPHORE_DISPATCH:
  309. sem = &control_buffer->sem_response;
  310. break;
  311. case SEMAPHORE_REQUEST:
  312. sem = &control_buffer->sem_dispatch;
  313. break;
  314. }
  315. res = sem_getvalue (sem, sem_value);
  316. if (res == -1) {
  317. return (CS_ERR_LIBRARY);
  318. }
  319. #else
  320. sop.sem_num = sem_id;
  321. sop.sem_op = 1;
  322. sop.sem_flg = 0;
  323. retry_semctl:
  324. sem_value_hold = semctl (control_buffer->semid, sem_id, GETVAL);
  325. if (sem_value_hold == -1 && errno == EINTR) {
  326. goto retry_semctl;
  327. } else
  328. if (sem_value_hold == -1) {
  329. return (CS_ERR_LIBRARY);
  330. }
  331. *sem_value = sem_value_hold;
  332. #endif
  333. return (CS_OK);
  334. }
  335. #endif /* COROIPC_IPC_H_DEFINED */