ipc.c 34 KB

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