coroipcs.c 33 KB

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