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