coroipcs.c 39 KB

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