coroipcs.c 43 KB

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