ipc.c 35 KB

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