coroipcs.c 32 KB

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