coroipcs.c 45 KB

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