coroipcs.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  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. if (ipc_thread_active (conn_info) == 0) {
  542. coroipcs_refcount_dec (conn_info);
  543. pthread_exit (0);
  544. }
  545. res = semop (conn_info->semid, &sop, 1);
  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. goto retry_semop;
  914. } else
  915. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  916. return (0);
  917. }
  918. #endif
  919. stats_api->stats_increment_value (conn_info->stats_handle, "responses");
  920. return (0);
  921. }
  922. int coroipcs_response_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  923. {
  924. struct conn_info *conn_info = (struct conn_info *)conn;
  925. #if _POSIX_THREAD_PROCESS_SHARED < 1
  926. struct sembuf sop;
  927. #endif
  928. int res;
  929. int write_idx = 0;
  930. int i;
  931. for (i = 0; i < iov_len; i++) {
  932. memcpy (&conn_info->response_buffer[write_idx],
  933. iov[i].iov_base, iov[i].iov_len);
  934. write_idx += iov[i].iov_len;
  935. }
  936. #if _POSIX_THREAD_PROCESS_SHARED > 0
  937. res = sem_post (&conn_info->control_buffer->sem1);
  938. if (res == -1) {
  939. return (-1);
  940. }
  941. #else
  942. sop.sem_num = 1;
  943. sop.sem_op = 1;
  944. sop.sem_flg = 0;
  945. retry_semop:
  946. res = semop (conn_info->semid, &sop, 1);
  947. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  948. goto retry_semop;
  949. } else
  950. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  951. return (0);
  952. }
  953. #endif
  954. return (0);
  955. }
  956. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info)
  957. {
  958. unsigned int n_read;
  959. unsigned int n_write;
  960. unsigned int bytes_left;
  961. n_read = conn_info->control_buffer->read;
  962. n_write = conn_info->control_buffer->write;
  963. if (n_read <= n_write) {
  964. bytes_left = conn_info->dispatch_size - n_write + n_read;
  965. } else {
  966. bytes_left = n_read - n_write;
  967. }
  968. return (bytes_left);
  969. }
  970. static void memcpy_dwrap (struct conn_info *conn_info, void *msg, unsigned int len)
  971. {
  972. unsigned int write_idx;
  973. write_idx = conn_info->control_buffer->write;
  974. memcpy (&conn_info->dispatch_buffer[write_idx], msg, len);
  975. conn_info->control_buffer->write = (write_idx + len) % conn_info->dispatch_size;
  976. }
  977. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  978. int locked)
  979. {
  980. struct conn_info *conn_info = (struct conn_info *)conn;
  981. #if _POSIX_THREAD_PROCESS_SHARED < 1
  982. struct sembuf sop;
  983. #endif
  984. int res;
  985. int i;
  986. char buf;
  987. for (i = 0; i < iov_len; i++) {
  988. memcpy_dwrap (conn_info, iov[i].iov_base, iov[i].iov_len);
  989. }
  990. buf = !list_empty (&conn_info->outq_head);
  991. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  992. if (res == -1 && errno == EAGAIN) {
  993. if (locked == 0) {
  994. pthread_mutex_lock (&conn_info->mutex);
  995. }
  996. conn_info->pending_semops += 1;
  997. if (locked == 0) {
  998. pthread_mutex_unlock (&conn_info->mutex);
  999. }
  1000. api->poll_dispatch_modify (conn_info->fd,
  1001. POLLIN|POLLOUT|POLLNVAL);
  1002. } else
  1003. if (res == -1) {
  1004. ipc_disconnect (conn_info);
  1005. }
  1006. #if _POSIX_THREAD_PROCESS_SHARED > 0
  1007. res = sem_post (&conn_info->control_buffer->sem2);
  1008. #else
  1009. sop.sem_num = 2;
  1010. sop.sem_op = 1;
  1011. sop.sem_flg = 0;
  1012. retry_semop:
  1013. res = semop (conn_info->semid, &sop, 1);
  1014. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  1015. goto retry_semop;
  1016. } else
  1017. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  1018. return;
  1019. }
  1020. #endif
  1021. stats_api->stats_increment_value (conn_info->stats_handle, "dispatched");
  1022. }
  1023. static void outq_flush (struct conn_info *conn_info) {
  1024. struct list_head *list, *list_next;
  1025. struct outq_item *outq_item;
  1026. unsigned int bytes_left;
  1027. struct iovec iov;
  1028. char buf;
  1029. int res;
  1030. pthread_mutex_lock (&conn_info->mutex);
  1031. if (list_empty (&conn_info->outq_head)) {
  1032. buf = 3;
  1033. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1034. pthread_mutex_unlock (&conn_info->mutex);
  1035. return;
  1036. }
  1037. for (list = conn_info->outq_head.next;
  1038. list != &conn_info->outq_head; list = list_next) {
  1039. list_next = list->next;
  1040. outq_item = list_entry (list, struct outq_item, list);
  1041. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  1042. if (bytes_left > outq_item->mlen) {
  1043. iov.iov_base = outq_item->msg;
  1044. iov.iov_len = outq_item->mlen;
  1045. msg_send (conn_info, &iov, 1, MSG_SEND_UNLOCKED);
  1046. list_del (list);
  1047. api->free (iov.iov_base);
  1048. api->free (outq_item);
  1049. } else {
  1050. break;
  1051. }
  1052. }
  1053. pthread_mutex_unlock (&conn_info->mutex);
  1054. }
  1055. static int priv_change (struct conn_info *conn_info)
  1056. {
  1057. mar_req_priv_change req_priv_change;
  1058. unsigned int res;
  1059. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1060. union semun semun;
  1061. struct semid_ds ipc_set;
  1062. int i;
  1063. #endif
  1064. retry_recv:
  1065. res = recv (conn_info->fd, &req_priv_change,
  1066. sizeof (mar_req_priv_change),
  1067. MSG_NOSIGNAL);
  1068. if (res == -1 && errno == EINTR) {
  1069. stats_api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
  1070. goto retry_recv;
  1071. }
  1072. if (res == -1 && errno == EAGAIN) {
  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. return (-1);
  1078. }
  1079. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1080. /* Error on socket, EOF is detected when recv return 0
  1081. */
  1082. if (res == 0) {
  1083. return (-1);
  1084. }
  1085. #endif
  1086. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1087. ipc_set.sem_perm.uid = req_priv_change.euid;
  1088. ipc_set.sem_perm.gid = req_priv_change.egid;
  1089. ipc_set.sem_perm.mode = 0600;
  1090. semun.buf = &ipc_set;
  1091. for (i = 0; i < 3; i++) {
  1092. res = semctl (conn_info->semid, 0, IPC_SET, semun);
  1093. if (res == -1) {
  1094. return (-1);
  1095. }
  1096. }
  1097. #endif
  1098. return (0);
  1099. }
  1100. static void msg_send_or_queue (void *conn, const struct iovec *iov, unsigned int iov_len)
  1101. {
  1102. struct conn_info *conn_info = (struct conn_info *)conn;
  1103. unsigned int bytes_left;
  1104. unsigned int bytes_msg = 0;
  1105. int i;
  1106. struct outq_item *outq_item;
  1107. char *write_buf = 0;
  1108. /*
  1109. * Exit transmission if the connection is dead
  1110. */
  1111. if (ipc_thread_active (conn) == 0) {
  1112. return;
  1113. }
  1114. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  1115. for (i = 0; i < iov_len; i++) {
  1116. bytes_msg += iov[i].iov_len;
  1117. }
  1118. if (bytes_left < bytes_msg || list_empty (&conn_info->outq_head) == 0) {
  1119. outq_item = api->malloc (sizeof (struct outq_item));
  1120. if (outq_item == NULL) {
  1121. ipc_disconnect (conn);
  1122. return;
  1123. }
  1124. outq_item->msg = api->malloc (bytes_msg);
  1125. if (outq_item->msg == 0) {
  1126. api->free (outq_item);
  1127. ipc_disconnect (conn);
  1128. return;
  1129. }
  1130. write_buf = outq_item->msg;
  1131. for (i = 0; i < iov_len; i++) {
  1132. memcpy (write_buf, iov[i].iov_base, iov[i].iov_len);
  1133. write_buf += iov[i].iov_len;
  1134. }
  1135. outq_item->mlen = bytes_msg;
  1136. list_init (&outq_item->list);
  1137. pthread_mutex_lock (&conn_info->mutex);
  1138. if (list_empty (&conn_info->outq_head)) {
  1139. conn_info->notify_flow_control_enabled = 1;
  1140. api->poll_dispatch_modify (conn_info->fd,
  1141. POLLIN|POLLOUT|POLLNVAL);
  1142. }
  1143. list_add_tail (&outq_item->list, &conn_info->outq_head);
  1144. pthread_mutex_unlock (&conn_info->mutex);
  1145. return;
  1146. }
  1147. msg_send (conn, iov, iov_len, MSG_SEND_LOCKED);
  1148. }
  1149. void coroipcs_refcount_inc (void *conn)
  1150. {
  1151. struct conn_info *conn_info = (struct conn_info *)conn;
  1152. pthread_mutex_lock (&conn_info->mutex);
  1153. conn_info->refcount++;
  1154. pthread_mutex_unlock (&conn_info->mutex);
  1155. }
  1156. void coroipcs_refcount_dec (void *conn)
  1157. {
  1158. struct conn_info *conn_info = (struct conn_info *)conn;
  1159. pthread_mutex_lock (&conn_info->mutex);
  1160. conn_info->refcount--;
  1161. pthread_mutex_unlock (&conn_info->mutex);
  1162. }
  1163. int coroipcs_dispatch_send (void *conn, const void *msg, size_t mlen)
  1164. {
  1165. struct iovec iov;
  1166. iov.iov_base = (void *)msg;
  1167. iov.iov_len = mlen;
  1168. msg_send_or_queue (conn, &iov, 1);
  1169. return (0);
  1170. }
  1171. int coroipcs_dispatch_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  1172. {
  1173. msg_send_or_queue (conn, iov, iov_len);
  1174. return (0);
  1175. }
  1176. int coroipcs_handler_accept (
  1177. int fd,
  1178. int revent,
  1179. void *data)
  1180. {
  1181. socklen_t addrlen;
  1182. struct sockaddr_un un_addr;
  1183. int new_fd;
  1184. #ifdef COROSYNC_LINUX
  1185. int on = 1;
  1186. #endif
  1187. int res;
  1188. addrlen = sizeof (struct sockaddr_un);
  1189. retry_accept:
  1190. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  1191. if (new_fd == -1 && errno == EINTR) {
  1192. goto retry_accept;
  1193. }
  1194. if (new_fd == -1) {
  1195. api->log_printf ("Could not accept Library connection: %s\n", strerror (errno));
  1196. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1197. }
  1198. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  1199. if (res == -1) {
  1200. api->log_printf ("Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  1201. close (new_fd);
  1202. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1203. }
  1204. /*
  1205. * Valid accept
  1206. */
  1207. /*
  1208. * Request credentials of sender provided by kernel
  1209. */
  1210. #ifdef COROSYNC_LINUX
  1211. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  1212. #endif
  1213. res = conn_info_create (new_fd);
  1214. if (res != 0) {
  1215. close (new_fd);
  1216. }
  1217. return (0);
  1218. }
  1219. static char * pid_to_name (pid_t pid, char *out_name, size_t name_len)
  1220. {
  1221. char *name;
  1222. char *rest;
  1223. FILE *fp;
  1224. char fname[32];
  1225. char buf[256];
  1226. snprintf (fname, 32, "/proc/%d/stat", pid);
  1227. fp = fopen (fname, "r");
  1228. if (!fp) {
  1229. return NULL;
  1230. }
  1231. if (fgets (buf, sizeof (buf), fp) == NULL) {
  1232. fclose (fp);
  1233. return NULL;
  1234. }
  1235. fclose (fp);
  1236. name = strrchr (buf, '(');
  1237. if (!name) {
  1238. return NULL;
  1239. }
  1240. /* move past the bracket */
  1241. name++;
  1242. rest = strrchr (buf, ')');
  1243. if (rest == NULL || rest[1] != ' ') {
  1244. return NULL;
  1245. }
  1246. *rest = '\0';
  1247. /* move past the NULL and space */
  1248. rest += 2;
  1249. /* copy the name */
  1250. strncpy (out_name, name, name_len);
  1251. out_name[name_len] = '\0';
  1252. return out_name;
  1253. }
  1254. static void coroipcs_init_conn_stats (
  1255. struct conn_info *conn)
  1256. {
  1257. char conn_name[42];
  1258. char proc_name[32];
  1259. if (conn->client_pid > 0) {
  1260. if (pid_to_name (conn->client_pid, proc_name, sizeof(proc_name)))
  1261. snprintf (conn_name, sizeof(conn_name), "%s:%d:%d", proc_name, conn->client_pid, conn->fd);
  1262. else
  1263. snprintf (conn_name, sizeof(conn_name), "%d:%d", conn->client_pid, conn->fd);
  1264. } else
  1265. snprintf (conn_name, sizeof(conn_name), "%d", conn->fd);
  1266. conn->stats_handle = stats_api->stats_create_connection (conn_name, conn->client_pid, conn->fd);
  1267. stats_api->stats_update_value (conn->stats_handle, "service_id",
  1268. &conn->service, sizeof(conn->service));
  1269. }
  1270. int coroipcs_handler_dispatch (
  1271. int fd,
  1272. int revent,
  1273. void *context)
  1274. {
  1275. mar_req_setup_t *req_setup;
  1276. struct conn_info *conn_info = (struct conn_info *)context;
  1277. int res;
  1278. char buf;
  1279. if (ipc_thread_exiting (conn_info)) {
  1280. return conn_info_destroy (conn_info);
  1281. }
  1282. /*
  1283. * If an error occurs, request exit
  1284. */
  1285. if (revent & (POLLERR|POLLHUP)) {
  1286. ipc_disconnect (conn_info);
  1287. return (0);
  1288. }
  1289. /*
  1290. * Read the header and process it
  1291. */
  1292. if (conn_info->service == SOCKET_SERVICE_INIT && (revent & POLLIN)) {
  1293. /*
  1294. * Receive in a nonblocking fashion the request
  1295. * IF security invalid, send ERR_SECURITY, otherwise
  1296. * send OK
  1297. */
  1298. res = req_setup_recv (conn_info);
  1299. if (res == -1) {
  1300. req_setup_send (conn_info, CS_ERR_SECURITY);
  1301. }
  1302. if (res != 1) {
  1303. return (0);
  1304. }
  1305. pthread_mutex_init (&conn_info->mutex, NULL);
  1306. req_setup = (mar_req_setup_t *)conn_info->setup_msg;
  1307. /*
  1308. * Is the service registered ?
  1309. */
  1310. if (api->service_available (req_setup->service) == 0) {
  1311. req_setup_send (conn_info, CS_ERR_NOT_EXIST);
  1312. ipc_disconnect (conn_info);
  1313. return (0);
  1314. }
  1315. req_setup_send (conn_info, CS_OK);
  1316. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1317. conn_info->semkey = req_setup->semkey;
  1318. #endif
  1319. res = memory_map (
  1320. req_setup->control_file,
  1321. req_setup->control_size,
  1322. (void *)&conn_info->control_buffer);
  1323. conn_info->control_size = req_setup->control_size;
  1324. res = memory_map (
  1325. req_setup->request_file,
  1326. req_setup->request_size,
  1327. (void *)&conn_info->request_buffer);
  1328. conn_info->request_size = req_setup->request_size;
  1329. res = memory_map (
  1330. req_setup->response_file,
  1331. req_setup->response_size,
  1332. (void *)&conn_info->response_buffer);
  1333. conn_info->response_size = req_setup->response_size;
  1334. res = circular_memory_map (
  1335. req_setup->dispatch_file,
  1336. req_setup->dispatch_size,
  1337. (void *)&conn_info->dispatch_buffer);
  1338. conn_info->dispatch_size = req_setup->dispatch_size;
  1339. conn_info->service = req_setup->service;
  1340. conn_info->refcount = 0;
  1341. conn_info->notify_flow_control_enabled = 0;
  1342. conn_info->setup_bytes_read = 0;
  1343. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1344. conn_info->semid = semget (conn_info->semkey, 3, 0600);
  1345. #endif
  1346. conn_info->pending_semops = 0;
  1347. /*
  1348. * ipc thread is the only reference at startup
  1349. */
  1350. conn_info->refcount = 1;
  1351. conn_info->state = CONN_STATE_THREAD_ACTIVE;
  1352. conn_info->private_data = api->malloc (api->private_data_size_get (conn_info->service));
  1353. memset (conn_info->private_data, 0,
  1354. api->private_data_size_get (conn_info->service));
  1355. api->init_fn_get (conn_info->service) (conn_info);
  1356. /* create stats objects */
  1357. coroipcs_init_conn_stats (conn_info);
  1358. pthread_attr_init (&conn_info->thread_attr);
  1359. /*
  1360. * IA64 needs more stack space then other arches
  1361. */
  1362. #if defined(__ia64__)
  1363. pthread_attr_setstacksize (&conn_info->thread_attr, 400000);
  1364. #else
  1365. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  1366. #endif
  1367. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_JOINABLE);
  1368. res = pthread_create (&conn_info->thread,
  1369. &conn_info->thread_attr,
  1370. pthread_ipc_consumer,
  1371. conn_info);
  1372. /*
  1373. * Security check - disallow multiple configurations of
  1374. * the ipc connection
  1375. */
  1376. if (conn_info->service == SOCKET_SERVICE_INIT) {
  1377. conn_info->service = -1;
  1378. }
  1379. } else
  1380. if (revent & POLLIN) {
  1381. coroipcs_refcount_inc (conn_info);
  1382. res = recv (fd, &buf, 1, MSG_NOSIGNAL);
  1383. if (res == 1) {
  1384. switch (buf) {
  1385. case MESSAGE_REQ_OUTQ_FLUSH:
  1386. outq_flush (conn_info);
  1387. break;
  1388. case MESSAGE_REQ_CHANGE_EUID:
  1389. if (priv_change (conn_info) == -1) {
  1390. ipc_disconnect (conn_info);
  1391. }
  1392. break;
  1393. default:
  1394. res = 0;
  1395. break;
  1396. }
  1397. }
  1398. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1399. /* On many OS poll never return POLLHUP or POLLERR.
  1400. * EOF is detected when recvmsg return 0.
  1401. */
  1402. if (res == 0) {
  1403. ipc_disconnect (conn_info);
  1404. coroipcs_refcount_dec (conn_info);
  1405. return (0);
  1406. }
  1407. #endif
  1408. coroipcs_refcount_dec (conn_info);
  1409. }
  1410. coroipcs_refcount_inc (conn_info);
  1411. pthread_mutex_lock (&conn_info->mutex);
  1412. if ((conn_info->state == CONN_STATE_THREAD_ACTIVE) && (revent & POLLOUT)) {
  1413. buf = !list_empty (&conn_info->outq_head);
  1414. for (; conn_info->pending_semops;) {
  1415. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1416. if (res == 1) {
  1417. conn_info->pending_semops--;
  1418. } else {
  1419. break;
  1420. }
  1421. }
  1422. if (conn_info->notify_flow_control_enabled) {
  1423. buf = 2;
  1424. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1425. if (res == 1) {
  1426. conn_info->notify_flow_control_enabled = 0;
  1427. }
  1428. }
  1429. if (conn_info->notify_flow_control_enabled == 0 &&
  1430. conn_info->pending_semops == 0) {
  1431. api->poll_dispatch_modify (conn_info->fd,
  1432. POLLIN|POLLNVAL);
  1433. }
  1434. }
  1435. pthread_mutex_unlock (&conn_info->mutex);
  1436. coroipcs_refcount_dec (conn_info);
  1437. return (0);
  1438. }