coroipcs.c 41 KB

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