coroipcs.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. req_setup_send (conn_info, CS_OK);
  1195. pthread_mutex_init (&conn_info->mutex, NULL);
  1196. req_setup = (mar_req_setup_t *)conn_info->setup_msg;
  1197. /*
  1198. * Is the service registered ?
  1199. */
  1200. if (api->service_available (req_setup->service) == 0) {
  1201. ipc_disconnect (conn_info);
  1202. return (0);
  1203. }
  1204. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1205. conn_info->semkey = req_setup->semkey;
  1206. #endif
  1207. res = memory_map (
  1208. req_setup->control_file,
  1209. req_setup->control_size,
  1210. (void *)&conn_info->control_buffer);
  1211. conn_info->control_size = req_setup->control_size;
  1212. res = memory_map (
  1213. req_setup->request_file,
  1214. req_setup->request_size,
  1215. (void *)&conn_info->request_buffer);
  1216. conn_info->request_size = req_setup->request_size;
  1217. res = memory_map (
  1218. req_setup->response_file,
  1219. req_setup->response_size,
  1220. (void *)&conn_info->response_buffer);
  1221. conn_info->response_size = req_setup->response_size;
  1222. res = circular_memory_map (
  1223. req_setup->dispatch_file,
  1224. req_setup->dispatch_size,
  1225. (void *)&conn_info->dispatch_buffer);
  1226. conn_info->dispatch_size = req_setup->dispatch_size;
  1227. conn_info->service = req_setup->service;
  1228. conn_info->refcount = 0;
  1229. conn_info->notify_flow_control_enabled = 0;
  1230. conn_info->setup_bytes_read = 0;
  1231. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1232. conn_info->semid = semget (conn_info->semkey, 3, 0600);
  1233. #endif
  1234. conn_info->pending_semops = 0;
  1235. /*
  1236. * ipc thread is the only reference at startup
  1237. */
  1238. conn_info->refcount = 1;
  1239. conn_info->state = CONN_STATE_THREAD_ACTIVE;
  1240. conn_info->private_data = api->malloc (api->private_data_size_get (conn_info->service));
  1241. memset (conn_info->private_data, 0,
  1242. api->private_data_size_get (conn_info->service));
  1243. api->init_fn_get (conn_info->service) (conn_info);
  1244. pthread_attr_init (&conn_info->thread_attr);
  1245. /*
  1246. * IA64 needs more stack space then other arches
  1247. */
  1248. #if defined(__ia64__)
  1249. pthread_attr_setstacksize (&conn_info->thread_attr, 400000);
  1250. #else
  1251. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  1252. #endif
  1253. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_JOINABLE);
  1254. res = pthread_create (&conn_info->thread,
  1255. &conn_info->thread_attr,
  1256. pthread_ipc_consumer,
  1257. conn_info);
  1258. /*
  1259. * Security check - disallow multiple configurations of
  1260. * the ipc connection
  1261. */
  1262. if (conn_info->service == SOCKET_SERVICE_INIT) {
  1263. conn_info->service = -1;
  1264. }
  1265. } else
  1266. if (revent & POLLIN) {
  1267. coroipcs_refcount_inc (conn_info);
  1268. res = recv (fd, &buf, 1, MSG_NOSIGNAL);
  1269. if (res == 1) {
  1270. switch (buf) {
  1271. case MESSAGE_REQ_OUTQ_FLUSH:
  1272. outq_flush (conn_info);
  1273. break;
  1274. case MESSAGE_REQ_CHANGE_EUID:
  1275. if (priv_change (conn_info) == -1) {
  1276. ipc_disconnect (conn_info);
  1277. }
  1278. break;
  1279. default:
  1280. res = 0;
  1281. break;
  1282. }
  1283. }
  1284. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1285. /* On many OS poll never return POLLHUP or POLLERR.
  1286. * EOF is detected when recvmsg return 0.
  1287. */
  1288. if (res == 0) {
  1289. ipc_disconnect (conn_info);
  1290. coroipcs_refcount_dec (conn_info);
  1291. return (0);
  1292. }
  1293. #endif
  1294. coroipcs_refcount_dec (conn_info);
  1295. }
  1296. coroipcs_refcount_inc (conn_info);
  1297. pthread_mutex_lock (&conn_info->mutex);
  1298. if ((conn_info->state == CONN_STATE_THREAD_ACTIVE) && (revent & POLLOUT)) {
  1299. buf = !list_empty (&conn_info->outq_head);
  1300. for (; conn_info->pending_semops;) {
  1301. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1302. if (res == 1) {
  1303. conn_info->pending_semops--;
  1304. } else {
  1305. break;
  1306. }
  1307. }
  1308. if (conn_info->notify_flow_control_enabled) {
  1309. buf = 2;
  1310. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1311. if (res == 1) {
  1312. conn_info->notify_flow_control_enabled = 0;
  1313. }
  1314. }
  1315. if (conn_info->notify_flow_control_enabled == 0 &&
  1316. conn_info->pending_semops == 0) {
  1317. api->poll_dispatch_modify (conn_info->fd,
  1318. POLLIN|POLLNVAL);
  1319. }
  1320. }
  1321. pthread_mutex_unlock (&conn_info->mutex);
  1322. coroipcs_refcount_dec (conn_info);
  1323. return (0);
  1324. }