coroipcs.c 43 KB

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