coroipcs.c 33 KB

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