ipc.c 35 KB

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