coroipcs.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*
  2. * Copyright (c) 2006-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. #include <config.h>
  35. #ifndef _GNU_SOURCE
  36. #define _GNU_SOURCE 1
  37. #endif
  38. #include <pthread.h>
  39. #include <assert.h>
  40. #include <pwd.h>
  41. #include <grp.h>
  42. #include <sys/types.h>
  43. #include <sys/poll.h>
  44. #include <sys/uio.h>
  45. #include <sys/mman.h>
  46. #include <sys/socket.h>
  47. #include <sys/un.h>
  48. #include <sys/time.h>
  49. #include <sys/resource.h>
  50. #include <sys/wait.h>
  51. #include <netinet/in.h>
  52. #include <arpa/inet.h>
  53. #include <unistd.h>
  54. #include <fcntl.h>
  55. #include <stdlib.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <signal.h>
  59. #include <sched.h>
  60. #include <time.h>
  61. #if defined(HAVE_GETPEERUCRED)
  62. #include <ucred.h>
  63. #endif
  64. #include <string.h>
  65. #include <sys/shm.h>
  66. #include <sys/sem.h>
  67. #include <corosync/corotypes.h>
  68. #include <corosync/list.h>
  69. #include <corosync/coroipc_types.h>
  70. #include <corosync/coroipcs.h>
  71. #include <corosync/coroipc_ipc.h>
  72. #ifndef MSG_NOSIGNAL
  73. #define MSG_NOSIGNAL 0
  74. #endif
  75. #define SERVER_BACKLOG 5
  76. #define MSG_SEND_LOCKED 0
  77. #define MSG_SEND_UNLOCKED 1
  78. static struct coroipcs_init_state *api;
  79. DECLARE_LIST_INIT (conn_info_list_head);
  80. struct outq_item {
  81. void *msg;
  82. size_t mlen;
  83. struct list_head list;
  84. };
  85. struct zcb_mapped {
  86. struct list_head list;
  87. void *addr;
  88. size_t size;
  89. };
  90. #if defined(_SEM_SEMUN_UNDEFINED)
  91. union semun {
  92. int val;
  93. struct semid_ds *buf;
  94. unsigned short int *array;
  95. struct seminfo *__buf;
  96. };
  97. #endif
  98. enum conn_state {
  99. CONN_STATE_THREAD_INACTIVE = 0,
  100. CONN_STATE_THREAD_ACTIVE = 1,
  101. CONN_STATE_THREAD_REQUEST_EXIT = 2,
  102. CONN_STATE_THREAD_DESTROYED = 3,
  103. CONN_STATE_LIB_EXIT_CALLED = 4,
  104. CONN_STATE_DISCONNECT_INACTIVE = 5
  105. };
  106. struct conn_info {
  107. int fd;
  108. pthread_t thread;
  109. pthread_attr_t thread_attr;
  110. unsigned int service;
  111. enum conn_state state;
  112. int notify_flow_control_enabled;
  113. int refcount;
  114. key_t shmkey;
  115. key_t semkey;
  116. int semid;
  117. unsigned int pending_semops;
  118. pthread_mutex_t mutex;
  119. struct control_buffer *control_buffer;
  120. char *request_buffer;
  121. char *response_buffer;
  122. char *dispatch_buffer;
  123. size_t control_size;
  124. size_t request_size;
  125. size_t response_size;
  126. size_t dispatch_size;
  127. struct list_head outq_head;
  128. void *private_data;
  129. struct list_head list;
  130. char setup_msg[sizeof (mar_req_setup_t)];
  131. unsigned int setup_bytes_read;
  132. struct list_head zcb_mapped_list_head;
  133. char *sending_allowed_private_data[64];
  134. };
  135. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info);
  136. static void outq_flush (struct conn_info *conn_info);
  137. static int priv_change (struct conn_info *conn_info);
  138. static void ipc_disconnect (struct conn_info *conn_info);
  139. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  140. int locked);
  141. static int
  142. memory_map (
  143. const char *path,
  144. size_t bytes,
  145. void **buf)
  146. {
  147. int fd;
  148. void *addr_orig;
  149. void *addr;
  150. int res;
  151. fd = open (path, O_RDWR, 0600);
  152. unlink (path);
  153. res = ftruncate (fd, bytes);
  154. addr_orig = mmap (NULL, bytes, PROT_NONE,
  155. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  156. if (addr_orig == MAP_FAILED) {
  157. return (-1);
  158. }
  159. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  160. MAP_FIXED | MAP_SHARED, fd, 0);
  161. if (addr != addr_orig) {
  162. return (-1);
  163. }
  164. #ifdef COROSYNC_BSD
  165. madvise(addr, bytes, MADV_NOSYNC);
  166. #endif
  167. res = close (fd);
  168. if (res) {
  169. return (-1);
  170. }
  171. *buf = addr_orig;
  172. return (0);
  173. }
  174. static int
  175. circular_memory_map (
  176. const char *path,
  177. size_t bytes,
  178. void **buf)
  179. {
  180. int fd;
  181. void *addr_orig;
  182. void *addr;
  183. int res;
  184. fd = open (path, O_RDWR, 0600);
  185. unlink (path);
  186. res = ftruncate (fd, bytes);
  187. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  188. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  189. if (addr_orig == MAP_FAILED) {
  190. return (-1);
  191. }
  192. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  193. MAP_FIXED | MAP_SHARED, fd, 0);
  194. if (addr != addr_orig) {
  195. return (-1);
  196. }
  197. #ifdef COROSYNC_BSD
  198. madvise(addr_orig, bytes, MADV_NOSYNC);
  199. #endif
  200. addr = mmap (((char *)addr_orig) + bytes,
  201. bytes, PROT_READ | PROT_WRITE,
  202. MAP_FIXED | MAP_SHARED, fd, 0);
  203. #ifdef COROSYNC_BSD
  204. madvise(((char *)addr_orig) + bytes, bytes, MADV_NOSYNC);
  205. #endif
  206. res = close (fd);
  207. if (res) {
  208. return (-1);
  209. }
  210. *buf = addr_orig;
  211. return (0);
  212. }
  213. static inline int
  214. circular_memory_unmap (void *buf, size_t bytes)
  215. {
  216. int res;
  217. res = munmap (buf, bytes << 1);
  218. return (res);
  219. }
  220. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  221. {
  222. unsigned int res;
  223. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  224. list_del (&zcb_mapped->list);
  225. free (zcb_mapped);
  226. return (res);
  227. }
  228. static inline int zcb_by_addr_free (struct conn_info *conn_info, void *addr)
  229. {
  230. struct list_head *list;
  231. struct zcb_mapped *zcb_mapped;
  232. unsigned int res = 0;
  233. for (list = conn_info->zcb_mapped_list_head.next;
  234. list != &conn_info->zcb_mapped_list_head; list = list->next) {
  235. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  236. if (zcb_mapped->addr == addr) {
  237. res = zcb_free (zcb_mapped);
  238. break;
  239. }
  240. }
  241. return (res);
  242. }
  243. static inline int zcb_all_free (
  244. struct conn_info *conn_info)
  245. {
  246. struct list_head *list;
  247. struct zcb_mapped *zcb_mapped;
  248. for (list = conn_info->zcb_mapped_list_head.next;
  249. list != &conn_info->zcb_mapped_list_head;) {
  250. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  251. list = list->next;
  252. zcb_free (zcb_mapped);
  253. }
  254. return (0);
  255. }
  256. static inline int zcb_alloc (
  257. struct conn_info *conn_info,
  258. const char *path_to_file,
  259. size_t size,
  260. void **addr)
  261. {
  262. struct zcb_mapped *zcb_mapped;
  263. unsigned int res;
  264. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  265. if (zcb_mapped == NULL) {
  266. return (-1);
  267. }
  268. res = memory_map (
  269. path_to_file,
  270. size,
  271. addr);
  272. if (res == -1) {
  273. return (-1);
  274. }
  275. list_init (&zcb_mapped->list);
  276. zcb_mapped->addr = *addr;
  277. zcb_mapped->size = size;
  278. list_add_tail (&zcb_mapped->list, &conn_info->zcb_mapped_list_head);
  279. return (0);
  280. }
  281. static int ipc_thread_active (void *conn)
  282. {
  283. struct conn_info *conn_info = (struct conn_info *)conn;
  284. int retval = 0;
  285. pthread_mutex_lock (&conn_info->mutex);
  286. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  287. retval = 1;
  288. }
  289. pthread_mutex_unlock (&conn_info->mutex);
  290. return (retval);
  291. }
  292. static int ipc_thread_exiting (void *conn)
  293. {
  294. struct conn_info *conn_info = (struct conn_info *)conn;
  295. int retval = 1;
  296. pthread_mutex_lock (&conn_info->mutex);
  297. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  298. retval = 0;
  299. } else
  300. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  301. retval = 0;
  302. }
  303. pthread_mutex_unlock (&conn_info->mutex);
  304. return (retval);
  305. }
  306. /*
  307. * returns 0 if should be called again, -1 if finished
  308. */
  309. static inline int conn_info_destroy (struct conn_info *conn_info)
  310. {
  311. unsigned int res;
  312. void *retval;
  313. list_del (&conn_info->list);
  314. list_init (&conn_info->list);
  315. if (conn_info->state == CONN_STATE_THREAD_REQUEST_EXIT) {
  316. res = pthread_join (conn_info->thread, &retval);
  317. conn_info->state = CONN_STATE_THREAD_DESTROYED;
  318. return (0);
  319. }
  320. if (conn_info->state == CONN_STATE_THREAD_INACTIVE ||
  321. conn_info->state == CONN_STATE_DISCONNECT_INACTIVE) {
  322. list_del (&conn_info->list);
  323. close (conn_info->fd);
  324. api->free (conn_info);
  325. return (-1);
  326. }
  327. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  328. pthread_kill (conn_info->thread, SIGUSR1);
  329. return (0);
  330. }
  331. api->serialize_lock ();
  332. /*
  333. * Retry library exit function if busy
  334. */
  335. if (conn_info->state == CONN_STATE_THREAD_DESTROYED) {
  336. res = api->exit_fn_get (conn_info->service) (conn_info);
  337. if (res == -1) {
  338. api->serialize_unlock ();
  339. return (0);
  340. } else {
  341. conn_info->state = CONN_STATE_LIB_EXIT_CALLED;
  342. }
  343. }
  344. pthread_mutex_lock (&conn_info->mutex);
  345. if (conn_info->refcount > 0) {
  346. pthread_mutex_unlock (&conn_info->mutex);
  347. api->serialize_unlock ();
  348. return (0);
  349. }
  350. list_del (&conn_info->list);
  351. pthread_mutex_unlock (&conn_info->mutex);
  352. /*
  353. * Destroy shared memory segment and semaphore
  354. */
  355. res = munmap ((void *)conn_info->control_buffer, conn_info->control_size);
  356. res = munmap ((void *)conn_info->request_buffer, conn_info->request_size);
  357. res = munmap ((void *)conn_info->response_buffer, conn_info->response_size);
  358. semctl (conn_info->semid, 0, IPC_RMID);
  359. /*
  360. * Free allocated data needed to retry exiting library IPC connection
  361. */
  362. if (conn_info->private_data) {
  363. api->free (conn_info->private_data);
  364. }
  365. close (conn_info->fd);
  366. res = circular_memory_unmap (conn_info->dispatch_buffer, conn_info->dispatch_size);
  367. zcb_all_free (conn_info);
  368. api->free (conn_info);
  369. api->serialize_unlock ();
  370. return (-1);
  371. }
  372. union u {
  373. uint64_t server_addr;
  374. void *server_ptr;
  375. };
  376. static uint64_t void2serveraddr (void *server_ptr)
  377. {
  378. union u u;
  379. u.server_ptr = server_ptr;
  380. return (u.server_addr);
  381. }
  382. static void *serveraddr2void (uint64_t server_addr)
  383. {
  384. union u u;
  385. u.server_addr = server_addr;
  386. return (u.server_ptr);
  387. };
  388. static inline void zerocopy_operations_process (
  389. struct conn_info *conn_info,
  390. coroipc_request_header_t **header_out,
  391. unsigned int *new_message)
  392. {
  393. coroipc_request_header_t *header;
  394. header = (coroipc_request_header_t *)conn_info->request_buffer;
  395. if (header->id == ZC_ALLOC_HEADER) {
  396. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)header;
  397. coroipc_response_header_t res_header;
  398. void *addr = NULL;
  399. struct coroipcs_zc_header *zc_header;
  400. unsigned int res;
  401. res = zcb_alloc (conn_info, hdr->path_to_file, hdr->map_size,
  402. &addr);
  403. zc_header = (struct coroipcs_zc_header *)addr;
  404. zc_header->server_address = void2serveraddr(addr);
  405. res_header.size = sizeof (coroipc_response_header_t);
  406. res_header.id = 0;
  407. coroipcs_response_send (
  408. conn_info, &res_header,
  409. res_header.size);
  410. *new_message = 0;
  411. return;
  412. } else
  413. if (header->id == ZC_FREE_HEADER) {
  414. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)header;
  415. coroipc_response_header_t res_header;
  416. void *addr = NULL;
  417. addr = serveraddr2void (hdr->server_address);
  418. zcb_by_addr_free (conn_info, addr);
  419. res_header.size = sizeof (coroipc_response_header_t);
  420. res_header.id = 0;
  421. coroipcs_response_send (
  422. conn_info, &res_header,
  423. res_header.size);
  424. *new_message = 0;
  425. return;
  426. } else
  427. if (header->id == ZC_EXECUTE_HEADER) {
  428. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)header;
  429. header = (coroipc_request_header_t *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  430. }
  431. *header_out = header;
  432. *new_message = 1;
  433. }
  434. static void *pthread_ipc_consumer (void *conn)
  435. {
  436. struct conn_info *conn_info = (struct conn_info *)conn;
  437. struct sembuf sop;
  438. int res;
  439. coroipc_request_header_t *header;
  440. coroipc_response_header_t coroipc_response_header;
  441. int send_ok;
  442. unsigned int new_message;
  443. if (api->sched_policy != 0) {
  444. res = pthread_setschedparam (conn_info->thread,
  445. api->sched_policy, api->sched_param);
  446. }
  447. for (;;) {
  448. sop.sem_num = 0;
  449. sop.sem_op = -1;
  450. sop.sem_flg = 0;
  451. retry_semop:
  452. if (ipc_thread_active (conn_info) == 0) {
  453. coroipcs_refcount_dec (conn_info);
  454. pthread_exit (0);
  455. }
  456. res = semop (conn_info->semid, &sop, 1);
  457. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  458. goto retry_semop;
  459. } else
  460. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  461. coroipcs_refcount_dec (conn_info);
  462. pthread_exit (0);
  463. }
  464. zerocopy_operations_process (conn_info, &header, &new_message);
  465. /*
  466. * There is no new message to process, continue for loop
  467. */
  468. if (new_message == 0) {
  469. continue;
  470. }
  471. coroipcs_refcount_inc (conn);
  472. send_ok = api->sending_allowed (conn_info->service,
  473. header->id,
  474. header,
  475. conn_info->sending_allowed_private_data);
  476. if (send_ok) {
  477. api->serialize_lock();
  478. api->handler_fn_get (conn_info->service, header->id) (conn_info, header);
  479. api->serialize_unlock();
  480. } else {
  481. /*
  482. * Overload, tell library to retry
  483. */
  484. coroipc_response_header.size = sizeof (coroipc_response_header_t);
  485. coroipc_response_header.id = 0;
  486. coroipc_response_header.error = CS_ERR_TRY_AGAIN;
  487. coroipcs_response_send (conn_info,
  488. &coroipc_response_header,
  489. sizeof (coroipc_response_header_t));
  490. }
  491. api->sending_allowed_release (conn_info->sending_allowed_private_data);
  492. coroipcs_refcount_dec (conn);
  493. }
  494. pthread_exit (0);
  495. }
  496. static int
  497. req_setup_send (
  498. struct conn_info *conn_info,
  499. int error)
  500. {
  501. mar_res_setup_t res_setup;
  502. unsigned int res;
  503. res_setup.error = error;
  504. retry_send:
  505. res = send (conn_info->fd, &res_setup, sizeof (mar_res_setup_t), MSG_WAITALL);
  506. if (res == -1 && errno == EINTR) {
  507. goto retry_send;
  508. } else
  509. if (res == -1 && errno == EAGAIN) {
  510. goto retry_send;
  511. }
  512. return (0);
  513. }
  514. static int
  515. req_setup_recv (
  516. struct conn_info *conn_info)
  517. {
  518. int res;
  519. struct msghdr msg_recv;
  520. struct iovec iov_recv;
  521. #ifdef COROSYNC_LINUX
  522. struct cmsghdr *cmsg;
  523. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  524. struct ucred *cred;
  525. int off = 0;
  526. int on = 1;
  527. #endif
  528. msg_recv.msg_iov = &iov_recv;
  529. msg_recv.msg_iovlen = 1;
  530. msg_recv.msg_name = 0;
  531. msg_recv.msg_namelen = 0;
  532. #ifdef COROSYNC_LINUX
  533. msg_recv.msg_control = (void *)cmsg_cred;
  534. msg_recv.msg_controllen = sizeof (cmsg_cred);
  535. #endif
  536. #ifdef COROSYNC_SOLARIS
  537. msg_recv.msg_accrights = 0;
  538. msg_recv.msg_accrightslen = 0;
  539. #else /* COROSYNC_SOLARIS */
  540. #ifdef HAVE_GETPEERUCRED
  541. ucred_t *uc;
  542. uid_t euid = -1;
  543. gid_t egid = -1;
  544. if (getpeerucred (conn_info->fd, &uc) == 0) {
  545. euid = ucred_geteuid (uc);
  546. egid = ucred_getegid (uc);
  547. if (api->security_valid (euid, egid) {
  548. conn_info->authenticated = 1;
  549. }
  550. ucred_free(uc);
  551. }
  552. if (conn_info->authenticated == 0) {
  553. api->log_printf ("Invalid security authentication\n");
  554. }
  555. #else /* HAVE_GETPEERUCRED */
  556. api->log_printf ("Connection not authenticated "
  557. "because platform does not support "
  558. "authentication with sockets, continuing "
  559. "with a fake authentication\n");
  560. #endif /* HAVE_GETPEERUCRED */
  561. #endif /* COROSYNC_SOLARIS */
  562. iov_recv.iov_base = &conn_info->setup_msg[conn_info->setup_bytes_read];
  563. iov_recv.iov_len = sizeof (mar_req_setup_t) - conn_info->setup_bytes_read;
  564. #ifdef COROSYNC_LINUX
  565. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  566. #endif
  567. retry_recv:
  568. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  569. if (res == -1 && errno == EINTR) {
  570. goto retry_recv;
  571. } else
  572. if (res == -1 && errno != EAGAIN) {
  573. return (0);
  574. } else
  575. if (res == 0) {
  576. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  577. /* On many OS poll never return POLLHUP or POLLERR.
  578. * EOF is detected when recvmsg return 0.
  579. */
  580. ipc_disconnect (conn_info);
  581. #endif
  582. return (-1);
  583. }
  584. conn_info->setup_bytes_read += res;
  585. #ifdef COROSYNC_LINUX
  586. cmsg = CMSG_FIRSTHDR (&msg_recv);
  587. assert (cmsg);
  588. cred = (struct ucred *)CMSG_DATA (cmsg);
  589. if (cred) {
  590. if (api->security_valid (cred->uid, cred->gid)) {
  591. } else {
  592. ipc_disconnect (conn_info);
  593. api->log_printf ("Invalid security authentication\n");
  594. return (-1);
  595. }
  596. }
  597. #endif
  598. if (conn_info->setup_bytes_read == sizeof (mar_req_setup_t)) {
  599. #ifdef COROSYNC_LINUX
  600. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED,
  601. &off, sizeof (off));
  602. #endif
  603. return (1);
  604. }
  605. return (0);
  606. }
  607. static void ipc_disconnect (struct conn_info *conn_info)
  608. {
  609. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  610. conn_info->state = CONN_STATE_DISCONNECT_INACTIVE;
  611. return;
  612. }
  613. if (conn_info->state != CONN_STATE_THREAD_ACTIVE) {
  614. return;
  615. }
  616. pthread_mutex_lock (&conn_info->mutex);
  617. conn_info->state = CONN_STATE_THREAD_REQUEST_EXIT;
  618. pthread_mutex_unlock (&conn_info->mutex);
  619. pthread_kill (conn_info->thread, SIGUSR1);
  620. }
  621. static int conn_info_create (int fd)
  622. {
  623. struct conn_info *conn_info;
  624. conn_info = api->malloc (sizeof (struct conn_info));
  625. if (conn_info == NULL) {
  626. return (-1);
  627. }
  628. memset (conn_info, 0, sizeof (struct conn_info));
  629. conn_info->fd = fd;
  630. conn_info->service = SOCKET_SERVICE_INIT;
  631. conn_info->state = CONN_STATE_THREAD_INACTIVE;
  632. list_init (&conn_info->outq_head);
  633. list_init (&conn_info->list);
  634. list_init (&conn_info->zcb_mapped_list_head);
  635. list_add (&conn_info->list, &conn_info_list_head);
  636. api->poll_dispatch_add (fd, conn_info);
  637. return (0);
  638. }
  639. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  640. /* SUN_LEN is broken for abstract namespace
  641. */
  642. #define COROSYNC_SUN_LEN(a) sizeof(*(a))
  643. #else
  644. #define COROSYNC_SUN_LEN(a) SUN_LEN(a)
  645. #endif
  646. /*
  647. * Exported functions
  648. */
  649. extern void coroipcs_ipc_init (
  650. struct coroipcs_init_state *init_state)
  651. {
  652. int server_fd;
  653. struct sockaddr_un un_addr;
  654. int res;
  655. api = init_state;
  656. /*
  657. * Create socket for IPC clients, name socket, listen for connections
  658. */
  659. #if defined(COROSYNC_SOLARIS)
  660. server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  661. #else
  662. server_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  663. #endif
  664. if (server_fd == -1) {
  665. api->log_printf ("Cannot create client connections socket.\n");
  666. api->fatal_error ("Can't create library listen socket");
  667. };
  668. res = fcntl (server_fd, F_SETFL, O_NONBLOCK);
  669. if (res == -1) {
  670. api->log_printf ("Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  671. api->fatal_error ("Could not set non-blocking operation on server socket");
  672. }
  673. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  674. un_addr.sun_family = AF_UNIX;
  675. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  676. un_addr.sun_len = SUN_LEN(&un_addr);
  677. #endif
  678. #if defined(COROSYNC_LINUX)
  679. sprintf (un_addr.sun_path + 1, "%s", api->socket_name);
  680. #else
  681. sprintf (un_addr.sun_path, "%s/%s", SOCKETDIR, api->socket_name);
  682. unlink (un_addr.sun_path);
  683. #endif
  684. res = bind (server_fd, (struct sockaddr *)&un_addr, COROSYNC_SUN_LEN(&un_addr));
  685. if (res) {
  686. api->log_printf ("Could not bind AF_UNIX (%s): %s.\n", un_addr.sun_path, strerror (errno));
  687. api->fatal_error ("Could not bind to AF_UNIX socket\n");
  688. }
  689. listen (server_fd, SERVER_BACKLOG);
  690. /*
  691. * Setup connection dispatch routine
  692. */
  693. api->poll_accept_add (server_fd);
  694. }
  695. void coroipcs_ipc_exit (void)
  696. {
  697. struct list_head *list;
  698. struct conn_info *conn_info;
  699. unsigned int res;
  700. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  701. list = list->next) {
  702. conn_info = list_entry (list, struct conn_info, list);
  703. /*
  704. * Unmap memory segments
  705. */
  706. res = munmap ((void *)conn_info->control_buffer,
  707. conn_info->control_size);
  708. res = munmap ((void *)conn_info->request_buffer,
  709. conn_info->request_size);
  710. res = munmap ((void *)conn_info->response_buffer,
  711. conn_info->response_size);
  712. res = circular_memory_unmap (conn_info->dispatch_buffer,
  713. conn_info->dispatch_size);
  714. semctl (conn_info->semid, 0, IPC_RMID);
  715. pthread_kill (conn_info->thread, SIGUSR1);
  716. }
  717. }
  718. /*
  719. * Get the conn info private data
  720. */
  721. void *coroipcs_private_data_get (void *conn)
  722. {
  723. struct conn_info *conn_info = (struct conn_info *)conn;
  724. return (conn_info->private_data);
  725. }
  726. int coroipcs_response_send (void *conn, const void *msg, size_t mlen)
  727. {
  728. struct conn_info *conn_info = (struct conn_info *)conn;
  729. struct sembuf sop;
  730. int res;
  731. memcpy (conn_info->response_buffer, msg, mlen);
  732. sop.sem_num = 1;
  733. sop.sem_op = 1;
  734. sop.sem_flg = 0;
  735. retry_semop:
  736. res = semop (conn_info->semid, &sop, 1);
  737. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  738. goto retry_semop;
  739. } else
  740. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  741. return (0);
  742. }
  743. return (0);
  744. }
  745. int coroipcs_response_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  746. {
  747. struct conn_info *conn_info = (struct conn_info *)conn;
  748. struct sembuf sop;
  749. int res;
  750. int write_idx = 0;
  751. int i;
  752. for (i = 0; i < iov_len; i++) {
  753. memcpy (&conn_info->response_buffer[write_idx],
  754. iov[i].iov_base, iov[i].iov_len);
  755. write_idx += iov[i].iov_len;
  756. }
  757. sop.sem_num = 1;
  758. sop.sem_op = 1;
  759. sop.sem_flg = 0;
  760. retry_semop:
  761. res = semop (conn_info->semid, &sop, 1);
  762. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  763. goto retry_semop;
  764. } else
  765. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  766. return (0);
  767. }
  768. return (0);
  769. }
  770. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info)
  771. {
  772. unsigned int n_read;
  773. unsigned int n_write;
  774. unsigned int bytes_left;
  775. n_read = conn_info->control_buffer->read;
  776. n_write = conn_info->control_buffer->write;
  777. if (n_read <= n_write) {
  778. bytes_left = conn_info->dispatch_size - n_write + n_read;
  779. } else {
  780. bytes_left = n_read - n_write;
  781. }
  782. return (bytes_left);
  783. }
  784. static void memcpy_dwrap (struct conn_info *conn_info, void *msg, unsigned int len)
  785. {
  786. unsigned int write_idx;
  787. write_idx = conn_info->control_buffer->write;
  788. memcpy (&conn_info->dispatch_buffer[write_idx], msg, len);
  789. conn_info->control_buffer->write = (write_idx + len) % conn_info->dispatch_size;
  790. }
  791. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  792. int locked)
  793. {
  794. struct conn_info *conn_info = (struct conn_info *)conn;
  795. struct sembuf sop;
  796. int res;
  797. int i;
  798. char buf;
  799. for (i = 0; i < iov_len; i++) {
  800. memcpy_dwrap (conn_info, iov[i].iov_base, iov[i].iov_len);
  801. }
  802. buf = !list_empty (&conn_info->outq_head);
  803. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  804. if (res == -1 && errno == EAGAIN) {
  805. if (locked == 0) {
  806. pthread_mutex_lock (&conn_info->mutex);
  807. }
  808. conn_info->pending_semops += 1;
  809. if (locked == 0) {
  810. pthread_mutex_unlock (&conn_info->mutex);
  811. }
  812. api->poll_dispatch_modify (conn_info->fd,
  813. POLLIN|POLLOUT|POLLNVAL);
  814. } else
  815. if (res == -1) {
  816. ipc_disconnect (conn_info);
  817. }
  818. sop.sem_num = 2;
  819. sop.sem_op = 1;
  820. sop.sem_flg = 0;
  821. retry_semop:
  822. res = semop (conn_info->semid, &sop, 1);
  823. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  824. goto retry_semop;
  825. } else
  826. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  827. return;
  828. }
  829. }
  830. static void outq_flush (struct conn_info *conn_info) {
  831. struct list_head *list, *list_next;
  832. struct outq_item *outq_item;
  833. unsigned int bytes_left;
  834. struct iovec iov;
  835. char buf;
  836. int res;
  837. pthread_mutex_lock (&conn_info->mutex);
  838. if (list_empty (&conn_info->outq_head)) {
  839. buf = 3;
  840. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  841. pthread_mutex_unlock (&conn_info->mutex);
  842. return;
  843. }
  844. for (list = conn_info->outq_head.next;
  845. list != &conn_info->outq_head; list = list_next) {
  846. list_next = list->next;
  847. outq_item = list_entry (list, struct outq_item, list);
  848. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  849. if (bytes_left > outq_item->mlen) {
  850. iov.iov_base = outq_item->msg;
  851. iov.iov_len = outq_item->mlen;
  852. msg_send (conn_info, &iov, 1, MSG_SEND_UNLOCKED);
  853. list_del (list);
  854. api->free (iov.iov_base);
  855. api->free (outq_item);
  856. } else {
  857. break;
  858. }
  859. }
  860. pthread_mutex_unlock (&conn_info->mutex);
  861. }
  862. static int priv_change (struct conn_info *conn_info)
  863. {
  864. mar_req_priv_change req_priv_change;
  865. unsigned int res;
  866. union semun semun;
  867. struct semid_ds ipc_set;
  868. int i;
  869. retry_recv:
  870. res = recv (conn_info->fd, &req_priv_change,
  871. sizeof (mar_req_priv_change),
  872. MSG_NOSIGNAL);
  873. if (res == -1 && errno == EINTR) {
  874. goto retry_recv;
  875. }
  876. if (res == -1 && errno == EAGAIN) {
  877. goto retry_recv;
  878. }
  879. if (res == -1 && errno != EAGAIN) {
  880. return (-1);
  881. }
  882. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  883. /* Error on socket, EOF is detected when recv return 0
  884. */
  885. if (res == 0) {
  886. return (-1);
  887. }
  888. #endif
  889. ipc_set.sem_perm.uid = req_priv_change.euid;
  890. ipc_set.sem_perm.gid = req_priv_change.egid;
  891. ipc_set.sem_perm.mode = 0600;
  892. semun.buf = &ipc_set;
  893. for (i = 0; i < 3; i++) {
  894. res = semctl (conn_info->semid, 0, IPC_SET, semun);
  895. if (res == -1) {
  896. return (-1);
  897. }
  898. }
  899. return (0);
  900. }
  901. static void msg_send_or_queue (void *conn, const struct iovec *iov, unsigned int iov_len)
  902. {
  903. struct conn_info *conn_info = (struct conn_info *)conn;
  904. unsigned int bytes_left;
  905. unsigned int bytes_msg = 0;
  906. int i;
  907. struct outq_item *outq_item;
  908. char *write_buf = 0;
  909. /*
  910. * Exit transmission if the connection is dead
  911. */
  912. if (ipc_thread_active (conn) == 0) {
  913. return;
  914. }
  915. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  916. for (i = 0; i < iov_len; i++) {
  917. bytes_msg += iov[i].iov_len;
  918. }
  919. if (bytes_left < bytes_msg || list_empty (&conn_info->outq_head) == 0) {
  920. outq_item = api->malloc (sizeof (struct outq_item));
  921. if (outq_item == NULL) {
  922. ipc_disconnect (conn);
  923. return;
  924. }
  925. outq_item->msg = api->malloc (bytes_msg);
  926. if (outq_item->msg == 0) {
  927. api->free (outq_item);
  928. ipc_disconnect (conn);
  929. return;
  930. }
  931. write_buf = outq_item->msg;
  932. for (i = 0; i < iov_len; i++) {
  933. memcpy (write_buf, iov[i].iov_base, iov[i].iov_len);
  934. write_buf += iov[i].iov_len;
  935. }
  936. outq_item->mlen = bytes_msg;
  937. list_init (&outq_item->list);
  938. pthread_mutex_lock (&conn_info->mutex);
  939. if (list_empty (&conn_info->outq_head)) {
  940. conn_info->notify_flow_control_enabled = 1;
  941. api->poll_dispatch_modify (conn_info->fd,
  942. POLLIN|POLLOUT|POLLNVAL);
  943. }
  944. list_add_tail (&outq_item->list, &conn_info->outq_head);
  945. pthread_mutex_unlock (&conn_info->mutex);
  946. return;
  947. }
  948. msg_send (conn, iov, iov_len, MSG_SEND_LOCKED);
  949. }
  950. void coroipcs_refcount_inc (void *conn)
  951. {
  952. struct conn_info *conn_info = (struct conn_info *)conn;
  953. pthread_mutex_lock (&conn_info->mutex);
  954. conn_info->refcount++;
  955. pthread_mutex_unlock (&conn_info->mutex);
  956. }
  957. void coroipcs_refcount_dec (void *conn)
  958. {
  959. struct conn_info *conn_info = (struct conn_info *)conn;
  960. pthread_mutex_lock (&conn_info->mutex);
  961. conn_info->refcount--;
  962. pthread_mutex_unlock (&conn_info->mutex);
  963. }
  964. int coroipcs_dispatch_send (void *conn, const void *msg, size_t mlen)
  965. {
  966. struct iovec iov;
  967. iov.iov_base = (void *)msg;
  968. iov.iov_len = mlen;
  969. msg_send_or_queue (conn, &iov, 1);
  970. return (0);
  971. }
  972. int coroipcs_dispatch_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  973. {
  974. msg_send_or_queue (conn, iov, iov_len);
  975. return (0);
  976. }
  977. int coroipcs_handler_accept (
  978. int fd,
  979. int revent,
  980. void *data)
  981. {
  982. socklen_t addrlen;
  983. struct sockaddr_un un_addr;
  984. int new_fd;
  985. #ifdef COROSYNC_LINUX
  986. int on = 1;
  987. #endif
  988. int res;
  989. addrlen = sizeof (struct sockaddr_un);
  990. retry_accept:
  991. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  992. if (new_fd == -1 && errno == EINTR) {
  993. goto retry_accept;
  994. }
  995. if (new_fd == -1) {
  996. api->log_printf ("Could not accept Library connection: %s\n", strerror (errno));
  997. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  998. }
  999. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  1000. if (res == -1) {
  1001. api->log_printf ("Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  1002. close (new_fd);
  1003. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1004. }
  1005. /*
  1006. * Valid accept
  1007. */
  1008. /*
  1009. * Request credentials of sender provided by kernel
  1010. */
  1011. #ifdef COROSYNC_LINUX
  1012. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  1013. #endif
  1014. res = conn_info_create (new_fd);
  1015. if (res != 0) {
  1016. close (new_fd);
  1017. }
  1018. return (0);
  1019. }
  1020. int coroipcs_handler_dispatch (
  1021. int fd,
  1022. int revent,
  1023. void *context)
  1024. {
  1025. mar_req_setup_t *req_setup;
  1026. struct conn_info *conn_info = (struct conn_info *)context;
  1027. int res;
  1028. char buf;
  1029. if (ipc_thread_exiting (conn_info)) {
  1030. return conn_info_destroy (conn_info);
  1031. }
  1032. /*
  1033. * If an error occurs, request exit
  1034. */
  1035. if (revent & (POLLERR|POLLHUP)) {
  1036. ipc_disconnect (conn_info);
  1037. return (0);
  1038. }
  1039. /*
  1040. * Read the header and process it
  1041. */
  1042. if (conn_info->service == SOCKET_SERVICE_INIT && (revent & POLLIN)) {
  1043. /*
  1044. * Receive in a nonblocking fashion the request
  1045. * IF security invalid, send TRY_AGAIN, otherwise
  1046. * send OK
  1047. */
  1048. res = req_setup_recv (conn_info);
  1049. if (res == -1) {
  1050. req_setup_send (conn_info, CS_ERR_TRY_AGAIN);
  1051. }
  1052. if (res != 1) {
  1053. return (0);
  1054. }
  1055. req_setup_send (conn_info, CS_OK);
  1056. pthread_mutex_init (&conn_info->mutex, NULL);
  1057. req_setup = (mar_req_setup_t *)conn_info->setup_msg;
  1058. /*
  1059. * Is the service registered ?
  1060. */
  1061. if (api->service_available (req_setup->service) == 0) {
  1062. ipc_disconnect (conn_info);
  1063. return (0);
  1064. }
  1065. conn_info->semkey = req_setup->semkey;
  1066. res = memory_map (
  1067. req_setup->control_file,
  1068. req_setup->control_size,
  1069. (void *)&conn_info->control_buffer);
  1070. conn_info->control_size = req_setup->control_size;
  1071. res = memory_map (
  1072. req_setup->request_file,
  1073. req_setup->request_size,
  1074. (void *)&conn_info->request_buffer);
  1075. conn_info->request_size = req_setup->request_size;
  1076. res = memory_map (
  1077. req_setup->response_file,
  1078. req_setup->response_size,
  1079. (void *)&conn_info->response_buffer);
  1080. conn_info->response_size = req_setup->response_size;
  1081. res = circular_memory_map (
  1082. req_setup->dispatch_file,
  1083. req_setup->dispatch_size,
  1084. (void *)&conn_info->dispatch_buffer);
  1085. conn_info->dispatch_size = req_setup->dispatch_size;
  1086. conn_info->service = req_setup->service;
  1087. conn_info->refcount = 0;
  1088. conn_info->notify_flow_control_enabled = 0;
  1089. conn_info->setup_bytes_read = 0;
  1090. conn_info->semid = semget (conn_info->semkey, 3, 0600);
  1091. conn_info->pending_semops = 0;
  1092. /*
  1093. * ipc thread is the only reference at startup
  1094. */
  1095. conn_info->refcount = 1;
  1096. conn_info->state = CONN_STATE_THREAD_ACTIVE;
  1097. conn_info->private_data = api->malloc (api->private_data_size_get (conn_info->service));
  1098. memset (conn_info->private_data, 0,
  1099. api->private_data_size_get (conn_info->service));
  1100. api->init_fn_get (conn_info->service) (conn_info);
  1101. pthread_attr_init (&conn_info->thread_attr);
  1102. /*
  1103. * IA64 needs more stack space then other arches
  1104. */
  1105. #if defined(__ia64__)
  1106. pthread_attr_setstacksize (&conn_info->thread_attr, 400000);
  1107. #else
  1108. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  1109. #endif
  1110. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_JOINABLE);
  1111. res = pthread_create (&conn_info->thread,
  1112. &conn_info->thread_attr,
  1113. pthread_ipc_consumer,
  1114. conn_info);
  1115. /*
  1116. * Security check - disallow multiple configurations of
  1117. * the ipc connection
  1118. */
  1119. if (conn_info->service == SOCKET_SERVICE_INIT) {
  1120. conn_info->service = -1;
  1121. }
  1122. } else
  1123. if (revent & POLLIN) {
  1124. coroipcs_refcount_inc (conn_info);
  1125. res = recv (fd, &buf, 1, MSG_NOSIGNAL);
  1126. if (res == 1) {
  1127. switch (buf) {
  1128. case MESSAGE_REQ_OUTQ_FLUSH:
  1129. outq_flush (conn_info);
  1130. break;
  1131. case MESSAGE_REQ_CHANGE_EUID:
  1132. if (priv_change (conn_info) == -1) {
  1133. ipc_disconnect (conn_info);
  1134. }
  1135. break;
  1136. default:
  1137. res = 0;
  1138. break;
  1139. }
  1140. }
  1141. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1142. /* On many OS poll never return POLLHUP or POLLERR.
  1143. * EOF is detected when recvmsg return 0.
  1144. */
  1145. if (res == 0) {
  1146. ipc_disconnect (conn_info);
  1147. coroipcs_refcount_dec (conn_info);
  1148. return (0);
  1149. }
  1150. #endif
  1151. coroipcs_refcount_dec (conn_info);
  1152. }
  1153. coroipcs_refcount_inc (conn_info);
  1154. pthread_mutex_lock (&conn_info->mutex);
  1155. if ((conn_info->state == CONN_STATE_THREAD_ACTIVE) && (revent & POLLOUT)) {
  1156. buf = !list_empty (&conn_info->outq_head);
  1157. for (; conn_info->pending_semops;) {
  1158. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1159. if (res == 1) {
  1160. conn_info->pending_semops--;
  1161. } else {
  1162. break;
  1163. }
  1164. }
  1165. if (conn_info->notify_flow_control_enabled) {
  1166. buf = 2;
  1167. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1168. if (res == 1) {
  1169. conn_info->notify_flow_control_enabled = 0;
  1170. }
  1171. }
  1172. if (conn_info->notify_flow_control_enabled == 0 &&
  1173. conn_info->pending_semops == 0) {
  1174. api->poll_dispatch_modify (conn_info->fd,
  1175. POLLIN|POLLNVAL);
  1176. }
  1177. }
  1178. pthread_mutex_unlock (&conn_info->mutex);
  1179. coroipcs_refcount_dec (conn_info);
  1180. return (0);
  1181. }