coroipcs.c 43 KB

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