ipc.c 35 KB

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