coroipcs.c 32 KB

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