coroipcs.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  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 <corosync/coroipc_types.h>
  69. #include <corosync/coroipcs.h>
  70. #include <corosync/coroipc_ipc.h>
  71. #ifndef MSG_NOSIGNAL
  72. #define MSG_NOSIGNAL 0
  73. #endif
  74. #define SERVER_BACKLOG 5
  75. #define MSG_SEND_LOCKED 0
  76. #define MSG_SEND_UNLOCKED 1
  77. static struct coroipcs_init_state *api;
  78. DECLARE_LIST_INIT (conn_info_list_head);
  79. struct outq_item {
  80. void *msg;
  81. size_t mlen;
  82. struct list_head list;
  83. };
  84. struct zcb_mapped {
  85. struct list_head list;
  86. void *addr;
  87. size_t size;
  88. };
  89. #if defined(_SEM_SEMUN_UNDEFINED)
  90. union semun {
  91. int val;
  92. struct semid_ds *buf;
  93. unsigned short int *array;
  94. struct seminfo *__buf;
  95. };
  96. #endif
  97. enum conn_state {
  98. CONN_STATE_THREAD_INACTIVE = 0,
  99. CONN_STATE_THREAD_ACTIVE = 1,
  100. CONN_STATE_THREAD_REQUEST_EXIT = 2,
  101. CONN_STATE_THREAD_DESTROYED = 3,
  102. CONN_STATE_LIB_EXIT_CALLED = 4,
  103. CONN_STATE_DISCONNECT_INACTIVE = 5
  104. };
  105. struct conn_info {
  106. int fd;
  107. pthread_t thread;
  108. pthread_attr_t thread_attr;
  109. unsigned int service;
  110. enum conn_state state;
  111. int notify_flow_control_enabled;
  112. int refcount;
  113. key_t shmkey;
  114. key_t semkey;
  115. int semid;
  116. unsigned int pending_semops;
  117. pthread_mutex_t mutex;
  118. struct control_buffer *control_buffer;
  119. char *request_buffer;
  120. char *response_buffer;
  121. char *dispatch_buffer;
  122. size_t control_size;
  123. size_t request_size;
  124. size_t response_size;
  125. size_t dispatch_size;
  126. struct list_head outq_head;
  127. void *private_data;
  128. struct list_head list;
  129. char setup_msg[sizeof (mar_req_setup_t)];
  130. unsigned int setup_bytes_read;
  131. struct list_head zcb_mapped_list_head;
  132. char *sending_allowed_private_data[64];
  133. };
  134. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info);
  135. static void outq_flush (struct conn_info *conn_info);
  136. static int priv_change (struct conn_info *conn_info);
  137. static void ipc_disconnect (struct conn_info *conn_info);
  138. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  139. int locked);
  140. static int
  141. memory_map (
  142. const char *path,
  143. size_t bytes,
  144. void **buf)
  145. {
  146. int fd;
  147. void *addr_orig;
  148. void *addr;
  149. int res;
  150. fd = open (path, O_RDWR, 0600);
  151. unlink (path);
  152. res = ftruncate (fd, bytes);
  153. addr_orig = mmap (NULL, bytes, PROT_NONE,
  154. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  155. if (addr_orig == MAP_FAILED) {
  156. return (-1);
  157. }
  158. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  159. MAP_FIXED | MAP_SHARED, fd, 0);
  160. if (addr != addr_orig) {
  161. return (-1);
  162. }
  163. res = close (fd);
  164. if (res) {
  165. return (-1);
  166. }
  167. *buf = addr_orig;
  168. return (0);
  169. }
  170. static int
  171. circular_memory_map (
  172. const char *path,
  173. size_t bytes,
  174. void **buf)
  175. {
  176. int fd;
  177. void *addr_orig;
  178. void *addr;
  179. int res;
  180. fd = open (path, O_RDWR, 0600);
  181. unlink (path);
  182. res = ftruncate (fd, bytes);
  183. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  184. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  185. if (addr_orig == MAP_FAILED) {
  186. return (-1);
  187. }
  188. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  189. MAP_FIXED | MAP_SHARED, fd, 0);
  190. if (addr != addr_orig) {
  191. return (-1);
  192. }
  193. addr = mmap (((char *)addr_orig) + bytes,
  194. bytes, PROT_READ | PROT_WRITE,
  195. MAP_FIXED | MAP_SHARED, fd, 0);
  196. res = close (fd);
  197. if (res) {
  198. return (-1);
  199. }
  200. *buf = addr_orig;
  201. return (0);
  202. }
  203. static inline int
  204. circular_memory_unmap (void *buf, size_t bytes)
  205. {
  206. int res;
  207. res = munmap (buf, bytes << 1);
  208. return (res);
  209. }
  210. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  211. {
  212. unsigned int res;
  213. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  214. list_del (&zcb_mapped->list);
  215. free (zcb_mapped);
  216. return (res);
  217. }
  218. static inline int zcb_by_addr_free (struct conn_info *conn_info, void *addr)
  219. {
  220. struct list_head *list;
  221. struct zcb_mapped *zcb_mapped;
  222. unsigned int res = 0;
  223. for (list = conn_info->zcb_mapped_list_head.next;
  224. list != &conn_info->zcb_mapped_list_head; list = list->next) {
  225. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  226. if (zcb_mapped->addr == addr) {
  227. res = zcb_free (zcb_mapped);
  228. break;
  229. }
  230. }
  231. return (res);
  232. }
  233. static inline int zcb_all_free (
  234. struct conn_info *conn_info)
  235. {
  236. struct list_head *list;
  237. struct zcb_mapped *zcb_mapped;
  238. for (list = conn_info->zcb_mapped_list_head.next;
  239. list != &conn_info->zcb_mapped_list_head;) {
  240. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  241. list = list->next;
  242. zcb_free (zcb_mapped);
  243. }
  244. return (0);
  245. }
  246. static inline int zcb_alloc (
  247. struct conn_info *conn_info,
  248. const char *path_to_file,
  249. size_t size,
  250. void **addr)
  251. {
  252. struct zcb_mapped *zcb_mapped;
  253. unsigned int res;
  254. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  255. if (zcb_mapped == NULL) {
  256. return (-1);
  257. }
  258. res = memory_map (
  259. path_to_file,
  260. size,
  261. addr);
  262. if (res == -1) {
  263. return (-1);
  264. }
  265. list_init (&zcb_mapped->list);
  266. zcb_mapped->addr = *addr;
  267. zcb_mapped->size = size;
  268. list_add_tail (&zcb_mapped->list, &conn_info->zcb_mapped_list_head);
  269. return (0);
  270. }
  271. static int ipc_thread_active (void *conn)
  272. {
  273. struct conn_info *conn_info = (struct conn_info *)conn;
  274. int retval = 0;
  275. pthread_mutex_lock (&conn_info->mutex);
  276. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  277. retval = 1;
  278. }
  279. pthread_mutex_unlock (&conn_info->mutex);
  280. return (retval);
  281. }
  282. static int ipc_thread_exiting (void *conn)
  283. {
  284. struct conn_info *conn_info = (struct conn_info *)conn;
  285. int retval = 1;
  286. pthread_mutex_lock (&conn_info->mutex);
  287. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  288. retval = 0;
  289. } else
  290. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  291. retval = 0;
  292. }
  293. pthread_mutex_unlock (&conn_info->mutex);
  294. return (retval);
  295. }
  296. /*
  297. * returns 0 if should be called again, -1 if finished
  298. */
  299. static inline int conn_info_destroy (struct conn_info *conn_info)
  300. {
  301. unsigned int res;
  302. void *retval;
  303. list_del (&conn_info->list);
  304. list_init (&conn_info->list);
  305. if (conn_info->state == CONN_STATE_THREAD_REQUEST_EXIT) {
  306. res = pthread_join (conn_info->thread, &retval);
  307. conn_info->state = CONN_STATE_THREAD_DESTROYED;
  308. return (0);
  309. }
  310. if (conn_info->state == CONN_STATE_THREAD_INACTIVE ||
  311. conn_info->state == CONN_STATE_DISCONNECT_INACTIVE) {
  312. list_del (&conn_info->list);
  313. close (conn_info->fd);
  314. api->free (conn_info);
  315. return (-1);
  316. }
  317. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  318. pthread_kill (conn_info->thread, SIGUSR1);
  319. return (0);
  320. }
  321. api->serialize_lock ();
  322. /*
  323. * Retry library exit function if busy
  324. */
  325. if (conn_info->state == CONN_STATE_THREAD_DESTROYED) {
  326. res = api->exit_fn_get (conn_info->service) (conn_info);
  327. if (res == -1) {
  328. api->serialize_unlock ();
  329. return (0);
  330. } else {
  331. conn_info->state = CONN_STATE_LIB_EXIT_CALLED;
  332. }
  333. }
  334. pthread_mutex_lock (&conn_info->mutex);
  335. if (conn_info->refcount > 0) {
  336. pthread_mutex_unlock (&conn_info->mutex);
  337. api->serialize_unlock ();
  338. return (0);
  339. }
  340. list_del (&conn_info->list);
  341. pthread_mutex_unlock (&conn_info->mutex);
  342. /*
  343. * Destroy shared memory segment and semaphore
  344. */
  345. res = munmap (conn_info->control_buffer, conn_info->control_size);
  346. res = munmap (conn_info->request_buffer, conn_info->request_size);
  347. res = munmap (conn_info->response_buffer, conn_info->response_size);
  348. semctl (conn_info->semid, 0, IPC_RMID);
  349. /*
  350. * Free allocated data needed to retry exiting library IPC connection
  351. */
  352. if (conn_info->private_data) {
  353. api->free (conn_info->private_data);
  354. }
  355. close (conn_info->fd);
  356. res = circular_memory_unmap (conn_info->dispatch_buffer, conn_info->dispatch_size);
  357. zcb_all_free (conn_info);
  358. api->free (conn_info);
  359. api->serialize_unlock ();
  360. return (-1);
  361. }
  362. struct res_overlay {
  363. coroipc_response_header_t header __attribute__((aligned(8)));
  364. char buf[4096];
  365. };
  366. union u {
  367. uint64_t server_addr;
  368. void *server_ptr;
  369. };
  370. static uint64_t void2serveraddr (void *server_ptr)
  371. {
  372. union u u;
  373. u.server_ptr = server_ptr;
  374. return (u.server_addr);
  375. }
  376. static void *serveraddr2void (uint64_t server_addr)
  377. {
  378. union u u;
  379. u.server_addr = server_addr;
  380. return (u.server_ptr);
  381. };
  382. static inline void zerocopy_operations_process (
  383. struct conn_info *conn_info,
  384. coroipc_request_header_t **header_out,
  385. unsigned int *new_message)
  386. {
  387. coroipc_request_header_t *header;
  388. header = (coroipc_request_header_t *)conn_info->request_buffer;
  389. if (header->id == ZC_ALLOC_HEADER) {
  390. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)header;
  391. coroipc_response_header_t res_header;
  392. void *addr = NULL;
  393. struct coroipcs_zc_header *zc_header;
  394. unsigned int res;
  395. res = zcb_alloc (conn_info, hdr->path_to_file, hdr->map_size,
  396. &addr);
  397. zc_header = (struct coroipcs_zc_header *)addr;
  398. zc_header->server_address = void2serveraddr(addr);
  399. res_header.size = sizeof (coroipc_response_header_t);
  400. res_header.id = 0;
  401. coroipcs_response_send (
  402. conn_info, &res_header,
  403. res_header.size);
  404. *new_message = 0;
  405. return;
  406. } else
  407. if (header->id == ZC_FREE_HEADER) {
  408. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)header;
  409. coroipc_response_header_t res_header;
  410. void *addr = NULL;
  411. addr = serveraddr2void (hdr->server_address);
  412. zcb_by_addr_free (conn_info, addr);
  413. res_header.size = sizeof (coroipc_response_header_t);
  414. res_header.id = 0;
  415. coroipcs_response_send (
  416. conn_info, &res_header,
  417. res_header.size);
  418. *new_message = 0;
  419. return;
  420. } else
  421. if (header->id == ZC_EXECUTE_HEADER) {
  422. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)header;
  423. header = (coroipc_request_header_t *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  424. }
  425. *header_out = header;
  426. *new_message = 1;
  427. }
  428. static void *pthread_ipc_consumer (void *conn)
  429. {
  430. struct conn_info *conn_info = (struct conn_info *)conn;
  431. struct sembuf sop;
  432. int res;
  433. coroipc_request_header_t *header;
  434. struct res_overlay res_overlay;
  435. int send_ok;
  436. unsigned int new_message;
  437. if (api->sched_policy != 0) {
  438. res = pthread_setschedparam (conn_info->thread,
  439. api->sched_policy, api->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. }