coroipcs.c 42 KB

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