coroipcs.c 36 KB

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