4
0

coroipcs.c 42 KB

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