coroipcs.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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 = NULL;
  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. {
  706. struct stat stat_out;
  707. res = stat (SOCKETDIR, &stat_out);
  708. if (res == -1 || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  709. api->log_printf ("Required directory not present %s\n", SOCKETDIR);
  710. api->fatal_error ("Please create required directory.");
  711. }
  712. sprintf (un_addr.sun_path, "%s/%s", SOCKETDIR, api->socket_name);
  713. unlink (un_addr.sun_path);
  714. }
  715. #endif
  716. res = bind (server_fd, (struct sockaddr *)&un_addr, COROSYNC_SUN_LEN(&un_addr));
  717. if (res) {
  718. api->log_printf ("Could not bind AF_UNIX (%s): %s.\n", un_addr.sun_path, strerror (errno));
  719. api->fatal_error ("Could not bind to AF_UNIX socket\n");
  720. }
  721. /*
  722. * Allow eveyrone to write to the socket since the IPC layer handles
  723. * security automatically
  724. */
  725. #if !defined(COROSYNC_LINUX)
  726. res = chmod (un_addr.sun_path, S_IRWXU|S_IRWXG|S_IRWXO);
  727. #endif
  728. listen (server_fd, SERVER_BACKLOG);
  729. /*
  730. * Setup connection dispatch routine
  731. */
  732. api->poll_accept_add (server_fd);
  733. }
  734. void coroipcs_ipc_exit (void)
  735. {
  736. struct list_head *list;
  737. struct conn_info *conn_info;
  738. unsigned int res;
  739. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  740. list = list->next) {
  741. conn_info = list_entry (list, struct conn_info, list);
  742. /*
  743. * Unmap memory segments
  744. */
  745. res = munmap ((void *)conn_info->control_buffer,
  746. conn_info->control_size);
  747. res = munmap ((void *)conn_info->request_buffer,
  748. conn_info->request_size);
  749. res = munmap ((void *)conn_info->response_buffer,
  750. conn_info->response_size);
  751. res = circular_memory_unmap (conn_info->dispatch_buffer,
  752. conn_info->dispatch_size);
  753. semctl (conn_info->semid, 0, IPC_RMID);
  754. pthread_kill (conn_info->thread, SIGUSR1);
  755. }
  756. }
  757. /*
  758. * Get the conn info private data
  759. */
  760. void *coroipcs_private_data_get (void *conn)
  761. {
  762. struct conn_info *conn_info = (struct conn_info *)conn;
  763. return (conn_info->private_data);
  764. }
  765. int coroipcs_response_send (void *conn, const void *msg, size_t mlen)
  766. {
  767. struct conn_info *conn_info = (struct conn_info *)conn;
  768. struct sembuf sop;
  769. int res;
  770. memcpy (conn_info->response_buffer, msg, mlen);
  771. sop.sem_num = 1;
  772. sop.sem_op = 1;
  773. sop.sem_flg = 0;
  774. retry_semop:
  775. res = semop (conn_info->semid, &sop, 1);
  776. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  777. goto retry_semop;
  778. } else
  779. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  780. return (0);
  781. }
  782. return (0);
  783. }
  784. int coroipcs_response_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  785. {
  786. struct conn_info *conn_info = (struct conn_info *)conn;
  787. struct sembuf sop;
  788. int res;
  789. int write_idx = 0;
  790. int i;
  791. for (i = 0; i < iov_len; i++) {
  792. memcpy (&conn_info->response_buffer[write_idx],
  793. iov[i].iov_base, iov[i].iov_len);
  794. write_idx += iov[i].iov_len;
  795. }
  796. sop.sem_num = 1;
  797. sop.sem_op = 1;
  798. sop.sem_flg = 0;
  799. retry_semop:
  800. res = semop (conn_info->semid, &sop, 1);
  801. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  802. goto retry_semop;
  803. } else
  804. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  805. return (0);
  806. }
  807. return (0);
  808. }
  809. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info)
  810. {
  811. unsigned int n_read;
  812. unsigned int n_write;
  813. unsigned int bytes_left;
  814. n_read = conn_info->control_buffer->read;
  815. n_write = conn_info->control_buffer->write;
  816. if (n_read <= n_write) {
  817. bytes_left = conn_info->dispatch_size - n_write + n_read;
  818. } else {
  819. bytes_left = n_read - n_write;
  820. }
  821. return (bytes_left);
  822. }
  823. static void memcpy_dwrap (struct conn_info *conn_info, void *msg, unsigned int len)
  824. {
  825. unsigned int write_idx;
  826. write_idx = conn_info->control_buffer->write;
  827. memcpy (&conn_info->dispatch_buffer[write_idx], msg, len);
  828. conn_info->control_buffer->write = (write_idx + len) % conn_info->dispatch_size;
  829. }
  830. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  831. int locked)
  832. {
  833. struct conn_info *conn_info = (struct conn_info *)conn;
  834. struct sembuf sop;
  835. int res;
  836. int i;
  837. char buf;
  838. for (i = 0; i < iov_len; i++) {
  839. memcpy_dwrap (conn_info, iov[i].iov_base, iov[i].iov_len);
  840. }
  841. buf = !list_empty (&conn_info->outq_head);
  842. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  843. if (res == -1 && errno == EAGAIN) {
  844. if (locked == 0) {
  845. pthread_mutex_lock (&conn_info->mutex);
  846. }
  847. conn_info->pending_semops += 1;
  848. if (locked == 0) {
  849. pthread_mutex_unlock (&conn_info->mutex);
  850. }
  851. api->poll_dispatch_modify (conn_info->fd,
  852. POLLIN|POLLOUT|POLLNVAL);
  853. } else
  854. if (res == -1) {
  855. ipc_disconnect (conn_info);
  856. }
  857. sop.sem_num = 2;
  858. sop.sem_op = 1;
  859. sop.sem_flg = 0;
  860. retry_semop:
  861. res = semop (conn_info->semid, &sop, 1);
  862. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  863. goto retry_semop;
  864. } else
  865. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  866. return;
  867. }
  868. }
  869. static void outq_flush (struct conn_info *conn_info) {
  870. struct list_head *list, *list_next;
  871. struct outq_item *outq_item;
  872. unsigned int bytes_left;
  873. struct iovec iov;
  874. char buf;
  875. int res;
  876. pthread_mutex_lock (&conn_info->mutex);
  877. if (list_empty (&conn_info->outq_head)) {
  878. buf = 3;
  879. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  880. pthread_mutex_unlock (&conn_info->mutex);
  881. return;
  882. }
  883. for (list = conn_info->outq_head.next;
  884. list != &conn_info->outq_head; list = list_next) {
  885. list_next = list->next;
  886. outq_item = list_entry (list, struct outq_item, list);
  887. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  888. if (bytes_left > outq_item->mlen) {
  889. iov.iov_base = outq_item->msg;
  890. iov.iov_len = outq_item->mlen;
  891. msg_send (conn_info, &iov, 1, MSG_SEND_UNLOCKED);
  892. list_del (list);
  893. api->free (iov.iov_base);
  894. api->free (outq_item);
  895. } else {
  896. break;
  897. }
  898. }
  899. pthread_mutex_unlock (&conn_info->mutex);
  900. }
  901. static int priv_change (struct conn_info *conn_info)
  902. {
  903. mar_req_priv_change req_priv_change;
  904. unsigned int res;
  905. union semun semun;
  906. struct semid_ds ipc_set;
  907. int i;
  908. retry_recv:
  909. res = recv (conn_info->fd, &req_priv_change,
  910. sizeof (mar_req_priv_change),
  911. MSG_NOSIGNAL);
  912. if (res == -1 && errno == EINTR) {
  913. goto retry_recv;
  914. }
  915. if (res == -1 && errno == EAGAIN) {
  916. goto retry_recv;
  917. }
  918. if (res == -1 && errno != EAGAIN) {
  919. return (-1);
  920. }
  921. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  922. /* Error on socket, EOF is detected when recv return 0
  923. */
  924. if (res == 0) {
  925. return (-1);
  926. }
  927. #endif
  928. ipc_set.sem_perm.uid = req_priv_change.euid;
  929. ipc_set.sem_perm.gid = req_priv_change.egid;
  930. ipc_set.sem_perm.mode = 0600;
  931. semun.buf = &ipc_set;
  932. for (i = 0; i < 3; i++) {
  933. res = semctl (conn_info->semid, 0, IPC_SET, semun);
  934. if (res == -1) {
  935. return (-1);
  936. }
  937. }
  938. return (0);
  939. }
  940. static void msg_send_or_queue (void *conn, const struct iovec *iov, unsigned int iov_len)
  941. {
  942. struct conn_info *conn_info = (struct conn_info *)conn;
  943. unsigned int bytes_left;
  944. unsigned int bytes_msg = 0;
  945. int i;
  946. struct outq_item *outq_item;
  947. char *write_buf = 0;
  948. /*
  949. * Exit transmission if the connection is dead
  950. */
  951. if (ipc_thread_active (conn) == 0) {
  952. return;
  953. }
  954. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  955. for (i = 0; i < iov_len; i++) {
  956. bytes_msg += iov[i].iov_len;
  957. }
  958. if (bytes_left < bytes_msg || list_empty (&conn_info->outq_head) == 0) {
  959. outq_item = api->malloc (sizeof (struct outq_item));
  960. if (outq_item == NULL) {
  961. ipc_disconnect (conn);
  962. return;
  963. }
  964. outq_item->msg = api->malloc (bytes_msg);
  965. if (outq_item->msg == 0) {
  966. api->free (outq_item);
  967. ipc_disconnect (conn);
  968. return;
  969. }
  970. write_buf = outq_item->msg;
  971. for (i = 0; i < iov_len; i++) {
  972. memcpy (write_buf, iov[i].iov_base, iov[i].iov_len);
  973. write_buf += iov[i].iov_len;
  974. }
  975. outq_item->mlen = bytes_msg;
  976. list_init (&outq_item->list);
  977. pthread_mutex_lock (&conn_info->mutex);
  978. if (list_empty (&conn_info->outq_head)) {
  979. conn_info->notify_flow_control_enabled = 1;
  980. api->poll_dispatch_modify (conn_info->fd,
  981. POLLIN|POLLOUT|POLLNVAL);
  982. }
  983. list_add_tail (&outq_item->list, &conn_info->outq_head);
  984. pthread_mutex_unlock (&conn_info->mutex);
  985. return;
  986. }
  987. msg_send (conn, iov, iov_len, MSG_SEND_LOCKED);
  988. }
  989. void coroipcs_refcount_inc (void *conn)
  990. {
  991. struct conn_info *conn_info = (struct conn_info *)conn;
  992. pthread_mutex_lock (&conn_info->mutex);
  993. conn_info->refcount++;
  994. pthread_mutex_unlock (&conn_info->mutex);
  995. }
  996. void coroipcs_refcount_dec (void *conn)
  997. {
  998. struct conn_info *conn_info = (struct conn_info *)conn;
  999. pthread_mutex_lock (&conn_info->mutex);
  1000. conn_info->refcount--;
  1001. pthread_mutex_unlock (&conn_info->mutex);
  1002. }
  1003. int coroipcs_dispatch_send (void *conn, const void *msg, size_t mlen)
  1004. {
  1005. struct iovec iov;
  1006. iov.iov_base = (void *)msg;
  1007. iov.iov_len = mlen;
  1008. msg_send_or_queue (conn, &iov, 1);
  1009. return (0);
  1010. }
  1011. int coroipcs_dispatch_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  1012. {
  1013. msg_send_or_queue (conn, iov, iov_len);
  1014. return (0);
  1015. }
  1016. int coroipcs_handler_accept (
  1017. int fd,
  1018. int revent,
  1019. void *data)
  1020. {
  1021. socklen_t addrlen;
  1022. struct sockaddr_un un_addr;
  1023. int new_fd;
  1024. #ifdef COROSYNC_LINUX
  1025. int on = 1;
  1026. #endif
  1027. int res;
  1028. addrlen = sizeof (struct sockaddr_un);
  1029. retry_accept:
  1030. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  1031. if (new_fd == -1 && errno == EINTR) {
  1032. goto retry_accept;
  1033. }
  1034. if (new_fd == -1) {
  1035. api->log_printf ("Could not accept Library connection: %s\n", strerror (errno));
  1036. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1037. }
  1038. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  1039. if (res == -1) {
  1040. api->log_printf ("Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  1041. close (new_fd);
  1042. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1043. }
  1044. /*
  1045. * Valid accept
  1046. */
  1047. /*
  1048. * Request credentials of sender provided by kernel
  1049. */
  1050. #ifdef COROSYNC_LINUX
  1051. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  1052. #endif
  1053. res = conn_info_create (new_fd);
  1054. if (res != 0) {
  1055. close (new_fd);
  1056. }
  1057. return (0);
  1058. }
  1059. int coroipcs_handler_dispatch (
  1060. int fd,
  1061. int revent,
  1062. void *context)
  1063. {
  1064. mar_req_setup_t *req_setup;
  1065. struct conn_info *conn_info = (struct conn_info *)context;
  1066. int res;
  1067. char buf;
  1068. if (ipc_thread_exiting (conn_info)) {
  1069. return conn_info_destroy (conn_info);
  1070. }
  1071. /*
  1072. * If an error occurs, request exit
  1073. */
  1074. if (revent & (POLLERR|POLLHUP)) {
  1075. ipc_disconnect (conn_info);
  1076. return (0);
  1077. }
  1078. /*
  1079. * Read the header and process it
  1080. */
  1081. if (conn_info->service == SOCKET_SERVICE_INIT && (revent & POLLIN)) {
  1082. /*
  1083. * Receive in a nonblocking fashion the request
  1084. * IF security invalid, send TRY_AGAIN, otherwise
  1085. * send OK
  1086. */
  1087. res = req_setup_recv (conn_info);
  1088. if (res == -1) {
  1089. req_setup_send (conn_info, CS_ERR_TRY_AGAIN);
  1090. }
  1091. if (res != 1) {
  1092. return (0);
  1093. }
  1094. req_setup_send (conn_info, CS_OK);
  1095. pthread_mutex_init (&conn_info->mutex, NULL);
  1096. req_setup = (mar_req_setup_t *)conn_info->setup_msg;
  1097. /*
  1098. * Is the service registered ?
  1099. */
  1100. if (api->service_available (req_setup->service) == 0) {
  1101. ipc_disconnect (conn_info);
  1102. return (0);
  1103. }
  1104. conn_info->semkey = req_setup->semkey;
  1105. res = memory_map (
  1106. req_setup->control_file,
  1107. req_setup->control_size,
  1108. (void *)&conn_info->control_buffer);
  1109. conn_info->control_size = req_setup->control_size;
  1110. res = memory_map (
  1111. req_setup->request_file,
  1112. req_setup->request_size,
  1113. (void *)&conn_info->request_buffer);
  1114. conn_info->request_size = req_setup->request_size;
  1115. res = memory_map (
  1116. req_setup->response_file,
  1117. req_setup->response_size,
  1118. (void *)&conn_info->response_buffer);
  1119. conn_info->response_size = req_setup->response_size;
  1120. res = circular_memory_map (
  1121. req_setup->dispatch_file,
  1122. req_setup->dispatch_size,
  1123. (void *)&conn_info->dispatch_buffer);
  1124. conn_info->dispatch_size = req_setup->dispatch_size;
  1125. conn_info->service = req_setup->service;
  1126. conn_info->refcount = 0;
  1127. conn_info->notify_flow_control_enabled = 0;
  1128. conn_info->setup_bytes_read = 0;
  1129. conn_info->semid = semget (conn_info->semkey, 3, 0600);
  1130. conn_info->pending_semops = 0;
  1131. /*
  1132. * ipc thread is the only reference at startup
  1133. */
  1134. conn_info->refcount = 1;
  1135. conn_info->state = CONN_STATE_THREAD_ACTIVE;
  1136. conn_info->private_data = api->malloc (api->private_data_size_get (conn_info->service));
  1137. memset (conn_info->private_data, 0,
  1138. api->private_data_size_get (conn_info->service));
  1139. api->init_fn_get (conn_info->service) (conn_info);
  1140. pthread_attr_init (&conn_info->thread_attr);
  1141. /*
  1142. * IA64 needs more stack space then other arches
  1143. */
  1144. #if defined(__ia64__)
  1145. pthread_attr_setstacksize (&conn_info->thread_attr, 400000);
  1146. #else
  1147. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  1148. #endif
  1149. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_JOINABLE);
  1150. res = pthread_create (&conn_info->thread,
  1151. &conn_info->thread_attr,
  1152. pthread_ipc_consumer,
  1153. conn_info);
  1154. /*
  1155. * Security check - disallow multiple configurations of
  1156. * the ipc connection
  1157. */
  1158. if (conn_info->service == SOCKET_SERVICE_INIT) {
  1159. conn_info->service = -1;
  1160. }
  1161. } else
  1162. if (revent & POLLIN) {
  1163. coroipcs_refcount_inc (conn_info);
  1164. res = recv (fd, &buf, 1, MSG_NOSIGNAL);
  1165. if (res == 1) {
  1166. switch (buf) {
  1167. case MESSAGE_REQ_OUTQ_FLUSH:
  1168. outq_flush (conn_info);
  1169. break;
  1170. case MESSAGE_REQ_CHANGE_EUID:
  1171. if (priv_change (conn_info) == -1) {
  1172. ipc_disconnect (conn_info);
  1173. }
  1174. break;
  1175. default:
  1176. res = 0;
  1177. break;
  1178. }
  1179. }
  1180. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1181. /* On many OS poll never return POLLHUP or POLLERR.
  1182. * EOF is detected when recvmsg return 0.
  1183. */
  1184. if (res == 0) {
  1185. ipc_disconnect (conn_info);
  1186. coroipcs_refcount_dec (conn_info);
  1187. return (0);
  1188. }
  1189. #endif
  1190. coroipcs_refcount_dec (conn_info);
  1191. }
  1192. coroipcs_refcount_inc (conn_info);
  1193. pthread_mutex_lock (&conn_info->mutex);
  1194. if ((conn_info->state == CONN_STATE_THREAD_ACTIVE) && (revent & POLLOUT)) {
  1195. buf = !list_empty (&conn_info->outq_head);
  1196. for (; conn_info->pending_semops;) {
  1197. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1198. if (res == 1) {
  1199. conn_info->pending_semops--;
  1200. } else {
  1201. break;
  1202. }
  1203. }
  1204. if (conn_info->notify_flow_control_enabled) {
  1205. buf = 2;
  1206. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1207. if (res == 1) {
  1208. conn_info->notify_flow_control_enabled = 0;
  1209. }
  1210. }
  1211. if (conn_info->notify_flow_control_enabled == 0 &&
  1212. conn_info->pending_semops == 0) {
  1213. api->poll_dispatch_modify (conn_info->fd,
  1214. POLLIN|POLLNVAL);
  1215. }
  1216. }
  1217. pthread_mutex_unlock (&conn_info->mutex);
  1218. coroipcs_refcount_dec (conn_info);
  1219. return (0);
  1220. }