coroipcs.c 33 KB

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