ipc.c 35 KB

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