coroipc_ipc.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 SOCKET_SERVICE_SECURITY_VIOLATION 0xFFFFFFFE
  140. #define ZC_ALLOC_HEADER 0xFFFFFFFF
  141. #define ZC_FREE_HEADER 0xFFFFFFFE
  142. #define ZC_EXECUTE_HEADER 0xFFFFFFFD
  143. static inline cs_error_t
  144. ipc_sem_wait (
  145. struct control_buffer *control_buffer,
  146. enum ipc_semaphore_identifiers sem_id,
  147. int fd)
  148. {
  149. #if _POSIX_THREAD_PROCESS_SHARED < 1
  150. struct sembuf sop;
  151. #else
  152. struct timespec timeout;
  153. struct pollfd pfd;
  154. sem_t *sem = NULL;
  155. #endif
  156. int res;
  157. #if _POSIX_THREAD_PROCESS_SHARED > 0
  158. switch (sem_id) {
  159. case SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT:
  160. sem = &control_buffer->sem_request_or_flush_or_exit;
  161. break;
  162. case SEMAPHORE_RESPONSE:
  163. sem = &control_buffer->sem_request;
  164. break;
  165. case SEMAPHORE_DISPATCH:
  166. sem = &control_buffer->sem_response;
  167. break;
  168. case SEMAPHORE_REQUEST:
  169. sem = &control_buffer->sem_dispatch;
  170. break;
  171. }
  172. if (fd == IPC_SEMWAIT_NOFILE) {
  173. retry_sem_wait:
  174. res = sem_wait (sem);
  175. if (res == -1 && errno == EINTR) {
  176. goto retry_sem_wait;
  177. } else
  178. if (res == -1) {
  179. return (CS_ERR_LIBRARY);
  180. }
  181. } else {
  182. if (control_buffer->ipc_closed) {
  183. return (CS_ERR_LIBRARY);
  184. }
  185. timeout.tv_sec = time(NULL) + IPC_SEMWAIT_TIMEOUT;
  186. timeout.tv_nsec = 0;
  187. retry_sem_timedwait:
  188. res = sem_timedwait (sem, &timeout);
  189. if (res == -1 && errno == ETIMEDOUT) {
  190. pfd.fd = fd;
  191. pfd.events = 0;
  192. /*
  193. * Determine if server has failed (ERR_LIBRARY) or
  194. * is just performing slowly or in configuration change
  195. * (retry sem op)
  196. */
  197. retry_poll:
  198. res = poll (&pfd, 1, 0);
  199. if (res == -1 && errno == EINTR) {
  200. goto retry_poll;
  201. } else
  202. if (res == -1) {
  203. return (CS_ERR_LIBRARY);
  204. }
  205. if (res == 1) {
  206. if (pfd.revents == POLLERR ||
  207. pfd.revents == POLLHUP ||
  208. pfd.revents == POLLNVAL) {
  209. return (CS_ERR_LIBRARY);
  210. }
  211. }
  212. goto retry_sem_timedwait;
  213. } else
  214. if (res == -1 && errno == EINTR) {
  215. goto retry_sem_timedwait;
  216. } else
  217. if (res == -1) {
  218. return (CS_ERR_LIBRARY);
  219. }
  220. if (res == 0 && control_buffer->ipc_closed) {
  221. return (CS_ERR_LIBRARY);
  222. }
  223. }
  224. #else
  225. sop.sem_num = sem_id;
  226. sop.sem_op = -1;
  227. sop.sem_flg = 0;
  228. retry_semop:
  229. res = semop (control_buffer->semid, &sop, 1);
  230. if (res == -1 && errno == EINTR) {
  231. return (CS_ERR_TRY_AGAIN);
  232. goto retry_semop;
  233. } else
  234. if (res == -1 && errno == EACCES) {
  235. return (CS_ERR_TRY_AGAIN);
  236. } else
  237. if (res == -1) {
  238. return (CS_ERR_LIBRARY);
  239. }
  240. #endif
  241. return (CS_OK);
  242. }
  243. static inline cs_error_t
  244. ipc_sem_post (
  245. struct control_buffer *control_buffer,
  246. enum ipc_semaphore_identifiers sem_id)
  247. {
  248. #if _POSIX_THREAD_PROCESS_SHARED < 1
  249. struct sembuf sop;
  250. #else
  251. sem_t *sem = NULL;
  252. #endif
  253. int res;
  254. #if _POSIX_THREAD_PROCESS_SHARED > 0
  255. switch (sem_id) {
  256. case SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT:
  257. sem = &control_buffer->sem_request_or_flush_or_exit;
  258. break;
  259. case SEMAPHORE_RESPONSE:
  260. sem = &control_buffer->sem_request;
  261. break;
  262. case SEMAPHORE_DISPATCH:
  263. sem = &control_buffer->sem_response;
  264. break;
  265. case SEMAPHORE_REQUEST:
  266. sem = &control_buffer->sem_dispatch;
  267. break;
  268. }
  269. res = sem_post (sem);
  270. if (res == -1) {
  271. return (CS_ERR_LIBRARY);
  272. }
  273. #else
  274. sop.sem_num = sem_id;
  275. sop.sem_op = 1;
  276. sop.sem_flg = 0;
  277. retry_semop:
  278. res = semop (control_buffer->semid, &sop, 1);
  279. if (res == -1 && errno == EINTR) {
  280. goto retry_semop;
  281. } else
  282. if (res == -1) {
  283. return (CS_ERR_LIBRARY);
  284. }
  285. #endif
  286. return (CS_OK);
  287. }
  288. static inline cs_error_t
  289. ipc_sem_getvalue (
  290. struct control_buffer *control_buffer,
  291. enum ipc_semaphore_identifiers sem_id,
  292. int *sem_value)
  293. {
  294. #if _POSIX_THREAD_PROCESS_SHARED < 1
  295. struct sembuf sop;
  296. int sem_value_hold;
  297. #else
  298. sem_t *sem = NULL;
  299. int res;
  300. #endif
  301. #if _POSIX_THREAD_PROCESS_SHARED > 0
  302. switch (sem_id) {
  303. case SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT:
  304. sem = &control_buffer->sem_request_or_flush_or_exit;
  305. break;
  306. case SEMAPHORE_RESPONSE:
  307. sem = &control_buffer->sem_request;
  308. break;
  309. case SEMAPHORE_DISPATCH:
  310. sem = &control_buffer->sem_response;
  311. break;
  312. case SEMAPHORE_REQUEST:
  313. sem = &control_buffer->sem_dispatch;
  314. break;
  315. }
  316. res = sem_getvalue (sem, sem_value);
  317. if (res == -1) {
  318. return (CS_ERR_LIBRARY);
  319. }
  320. #else
  321. sop.sem_num = sem_id;
  322. sop.sem_op = 1;
  323. sop.sem_flg = 0;
  324. retry_semctl:
  325. sem_value_hold = semctl (control_buffer->semid, sem_id, GETVAL);
  326. if (sem_value_hold == -1 && errno == EINTR) {
  327. goto retry_semctl;
  328. } else
  329. if (sem_value_hold == -1) {
  330. return (CS_ERR_LIBRARY);
  331. }
  332. *sem_value = sem_value_hold;
  333. #endif
  334. return (CS_OK);
  335. }
  336. #endif /* COROIPC_IPC_H_DEFINED */