ipc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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. pthread_mutex_t *rel2_mutex;
  425. unsigned int service;
  426. struct conn_info *cinfo_partner;
  427. void *private_data;
  428. sched_param.sched_priority = 1;
  429. res = pthread_setschedparam (conn_info->thread, SCHED_RR, &sched_param);
  430. ufd.fd = conn_info->fd;
  431. for (;;) {
  432. retry_poll:
  433. service = conn_info->service;
  434. ufd.events = conn_info->events;
  435. ufd.revents = 0;
  436. fds = poll (&ufd, 1, -1);
  437. conn_info_mutex_lock (conn_info, service);
  438. switch (conn_info->state) {
  439. case CONN_STATE_SECURITY:
  440. conn_info_mutex_unlock (conn_info, service);
  441. pthread_mutex_destroy (conn_info->shared_mutex);
  442. free (conn_info->shared_mutex);
  443. conn_info_destroy (conn);
  444. pthread_exit (0);
  445. break;
  446. case CONN_STATE_REQUESTED:
  447. case CONN_STATE_CLOSED:
  448. res = libais_disconnect (conn);
  449. if (res != 0) {
  450. conn_info_mutex_unlock (conn_info, service);
  451. goto retry_poll;
  452. }
  453. break;
  454. case CONN_STATE_DISCONNECTED:
  455. rel_mutex = conn_info->shared_mutex;
  456. rel2_mutex = &conn_info->mutex;
  457. private_data = conn_info->private_data;
  458. cinfo_partner = conn_info->conn_info_partner;
  459. conn_info_destroy (conn);
  460. if (service == SOCKET_SERVICE_INIT) {
  461. pthread_mutex_unlock (rel2_mutex);
  462. } else {
  463. pthread_mutex_unlock (rel_mutex);
  464. }
  465. if (cinfo_partner == NULL) {
  466. pthread_mutex_destroy (rel_mutex);
  467. free (rel_mutex);
  468. free (private_data);
  469. }
  470. pthread_exit (0);
  471. /*
  472. * !! NOTE !! this is the exit point for this thread
  473. */
  474. break;
  475. default:
  476. break;
  477. }
  478. if (fds == -1) {
  479. conn_info_mutex_unlock (conn_info, service);
  480. goto retry_poll;
  481. }
  482. ipc_serialize_lock_fn ();
  483. if (fds == 1 && ufd.revents) {
  484. if (ufd.revents & (POLLERR|POLLHUP)) {
  485. libais_disconnect_request (conn_info);
  486. conn_info_mutex_unlock (conn_info, service);
  487. ipc_serialize_unlock_fn ();
  488. continue;
  489. }
  490. if (ufd.revents & POLLOUT) {
  491. conn_info_outq_flush (conn_info);
  492. }
  493. if ((ufd.revents & POLLIN) == POLLIN) {
  494. libais_deliver (conn_info);
  495. }
  496. ipc_flow_control (conn_info);
  497. }
  498. ipc_serialize_unlock_fn ();
  499. conn_info_mutex_unlock (conn_info, service);
  500. }
  501. /*
  502. * This code never reached
  503. */
  504. return (0);
  505. }
  506. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  507. /* SUN_LEN is broken for abstract namespace
  508. */
  509. #define AIS_SUN_LEN(a) sizeof(*(a))
  510. #else
  511. #define AIS_SUN_LEN(a) SUN_LEN(a)
  512. #endif
  513. #if defined(COROSYNC_LINUX)
  514. char *socketname = "libcorosync.socket";
  515. #else
  516. char *socketname = "/var/run/libcorosync.socket";
  517. #endif
  518. static void ipc_flow_control (struct conn_info *conn_info)
  519. {
  520. unsigned int entries_used;
  521. unsigned int entries_usedhw;
  522. unsigned int flow_control_local_count;
  523. unsigned int fcc;
  524. /*
  525. * Determine FCC variable and printing variables
  526. */
  527. entries_used = queue_used (&conn_info->outq);
  528. if (conn_info->conn_info_partner &&
  529. queue_used (&conn_info->conn_info_partner->outq) > entries_used) {
  530. entries_used = queue_used (&conn_info->conn_info_partner->outq);
  531. }
  532. entries_usedhw = queue_usedhw (&conn_info->outq);
  533. if (conn_info->conn_info_partner &&
  534. queue_usedhw (&conn_info->conn_info_partner->outq) > entries_used) {
  535. entries_usedhw = queue_usedhw (&conn_info->conn_info_partner->outq);
  536. }
  537. flow_control_local_count = conn_info->flow_control_local_count;
  538. if (conn_info->conn_info_partner &&
  539. conn_info->conn_info_partner->flow_control_local_count > flow_control_local_count) {
  540. flow_control_local_count = conn_info->conn_info_partner->flow_control_local_count;
  541. }
  542. fcc = entries_used;
  543. if (flow_control_local_count > fcc) {
  544. fcc = flow_control_local_count;
  545. }
  546. /*
  547. * IPC group-wide flow control
  548. */
  549. if (conn_info->flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) {
  550. if (conn_info->flow_control_enabled == 0 &&
  551. ((fcc + FLOW_CONTROL_ENTRIES_ENABLE) > SIZEQUEUE)) {
  552. log_printf (LOG_LEVEL_NOTICE, "Enabling flow control [%d/%d] - [%d].\n",
  553. entries_usedhw, SIZEQUEUE,
  554. flow_control_local_count);
  555. cs_flow_control_enable (conn_info->flow_control_handle);
  556. conn_info->flow_control_enabled = 1;
  557. if (conn_info->conn_info_partner) {
  558. conn_info->conn_info_partner->flow_control_enabled = 1;
  559. }
  560. }
  561. if (conn_info->flow_control_enabled == 1 &&
  562. fcc <= FLOW_CONTROL_ENTRIES_DISABLE) {
  563. log_printf (LOG_LEVEL_NOTICE, "Disabling flow control [%d/%d] - [%d].\n",
  564. entries_usedhw, SIZEQUEUE,
  565. flow_control_local_count);
  566. cs_flow_control_disable (conn_info->flow_control_handle);
  567. conn_info->flow_control_enabled = 0;
  568. if (conn_info->conn_info_partner) {
  569. conn_info->conn_info_partner->flow_control_enabled = 0;
  570. }
  571. }
  572. }
  573. }
  574. static int conn_info_outq_flush (struct conn_info *conn_info) {
  575. struct queue *outq;
  576. ssize_t res = 0;
  577. struct outq_item *queue_item;
  578. struct msghdr msg_send;
  579. struct iovec iov_send;
  580. char *msg_addr;
  581. if (!libais_connection_active (conn_info)) {
  582. return (-1);
  583. }
  584. outq = &conn_info->outq;
  585. msg_send.msg_iov = &iov_send;
  586. msg_send.msg_name = 0;
  587. msg_send.msg_namelen = 0;
  588. msg_send.msg_iovlen = 1;
  589. #ifndef COROSYNC_SOLARIS
  590. msg_send.msg_control = 0;
  591. msg_send.msg_controllen = 0;
  592. msg_send.msg_flags = 0;
  593. #else
  594. msg_send.msg_accrights = 0;
  595. msg_send.msg_accrightslen = 0;
  596. #endif
  597. while (!queue_is_empty (outq)) {
  598. queue_item = queue_item_get (outq);
  599. msg_addr = (char *)queue_item->msg;
  600. msg_addr = &msg_addr[conn_info->byte_start];
  601. iov_send.iov_base = msg_addr;
  602. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  603. retry_sendmsg:
  604. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  605. if (res == -1 && errno == EINTR) {
  606. goto retry_sendmsg;
  607. }
  608. if (res == -1 && errno == EAGAIN) {
  609. return (0);
  610. }
  611. if (res == -1 && errno == EPIPE) {
  612. libais_disconnect_request (conn_info);
  613. return (0);
  614. }
  615. if (res == -1) {
  616. printf ("ERRNO is %d\n", errno);
  617. assert (0); /* some other unhandled error here */
  618. }
  619. if (res + conn_info->byte_start != queue_item->mlen) {
  620. conn_info->byte_start += res;
  621. return (0);
  622. }
  623. /*
  624. * Message sent, try sending another message
  625. */
  626. queue_item_remove (outq);
  627. conn_info->byte_start = 0;
  628. free (queue_item->msg);
  629. } /* while queue not empty */
  630. if (queue_is_empty (outq)) {
  631. conn_info->events = POLLIN|POLLNVAL;
  632. }
  633. return (0);
  634. }
  635. struct ipc_res_overlay {
  636. mar_res_header_t header __attribute((aligned(8)));
  637. char buf[4096];
  638. };
  639. static void libais_deliver (struct conn_info *conn_info)
  640. {
  641. ssize_t res;
  642. int dispatch_res;
  643. mar_req_header_t *header;
  644. int service;
  645. struct msghdr msg_recv;
  646. struct iovec iov_recv;
  647. #ifdef COROSYNC_LINUX
  648. struct cmsghdr *cmsg;
  649. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  650. struct ucred *cred;
  651. int on = 0;
  652. #endif
  653. int send_ok = 0;
  654. int send_ok_joined = 0;
  655. struct iovec send_ok_joined_iovec;
  656. struct ipc_res_overlay res_overlay;
  657. msg_recv.msg_iov = &iov_recv;
  658. msg_recv.msg_iovlen = 1;
  659. msg_recv.msg_name = 0;
  660. msg_recv.msg_namelen = 0;
  661. #ifndef COROSYNC_SOLARIS
  662. msg_recv.msg_flags = 0;
  663. if (conn_info->authenticated) {
  664. msg_recv.msg_control = 0;
  665. msg_recv.msg_controllen = 0;
  666. } else {
  667. #ifdef COROSYNC_LINUX
  668. msg_recv.msg_control = (void *)cmsg_cred;
  669. msg_recv.msg_controllen = sizeof (cmsg_cred);
  670. #else
  671. uid_t euid = -1;
  672. gid_t egid = -1;
  673. if (getpeereid(conn_info->fd, &euid, &egid) != -1 &&
  674. (euid == 0 || egid == g_gid_valid)) {
  675. conn_info->authenticated = 1;
  676. }
  677. if (conn_info->authenticated == 0) {
  678. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", egid, g_gid_valid);
  679. }
  680. #endif
  681. }
  682. #else /* COROSYNC_SOLARIS */
  683. msg_recv.msg_accrights = 0;
  684. msg_recv.msg_accrightslen = 0;
  685. if (! conn_info->authenticated) {
  686. #ifdef HAVE_GETPEERUCRED
  687. ucred_t *uc;
  688. uid_t euid = -1;
  689. gid_t egid = -1;
  690. if (getpeerucred(conn_info->fd, &uc) == 0) {
  691. euid = ucred_geteuid(uc);
  692. egid = ucred_getegid(uc);
  693. if ((euid == 0) || (egid == g_gid_valid)) {
  694. conn_info->authenticated = 1;
  695. }
  696. ucred_free(uc);
  697. }
  698. if (conn_info->authenticated == 0) {
  699. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", (int)egid, g_gid_valid);
  700. }
  701. #else
  702. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated "
  703. "because platform does not support "
  704. "authentication with sockets, continuing "
  705. "with a fake authentication\n");
  706. conn_info->authenticated = 1;
  707. #endif
  708. }
  709. #endif
  710. iov_recv.iov_base = &conn_info->inb[conn_info->inb_start];
  711. iov_recv.iov_len = (SIZEINB) - conn_info->inb_start;
  712. if (conn_info->inb_inuse == SIZEINB) {
  713. return;
  714. }
  715. retry_recv:
  716. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  717. if (res == -1 && errno == EINTR) {
  718. goto retry_recv;
  719. } else
  720. if (res == -1 && errno != EAGAIN) {
  721. return;
  722. } else
  723. if (res == 0) {
  724. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  725. /* On many OS poll never return POLLHUP or POLLERR.
  726. * EOF is detected when recvmsg return 0.
  727. */
  728. libais_disconnect_request (conn_info);
  729. #endif
  730. return;
  731. }
  732. /*
  733. * Authenticate if this connection has not been authenticated
  734. */
  735. #ifdef COROSYNC_LINUX
  736. if (conn_info->authenticated == 0) {
  737. cmsg = CMSG_FIRSTHDR (&msg_recv);
  738. assert (cmsg != NULL);
  739. cred = (struct ucred *)CMSG_DATA (cmsg);
  740. if (cred) {
  741. if (cred->uid == 0 || cred->gid == g_gid_valid) {
  742. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  743. conn_info->authenticated = 1;
  744. }
  745. }
  746. if (conn_info->authenticated == 0) {
  747. log_printf (LOG_LEVEL_SECURITY, "Connection not authenticated because gid is %d, expecting %d\n", cred->gid, g_gid_valid);
  748. }
  749. }
  750. #endif
  751. /*
  752. * Dispatch all messages received in recvmsg that can be dispatched
  753. * sizeof (mar_req_header_t) needed at minimum to do any processing
  754. */
  755. conn_info->inb_inuse += res;
  756. conn_info->inb_start += res;
  757. dispatch_res = 0;
  758. while (conn_info->inb_inuse >= sizeof (mar_req_header_t) && dispatch_res != -1) {
  759. header = (mar_req_header_t *)&conn_info->inb[conn_info->inb_start - conn_info->inb_inuse];
  760. if (header->size > conn_info->inb_inuse) {
  761. break;
  762. }
  763. service = conn_info->service;
  764. /*
  765. * If this service is in init phase, initialize service
  766. * else handle message using service service
  767. */
  768. if (service == SOCKET_SERVICE_INIT) {
  769. dispatch_res = ais_init_service[header->id] (conn_info, header);
  770. } else {
  771. /*
  772. * Not an init service, but a standard service
  773. */
  774. if (header->id < 0 || header->id > ais_service[service]->lib_engine_count) {
  775. log_printf (LOG_LEVEL_SECURITY, "Invalid header id is %d min 0 max %d\n",
  776. header->id, ais_service[service]->lib_engine_count);
  777. return ;
  778. }
  779. /*
  780. * If flow control is required of the library handle, determine that
  781. * corosync is not in synchronization and that totempg has room available
  782. * to queue a message, otherwise tell the library we are busy and to
  783. * try again later
  784. */
  785. send_ok_joined_iovec.iov_base = (char *)header;
  786. send_ok_joined_iovec.iov_len = header->size;
  787. send_ok_joined = totempg_groups_send_ok_joined (corosync_group_handle,
  788. &send_ok_joined_iovec, 1);
  789. send_ok =
  790. (corosync_quorum_is_quorate() == 1 || ais_service[service]->allow_inquorate == CS_LIB_ALLOW_INQUORATE) && (
  791. (ais_service[service]->lib_engine[header->id].flow_control == CS_LIB_FLOW_CONTROL_NOT_REQUIRED) ||
  792. ((ais_service[service]->lib_engine[header->id].flow_control == CS_LIB_FLOW_CONTROL_REQUIRED) &&
  793. (send_ok_joined) &&
  794. (sync_in_process() == 0)));
  795. if (send_ok) {
  796. ais_service[service]->lib_engine[header->id].lib_handler_fn(conn_info, header);
  797. } else {
  798. /*
  799. * Overload, tell library to retry
  800. */
  801. res_overlay.header.size =
  802. ais_service[service]->lib_engine[header->id].response_size;
  803. res_overlay.header.id =
  804. ais_service[service]->lib_engine[header->id].response_id;
  805. res_overlay.header.error = CS_ERR_TRY_AGAIN;
  806. cs_conn_send_response (
  807. conn_info,
  808. &res_overlay,
  809. res_overlay.header.size);
  810. }
  811. }
  812. conn_info->inb_inuse -= header->size;
  813. } /* while */
  814. if (conn_info->inb_inuse == 0) {
  815. conn_info->inb_start = 0;
  816. } else
  817. // BUG if (connections[conn_info->fd].inb_start + connections[conn_info->fd].inb_inuse >= SIZEINB) {
  818. if (conn_info->inb_start >= SIZEINB) {
  819. /*
  820. * If in buffer is full, move it back to start
  821. */
  822. memmove (conn_info->inb,
  823. &conn_info->inb[conn_info->inb_start - conn_info->inb_inuse],
  824. sizeof (char) * conn_info->inb_inuse);
  825. conn_info->inb_start = conn_info->inb_inuse;
  826. }
  827. return;
  828. }
  829. static int poll_handler_libais_accept (
  830. poll_handle handle,
  831. int fd,
  832. int revent,
  833. void *data)
  834. {
  835. socklen_t addrlen;
  836. struct sockaddr_un un_addr;
  837. int new_fd;
  838. #ifdef COROSYNC_LINUX
  839. int on = 1;
  840. #endif
  841. int res;
  842. addrlen = sizeof (struct sockaddr_un);
  843. retry_accept:
  844. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  845. if (new_fd == -1 && errno == EINTR) {
  846. goto retry_accept;
  847. }
  848. if (new_fd == -1) {
  849. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not accept Library connection: %s\n", strerror (errno));
  850. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  851. }
  852. totemip_nosigpipe(new_fd);
  853. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  854. if (res == -1) {
  855. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  856. close (new_fd);
  857. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  858. }
  859. /*
  860. * Valid accept
  861. */
  862. /*
  863. * Request credentials of sender provided by kernel
  864. */
  865. #ifdef COROSYNC_LINUX
  866. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  867. #endif
  868. log_printf (LOG_LEVEL_DEBUG, "connection received from libais client %d.\n", new_fd);
  869. res = conn_info_create (new_fd);
  870. if (res != 0) {
  871. close (new_fd);
  872. }
  873. return (0);
  874. }
  875. /*
  876. * Exported functions
  877. */
  878. int message_source_is_local(mar_message_source_t *source)
  879. {
  880. int ret = 0;
  881. assert (source != NULL);
  882. if (source->nodeid == totempg_my_nodeid_get ()) {
  883. ret = 1;
  884. }
  885. return ret;
  886. }
  887. void message_source_set (
  888. mar_message_source_t *source,
  889. void *conn)
  890. {
  891. assert ((source != NULL) && (conn != NULL));
  892. memset (source, 0, sizeof (mar_message_source_t));
  893. source->nodeid = totempg_my_nodeid_get ();
  894. source->conn = conn;
  895. }
  896. static void ipc_confchg_fn (
  897. enum totem_configuration_type configuration_type,
  898. unsigned int *member_list, int member_list_entries,
  899. unsigned int *left_list, int left_list_entries,
  900. unsigned int *joined_list, int joined_list_entries,
  901. struct memb_ring_id *ring_id)
  902. {
  903. }
  904. void cs_ipc_init (
  905. void (*serialize_lock_fn) (void),
  906. void (*serialize_unlock_fn) (void),
  907. unsigned int gid_valid)
  908. {
  909. int libais_server_fd;
  910. struct sockaddr_un un_addr;
  911. int res;
  912. ipc_serialize_lock_fn = serialize_lock_fn;
  913. ipc_serialize_unlock_fn = serialize_unlock_fn;
  914. /*
  915. * Create socket for libais clients, name socket, listen for connections
  916. */
  917. libais_server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  918. if (libais_server_fd == -1) {
  919. log_printf (LOG_LEVEL_ERROR ,"Cannot create libais client connections socket.\n");
  920. corosync_exit_error (AIS_DONE_LIBAIS_SOCKET);
  921. };
  922. totemip_nosigpipe(libais_server_fd);
  923. res = fcntl (libais_server_fd, F_SETFL, O_NONBLOCK);
  924. if (res == -1) {
  925. log_printf (LOG_LEVEL_ERROR, "Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  926. corosync_exit_error (AIS_DONE_LIBAIS_SOCKET);
  927. }
  928. #if !defined(COROSYNC_LINUX)
  929. unlink(socketname);
  930. #endif
  931. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  932. un_addr.sun_family = AF_UNIX;
  933. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  934. un_addr.sun_len = sizeof(struct sockaddr_un);
  935. #endif
  936. #if defined(COROSYNC_LINUX)
  937. strcpy (un_addr.sun_path + 1, socketname);
  938. #else
  939. strcpy (un_addr.sun_path, socketname);
  940. #endif
  941. res = bind (libais_server_fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  942. if (res) {
  943. log_printf (LOG_LEVEL_ERROR, "ERROR: Could not bind AF_UNIX: %s.\n", strerror (errno));
  944. corosync_exit_error (AIS_DONE_LIBAIS_BIND);
  945. }
  946. listen (libais_server_fd, SERVER_BACKLOG);
  947. /*
  948. * Setup libais connection dispatch routine
  949. */
  950. poll_dispatch_add (aisexec_poll_handle, libais_server_fd,
  951. POLLIN, 0, poll_handler_libais_accept);
  952. g_gid_valid = gid_valid;
  953. /*
  954. * Reset internal state of flow control when
  955. * configuration change occurs
  956. */
  957. res = totempg_groups_initialize (
  958. &ipc_handle,
  959. NULL,
  960. ipc_confchg_fn);
  961. }
  962. /*
  963. * Get the conn info private data
  964. */
  965. void *cs_conn_private_data_get (void *conn)
  966. {
  967. struct conn_info *conn_info = (struct conn_info *)conn;
  968. if (conn != NULL) {
  969. return ((void *)conn_info->private_data);
  970. } else {
  971. return NULL;
  972. }
  973. }
  974. /*
  975. * Get the conn info partner connection
  976. */
  977. void *cs_conn_partner_get (void *conn)
  978. {
  979. struct conn_info *conn_info = (struct conn_info *)conn;
  980. if (conn != NULL) {
  981. return ((void *)conn_info->conn_info_partner);
  982. } else {
  983. return NULL;
  984. }
  985. }
  986. int cs_conn_send_response_no_fcc (
  987. void *conn,
  988. void *msg,
  989. int mlen)
  990. {
  991. int ret;
  992. dont_call_flow_control = 1;
  993. ret = cs_conn_send_response (
  994. conn, msg, mlen);
  995. dont_call_flow_control = 0;
  996. return ret;
  997. }
  998. int cs_conn_send_response (
  999. void *conn,
  1000. void *msg,
  1001. int mlen)
  1002. {
  1003. struct queue *outq;
  1004. char *cmsg;
  1005. ssize_t res = 0;
  1006. int queue_empty;
  1007. struct outq_item *queue_item;
  1008. struct outq_item queue_item_out;
  1009. struct msghdr msg_send;
  1010. struct iovec iov_send;
  1011. char *msg_addr;
  1012. struct conn_info *conn_info = (struct conn_info *)conn;
  1013. if (conn_info == NULL) {
  1014. return -1;
  1015. }
  1016. if (!libais_connection_active (conn_info)) {
  1017. return (-1);
  1018. }
  1019. if (dont_call_flow_control == 0) {
  1020. ipc_flow_control (conn_info);
  1021. }
  1022. outq = &conn_info->outq;
  1023. msg_send.msg_iov = &iov_send;
  1024. msg_send.msg_name = 0;
  1025. msg_send.msg_namelen = 0;
  1026. msg_send.msg_iovlen = 1;
  1027. #ifndef COROSYNC_SOLARIS
  1028. msg_send.msg_control = 0;
  1029. msg_send.msg_controllen = 0;
  1030. msg_send.msg_flags = 0;
  1031. #else
  1032. msg_send.msg_accrights = 0;
  1033. msg_send.msg_accrightslen = 0;
  1034. #endif
  1035. if (queue_is_full (outq)) {
  1036. /*
  1037. * Start a disconnect if we have not already started one
  1038. * and report that the outgoing queue is full
  1039. */
  1040. log_printf (LOG_LEVEL_ERROR, "Library queue is full, disconnecting library connection.\n");
  1041. libais_disconnect_request (conn_info);
  1042. return (-1);
  1043. }
  1044. while (!queue_is_empty (outq)) {
  1045. queue_item = queue_item_get (outq);
  1046. msg_addr = (char *)queue_item->msg;
  1047. msg_addr = &msg_addr[conn_info->byte_start];
  1048. iov_send.iov_base = msg_addr;
  1049. iov_send.iov_len = queue_item->mlen - conn_info->byte_start;
  1050. retry_sendmsg:
  1051. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  1052. if (res == -1 && errno == EINTR) {
  1053. goto retry_sendmsg;
  1054. }
  1055. if (res == -1 && errno == EAGAIN) {
  1056. break; /* outgoing kernel queue full */
  1057. }
  1058. if (res == -1 && errno == EPIPE) {
  1059. libais_disconnect_request (conn_info);
  1060. return (0);
  1061. }
  1062. if (res == -1) {
  1063. assert (0);
  1064. break; /* some other error, stop trying to send message */
  1065. }
  1066. if (res + conn_info->byte_start != queue_item->mlen) {
  1067. conn_info->byte_start += res;
  1068. break;
  1069. }
  1070. /*
  1071. * Message sent, try sending another message
  1072. */
  1073. queue_item_remove (outq);
  1074. conn_info->byte_start = 0;
  1075. free (queue_item->msg);
  1076. } /* while queue not empty */
  1077. res = -1;
  1078. queue_empty = queue_is_empty (outq);
  1079. /*
  1080. * Send request message
  1081. */
  1082. if (queue_empty) {
  1083. iov_send.iov_base = msg;
  1084. iov_send.iov_len = mlen;
  1085. retry_sendmsg_two:
  1086. res = sendmsg (conn_info->fd, &msg_send, MSG_NOSIGNAL);
  1087. if (res == -1 && errno == EINTR) {
  1088. goto retry_sendmsg_two;
  1089. }
  1090. if (res == -1 && errno == EAGAIN) {
  1091. conn_info->byte_start = 0;
  1092. conn_info->events = POLLIN|POLLNVAL;
  1093. }
  1094. if (res != -1) {
  1095. if (res != mlen) {
  1096. conn_info->byte_start += res;
  1097. res = -1;
  1098. } else {
  1099. conn_info->byte_start = 0;
  1100. conn_info->events = POLLIN|POLLNVAL;
  1101. }
  1102. }
  1103. }
  1104. /*
  1105. * If res == -1 , errrno == EAGAIN which means kernel queue full
  1106. */
  1107. if (res == -1) {
  1108. cmsg = malloc (mlen);
  1109. if (cmsg == 0) {
  1110. log_printf (LOG_LEVEL_ERROR, "Library queue couldn't allocate a message, disconnecting library connection.\n");
  1111. libais_disconnect_request (conn_info);
  1112. return (-1);
  1113. }
  1114. queue_item_out.msg = cmsg;
  1115. queue_item_out.mlen = mlen;
  1116. memcpy (cmsg, msg, mlen);
  1117. queue_item_add (outq, &queue_item_out);
  1118. /*
  1119. * Send a pthread_kill to interrupt the poll syscall
  1120. * and start a new poll operation in the thread
  1121. */
  1122. conn_info->events = POLLIN|POLLOUT|POLLNVAL;
  1123. pthread_kill (conn_info->thread, SIGUSR1);
  1124. }
  1125. return (0);
  1126. }
  1127. void cs_ipc_flow_control_create (
  1128. void *conn,
  1129. unsigned int service,
  1130. char *id,
  1131. int id_len,
  1132. void (*flow_control_state_set_fn) (void *conn, enum cs_flow_control_state),
  1133. void *context)
  1134. {
  1135. struct conn_info *conn_info = (struct conn_info *)conn;
  1136. cs_flow_control_create (
  1137. conn_info->flow_control_handle,
  1138. service,
  1139. id,
  1140. id_len,
  1141. flow_control_state_set_fn,
  1142. context);
  1143. conn_info->conn_info_partner->flow_control_handle = conn_info->flow_control_handle;
  1144. }
  1145. void cs_ipc_flow_control_destroy (
  1146. void *conn,
  1147. unsigned int service,
  1148. unsigned char *id,
  1149. int id_len)
  1150. {
  1151. struct conn_info *conn_info = (struct conn_info *)conn;
  1152. cs_flow_control_destroy (
  1153. conn_info->flow_control_handle,
  1154. service,
  1155. id,
  1156. id_len);
  1157. }
  1158. void cs_ipc_flow_control_local_increment (
  1159. void *conn)
  1160. {
  1161. struct conn_info *conn_info = (struct conn_info *)conn;
  1162. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1163. conn_info->flow_control_local_count++;
  1164. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1165. }
  1166. void cs_ipc_flow_control_local_decrement (
  1167. void *conn)
  1168. {
  1169. struct conn_info *conn_info = (struct conn_info *)conn;
  1170. pthread_mutex_lock (&conn_info->flow_control_mutex);
  1171. conn_info->flow_control_local_count--;
  1172. pthread_mutex_unlock (&conn_info->flow_control_mutex);
  1173. }