ipc.c 34 KB

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