ipc.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  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 <netinet/in.h>
  51. #include <arpa/inet.h>
  52. #include <unistd.h>
  53. #include <fcntl.h>
  54. #include <stdlib.h>
  55. #include <stdio.h>
  56. #include <errno.h>
  57. #include <signal.h>
  58. #include <sched.h>
  59. #include <time.h>
  60. #if defined(COROSYNC_SOLARIS) && defined(HAVE_GETPEERUCRED)
  61. #include <ucred.h>
  62. #endif
  63. #include <corosync/swab.h>
  64. #include <corosync/saAis.h>
  65. #include <corosync/list.h>
  66. #include <corosync/queue.h>
  67. #include <corosync/lcr/lcr_ifact.h>
  68. #include <corosync/totem/coropoll.h>
  69. #include <corosync/totem/totempg.h>
  70. #include <corosync/engine/objdb.h>
  71. #include <corosync/engine/config.h>
  72. #include <corosync/engine/logsys.h>
  73. #include "poll.h"
  74. #include "totemsrp.h"
  75. #include "mempool.h"
  76. #include "mainconfig.h"
  77. #include "totemconfig.h"
  78. #include "main.h"
  79. #include "flow.h"
  80. #include "tlist.h"
  81. #include "ipc.h"
  82. #include "flow.h"
  83. #include "sync.h"
  84. #include <corosync/engine/coroapi.h>
  85. #include "service.h"
  86. #include "util.h"
  87. LOGSYS_DECLARE_SUBSYS ("IPC", LOG_INFO);
  88. #ifdef COROSYNC_SOLARIS
  89. #define MSG_NOSIGNAL 0
  90. #endif
  91. #define SERVER_BACKLOG 5
  92. /*
  93. * When there are this many entries left in a queue, turn on flow control
  94. */
  95. #ifndef FLOW_CONTROL_ENTRIES_ENABLE
  96. #define FLOW_CONTROL_ENTRIES_ENABLE 400
  97. #endif /* FLOW_CONTROL_ENTRIES_ENABLE */
  98. /*
  99. * When there are this many entries in a queue, turn off flow control
  100. */
  101. #define FLOW_CONTROL_ENTRIES_DISABLE 64
  102. static unsigned int g_gid_valid = 0;
  103. static totempg_groups_handle ipc_handle;
  104. DECLARE_LIST_INIT (conn_info_list_head);
  105. static void (*ipc_serialize_lock_fn) (void);
  106. static void (*ipc_serialize_unlock_fn) (void);
  107. struct outq_item {
  108. void *msg;
  109. size_t mlen;
  110. };
  111. enum conn_state {
  112. CONN_STATE_ACTIVE,
  113. CONN_STATE_SECURITY,
  114. CONN_STATE_REQUESTED,
  115. CONN_STATE_CLOSED,
  116. CONN_STATE_DISCONNECTED
  117. };
  118. struct conn_info {
  119. int fd; /* File descriptor */
  120. unsigned int events; /* events polled for by file descriptor */
  121. enum conn_state state; /* State of this connection */
  122. pthread_t thread; /* thread identifier */
  123. pthread_attr_t thread_attr; /* thread attribute */
  124. char *inb; /* Input buffer for non-blocking reads */
  125. int inb_nextheader; /* Next message header starts here */
  126. int inb_start; /* Start location of input buffer */
  127. int inb_inuse; /* Bytes currently stored in input buffer */
  128. struct queue outq; /* Circular queue for outgoing requests */
  129. int byte_start; /* Byte to start sending from in head of queue */
  130. enum service_types service;/* Type of service so dispatch knows how to route message */
  131. int authenticated; /* Is this connection authenticated? */
  132. void *private_data; /* library connection private data */
  133. struct conn_info *conn_info_partner; /* partner connection dispatch<->response */
  134. unsigned int flow_control_handle; /* flow control identifier */
  135. unsigned int flow_control_enabled; /* flow control enabled bit */
  136. unsigned int flow_control_local_count; /* flow control local count */
  137. enum corosync_lib_flow_control flow_control; /* Does this service use IPC flow control */
  138. pthread_mutex_t flow_control_mutex;
  139. int (*lib_exit_fn) (void *conn);
  140. struct timerlist timerlist;
  141. pthread_mutex_t mutex;
  142. pthread_mutex_t *shared_mutex;
  143. struct list_head list;
  144. };
  145. static void *prioritized_poll_thread (void *conn);
  146. static int conn_info_outq_flush (struct conn_info *conn_info);
  147. static void libais_deliver (struct conn_info *conn_info);
  148. static void ipc_flow_control (struct conn_info *conn_info);
  149. /*
  150. * IPC Initializers
  151. */
  152. static int response_init_send_response (
  153. struct conn_info *conn_info,
  154. void *message);
  155. static int dispatch_init_send_response (
  156. struct conn_info *conn_info,
  157. void *message);
  158. static int (*ais_init_service[]) (struct conn_info *conn_info, void *message) = {
  159. response_init_send_response,
  160. dispatch_init_send_response
  161. };
  162. static void libais_disconnect_security (struct conn_info *conn_info)
  163. {
  164. conn_info->state = CONN_STATE_SECURITY;
  165. close (conn_info->fd);
  166. }
  167. static int response_init_send_response (
  168. struct conn_info *conn_info,
  169. void *message)
  170. {
  171. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  172. uintptr_t cinfo = (uintptr_t)conn_info;
  173. mar_req_lib_response_init_t *req_lib_response_init = (mar_req_lib_response_init_t *)message;
  174. mar_res_lib_response_init_t res_lib_response_init;
  175. if (conn_info->authenticated) {
  176. conn_info->service = req_lib_response_init->resdis_header.service;
  177. error = SA_AIS_OK;
  178. }
  179. res_lib_response_init.header.size = sizeof (mar_res_lib_response_init_t);
  180. res_lib_response_init.header.id = MESSAGE_RES_INIT;
  181. res_lib_response_init.header.error = error;
  182. res_lib_response_init.conn_info = (mar_uint64_t)cinfo;
  183. corosync_conn_send_response (
  184. conn_info,
  185. &res_lib_response_init,
  186. sizeof (res_lib_response_init));
  187. if (error == SA_AIS_ERR_ACCESS) {
  188. libais_disconnect_security (conn_info);
  189. return (-1);
  190. }
  191. return (0);
  192. }
  193. static int dispatch_init_send_response (
  194. struct conn_info *conn_info,
  195. void *message)
  196. {
  197. SaAisErrorT error = SA_AIS_ERR_ACCESS;
  198. uintptr_t cinfo;
  199. mar_req_lib_dispatch_init_t *req_lib_dispatch_init = (mar_req_lib_dispatch_init_t *)message;
  200. mar_res_lib_dispatch_init_t res_lib_dispatch_init;
  201. struct conn_info *msg_conn_info;
  202. if (conn_info->authenticated) {
  203. conn_info->service = req_lib_dispatch_init->resdis_header.service;
  204. if (!ais_service[req_lib_dispatch_init->resdis_header.service])
  205. error = SA_AIS_ERR_NOT_SUPPORTED;
  206. else
  207. error = SA_AIS_OK;
  208. cinfo = (uintptr_t)req_lib_dispatch_init->conn_info;
  209. conn_info->conn_info_partner = (struct conn_info *)cinfo;
  210. /* temporary fix for memory leak
  211. */
  212. pthread_mutex_destroy (conn_info->conn_info_partner->shared_mutex);
  213. free (conn_info->conn_info_partner->shared_mutex);
  214. conn_info->conn_info_partner->shared_mutex = conn_info->shared_mutex;
  215. list_add (&conn_info_list_head, &conn_info->list);
  216. list_add (&conn_info_list_head, &conn_info->conn_info_partner->list);
  217. msg_conn_info = (struct conn_info *)cinfo;
  218. msg_conn_info->conn_info_partner = conn_info;
  219. if (error == SA_AIS_OK) {
  220. int private_data_size;
  221. private_data_size = ais_service[req_lib_dispatch_init->resdis_header.service]->private_data_size;
  222. if (private_data_size) {
  223. conn_info->private_data = malloc (private_data_size);
  224. conn_info->conn_info_partner->private_data = conn_info->private_data;
  225. if (conn_info->private_data == NULL) {
  226. error = SA_AIS_ERR_NO_MEMORY;
  227. } else {
  228. memset (conn_info->private_data, 0, private_data_size);
  229. }
  230. } else {
  231. conn_info->private_data = NULL;
  232. conn_info->conn_info_partner->private_data = NULL;
  233. }
  234. }
  235. }
  236. res_lib_dispatch_init.header.size = sizeof (mar_res_lib_dispatch_init_t);
  237. res_lib_dispatch_init.header.id = MESSAGE_RES_INIT;
  238. res_lib_dispatch_init.header.error = error;
  239. corosync_conn_send_response (
  240. conn_info,
  241. &res_lib_dispatch_init,
  242. sizeof (res_lib_dispatch_init));
  243. if (error == SA_AIS_ERR_ACCESS) {
  244. libais_disconnect_security (conn_info);
  245. return (-1);
  246. }
  247. if (error != SA_AIS_OK) {
  248. return (-1);
  249. }
  250. conn_info->state = CONN_STATE_ACTIVE;
  251. conn_info->conn_info_partner->state = CONN_STATE_ACTIVE;
  252. conn_info->lib_exit_fn = ais_service[conn_info->service]->lib_exit_fn;
  253. ais_service[conn_info->service]->lib_init_fn (conn_info);
  254. conn_info->flow_control = ais_service[conn_info->service]->flow_control;
  255. conn_info->conn_info_partner->flow_control = ais_service[conn_info->service]->flow_control;
  256. if (ais_service[conn_info->service]->flow_control == COROSYNC_LIB_FLOW_CONTROL_REQUIRED) {
  257. corosync_flow_control_ipc_init (
  258. &conn_info->flow_control_handle,
  259. conn_info->service);
  260. }
  261. return (0);
  262. }
  263. /*
  264. * Create a connection data structure
  265. */
  266. static inline unsigned int conn_info_create (int fd) {
  267. struct conn_info *conn_info;
  268. int res;
  269. conn_info = malloc (sizeof (struct conn_info));
  270. if (conn_info == 0) {
  271. return (ENOMEM);
  272. }
  273. memset (conn_info, 0, sizeof (struct conn_info));
  274. res = queue_init (&conn_info->outq, SIZEQUEUE,
  275. sizeof (struct outq_item));
  276. if (res != 0) {
  277. free (conn_info);
  278. return (ENOMEM);
  279. }
  280. conn_info->inb = malloc (sizeof (char) * SIZEINB);
  281. if (conn_info->inb == NULL) {
  282. queue_free (&conn_info->outq);
  283. free (conn_info);
  284. return (ENOMEM);
  285. }
  286. conn_info->shared_mutex = malloc (sizeof (pthread_mutex_t));
  287. if (conn_info->shared_mutex == NULL) {
  288. free (conn_info->inb);
  289. queue_free (&conn_info->outq);
  290. free (conn_info);
  291. return (ENOMEM);
  292. }
  293. pthread_mutex_init (&conn_info->mutex, NULL);
  294. pthread_mutex_init (&conn_info->flow_control_mutex, NULL);
  295. pthread_mutex_init (conn_info->shared_mutex, NULL);
  296. list_init (&conn_info->list);
  297. conn_info->state = CONN_STATE_ACTIVE;
  298. conn_info->fd = fd;
  299. conn_info->events = POLLIN|POLLNVAL;
  300. conn_info->service = SOCKET_SERVICE_INIT;
  301. pthread_attr_init (&conn_info->thread_attr);
  302. /*
  303. * IA64 needs more stack space then other arches
  304. */
  305. #if defined(__ia64__)
  306. pthread_attr_setstacksize (&conn_info->thread_attr, 400000);
  307. #else
  308. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  309. #endif
  310. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_DETACHED);
  311. res = pthread_create (&conn_info->thread, &conn_info->thread_attr,
  312. prioritized_poll_thread, conn_info);
  313. return (res);
  314. }
  315. static void conn_info_destroy (struct conn_info *conn_info)
  316. {
  317. struct outq_item *outq_item;
  318. /*
  319. * Free the outq queued items
  320. */
  321. while (!queue_is_empty (&conn_info->outq)) {
  322. outq_item = queue_item_get (&conn_info->outq);
  323. free (outq_item->msg);
  324. queue_item_remove (&conn_info->outq);
  325. }
  326. queue_free (&conn_info->outq);
  327. free (conn_info->inb);
  328. if (conn_info->conn_info_partner) {
  329. conn_info->conn_info_partner->conn_info_partner = NULL;
  330. }
  331. pthread_attr_destroy (&conn_info->thread_attr);
  332. pthread_mutex_destroy (&conn_info->mutex);
  333. pthread_mutex_destroy (&conn_info->flow_control_mutex);
  334. list_del (&conn_info->list);
  335. free (conn_info);
  336. }
  337. int libais_connection_active (struct conn_info *conn_info);
  338. int libais_connection_active (struct conn_info *conn_info)
  339. {
  340. return (conn_info->state == CONN_STATE_ACTIVE);
  341. }
  342. static void libais_disconnect_request (struct conn_info *conn_info)
  343. {
  344. if (conn_info->state == CONN_STATE_ACTIVE) {
  345. conn_info->state = CONN_STATE_REQUESTED;
  346. conn_info->conn_info_partner->state = CONN_STATE_REQUESTED;
  347. }
  348. }
  349. static int libais_disconnect (struct conn_info *conn_info)
  350. {
  351. int res = 0;
  352. assert (conn_info->state != CONN_STATE_ACTIVE);
  353. if (conn_info->state == CONN_STATE_DISCONNECTED) {
  354. assert (0);
  355. }
  356. /*
  357. * Close active connections
  358. */
  359. if (conn_info->state == CONN_STATE_ACTIVE || conn_info->state == CONN_STATE_REQUESTED) {
  360. close (conn_info->fd);
  361. conn_info->state = CONN_STATE_CLOSED;
  362. close (conn_info->conn_info_partner->fd);
  363. conn_info->conn_info_partner->state = CONN_STATE_CLOSED;
  364. }
  365. /*
  366. * Note we will only call the close operation once on the first time
  367. * one of the connections is closed
  368. */
  369. if (conn_info->state == CONN_STATE_CLOSED) {
  370. if (conn_info->lib_exit_fn) {
  371. res = conn_info->lib_exit_fn (conn_info);
  372. }
  373. if (res == -1) {
  374. return (-1);
  375. }
  376. if (conn_info->conn_info_partner->lib_exit_fn) {
  377. res = conn_info->conn_info_partner->lib_exit_fn (conn_info);
  378. }
  379. if (res == -1) {
  380. return (-1);
  381. }
  382. }
  383. conn_info->state = CONN_STATE_DISCONNECTED;
  384. conn_info->conn_info_partner->state = CONN_STATE_DISCONNECTED;
  385. if (conn_info->flow_control_enabled == 1) {
  386. corosync_flow_control_disable (conn_info->flow_control_handle);
  387. }
  388. return (0);
  389. }
  390. static inline void conn_info_mutex_lock (
  391. struct conn_info *conn_info,
  392. unsigned int service)
  393. {
  394. if (service == SOCKET_SERVICE_INIT) {
  395. pthread_mutex_lock (&conn_info->mutex);
  396. } else {
  397. pthread_mutex_lock (conn_info->shared_mutex);
  398. }
  399. }
  400. static inline void conn_info_mutex_unlock (
  401. struct conn_info *conn_info,
  402. unsigned int service)
  403. {
  404. if (service == SOCKET_SERVICE_INIT) {
  405. pthread_mutex_unlock (&conn_info->mutex);
  406. } else {
  407. pthread_mutex_unlock (conn_info->shared_mutex);
  408. }
  409. }
  410. /*
  411. * This thread runs in a specific thread priority mode to handle
  412. * I/O requests from the library
  413. */
  414. static void *prioritized_poll_thread (void *conn)
  415. {
  416. struct conn_info *conn_info = (struct conn_info *)conn;
  417. struct pollfd ufd;
  418. int fds;
  419. struct sched_param sched_param;
  420. int res;
  421. pthread_mutex_t *rel_mutex;
  422. unsigned int service;
  423. struct conn_info *cinfo_partner;
  424. void *private_data;
  425. sched_param.sched_priority = 1;
  426. res = pthread_setschedparam (conn_info->thread, SCHED_RR, &sched_param);
  427. ufd.fd = conn_info->fd;
  428. for (;;) {
  429. retry_poll:
  430. service = conn_info->service;
  431. ufd.events = conn_info->events;
  432. ufd.revents = 0;
  433. fds = poll (&ufd, 1, -1);
  434. conn_info_mutex_lock (conn_info, service);
  435. switch (conn_info->state) {
  436. case CONN_STATE_SECURITY:
  437. conn_info_mutex_unlock (conn_info, service);
  438. pthread_mutex_destroy (conn_info->shared_mutex);
  439. free (conn_info->shared_mutex);
  440. conn_info_destroy (conn);
  441. pthread_exit (0);
  442. break;
  443. case CONN_STATE_REQUESTED:
  444. case CONN_STATE_CLOSED:
  445. res = libais_disconnect (conn);
  446. if (res != 0) {
  447. conn_info_mutex_unlock (conn_info, service);
  448. goto retry_poll;
  449. }
  450. break;
  451. case CONN_STATE_DISCONNECTED:
  452. rel_mutex = conn_info->shared_mutex;
  453. private_data = conn_info->private_data;
  454. cinfo_partner = conn_info->conn_info_partner;
  455. conn_info_destroy (conn);
  456. if (service == SOCKET_SERVICE_INIT) {
  457. pthread_mutex_unlock (&conn_info->mutex);
  458. } else {
  459. pthread_mutex_unlock (rel_mutex);
  460. }
  461. if (cinfo_partner == NULL) {
  462. pthread_mutex_destroy (rel_mutex);
  463. free (rel_mutex);
  464. free (private_data);
  465. }
  466. pthread_exit (0);
  467. /*
  468. * !! NOTE !! this is the exit point for this thread
  469. */
  470. break;
  471. default:
  472. break;
  473. }
  474. if (fds == -1) {
  475. conn_info_mutex_unlock (conn_info, service);
  476. goto retry_poll;
  477. }
  478. ipc_serialize_lock_fn ();
  479. if (fds == 1 && ufd.revents) {
  480. if (ufd.revents & (POLLERR|POLLHUP)) {
  481. libais_disconnect_request (conn_info);
  482. conn_info_mutex_unlock (conn_info, service);
  483. ipc_serialize_unlock_fn ();
  484. continue;
  485. }
  486. if (ufd.revents & POLLOUT) {
  487. conn_info_outq_flush (conn_info);
  488. }
  489. if ((ufd.revents & POLLIN) == POLLIN) {
  490. libais_deliver (conn_info);
  491. }
  492. ipc_flow_control (conn_info);
  493. }
  494. ipc_serialize_unlock_fn ();
  495. conn_info_mutex_unlock (conn_info, service);
  496. }
  497. /*
  498. * This code never reached
  499. */
  500. return (0);
  501. }
  502. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  503. /* SUN_LEN is broken for abstract namespace
  504. */
  505. #define AIS_SUN_LEN(a) sizeof(*(a))
  506. #else
  507. #define AIS_SUN_LEN(a) SUN_LEN(a)
  508. #endif
  509. #if defined(COROSYNC_LINUX)
  510. char *socketname = "libcorosync.socket";
  511. #else
  512. char *socketname = "/var/run/libcorosync.socket";
  513. #endif
  514. static void ipc_flow_control (struct conn_info *conn_info)
  515. {
  516. unsigned int entries_used;
  517. unsigned int entries_usedhw;
  518. unsigned int flow_control_local_count;
  519. unsigned int fcc;
  520. /*
  521. * Determine FCC variable and printing variables
  522. */
  523. entries_used = queue_used (&conn_info->outq);
  524. if (conn_info->conn_info_partner &&
  525. queue_used (&conn_info->conn_info_partner->outq) > entries_used) {
  526. entries_used = queue_used (&conn_info->conn_info_partner->outq);
  527. }
  528. entries_usedhw = queue_usedhw (&conn_info->outq);
  529. if (conn_info->conn_info_partner &&
  530. queue_usedhw (&conn_info->conn_info_partner->outq) > entries_used) {
  531. entries_usedhw = queue_usedhw (&conn_info->conn_info_partner->outq);
  532. }
  533. flow_control_local_count = conn_info->flow_control_local_count;
  534. if (conn_info->conn_info_partner &&
  535. conn_info->conn_info_partner->flow_control_local_count > flow_control_local_count) {
  536. flow_control_local_count = conn_info->conn_info_partner->flow_control_local_count;
  537. }
  538. fcc = entries_used;
  539. if (flow_control_local_count > fcc) {
  540. fcc = flow_control_local_count;
  541. }
  542. /*
  543. * IPC group-wide flow control
  544. */
  545. if (conn_info->flow_control == COROSYNC_LIB_FLOW_CONTROL_REQUIRED) {
  546. if (conn_info->flow_control_enabled == 0 &&
  547. ((fcc + FLOW_CONTROL_ENTRIES_ENABLE) > SIZEQUEUE)) {
  548. log_printf (LOG_LEVEL_NOTICE, "Enabling flow control [%d/%d] - [%d].\n",
  549. entries_usedhw, SIZEQUEUE,
  550. flow_control_local_count);
  551. corosync_flow_control_enable (conn_info->flow_control_handle);
  552. conn_info->flow_control_enabled = 1;
  553. conn_info->conn_info_partner->flow_control_enabled = 1;
  554. }
  555. if (conn_info->flow_control_enabled == 1 &&
  556. fcc <= FLOW_CONTROL_ENTRIES_DISABLE) {
  557. log_printf (LOG_LEVEL_NOTICE, "Disabling flow control [%d/%d] - [%d].\n",
  558. entries_usedhw, SIZEQUEUE,
  559. flow_control_local_count);
  560. corosync_flow_control_disable (conn_info->flow_control_handle);
  561. conn_info->flow_control_enabled = 0;
  562. conn_info->conn_info_partner->flow_control_enabled = 0;
  563. }
  564. }
  565. }
  566. static int conn_info_outq_flush (struct conn_info *conn_info) {
  567. struct queue *outq;
  568. int res = 0;
  569. struct outq_item *queue_item;
  570. struct msghdr msg_send;
  571. struct iovec iov_send;
  572. char *msg_addr;
  573. if (!libais_connection_active (conn_info)) {
  574. return (-1);
  575. }
  576. outq = &conn_info->outq;
  577. msg_send.msg_iov = &iov_send;
  578. msg_send.msg_name = 0;
  579. msg_send.msg_namelen = 0;
  580. msg_send.msg_iovlen = 1;
  581. #ifndef COROSYNC_SOLARIS
  582. msg_send.msg_control = 0;
  583. msg_send.msg_controllen = 0;
  584. msg_send.msg_flags = 0;
  585. #else
  586. msg_send.msg_accrights = 0;
  587. msg_send.msg_accrightslen = 0;
  588. #endif
  589. while (!queue_is_empty (outq)) {
  590. queue_item = queue_item_get (outq);
  591. msg_addr = (char *)queue_item->msg;
  592. msg_addr = &msg_addr[conn_info->byte_start];
  593. iov_send.iov_base = msg_addr;
  594. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  595. retry_sendmsg:
  596. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  597. if (res == -1 && errno == EINTR) {
  598. goto retry_sendmsg;
  599. }
  600. if (res == -1 && errno == EAGAIN) {
  601. return (0);
  602. }
  603. if (res == -1 && errno == EPIPE) {
  604. libais_disconnect_request (conn_info);
  605. return (0);
  606. }
  607. if (res == -1) {
  608. printf ("ERRNO is %d\n", errno);
  609. assert (0); /* some other unhandled error here */
  610. }
  611. if (res + conn_info->byte_start != queue_item->mlen) {
  612. conn_info->byte_start += res;
  613. return (0);
  614. }
  615. /*
  616. * Message sent, try sending another message
  617. */
  618. queue_item_remove (outq);
  619. conn_info->byte_start = 0;
  620. free (queue_item->msg);
  621. } /* while queue not empty */
  622. if (queue_is_empty (outq)) {
  623. conn_info->events = POLLIN|POLLNVAL;
  624. }
  625. return (0);
  626. }
  627. struct res_overlay {
  628. mar_res_header_t header __attribute((aligned(8)));
  629. char buf[4096];
  630. };
  631. static void libais_deliver (struct conn_info *conn_info)
  632. {
  633. int res;
  634. mar_req_header_t *header;
  635. int service;
  636. struct msghdr msg_recv;
  637. struct iovec iov_recv;
  638. #ifdef COROSYNC_LINUX
  639. struct cmsghdr *cmsg;
  640. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  641. struct ucred *cred;
  642. int on = 0;
  643. #endif
  644. int send_ok = 0;
  645. int send_ok_joined = 0;
  646. struct iovec send_ok_joined_iovec;
  647. struct res_overlay res_overlay;
  648. msg_recv.msg_iov = &iov_recv;
  649. msg_recv.msg_iovlen = 1;
  650. msg_recv.msg_name = 0;
  651. msg_recv.msg_namelen = 0;
  652. #ifndef COROSYNC_SOLARIS
  653. msg_recv.msg_flags = 0;
  654. if (conn_info->authenticated) {
  655. msg_recv.msg_control = 0;
  656. msg_recv.msg_controllen = 0;
  657. } else {
  658. #ifdef COROSYNC_LINUX
  659. msg_recv.msg_control = (void *)cmsg_cred;
  660. msg_recv.msg_controllen = sizeof (cmsg_cred);
  661. #else
  662. uid_t euid = -1;
  663. gid_t egid = -1;
  664. if (getpeereid(conn_info->fd, &euid, &egid) != -1 &&
  665. (euid == 0 || egid == g_gid_valid)) {
  666. conn_info->authenticated = 1;
  667. }
  668. if (conn_info->authenticated == 0) {
  669. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", egid, g_gid_valid);
  670. }
  671. #endif
  672. }
  673. #else /* COROSYNC_SOLARIS */
  674. msg_recv.msg_accrights = 0;
  675. msg_recv.msg_accrightslen = 0;
  676. if (! conn_info->authenticated) {
  677. #ifdef HAVE_GETPEERUCRED
  678. ucred_t *uc;
  679. uid_t euid = -1;
  680. gid_t egid = -1;
  681. if (getpeerucred(conn_info->fd, &uc) == 0) {
  682. euid = ucred_geteuid(uc);
  683. egid = ucred_getegid(uc);
  684. if ((euid == 0) || (egid == g_gid_valid)) {
  685. conn_info->authenticated = 1;
  686. }
  687. ucred_free(uc);
  688. }
  689. if (conn_info->authenticated == 0) {
  690. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", (int)egid, g_gid_valid);
  691. }
  692. #else
  693. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated "
  694. "because platform does not support "
  695. "authentication with sockets, continuing "
  696. "with a fake authentication\n");
  697. conn_info->authenticated = 1;
  698. #endif
  699. }
  700. #endif
  701. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  702. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  703. if (conn_info->inb_inuse == SIZEINB) {
  704. return;
  705. }
  706. retry_recv:
  707. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  708. if (res == -1 && errno == EINTR) {
  709. goto retry_recv;
  710. } else
  711. if (res == -1 && errno != EAGAIN) {
  712. return;
  713. } else
  714. if (res == 0) {
  715. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  716. /* On many OS poll never return POLLHUP or POLLERR.
  717. * EOF is detected when recvmsg return 0.
  718. */
  719. libais_disconnect_request (conn_info);
  720. #endif
  721. return;
  722. }
  723. /*
  724. * Authenticate if this connection has not been authenticated
  725. */
  726. #ifdef COROSYNC_LINUX
  727. if (conn_info->authenticated == 0) {
  728. cmsg = CMSG_FIRSTHDR (&msg_recv);
  729. assert (cmsg);
  730. cred = (struct ucred *)CMSG_DATA (cmsg);
  731. if (cred) {
  732. if (cred->uid == 0 || cred->gid == g_gid_valid) {
  733. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  734. conn_info->authenticated = 1;
  735. }
  736. }
  737. if (conn_info->authenticated == 0) {
  738. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, g_gid_valid);
  739. }
  740. }
  741. #endif
  742. /*
  743. * Dispatch all messages received in recvmsg that can be dispatched
  744. * sizeof (mar_req_header_t) needed at minimum to do any processing
  745. */
  746. conn_info->inb_inuse += res;
  747. conn_info->inb_start += res;
  748. while (conn_info->inb_inuse >= sizeof (mar_req_header_t) && res != -1) {
  749. header = (mar_req_header_t *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  750. if (header->size > conn_info->inb_inuse) {
  751. break;
  752. }
  753. service = conn_info->service;
  754. /*
  755. * If this service is in init phase, initialize service
  756. * else handle message using service service
  757. */
  758. if (service == SOCKET_SERVICE_INIT) {
  759. res = ais_init_service[header->id] (conn_info, header);
  760. } else {
  761. /*
  762. * Not an init service, but a standard service
  763. */
  764. if (header->id < 0 || header->id > ais_service[service]->lib_engine_count) {
  765. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  766. header->id, ais_service[service]->lib_engine_count);
  767. return ;
  768. }
  769. /*
  770. * If flow control is required of the library handle, determine that
  771. * corosync is not in synchronization and that totempg has room available
  772. * to queue a message, otherwise tell the library we are busy and to
  773. * try again later
  774. */
  775. send_ok_joined_iovec.iov_base = (char *)header;
  776. send_ok_joined_iovec.iov_len = header->size;
  777. send_ok_joined = totempg_groups_send_ok_joined (corosync_group_handle,
  778. &send_ok_joined_iovec, 1);
  779. send_ok =
  780. (sync_primary_designated() == 1) && (
  781. (ais_service[service]->lib_engine[header->id].flow_control == COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  782. ((ais_service[service]->lib_engine[header->id].flow_control == COROSYNC_LIB_FLOW_CONTROL_REQUIRED) &&
  783. (send_ok_joined) &&
  784. (sync_in_process() == 0)));
  785. if (send_ok) {
  786. ais_service[service]->lib_engine[header->id].lib_handler_fn(conn_info, header);
  787. } else {
  788. /*
  789. * Overload, tell library to retry
  790. */
  791. res_overlay.header.size =
  792. ais_service[service]->lib_engine[header->id].response_size;
  793. res_overlay.header.id =
  794. ais_service[service]->lib_engine[header->id].response_id;
  795. res_overlay.header.error = SA_AIS_ERR_TRY_AGAIN;
  796. corosync_conn_send_response (
  797. conn_info,
  798. &res_overlay,
  799. res_overlay.header.size);
  800. }
  801. }
  802. conn_info->inb_inuse -= header->size;
  803. } /* while */
  804. if (conn_info->inb_inuse == 0) {
  805. conn_info->inb_start = 0;
  806. } else
  807. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  808. if (conn_info->inb_start >= SIZEINB) {
  809. /*
  810. * If in buffer is full, move it back to start
  811. */
  812. memmove (conn_info->inb,
  813. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  814. sizeof (char) * conn_info->inb_inuse);
  815. conn_info->inb_start = conn_info->inb_inuse;
  816. }
  817. return;
  818. }
  819. static int poll_handler_libais_accept (
  820. poll_handle handle,
  821. int fd,
  822. int revent,
  823. void *data)
  824. {
  825. socklen_t addrlen;
  826. struct sockaddr_un un_addr;
  827. int new_fd;
  828. #ifdef COROSYNC_LINUX
  829. int on = 1;
  830. #endif
  831. int res;
  832. addrlen = sizeof (struct sockaddr_un);
  833. retry_accept:
  834. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  835. if (new_fd == -1 && errno == EINTR) {
  836. goto retry_accept;
  837. }
  838. if (new_fd == -1) {
  839. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  840. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  841. }
  842. totemip_nosigpipe(new_fd);
  843. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  844. if (res == -1) {
  845. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  846. close (new_fd);
  847. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  848. }
  849. /*
  850. * Valid accept
  851. */
  852. /*
  853. * Request credentials of sender provided by kernel
  854. */
  855. #ifdef COROSYNC_LINUX
  856. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  857. #endif
  858. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  859. res = conn_info_create (new_fd);
  860. if (res != 0) {
  861. close (new_fd);
  862. }
  863. return (0);
  864. }
  865. /*
  866. * Exported functions
  867. */
  868. int message_source_is_local(mar_message_source_t *source)
  869. {
  870. int ret = 0;
  871. assert (source != NULL);
  872. if (source->nodeid == totempg_my_nodeid_get ()) {
  873. ret = 1;
  874. }
  875. return ret;
  876. }
  877. void message_source_set (
  878. mar_message_source_t *source,
  879. void *conn)
  880. {
  881. assert ((source != NULL) && (conn != NULL));
  882. memset (source, 0, sizeof (mar_message_source_t));
  883. source->nodeid = totempg_my_nodeid_get ();
  884. source->conn = conn;
  885. }
  886. static void ipc_confchg_fn (
  887. enum totem_configuration_type configuration_type,
  888. unsigned int *member_list, int member_list_entries,
  889. unsigned int *left_list, int left_list_entries,
  890. unsigned int *joined_list, int joined_list_entries,
  891. struct memb_ring_id *ring_id)
  892. {
  893. }
  894. void corosync_ipc_init (
  895. void (*serialize_lock_fn) (void),
  896. void (*serialize_unlock_fn) (void),
  897. unsigned int gid_valid)
  898. {
  899. int libais_server_fd;
  900. struct sockaddr_un un_addr;
  901. int res;
  902. ipc_serialize_lock_fn = serialize_lock_fn;
  903. ipc_serialize_unlock_fn = serialize_unlock_fn;
  904. /*
  905. * Create socket for libais clients, name socket, listen for connections
  906. */
  907. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  908. if (libais_server_fd == -1) {
  909. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  910. corosync_exit_error (AIS_DONE_LIBAIS_SOCKET);
  911. };
  912. totemip_nosigpipe(libais_server_fd);
  913. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  914. if (res == -1) {
  915. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  916. corosync_exit_error (AIS_DONE_LIBAIS_SOCKET);
  917. }
  918. #if !defined(COROSYNC_LINUX)
  919. unlink(socketname);
  920. #endif
  921. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  922. un_addr.sun_family = AF_UNIX;
  923. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  924. un_addr.sun_len = sizeof(struct sockaddr_un);
  925. #endif
  926. #if defined(COROSYNC_LINUX)
  927. strcpy (un_addr.sun_path + 1, socketname);
  928. #else
  929. strcpy (un_addr.sun_path, socketname);
  930. #endif
  931. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  932. if (res) {
  933. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  934. corosync_exit_error (AIS_DONE_LIBAIS_BIND);
  935. }
  936. listen (libais_server_fd, SERVER_BACKLOG);
  937. /*
  938. * Setup libais connection dispatch routine
  939. */
  940. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  941. POLLIN, 0, poll_handler_libais_accept);
  942. g_gid_valid = gid_valid;
  943. /*
  944. * Reset internal state of flow control when
  945. * configuration change occurs
  946. */
  947. res = totempg_groups_initialize (
  948. &ipc_handle,
  949. NULL,
  950. ipc_confchg_fn);
  951. }
  952. /*
  953. * Get the conn info private data
  954. */
  955. void *corosync_conn_private_data_get (void *conn)
  956. {
  957. struct conn_info *conn_info = (struct conn_info *)conn;
  958. if (conn != NULL) {
  959. return ((void *)conn_info->private_data);
  960. } else {
  961. return NULL;
  962. }
  963. }
  964. /*
  965. * Get the conn info partner connection
  966. */
  967. void *corosync_conn_partner_get (void *conn)
  968. {
  969. struct conn_info *conn_info = (struct conn_info *)conn;
  970. if (conn != NULL) {
  971. return ((void *)conn_info->conn_info_partner);
  972. } else {
  973. return NULL;
  974. }
  975. }
  976. int corosync_conn_send_response (
  977. void *conn,
  978. void *msg,
  979. int mlen)
  980. {
  981. struct queue *outq;
  982. char *cmsg;
  983. int res = 0;
  984. int queue_empty;
  985. struct outq_item *queue_item;
  986. struct outq_item queue_item_out;
  987. struct msghdr msg_send;
  988. struct iovec iov_send;
  989. char *msg_addr;
  990. struct conn_info *conn_info = (struct conn_info *)conn;
  991. if (conn_info == NULL) {
  992. return -1;
  993. }
  994. if (!libais_connection_active (conn_info)) {
  995. return (-1);
  996. }
  997. ipc_flow_control (conn_info);
  998. outq = &conn_info->outq;
  999. msg_send.msg_iov = &iov_send;
  1000. msg_send.msg_name = 0;
  1001. msg_send.msg_namelen = 0;
  1002. msg_send.msg_iovlen = 1;
  1003. #ifndef COROSYNC_SOLARIS
  1004. msg_send.msg_control = 0;
  1005. msg_send.msg_controllen = 0;
  1006. msg_send.msg_flags = 0;
  1007. #else
  1008. msg_send.msg_accrights = 0;
  1009. msg_send.msg_accrightslen = 0;
  1010. #endif
  1011. if (queue_is_full (outq)) {
  1012. /*
  1013. * Start a disconnect if we have not already started one
  1014. * and report that the outgoing queue is full
  1015. */
  1016. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  1017. libais_disconnect_request (conn_info);
  1018. return (-1);
  1019. }
  1020. while (!queue_is_empty (outq)) {
  1021. queue_item = queue_item_get (outq);
  1022. msg_addr = (char *)queue_item->msg;
  1023. msg_addr = &msg_addr[conn_info->byte_start];
  1024. iov_send.iov_base = msg_addr;
  1025. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  1026. retry_sendmsg:
  1027. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  1028. if (res == -1 && errno == EINTR) {
  1029. goto retry_sendmsg;
  1030. }
  1031. if (res == -1 && errno == EAGAIN) {
  1032. break; /* outgoing kernel queue full */
  1033. }
  1034. if (res == -1 && errno == EPIPE) {
  1035. libais_disconnect_request (conn_info);
  1036. return (0);
  1037. }
  1038. if (res == -1) {
  1039. assert (0);
  1040. break; /* some other error, stop trying to send message */
  1041. }
  1042. if (res + conn_info->byte_start != queue_item->mlen) {
  1043. conn_info->byte_start += res;
  1044. break;
  1045. }
  1046. /*
  1047. * Message sent, try sending another message
  1048. */
  1049. queue_item_remove (outq);
  1050. conn_info->byte_start = 0;
  1051. free (queue_item->msg);
  1052. } /* while queue not empty */
  1053. res = -1;
  1054. queue_empty = queue_is_empty (outq);
  1055. /*
  1056. * Send request message
  1057. */
  1058. if (queue_empty) {
  1059. iov_send.iov_base = msg;
  1060. iov_send.iov_len = mlen;
  1061. retry_sendmsg_two:
  1062. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  1063. if (res == -1 && errno == EINTR) {
  1064. goto retry_sendmsg_two;
  1065. }
  1066. if (res == -1 && errno == EAGAIN) {
  1067. conn_info->byte_start = 0;
  1068. conn_info->events = POLLIN|POLLNVAL;
  1069. }
  1070. if (res != -1) {
  1071. if (res != mlen) {
  1072. conn_info->byte_start += res;
  1073. res = -1;
  1074. } else {
  1075. conn_info->byte_start = 0;
  1076. conn_info->events = POLLIN|POLLNVAL;
  1077. }
  1078. }
  1079. }
  1080. /*
  1081. * If res == -1 , errrno == EAGAIN which means kernel queue full
  1082. */
  1083. if (res == -1) {
  1084. cmsg = malloc (mlen);
  1085. if (cmsg == 0) {
  1086. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  1087. libais_disconnect_request (conn_info);
  1088. return (-1);
  1089. }
  1090. queue_item_out.msg = cmsg;
  1091. queue_item_out.mlen = mlen;
  1092. memcpy (cmsg, msg, mlen);
  1093. queue_item_add (outq, &queue_item_out);
  1094. /*
  1095. * Send a pthread_kill to interrupt the poll syscall
  1096. * and start a new poll operation in the thread
  1097. */
  1098. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  1099. pthread_kill (conn_info->thread, SIGUSR1);
  1100. }
  1101. return (0);
  1102. }
  1103. void corosync_ipc_flow_control_create (
  1104. void *conn,
  1105. unsigned int service,
  1106. char *id,
  1107. int id_len,
  1108. void (*flow_control_state_set_fn) (void *conn, enum corosync_flow_control_state),
  1109. void *context)
  1110. {
  1111. struct conn_info *conn_info = (struct conn_info *)conn;
  1112. corosync_flow_control_create (
  1113. conn_info->flow_control_handle,
  1114. service,
  1115. id,
  1116. id_len,
  1117. flow_control_state_set_fn,
  1118. context);
  1119. conn_info->conn_info_partner->flow_control_handle = conn_info->flow_control_handle;
  1120. }
  1121. void corosync_ipc_flow_control_destroy (
  1122. void *conn,
  1123. unsigned int service,
  1124. unsigned char *id,
  1125. int id_len)
  1126. {
  1127. struct conn_info *conn_info = (struct conn_info *)conn;
  1128. corosync_flow_control_destroy (
  1129. conn_info->flow_control_handle,
  1130. service,
  1131. id,
  1132. id_len);
  1133. }
  1134. void corosync_ipc_flow_control_local_increment (
  1135. void *conn)
  1136. {
  1137. struct conn_info *conn_info = (struct conn_info *)conn;
  1138. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1139. conn_info->flow_control_local_count++;
  1140. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1141. }
  1142. void corosync_ipc_flow_control_local_decrement (
  1143. void *conn)
  1144. {
  1145. struct conn_info *conn_info = (struct conn_info *)conn;
  1146. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1147. conn_info->flow_control_local_count--;
  1148. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1149. }