coroipcs.c 40 KB

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