coroipcs.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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 COROSYNC_SOLARIS
  528. msg_recv.msg_accrights = 0;
  529. msg_recv.msg_accrightslen = 0;
  530. #else /* COROSYNC_SOLARIS */
  531. #ifdef HAVE_GETPEERUCRED
  532. ucred_t *uc;
  533. uid_t euid = -1;
  534. gid_t egid = -1;
  535. if (getpeerucred (conn_info->fd, &uc) == 0) {
  536. euid = ucred_geteuid (uc);
  537. egid = ucred_getegid (uc);
  538. if (api->security_valid (euid, egid) {
  539. conn_info->authenticated = 1;
  540. }
  541. ucred_free(uc);
  542. }
  543. if (conn_info->authenticated == 0) {
  544. api->log_printf ("Invalid security authentication\n");
  545. }
  546. #else /* HAVE_GETPEERUCRED */
  547. api->log_printf ("Connection not authenticated "
  548. "because platform does not support "
  549. "authentication with sockets, continuing "
  550. "with a fake authentication\n");
  551. #endif /* HAVE_GETPEERUCRED */
  552. #endif /* COROSYNC_SOLARIS */
  553. iov_recv.iov_base = &conn_info->setup_msg[conn_info->setup_bytes_read];
  554. iov_recv.iov_len = sizeof (mar_req_setup_t) - conn_info->setup_bytes_read;
  555. #ifdef COROSYNC_LINUX
  556. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  557. #endif
  558. retry_recv:
  559. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  560. if (res == -1 && errno == EINTR) {
  561. goto retry_recv;
  562. } else
  563. if (res == -1 && errno != EAGAIN) {
  564. return (0);
  565. } else
  566. if (res == 0) {
  567. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  568. /* On many OS poll never return POLLHUP or POLLERR.
  569. * EOF is detected when recvmsg return 0.
  570. */
  571. ipc_disconnect (conn_info);
  572. #endif
  573. return (-1);
  574. }
  575. conn_info->setup_bytes_read += res;
  576. #ifdef COROSYNC_LINUX
  577. cmsg = CMSG_FIRSTHDR (&msg_recv);
  578. assert (cmsg);
  579. cred = (struct ucred *)CMSG_DATA (cmsg);
  580. if (cred) {
  581. if (api->security_valid (cred->uid, cred->gid)) {
  582. } else {
  583. ipc_disconnect (conn_info);
  584. api->log_printf ("Invalid security authentication\n");
  585. return (-1);
  586. }
  587. }
  588. #endif
  589. if (conn_info->setup_bytes_read == sizeof (mar_req_setup_t)) {
  590. #ifdef COROSYNC_LINUX
  591. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED,
  592. &off, sizeof (off));
  593. #endif
  594. return (1);
  595. }
  596. return (0);
  597. }
  598. static void ipc_disconnect (struct conn_info *conn_info)
  599. {
  600. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  601. conn_info->state = CONN_STATE_DISCONNECT_INACTIVE;
  602. return;
  603. }
  604. if (conn_info->state != CONN_STATE_THREAD_ACTIVE) {
  605. return;
  606. }
  607. pthread_mutex_lock (&conn_info->mutex);
  608. conn_info->state = CONN_STATE_THREAD_REQUEST_EXIT;
  609. pthread_mutex_unlock (&conn_info->mutex);
  610. pthread_kill (conn_info->thread, SIGUSR1);
  611. }
  612. static int conn_info_create (int fd)
  613. {
  614. struct conn_info *conn_info;
  615. conn_info = api->malloc (sizeof (struct conn_info));
  616. if (conn_info == NULL) {
  617. return (-1);
  618. }
  619. memset (conn_info, 0, sizeof (struct conn_info));
  620. conn_info->fd = fd;
  621. conn_info->service = SOCKET_SERVICE_INIT;
  622. conn_info->state = CONN_STATE_THREAD_INACTIVE;
  623. list_init (&conn_info->outq_head);
  624. list_init (&conn_info->list);
  625. list_init (&conn_info->zcb_mapped_list_head);
  626. list_add (&conn_info->list, &conn_info_list_head);
  627. api->poll_dispatch_add (fd, conn_info);
  628. return (0);
  629. }
  630. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  631. /* SUN_LEN is broken for abstract namespace
  632. */
  633. #define COROSYNC_SUN_LEN(a) sizeof(*(a))
  634. #else
  635. #define COROSYNC_SUN_LEN(a) SUN_LEN(a)
  636. #endif
  637. /*
  638. * Exported functions
  639. */
  640. extern void coroipcs_ipc_init (
  641. struct coroipcs_init_state *init_state)
  642. {
  643. int server_fd;
  644. struct sockaddr_un un_addr;
  645. int res;
  646. api = init_state;
  647. /*
  648. * Create socket for IPC clients, name socket, listen for connections
  649. */
  650. #if defined(COROSYNC_SOLARIS)
  651. server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  652. #else
  653. server_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  654. #endif
  655. if (server_fd == -1) {
  656. api->log_printf ("Cannot create client connections socket.\n");
  657. api->fatal_error ("Can't create library listen socket");
  658. };
  659. res = fcntl (server_fd, F_SETFL, O_NONBLOCK);
  660. if (res == -1) {
  661. api->log_printf ("Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  662. api->fatal_error ("Could not set non-blocking operation on server socket");
  663. }
  664. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  665. un_addr.sun_family = AF_UNIX;
  666. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  667. un_addr.sun_len = SUN_LEN(&un_addr);
  668. #endif
  669. #if defined(COROSYNC_LINUX)
  670. sprintf (un_addr.sun_path + 1, "%s", api->socket_name);
  671. #else
  672. sprintf (un_addr.sun_path, "%s/%s", SOCKETDIR, api->socket_name);
  673. unlink (un_addr.sun_path);
  674. #endif
  675. res = bind (server_fd, (struct sockaddr *)&un_addr, COROSYNC_SUN_LEN(&un_addr));
  676. if (res) {
  677. api->log_printf ("Could not bind AF_UNIX (%s): %s.\n", un_addr.sun_path, strerror (errno));
  678. api->fatal_error ("Could not bind to AF_UNIX socket\n");
  679. }
  680. listen (server_fd, SERVER_BACKLOG);
  681. /*
  682. * Setup connection dispatch routine
  683. */
  684. api->poll_accept_add (server_fd);
  685. }
  686. void coroipcs_ipc_exit (void)
  687. {
  688. struct list_head *list;
  689. struct conn_info *conn_info;
  690. unsigned int res;
  691. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  692. list = list->next) {
  693. conn_info = list_entry (list, struct conn_info, list);
  694. /*
  695. * Unmap memory segments
  696. */
  697. res = munmap ((void *)conn_info->control_buffer,
  698. conn_info->control_size);
  699. res = munmap ((void *)conn_info->request_buffer,
  700. conn_info->request_size);
  701. res = munmap ((void *)conn_info->response_buffer,
  702. conn_info->response_size);
  703. res = circular_memory_unmap (conn_info->dispatch_buffer,
  704. conn_info->dispatch_size);
  705. semctl (conn_info->semid, 0, IPC_RMID);
  706. pthread_kill (conn_info->thread, SIGUSR1);
  707. }
  708. }
  709. /*
  710. * Get the conn info private data
  711. */
  712. void *coroipcs_private_data_get (void *conn)
  713. {
  714. struct conn_info *conn_info = (struct conn_info *)conn;
  715. return (conn_info->private_data);
  716. }
  717. int coroipcs_response_send (void *conn, const void *msg, size_t mlen)
  718. {
  719. struct conn_info *conn_info = (struct conn_info *)conn;
  720. struct sembuf sop;
  721. int res;
  722. memcpy (conn_info->response_buffer, msg, mlen);
  723. sop.sem_num = 1;
  724. sop.sem_op = 1;
  725. sop.sem_flg = 0;
  726. retry_semop:
  727. res = semop (conn_info->semid, &sop, 1);
  728. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  729. goto retry_semop;
  730. } else
  731. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  732. return (0);
  733. }
  734. return (0);
  735. }
  736. int coroipcs_response_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  737. {
  738. struct conn_info *conn_info = (struct conn_info *)conn;
  739. struct sembuf sop;
  740. int res;
  741. int write_idx = 0;
  742. int i;
  743. for (i = 0; i < iov_len; i++) {
  744. memcpy (&conn_info->response_buffer[write_idx],
  745. iov[i].iov_base, iov[i].iov_len);
  746. write_idx += iov[i].iov_len;
  747. }
  748. sop.sem_num = 1;
  749. sop.sem_op = 1;
  750. sop.sem_flg = 0;
  751. retry_semop:
  752. res = semop (conn_info->semid, &sop, 1);
  753. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  754. goto retry_semop;
  755. } else
  756. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  757. return (0);
  758. }
  759. return (0);
  760. }
  761. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info)
  762. {
  763. unsigned int n_read;
  764. unsigned int n_write;
  765. unsigned int bytes_left;
  766. n_read = conn_info->control_buffer->read;
  767. n_write = conn_info->control_buffer->write;
  768. if (n_read <= n_write) {
  769. bytes_left = conn_info->dispatch_size - n_write + n_read;
  770. } else {
  771. bytes_left = n_read - n_write;
  772. }
  773. return (bytes_left);
  774. }
  775. static void memcpy_dwrap (struct conn_info *conn_info, void *msg, unsigned int len)
  776. {
  777. unsigned int write_idx;
  778. write_idx = conn_info->control_buffer->write;
  779. memcpy (&conn_info->dispatch_buffer[write_idx], msg, len);
  780. conn_info->control_buffer->write = (write_idx + len) % conn_info->dispatch_size;
  781. }
  782. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  783. int locked)
  784. {
  785. struct conn_info *conn_info = (struct conn_info *)conn;
  786. struct sembuf sop;
  787. int res;
  788. int i;
  789. char buf;
  790. for (i = 0; i < iov_len; i++) {
  791. memcpy_dwrap (conn_info, iov[i].iov_base, iov[i].iov_len);
  792. }
  793. buf = !list_empty (&conn_info->outq_head);
  794. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  795. if (res == -1 && errno == EAGAIN) {
  796. if (locked == 0) {
  797. pthread_mutex_lock (&conn_info->mutex);
  798. }
  799. conn_info->pending_semops += 1;
  800. if (locked == 0) {
  801. pthread_mutex_unlock (&conn_info->mutex);
  802. }
  803. api->poll_dispatch_modify (conn_info->fd,
  804. POLLIN|POLLOUT|POLLNVAL);
  805. } else
  806. if (res == -1) {
  807. ipc_disconnect (conn_info);
  808. }
  809. sop.sem_num = 2;
  810. sop.sem_op = 1;
  811. sop.sem_flg = 0;
  812. retry_semop:
  813. res = semop (conn_info->semid, &sop, 1);
  814. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  815. goto retry_semop;
  816. } else
  817. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  818. return;
  819. }
  820. }
  821. static void outq_flush (struct conn_info *conn_info) {
  822. struct list_head *list, *list_next;
  823. struct outq_item *outq_item;
  824. unsigned int bytes_left;
  825. struct iovec iov;
  826. char buf;
  827. int res;
  828. pthread_mutex_lock (&conn_info->mutex);
  829. if (list_empty (&conn_info->outq_head)) {
  830. buf = 3;
  831. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  832. pthread_mutex_unlock (&conn_info->mutex);
  833. return;
  834. }
  835. for (list = conn_info->outq_head.next;
  836. list != &conn_info->outq_head; list = list_next) {
  837. list_next = list->next;
  838. outq_item = list_entry (list, struct outq_item, list);
  839. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  840. if (bytes_left > outq_item->mlen) {
  841. iov.iov_base = outq_item->msg;
  842. iov.iov_len = outq_item->mlen;
  843. msg_send (conn_info, &iov, 1, MSG_SEND_UNLOCKED);
  844. list_del (list);
  845. api->free (iov.iov_base);
  846. api->free (outq_item);
  847. } else {
  848. break;
  849. }
  850. }
  851. pthread_mutex_unlock (&conn_info->mutex);
  852. }
  853. static int priv_change (struct conn_info *conn_info)
  854. {
  855. mar_req_priv_change req_priv_change;
  856. unsigned int res;
  857. union semun semun;
  858. struct semid_ds ipc_set;
  859. int i;
  860. retry_recv:
  861. res = recv (conn_info->fd, &req_priv_change,
  862. sizeof (mar_req_priv_change),
  863. MSG_NOSIGNAL);
  864. if (res == -1 && errno == EINTR) {
  865. goto retry_recv;
  866. }
  867. if (res == -1 && errno == EAGAIN) {
  868. goto retry_recv;
  869. }
  870. if (res == -1 && errno != EAGAIN) {
  871. return (-1);
  872. }
  873. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  874. /* Error on socket, EOF is detected when recv return 0
  875. */
  876. if (res == 0) {
  877. return (-1);
  878. }
  879. #endif
  880. ipc_set.sem_perm.uid = req_priv_change.euid;
  881. ipc_set.sem_perm.gid = req_priv_change.egid;
  882. ipc_set.sem_perm.mode = 0600;
  883. semun.buf = &ipc_set;
  884. for (i = 0; i < 3; i++) {
  885. res = semctl (conn_info->semid, 0, IPC_SET, semun);
  886. if (res == -1) {
  887. return (-1);
  888. }
  889. }
  890. return (0);
  891. }
  892. static void msg_send_or_queue (void *conn, const struct iovec *iov, unsigned int iov_len)
  893. {
  894. struct conn_info *conn_info = (struct conn_info *)conn;
  895. unsigned int bytes_left;
  896. unsigned int bytes_msg = 0;
  897. int i;
  898. struct outq_item *outq_item;
  899. char *write_buf = 0;
  900. /*
  901. * Exit transmission if the connection is dead
  902. */
  903. if (ipc_thread_active (conn) == 0) {
  904. return;
  905. }
  906. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  907. for (i = 0; i < iov_len; i++) {
  908. bytes_msg += iov[i].iov_len;
  909. }
  910. if (bytes_left < bytes_msg || list_empty (&conn_info->outq_head) == 0) {
  911. outq_item = api->malloc (sizeof (struct outq_item));
  912. if (outq_item == NULL) {
  913. ipc_disconnect (conn);
  914. return;
  915. }
  916. outq_item->msg = api->malloc (bytes_msg);
  917. if (outq_item->msg == 0) {
  918. api->free (outq_item);
  919. ipc_disconnect (conn);
  920. return;
  921. }
  922. write_buf = outq_item->msg;
  923. for (i = 0; i < iov_len; i++) {
  924. memcpy (write_buf, iov[i].iov_base, iov[i].iov_len);
  925. write_buf += iov[i].iov_len;
  926. }
  927. outq_item->mlen = bytes_msg;
  928. list_init (&outq_item->list);
  929. pthread_mutex_lock (&conn_info->mutex);
  930. if (list_empty (&conn_info->outq_head)) {
  931. conn_info->notify_flow_control_enabled = 1;
  932. api->poll_dispatch_modify (conn_info->fd,
  933. POLLIN|POLLOUT|POLLNVAL);
  934. }
  935. list_add_tail (&outq_item->list, &conn_info->outq_head);
  936. pthread_mutex_unlock (&conn_info->mutex);
  937. return;
  938. }
  939. msg_send (conn, iov, iov_len, MSG_SEND_LOCKED);
  940. }
  941. void coroipcs_refcount_inc (void *conn)
  942. {
  943. struct conn_info *conn_info = (struct conn_info *)conn;
  944. pthread_mutex_lock (&conn_info->mutex);
  945. conn_info->refcount++;
  946. pthread_mutex_unlock (&conn_info->mutex);
  947. }
  948. void coroipcs_refcount_dec (void *conn)
  949. {
  950. struct conn_info *conn_info = (struct conn_info *)conn;
  951. pthread_mutex_lock (&conn_info->mutex);
  952. conn_info->refcount--;
  953. pthread_mutex_unlock (&conn_info->mutex);
  954. }
  955. int coroipcs_dispatch_send (void *conn, const void *msg, size_t mlen)
  956. {
  957. struct iovec iov;
  958. iov.iov_base = (void *)msg;
  959. iov.iov_len = mlen;
  960. msg_send_or_queue (conn, &iov, 1);
  961. return (0);
  962. }
  963. int coroipcs_dispatch_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  964. {
  965. msg_send_or_queue (conn, iov, iov_len);
  966. return (0);
  967. }
  968. int coroipcs_handler_accept (
  969. int fd,
  970. int revent,
  971. void *data)
  972. {
  973. socklen_t addrlen;
  974. struct sockaddr_un un_addr;
  975. int new_fd;
  976. #ifdef COROSYNC_LINUX
  977. int on = 1;
  978. #endif
  979. int res;
  980. addrlen = sizeof (struct sockaddr_un);
  981. retry_accept:
  982. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  983. if (new_fd == -1 && errno == EINTR) {
  984. goto retry_accept;
  985. }
  986. if (new_fd == -1) {
  987. api->log_printf ("Could not accept Library connection: %s\n", strerror (errno));
  988. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  989. }
  990. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  991. if (res == -1) {
  992. api->log_printf ("Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  993. close (new_fd);
  994. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  995. }
  996. /*
  997. * Valid accept
  998. */
  999. /*
  1000. * Request credentials of sender provided by kernel
  1001. */
  1002. #ifdef COROSYNC_LINUX
  1003. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  1004. #endif
  1005. res = conn_info_create (new_fd);
  1006. if (res != 0) {
  1007. close (new_fd);
  1008. }
  1009. return (0);
  1010. }
  1011. int coroipcs_handler_dispatch (
  1012. int fd,
  1013. int revent,
  1014. void *context)
  1015. {
  1016. mar_req_setup_t *req_setup;
  1017. struct conn_info *conn_info = (struct conn_info *)context;
  1018. int res;
  1019. char buf;
  1020. if (ipc_thread_exiting (conn_info)) {
  1021. return conn_info_destroy (conn_info);
  1022. }
  1023. /*
  1024. * If an error occurs, request exit
  1025. */
  1026. if (revent & (POLLERR|POLLHUP)) {
  1027. ipc_disconnect (conn_info);
  1028. return (0);
  1029. }
  1030. /*
  1031. * Read the header and process it
  1032. */
  1033. if (conn_info->service == SOCKET_SERVICE_INIT && (revent & POLLIN)) {
  1034. /*
  1035. * Receive in a nonblocking fashion the request
  1036. * IF security invalid, send TRY_AGAIN, otherwise
  1037. * send OK
  1038. */
  1039. res = req_setup_recv (conn_info);
  1040. if (res == -1) {
  1041. req_setup_send (conn_info, CS_ERR_TRY_AGAIN);
  1042. }
  1043. if (res != 1) {
  1044. return (0);
  1045. }
  1046. req_setup_send (conn_info, CS_OK);
  1047. pthread_mutex_init (&conn_info->mutex, NULL);
  1048. req_setup = (mar_req_setup_t *)conn_info->setup_msg;
  1049. /*
  1050. * Is the service registered ?
  1051. */
  1052. if (api->service_available (req_setup->service) == 0) {
  1053. ipc_disconnect (conn_info);
  1054. return (0);
  1055. }
  1056. conn_info->semkey = req_setup->semkey;
  1057. res = memory_map (
  1058. req_setup->control_file,
  1059. req_setup->control_size,
  1060. (void *)&conn_info->control_buffer);
  1061. conn_info->control_size = req_setup->control_size;
  1062. res = memory_map (
  1063. req_setup->request_file,
  1064. req_setup->request_size,
  1065. (void *)&conn_info->request_buffer);
  1066. conn_info->request_size = req_setup->request_size;
  1067. res = memory_map (
  1068. req_setup->response_file,
  1069. req_setup->response_size,
  1070. (void *)&conn_info->response_buffer);
  1071. conn_info->response_size = req_setup->response_size;
  1072. res = circular_memory_map (
  1073. req_setup->dispatch_file,
  1074. req_setup->dispatch_size,
  1075. (void *)&conn_info->dispatch_buffer);
  1076. conn_info->dispatch_size = req_setup->dispatch_size;
  1077. conn_info->service = req_setup->service;
  1078. conn_info->refcount = 0;
  1079. conn_info->notify_flow_control_enabled = 0;
  1080. conn_info->setup_bytes_read = 0;
  1081. conn_info->semid = semget (conn_info->semkey, 3, 0600);
  1082. conn_info->pending_semops = 0;
  1083. /*
  1084. * ipc thread is the only reference at startup
  1085. */
  1086. conn_info->refcount = 1;
  1087. conn_info->state = CONN_STATE_THREAD_ACTIVE;
  1088. conn_info->private_data = api->malloc (api->private_data_size_get (conn_info->service));
  1089. memset (conn_info->private_data, 0,
  1090. api->private_data_size_get (conn_info->service));
  1091. api->init_fn_get (conn_info->service) (conn_info);
  1092. pthread_attr_init (&conn_info->thread_attr);
  1093. /*
  1094. * IA64 needs more stack space then other arches
  1095. */
  1096. #if defined(__ia64__)
  1097. pthread_attr_setstacksize (&conn_info->thread_attr, 400000);
  1098. #else
  1099. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  1100. #endif
  1101. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_JOINABLE);
  1102. res = pthread_create (&conn_info->thread,
  1103. &conn_info->thread_attr,
  1104. pthread_ipc_consumer,
  1105. conn_info);
  1106. /*
  1107. * Security check - disallow multiple configurations of
  1108. * the ipc connection
  1109. */
  1110. if (conn_info->service == SOCKET_SERVICE_INIT) {
  1111. conn_info->service = -1;
  1112. }
  1113. } else
  1114. if (revent & POLLIN) {
  1115. coroipcs_refcount_inc (conn_info);
  1116. res = recv (fd, &buf, 1, MSG_NOSIGNAL);
  1117. if (res == 1) {
  1118. switch (buf) {
  1119. case MESSAGE_REQ_OUTQ_FLUSH:
  1120. outq_flush (conn_info);
  1121. break;
  1122. case MESSAGE_REQ_CHANGE_EUID:
  1123. if (priv_change (conn_info) == -1) {
  1124. ipc_disconnect (conn_info);
  1125. }
  1126. break;
  1127. default:
  1128. res = 0;
  1129. break;
  1130. }
  1131. }
  1132. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1133. /* On many OS poll never return POLLHUP or POLLERR.
  1134. * EOF is detected when recvmsg return 0.
  1135. */
  1136. if (res == 0) {
  1137. ipc_disconnect (conn_info);
  1138. coroipcs_refcount_dec (conn_info);
  1139. return (0);
  1140. }
  1141. #endif
  1142. coroipcs_refcount_dec (conn_info);
  1143. }
  1144. coroipcs_refcount_inc (conn_info);
  1145. pthread_mutex_lock (&conn_info->mutex);
  1146. if ((conn_info->state == CONN_STATE_THREAD_ACTIVE) && (revent & POLLOUT)) {
  1147. buf = !list_empty (&conn_info->outq_head);
  1148. for (; conn_info->pending_semops;) {
  1149. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1150. if (res == 1) {
  1151. conn_info->pending_semops--;
  1152. } else {
  1153. break;
  1154. }
  1155. }
  1156. if (conn_info->notify_flow_control_enabled) {
  1157. buf = 2;
  1158. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1159. if (res == 1) {
  1160. conn_info->notify_flow_control_enabled = 0;
  1161. }
  1162. }
  1163. if (conn_info->notify_flow_control_enabled == 0 &&
  1164. conn_info->pending_semops == 0) {
  1165. api->poll_dispatch_modify (conn_info->fd,
  1166. POLLIN|POLLNVAL);
  1167. }
  1168. }
  1169. pthread_mutex_unlock (&conn_info->mutex);
  1170. coroipcs_refcount_dec (conn_info);
  1171. return (0);
  1172. }