coroipcs.c 32 KB

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