ipc.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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/saAis.h>
  65. #include <corosync/list.h>
  66. #include <corosync/queue.h>
  67. #include <corosync/lcr/lcr_ifact.h>
  68. #include <corosync/totem/coropoll.h>
  69. #include <corosync/totem/totempg.h>
  70. #include <corosync/engine/objdb.h>
  71. #include <corosync/engine/config.h>
  72. #include <corosync/engine/logsys.h>
  73. #include "poll.h"
  74. #include "totemsrp.h"
  75. #include "mempool.h"
  76. #include "mainconfig.h"
  77. #include "totemconfig.h"
  78. #include "main.h"
  79. #include "flow.h"
  80. #include "tlist.h"
  81. #include "ipc.h"
  82. #include "flow.h"
  83. #include "sync.h"
  84. #include <corosync/engine/coroapi.h>
  85. #include "service.h"
  86. #include "util.h"
  87. LOGSYS_DECLARE_SUBSYS ("IPC", LOG_INFO);
  88. #ifdef 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 corosync_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. SaAisErrorT error = SA_AIS_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 = SA_AIS_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. corosync_conn_send_response (
  185. conn_info,
  186. &res_lib_response_init,
  187. sizeof (res_lib_response_init));
  188. if (error == SA_AIS_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. SaAisErrorT error = SA_AIS_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 = SA_AIS_ERR_NOT_SUPPORTED;
  207. else
  208. error = SA_AIS_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 == SA_AIS_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 = SA_AIS_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. corosync_conn_send_response (
  241. conn_info,
  242. &res_lib_dispatch_init,
  243. sizeof (res_lib_dispatch_init));
  244. if (error == SA_AIS_ERR_ACCESS) {
  245. libais_disconnect_security (conn_info);
  246. return (-1);
  247. }
  248. if (error != SA_AIS_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 == COROSYNC_LIB_FLOW_CONTROL_REQUIRED) {
  258. corosync_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. corosync_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 == COROSYNC_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. corosync_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. corosync_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) && (
  782. (ais_service[service]->lib_engine[header->id].flow_control == COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  783. ((ais_service[service]->lib_engine[header->id].flow_control == COROSYNC_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 = SA_AIS_ERR_TRY_AGAIN;
  797. corosync_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 corosync_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 *corosync_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 *corosync_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 corosync_conn_send_response_no_fcc (
  978. void *conn,
  979. void *msg,
  980. int mlen)
  981. {
  982. dont_call_flow_control = 1;
  983. corosync_conn_send_response (
  984. conn, msg, mlen);
  985. dont_call_flow_control = 0;
  986. }
  987. int corosync_conn_send_response (
  988. void *conn,
  989. void *msg,
  990. int mlen)
  991. {
  992. struct queue *outq;
  993. char *cmsg;
  994. int res = 0;
  995. int queue_empty;
  996. struct outq_item *queue_item;
  997. struct outq_item queue_item_out;
  998. struct msghdr msg_send;
  999. struct iovec iov_send;
  1000. char *msg_addr;
  1001. struct conn_info *conn_info = (struct conn_info *)conn;
  1002. if (conn_info == NULL) {
  1003. return -1;
  1004. }
  1005. if (!libais_connection_active (conn_info)) {
  1006. return (-1);
  1007. }
  1008. if (dont_call_flow_control == 0) {
  1009. ipc_flow_control (conn_info);
  1010. }
  1011. outq = &conn_info->outq;
  1012. msg_send.msg_iov = &iov_send;
  1013. msg_send.msg_name = 0;
  1014. msg_send.msg_namelen = 0;
  1015. msg_send.msg_iovlen = 1;
  1016. #ifndef COROSYNC_SOLARIS
  1017. msg_send.msg_control = 0;
  1018. msg_send.msg_controllen = 0;
  1019. msg_send.msg_flags = 0;
  1020. #else
  1021. msg_send.msg_accrights = 0;
  1022. msg_send.msg_accrightslen = 0;
  1023. #endif
  1024. if (queue_is_full (outq)) {
  1025. /*
  1026. * Start a disconnect if we have not already started one
  1027. * and report that the outgoing queue is full
  1028. */
  1029. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  1030. libais_disconnect_request (conn_info);
  1031. return (-1);
  1032. }
  1033. while (!queue_is_empty (outq)) {
  1034. queue_item = queue_item_get (outq);
  1035. msg_addr = (char *)queue_item->msg;
  1036. msg_addr = &msg_addr[conn_info->byte_start];
  1037. iov_send.iov_base = msg_addr;
  1038. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  1039. retry_sendmsg:
  1040. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  1041. if (res == -1 && errno == EINTR) {
  1042. goto retry_sendmsg;
  1043. }
  1044. if (res == -1 && errno == EAGAIN) {
  1045. break; /* outgoing kernel queue full */
  1046. }
  1047. if (res == -1 && errno == EPIPE) {
  1048. libais_disconnect_request (conn_info);
  1049. return (0);
  1050. }
  1051. if (res == -1) {
  1052. assert (0);
  1053. break; /* some other error, stop trying to send message */
  1054. }
  1055. if (res + conn_info->byte_start != queue_item->mlen) {
  1056. conn_info->byte_start += res;
  1057. break;
  1058. }
  1059. /*
  1060. * Message sent, try sending another message
  1061. */
  1062. queue_item_remove (outq);
  1063. conn_info->byte_start = 0;
  1064. free (queue_item->msg);
  1065. } /* while queue not empty */
  1066. res = -1;
  1067. queue_empty = queue_is_empty (outq);
  1068. /*
  1069. * Send request message
  1070. */
  1071. if (queue_empty) {
  1072. iov_send.iov_base = msg;
  1073. iov_send.iov_len = mlen;
  1074. retry_sendmsg_two:
  1075. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  1076. if (res == -1 && errno == EINTR) {
  1077. goto retry_sendmsg_two;
  1078. }
  1079. if (res == -1 && errno == EAGAIN) {
  1080. conn_info->byte_start = 0;
  1081. conn_info->events = POLLIN|POLLNVAL;
  1082. }
  1083. if (res != -1) {
  1084. if (res != mlen) {
  1085. conn_info->byte_start += res;
  1086. res = -1;
  1087. } else {
  1088. conn_info->byte_start = 0;
  1089. conn_info->events = POLLIN|POLLNVAL;
  1090. }
  1091. }
  1092. }
  1093. /*
  1094. * If res == -1 , errrno == EAGAIN which means kernel queue full
  1095. */
  1096. if (res == -1) {
  1097. cmsg = malloc (mlen);
  1098. if (cmsg == 0) {
  1099. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  1100. libais_disconnect_request (conn_info);
  1101. return (-1);
  1102. }
  1103. queue_item_out.msg = cmsg;
  1104. queue_item_out.mlen = mlen;
  1105. memcpy (cmsg, msg, mlen);
  1106. queue_item_add (outq, &queue_item_out);
  1107. /*
  1108. * Send a pthread_kill to interrupt the poll syscall
  1109. * and start a new poll operation in the thread
  1110. */
  1111. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  1112. pthread_kill (conn_info->thread, SIGUSR1);
  1113. }
  1114. return (0);
  1115. }
  1116. void corosync_ipc_flow_control_create (
  1117. void *conn,
  1118. unsigned int service,
  1119. char *id,
  1120. int id_len,
  1121. void (*flow_control_state_set_fn) (void *conn, enum corosync_flow_control_state),
  1122. void *context)
  1123. {
  1124. struct conn_info *conn_info = (struct conn_info *)conn;
  1125. corosync_flow_control_create (
  1126. conn_info->flow_control_handle,
  1127. service,
  1128. id,
  1129. id_len,
  1130. flow_control_state_set_fn,
  1131. context);
  1132. conn_info->conn_info_partner->flow_control_handle = conn_info->flow_control_handle;
  1133. }
  1134. void corosync_ipc_flow_control_destroy (
  1135. void *conn,
  1136. unsigned int service,
  1137. unsigned char *id,
  1138. int id_len)
  1139. {
  1140. struct conn_info *conn_info = (struct conn_info *)conn;
  1141. corosync_flow_control_destroy (
  1142. conn_info->flow_control_handle,
  1143. service,
  1144. id,
  1145. id_len);
  1146. }
  1147. void corosync_ipc_flow_control_local_increment (
  1148. void *conn)
  1149. {
  1150. struct conn_info *conn_info = (struct conn_info *)conn;
  1151. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1152. conn_info->flow_control_local_count++;
  1153. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1154. }
  1155. void corosync_ipc_flow_control_local_decrement (
  1156. void *conn)
  1157. {
  1158. struct conn_info *conn_info = (struct conn_info *)conn;
  1159. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1160. conn_info->flow_control_local_count--;
  1161. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1162. }