ipc.c 34 KB

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