coroipcs.c 32 KB

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