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