coroipcs.c 31 KB

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