coroipcs.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /*
  2. * Copyright (c) 2006-2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #ifndef _GNU_SOURCE
  36. #define _GNU_SOURCE 1
  37. #endif
  38. #include <pthread.h>
  39. #include <limits.h>
  40. #include <assert.h>
  41. #include <pwd.h>
  42. #include <grp.h>
  43. #include <sys/types.h>
  44. #include <sys/poll.h>
  45. #include <sys/uio.h>
  46. #include <sys/mman.h>
  47. #include <sys/socket.h>
  48. #include <sys/un.h>
  49. #include <sys/time.h>
  50. #include <sys/resource.h>
  51. #include <sys/wait.h>
  52. #include <sys/stat.h>
  53. #include <netinet/in.h>
  54. #include <arpa/inet.h>
  55. #include <unistd.h>
  56. #include <fcntl.h>
  57. #include <stdlib.h>
  58. #include <stdio.h>
  59. #include <errno.h>
  60. #include <signal.h>
  61. #include <sched.h>
  62. #include <time.h>
  63. #if defined(HAVE_GETPEERUCRED)
  64. #include <ucred.h>
  65. #endif
  66. #include <string.h>
  67. #include <sys/shm.h>
  68. #include <corosync/corotypes.h>
  69. #include <corosync/list.h>
  70. #include <corosync/coroipc_types.h>
  71. #include <corosync/hdb.h>
  72. #include <corosync/coroipcs.h>
  73. #include <corosync/coroipc_ipc.h>
  74. #define LOGSYS_UTILS_ONLY 1
  75. #include <corosync/engine/logsys.h>
  76. #if _POSIX_THREAD_PROCESS_SHARED > 0
  77. #include <semaphore.h>
  78. #else
  79. #include <sys/sem.h>
  80. #endif
  81. #include "util.h"
  82. #ifndef MSG_NOSIGNAL
  83. #define MSG_NOSIGNAL 0
  84. #endif
  85. #define SERVER_BACKLOG 5
  86. #define MSG_SEND_LOCKED 0
  87. #define MSG_SEND_UNLOCKED 1
  88. #define POLL_STATE_IN 1
  89. #define POLL_STATE_INOUT 2
  90. static struct coroipcs_init_state_v2 *api = NULL;
  91. DECLARE_LIST_INIT (conn_info_list_head);
  92. DECLARE_LIST_INIT (conn_info_exit_list_head);
  93. struct outq_item {
  94. void *msg;
  95. size_t mlen;
  96. struct list_head list;
  97. };
  98. struct zcb_mapped {
  99. struct list_head list;
  100. void *addr;
  101. size_t size;
  102. };
  103. enum conn_state {
  104. CONN_STATE_THREAD_INACTIVE = 0,
  105. CONN_STATE_THREAD_ACTIVE = 1,
  106. CONN_STATE_THREAD_REQUEST_EXIT = 2,
  107. CONN_STATE_THREAD_DESTROYED = 3,
  108. CONN_STATE_LIB_EXIT_CALLED = 4,
  109. CONN_STATE_DISCONNECT_INACTIVE = 5
  110. };
  111. struct conn_info {
  112. int fd;
  113. pthread_t thread;
  114. pid_t client_pid;
  115. unsigned int service;
  116. enum conn_state state;
  117. int refcount;
  118. hdb_handle_t stats_handle;
  119. #if _POSIX_THREAD_PROCESS_SHARED < 1
  120. key_t semkey;
  121. #endif
  122. unsigned int pending_semops;
  123. pthread_mutex_t mutex;
  124. struct control_buffer *control_buffer;
  125. char *request_buffer;
  126. char *response_buffer;
  127. char *dispatch_buffer;
  128. size_t control_size;
  129. size_t request_size;
  130. size_t response_size;
  131. size_t dispatch_size;
  132. struct list_head outq_head;
  133. void *private_data;
  134. struct list_head list;
  135. char setup_msg[sizeof (mar_req_setup_t)];
  136. unsigned int setup_bytes_read;
  137. struct list_head zcb_mapped_list_head;
  138. char *sending_allowed_private_data[64];
  139. int poll_state;
  140. };
  141. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info);
  142. static void outq_flush (struct conn_info *conn_info);
  143. static int priv_change (struct conn_info *conn_info);
  144. static void ipc_disconnect (struct conn_info *conn_info);
  145. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  146. int locked);
  147. static void _corosync_ipc_init(void);
  148. #define log_printf(level, format, args...) \
  149. do { \
  150. if (api->log_printf) \
  151. api->log_printf ( \
  152. LOGSYS_ENCODE_RECID(level, \
  153. api->log_subsys_id, \
  154. LOGSYS_RECID_LOG), \
  155. __FUNCTION__, __FILE__, __LINE__, \
  156. (const char *)format, ##args); \
  157. else \
  158. api->old_log_printf ((const char *)format, ##args); \
  159. } while (0)
  160. static hdb_handle_t dummy_stats_create_connection (
  161. const char *name,
  162. pid_t pid,
  163. int fd)
  164. {
  165. return (0ULL);
  166. }
  167. static void dummy_stats_destroy_connection (
  168. hdb_handle_t handle)
  169. {
  170. }
  171. static void dummy_stats_update_value (
  172. hdb_handle_t handle,
  173. const char *name,
  174. const void *value,
  175. size_t value_size)
  176. {
  177. }
  178. static void dummy_stats_increment_value (
  179. hdb_handle_t handle,
  180. const char *name)
  181. {
  182. }
  183. static int
  184. memory_map (
  185. const char *path,
  186. size_t bytes,
  187. void **buf)
  188. {
  189. int32_t fd;
  190. void *addr_orig;
  191. void *addr;
  192. int32_t res;
  193. fd = open (path, O_RDWR, 0600);
  194. unlink (path);
  195. if (fd == -1) {
  196. return (-1);
  197. }
  198. res = ftruncate (fd, bytes);
  199. if (res == -1) {
  200. goto error_close_unlink;
  201. }
  202. addr_orig = mmap (NULL, bytes, PROT_NONE,
  203. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  204. if (addr_orig == MAP_FAILED) {
  205. goto error_close_unlink;
  206. }
  207. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  208. MAP_FIXED | MAP_SHARED, fd, 0);
  209. if (addr != addr_orig) {
  210. munmap(addr_orig, bytes);
  211. goto error_close_unlink;
  212. }
  213. #ifdef COROSYNC_BSD
  214. madvise(addr, bytes, MADV_NOSYNC);
  215. #endif
  216. res = close (fd);
  217. if (res) {
  218. return (-1);
  219. }
  220. *buf = addr_orig;
  221. return (0);
  222. error_close_unlink:
  223. close (fd);
  224. unlink(path);
  225. return -1;
  226. }
  227. static int
  228. circular_memory_map (
  229. const char *path,
  230. size_t bytes,
  231. void **buf)
  232. {
  233. int32_t fd;
  234. void *addr_orig;
  235. void *addr;
  236. int32_t res;
  237. fd = open (path, O_RDWR, 0600);
  238. unlink (path);
  239. if (fd == -1) {
  240. return (-1);
  241. }
  242. res = ftruncate (fd, bytes);
  243. if (res == -1) {
  244. goto error_close_unlink;
  245. }
  246. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  247. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  248. if (addr_orig == MAP_FAILED) {
  249. munmap(addr_orig, bytes);
  250. goto error_close_unlink;
  251. }
  252. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  253. MAP_FIXED | MAP_SHARED, fd, 0);
  254. if (addr != addr_orig) {
  255. munmap(addr_orig, bytes);
  256. goto error_close_unlink;
  257. }
  258. #ifdef COROSYNC_BSD
  259. madvise(addr_orig, bytes, MADV_NOSYNC);
  260. #endif
  261. addr = mmap (((char *)addr_orig) + bytes,
  262. bytes, PROT_READ | PROT_WRITE,
  263. MAP_FIXED | MAP_SHARED, fd, 0);
  264. if (addr == MAP_FAILED) {
  265. munmap(addr_orig, bytes);
  266. munmap(addr, bytes);
  267. goto error_close_unlink;
  268. }
  269. #ifdef COROSYNC_BSD
  270. madvise(((char *)addr_orig) + bytes, bytes, MADV_NOSYNC);
  271. #endif
  272. res = close (fd);
  273. if (res) {
  274. munmap(addr_orig, bytes);
  275. munmap(addr, bytes);
  276. return (-1);
  277. }
  278. *buf = addr_orig;
  279. return (0);
  280. error_close_unlink:
  281. close (fd);
  282. unlink(path);
  283. return (-1);
  284. }
  285. static inline int
  286. circular_memory_unmap (void *buf, size_t bytes)
  287. {
  288. int res;
  289. res = munmap (buf, bytes << 1);
  290. return (res);
  291. }
  292. static int32_t flow_control_state_set (
  293. struct conn_info *conn_info,
  294. int flow_control_state)
  295. {
  296. if (conn_info->control_buffer->flow_control_enabled == flow_control_state) {
  297. return 0;
  298. }
  299. if (flow_control_state == 0) {
  300. log_printf (LOGSYS_LEVEL_DEBUG,
  301. "Disabling flow control for %d\n",
  302. conn_info->client_pid);
  303. } else
  304. if (flow_control_state == 1) {
  305. log_printf (LOGSYS_LEVEL_DEBUG,
  306. "Enabling flow control for %d\n",
  307. conn_info->client_pid);
  308. }
  309. conn_info->control_buffer->flow_control_enabled = flow_control_state;
  310. return 1;
  311. }
  312. static void flow_control_stats_update (
  313. hdb_handle_t stats_handle,
  314. int flow_control_state)
  315. {
  316. uint32_t fc_state = flow_control_state;
  317. api->stats_update_value (stats_handle, "flow_control",
  318. &fc_state, sizeof(fc_state));
  319. api->stats_increment_value (stats_handle, "flow_control_count");
  320. }
  321. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  322. {
  323. unsigned int res;
  324. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  325. list_del (&zcb_mapped->list);
  326. free (zcb_mapped);
  327. return (res);
  328. }
  329. static inline int zcb_by_addr_free (struct conn_info *conn_info, void *addr)
  330. {
  331. struct list_head *list;
  332. struct zcb_mapped *zcb_mapped;
  333. unsigned int res = 0;
  334. for (list = conn_info->zcb_mapped_list_head.next;
  335. list != &conn_info->zcb_mapped_list_head; list = list->next) {
  336. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  337. if (zcb_mapped->addr == addr) {
  338. res = zcb_free (zcb_mapped);
  339. break;
  340. }
  341. }
  342. return (res);
  343. }
  344. static inline int zcb_all_free (
  345. struct conn_info *conn_info)
  346. {
  347. struct list_head *list;
  348. struct zcb_mapped *zcb_mapped;
  349. for (list = conn_info->zcb_mapped_list_head.next;
  350. list != &conn_info->zcb_mapped_list_head;) {
  351. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  352. list = list->next;
  353. zcb_free (zcb_mapped);
  354. }
  355. return (0);
  356. }
  357. static inline int zcb_alloc (
  358. struct conn_info *conn_info,
  359. const char *path_to_file,
  360. size_t size,
  361. void **addr)
  362. {
  363. struct zcb_mapped *zcb_mapped;
  364. unsigned int res;
  365. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  366. if (zcb_mapped == NULL) {
  367. return (-1);
  368. }
  369. res = memory_map (
  370. path_to_file,
  371. size,
  372. addr);
  373. if (res == -1) {
  374. free (zcb_mapped);
  375. return (-1);
  376. }
  377. list_init (&zcb_mapped->list);
  378. zcb_mapped->addr = *addr;
  379. zcb_mapped->size = size;
  380. list_add_tail (&zcb_mapped->list, &conn_info->zcb_mapped_list_head);
  381. return (0);
  382. }
  383. static int ipc_thread_active (void *conn)
  384. {
  385. struct conn_info *conn_info = (struct conn_info *)conn;
  386. int retval = 0;
  387. pthread_mutex_lock (&conn_info->mutex);
  388. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  389. retval = 1;
  390. }
  391. pthread_mutex_unlock (&conn_info->mutex);
  392. return (retval);
  393. }
  394. static int ipc_thread_exiting (void *conn)
  395. {
  396. struct conn_info *conn_info = (struct conn_info *)conn;
  397. int retval = 1;
  398. pthread_mutex_lock (&conn_info->mutex);
  399. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  400. retval = 0;
  401. } else
  402. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  403. retval = 0;
  404. }
  405. pthread_mutex_unlock (&conn_info->mutex);
  406. return (retval);
  407. }
  408. /*
  409. * returns 0 if should be called again, -1 if finished
  410. */
  411. static inline int conn_info_destroy (struct conn_info *conn_info)
  412. {
  413. unsigned int res;
  414. void *retval;
  415. list_del (&conn_info->list);
  416. list_init (&conn_info->list);
  417. list_add (&conn_info->list, &conn_info_exit_list_head);
  418. if (conn_info->state == CONN_STATE_THREAD_REQUEST_EXIT) {
  419. res = pthread_join (conn_info->thread, &retval);
  420. conn_info->state = CONN_STATE_THREAD_DESTROYED;
  421. return (0);
  422. }
  423. if (conn_info->state == CONN_STATE_THREAD_INACTIVE ||
  424. conn_info->state == CONN_STATE_DISCONNECT_INACTIVE) {
  425. list_del (&conn_info->list);
  426. close (conn_info->fd);
  427. api->free (conn_info);
  428. return (-1);
  429. }
  430. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  431. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT);
  432. return (0);
  433. }
  434. /*
  435. * Retry library exit function if busy
  436. */
  437. if (conn_info->state == CONN_STATE_THREAD_DESTROYED) {
  438. api->serialize_lock ();
  439. res = api->exit_fn_get (conn_info->service) (conn_info);
  440. api->serialize_unlock ();
  441. api->stats_destroy_connection (conn_info->stats_handle);
  442. if (res == -1) {
  443. return (0);
  444. } else {
  445. conn_info->state = CONN_STATE_LIB_EXIT_CALLED;
  446. }
  447. }
  448. pthread_mutex_lock (&conn_info->mutex);
  449. if (conn_info->refcount > 0) {
  450. pthread_mutex_unlock (&conn_info->mutex);
  451. return (0);
  452. }
  453. list_del (&conn_info->list);
  454. pthread_mutex_unlock (&conn_info->mutex);
  455. /*
  456. * Let library know, that connection is now closed
  457. */
  458. conn_info->control_buffer->ipc_closed = 1;
  459. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_RESPONSE);
  460. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_DISPATCH);
  461. #if _POSIX_THREAD_PROCESS_SHARED > 0
  462. sem_destroy (&conn_info->control_buffer->sem_request_or_flush_or_exit);
  463. sem_destroy (&conn_info->control_buffer->sem_request);
  464. sem_destroy (&conn_info->control_buffer->sem_response);
  465. sem_destroy (&conn_info->control_buffer->sem_dispatch);
  466. #else
  467. semctl (conn_info->control_buffer->semid, 0, IPC_RMID);
  468. #endif
  469. /*
  470. * Destroy shared memory segment and semaphore
  471. */
  472. res = munmap ((void *)conn_info->control_buffer, conn_info->control_size);
  473. res = munmap ((void *)conn_info->request_buffer, conn_info->request_size);
  474. res = munmap ((void *)conn_info->response_buffer, conn_info->response_size);
  475. /*
  476. * Free allocated data needed to retry exiting library IPC connection
  477. */
  478. if (conn_info->private_data) {
  479. api->free (conn_info->private_data);
  480. }
  481. close (conn_info->fd);
  482. res = circular_memory_unmap (conn_info->dispatch_buffer, conn_info->dispatch_size);
  483. zcb_all_free (conn_info);
  484. api->free (conn_info);
  485. return (-1);
  486. }
  487. union u {
  488. uint64_t server_addr;
  489. void *server_ptr;
  490. };
  491. static uint64_t void2serveraddr (void *server_ptr)
  492. {
  493. union u u;
  494. u.server_ptr = server_ptr;
  495. return (u.server_addr);
  496. }
  497. static void *serveraddr2void (uint64_t server_addr)
  498. {
  499. union u u;
  500. u.server_addr = server_addr;
  501. return (u.server_ptr);
  502. };
  503. static inline void zerocopy_operations_process (
  504. struct conn_info *conn_info,
  505. coroipc_request_header_t **header_out,
  506. unsigned int *new_message)
  507. {
  508. coroipc_request_header_t *header;
  509. header = (coroipc_request_header_t *)conn_info->request_buffer;
  510. if (header->id == ZC_ALLOC_HEADER) {
  511. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)header;
  512. coroipc_response_header_t res_header;
  513. void *addr = NULL;
  514. struct coroipcs_zc_header *zc_header;
  515. unsigned int res;
  516. res = zcb_alloc (conn_info, hdr->path_to_file, hdr->map_size,
  517. &addr);
  518. zc_header = (struct coroipcs_zc_header *)addr;
  519. zc_header->server_address = void2serveraddr(addr);
  520. res_header.size = sizeof (coroipc_response_header_t);
  521. res_header.id = 0;
  522. coroipcs_response_send (
  523. conn_info, &res_header,
  524. res_header.size);
  525. *new_message = 0;
  526. return;
  527. } else
  528. if (header->id == ZC_FREE_HEADER) {
  529. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)header;
  530. coroipc_response_header_t res_header;
  531. void *addr = NULL;
  532. addr = serveraddr2void (hdr->server_address);
  533. zcb_by_addr_free (conn_info, addr);
  534. res_header.size = sizeof (coroipc_response_header_t);
  535. res_header.id = 0;
  536. coroipcs_response_send (
  537. conn_info, &res_header,
  538. res_header.size);
  539. *new_message = 0;
  540. return;
  541. } else
  542. if (header->id == ZC_EXECUTE_HEADER) {
  543. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)header;
  544. header = (coroipc_request_header_t *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  545. }
  546. *header_out = header;
  547. *new_message = 1;
  548. }
  549. static void *pthread_ipc_consumer (void *conn)
  550. {
  551. struct conn_info *conn_info = (struct conn_info *)conn;
  552. int res;
  553. coroipc_request_header_t *header;
  554. coroipc_response_header_t coroipc_response_header;
  555. int send_ok;
  556. unsigned int new_message;
  557. int sem_value = 0;
  558. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
  559. if (api->sched_policy != 0) {
  560. res = pthread_setschedparam (conn_info->thread,
  561. api->sched_policy, api->sched_param);
  562. }
  563. #endif
  564. for (;;) {
  565. ipc_sem_wait (conn_info->control_buffer, SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT, IPC_SEMWAIT_NOFILE);
  566. if (ipc_thread_active (conn_info) == 0) {
  567. coroipcs_refcount_dec (conn_info);
  568. pthread_exit (0);
  569. }
  570. outq_flush (conn_info);
  571. ipc_sem_getvalue (conn_info->control_buffer, SEMAPHORE_REQUEST, &sem_value);
  572. if (sem_value > 0) {
  573. res = ipc_sem_wait (conn_info->control_buffer, SEMAPHORE_REQUEST, IPC_SEMWAIT_NOFILE);
  574. } else {
  575. continue;
  576. }
  577. zerocopy_operations_process (conn_info, &header, &new_message);
  578. /*
  579. * There is no new message to process, continue for loop
  580. */
  581. if (new_message == 0) {
  582. continue;
  583. }
  584. coroipcs_refcount_inc (conn);
  585. send_ok = api->sending_allowed (conn_info->service,
  586. header->id,
  587. header,
  588. conn_info->sending_allowed_private_data);
  589. /*
  590. * This happens when the message contains some kind of invalid
  591. * parameter, such as an invalid size
  592. */
  593. if (send_ok == -1) {
  594. api->stats_increment_value (conn_info->stats_handle, "invalid_request");
  595. coroipc_response_header.size = sizeof (coroipc_response_header_t);
  596. coroipc_response_header.id = 0;
  597. coroipc_response_header.error = CS_ERR_INVALID_PARAM;
  598. coroipcs_response_send (conn_info,
  599. &coroipc_response_header,
  600. sizeof (coroipc_response_header_t));
  601. } else
  602. if (send_ok) {
  603. api->stats_increment_value (conn_info->stats_handle, "requests");
  604. api->serialize_lock();
  605. api->handler_fn_get (conn_info->service, header->id) (conn_info, header);
  606. api->serialize_unlock();
  607. } else {
  608. /*
  609. * Overload, tell library to retry
  610. */
  611. api->stats_increment_value (conn_info->stats_handle, "overload");
  612. coroipc_response_header.size = sizeof (coroipc_response_header_t);
  613. coroipc_response_header.id = 0;
  614. coroipc_response_header.error = CS_ERR_TRY_AGAIN;
  615. coroipcs_response_send (conn_info,
  616. &coroipc_response_header,
  617. sizeof (coroipc_response_header_t));
  618. }
  619. api->sending_allowed_release (conn_info->sending_allowed_private_data);
  620. coroipcs_refcount_dec (conn);
  621. }
  622. pthread_exit (0);
  623. }
  624. static int
  625. req_setup_send (
  626. struct conn_info *conn_info,
  627. int error)
  628. {
  629. mar_res_setup_t res_setup;
  630. unsigned int res;
  631. memset (&res_setup, 0, sizeof (res_setup));
  632. res_setup.error = error;
  633. retry_send:
  634. res = send (conn_info->fd, &res_setup, sizeof (mar_res_setup_t), MSG_WAITALL);
  635. if (res == -1 && errno == EINTR) {
  636. api->stats_increment_value (conn_info->stats_handle, "send_retry_count");
  637. goto retry_send;
  638. } else
  639. if (res == -1 && errno == EAGAIN) {
  640. api->stats_increment_value (conn_info->stats_handle, "send_retry_count");
  641. goto retry_send;
  642. }
  643. return (0);
  644. }
  645. static cs_error_t
  646. req_setup_recv (
  647. struct conn_info *conn_info)
  648. {
  649. int res;
  650. struct msghdr msg_recv;
  651. struct iovec iov_recv;
  652. cs_error_t auth_res = CS_ERR_LIBRARY;
  653. #ifdef COROSYNC_LINUX
  654. struct cmsghdr *cmsg;
  655. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  656. int off = 0;
  657. int on = 1;
  658. struct ucred *cred;
  659. #endif
  660. msg_recv.msg_flags = 0;
  661. msg_recv.msg_iov = &iov_recv;
  662. msg_recv.msg_iovlen = 1;
  663. msg_recv.msg_name = 0;
  664. msg_recv.msg_namelen = 0;
  665. #ifdef COROSYNC_LINUX
  666. msg_recv.msg_control = (void *)cmsg_cred;
  667. msg_recv.msg_controllen = sizeof (cmsg_cred);
  668. #endif
  669. #ifdef COROSYNC_SOLARIS
  670. msg_recv.msg_accrights = 0;
  671. msg_recv.msg_accrightslen = 0;
  672. #endif /* COROSYNC_SOLARIS */
  673. iov_recv.iov_base = &conn_info->setup_msg[conn_info->setup_bytes_read];
  674. iov_recv.iov_len = sizeof (mar_req_setup_t) - conn_info->setup_bytes_read;
  675. #ifdef COROSYNC_LINUX
  676. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  677. #endif
  678. retry_recv:
  679. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  680. if (res == -1 && errno == EINTR) {
  681. api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
  682. goto retry_recv;
  683. } else
  684. if (res == -1 && errno != EAGAIN) {
  685. return (CS_ERR_LIBRARY);
  686. } else
  687. if (res == 0) {
  688. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  689. /* On many OS poll never return POLLHUP or POLLERR.
  690. * EOF is detected when recvmsg return 0.
  691. */
  692. ipc_disconnect (conn_info);
  693. return (CS_ERR_LIBRARY);
  694. #else
  695. return (CS_ERR_SECURITY);
  696. #endif
  697. }
  698. conn_info->setup_bytes_read += res;
  699. /*
  700. * currently support getpeerucred, getpeereid, and SO_PASSCRED credential
  701. * retrieval mechanisms for various Platforms
  702. */
  703. #ifdef HAVE_GETPEERUCRED
  704. /*
  705. * Solaris and some BSD systems
  706. */
  707. {
  708. ucred_t *uc = NULL;
  709. uid_t euid = -1;
  710. gid_t egid = -1;
  711. if (getpeerucred (conn_info->fd, &uc) == 0) {
  712. euid = ucred_geteuid (uc);
  713. egid = ucred_getegid (uc);
  714. conn_info->client_pid = ucred_getpid (uc);
  715. if (api->security_valid (euid, egid)) {
  716. auth_res = CS_OK;
  717. } else {
  718. auth_res = hdb_error_to_cs(errno);
  719. }
  720. ucred_free(uc);
  721. }
  722. }
  723. #elif HAVE_GETPEEREID
  724. /*
  725. * Usually MacOSX systems
  726. */
  727. {
  728. uid_t euid;
  729. gid_t egid;
  730. /*
  731. * TODO get the peer's pid.
  732. * conn_info->client_pid = ?;
  733. */
  734. euid = -1;
  735. egid = -1;
  736. if (getpeereid (conn_info->fd, &euid, &egid) == 0) {
  737. if (api->security_valid (euid, egid)) {
  738. auth_res = CS_OK;
  739. } else {
  740. auth_res = hdb_error_to_cs(errno);
  741. }
  742. }
  743. }
  744. #elif SO_PASSCRED
  745. /*
  746. * Usually Linux systems
  747. */
  748. cmsg = CMSG_FIRSTHDR (&msg_recv);
  749. assert (cmsg);
  750. cred = (struct ucred *)CMSG_DATA (cmsg);
  751. if (cred) {
  752. conn_info->client_pid = cred->pid;
  753. if (api->security_valid (cred->uid, cred->gid)) {
  754. auth_res = CS_OK;
  755. } else {
  756. auth_res = hdb_error_to_cs(errno);
  757. }
  758. }
  759. #else /* no credentials */
  760. auth_res = CS_OK;
  761. log_printf (LOGSYS_LEVEL_ERROR, "Platform does not support IPC authentication. Using no authentication\n");
  762. #endif /* no credentials */
  763. if (auth_res != CS_OK) {
  764. ipc_disconnect (conn_info);
  765. if (auth_res == CS_ERR_NO_RESOURCES) {
  766. log_printf (LOGSYS_LEVEL_ERROR,
  767. "Not enough file desciptors for IPC connection.\n");
  768. } else {
  769. log_printf (LOGSYS_LEVEL_ERROR, "Invalid IPC credentials.\n");
  770. }
  771. return auth_res;
  772. }
  773. if (conn_info->setup_bytes_read == sizeof (mar_req_setup_t)) {
  774. #ifdef COROSYNC_LINUX
  775. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED,
  776. &off, sizeof (off));
  777. #endif
  778. return (CS_OK);
  779. }
  780. return (CS_ERR_LIBRARY);
  781. }
  782. static void ipc_disconnect (struct conn_info *conn_info)
  783. {
  784. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  785. conn_info->state = CONN_STATE_DISCONNECT_INACTIVE;
  786. return;
  787. }
  788. if (conn_info->state != CONN_STATE_THREAD_ACTIVE) {
  789. return;
  790. }
  791. pthread_mutex_lock (&conn_info->mutex);
  792. conn_info->state = CONN_STATE_THREAD_REQUEST_EXIT;
  793. pthread_mutex_unlock (&conn_info->mutex);
  794. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT);
  795. }
  796. static int conn_info_create (int fd)
  797. {
  798. struct conn_info *conn_info;
  799. conn_info = api->malloc (sizeof (struct conn_info));
  800. if (conn_info == NULL) {
  801. return (-1);
  802. }
  803. memset (conn_info, 0, sizeof (struct conn_info));
  804. conn_info->fd = fd;
  805. conn_info->client_pid = 0;
  806. conn_info->service = SOCKET_SERVICE_INIT;
  807. conn_info->state = CONN_STATE_THREAD_INACTIVE;
  808. conn_info->poll_state = POLL_STATE_IN;
  809. list_init (&conn_info->outq_head);
  810. list_init (&conn_info->list);
  811. list_init (&conn_info->zcb_mapped_list_head);
  812. list_add (&conn_info->list, &conn_info_list_head);
  813. api->poll_dispatch_add (fd, conn_info);
  814. return (0);
  815. }
  816. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  817. /* SUN_LEN is broken for abstract namespace
  818. */
  819. #define COROSYNC_SUN_LEN(a) sizeof(*(a))
  820. #else
  821. #define COROSYNC_SUN_LEN(a) SUN_LEN(a)
  822. #endif
  823. /*
  824. * Exported functions
  825. */
  826. extern void coroipcs_ipc_init_v2 (
  827. struct coroipcs_init_state_v2 *init_state_v2)
  828. {
  829. api = init_state_v2;
  830. api->old_log_printf = NULL;
  831. log_printf (LOGSYS_LEVEL_DEBUG, "you are using ipc api v2\n");
  832. _corosync_ipc_init ();
  833. }
  834. extern void coroipcs_ipc_init (
  835. struct coroipcs_init_state *init_state)
  836. {
  837. api = calloc (sizeof(struct coroipcs_init_state_v2), 1);
  838. /* v2 api */
  839. api->stats_create_connection = dummy_stats_create_connection;
  840. api->stats_destroy_connection = dummy_stats_destroy_connection;
  841. api->stats_update_value = dummy_stats_update_value;
  842. api->stats_increment_value = dummy_stats_increment_value;
  843. api->log_printf = NULL;
  844. /* v1 api */
  845. api->socket_name = init_state->socket_name;
  846. api->sched_policy = init_state->sched_policy;
  847. api->sched_param = init_state->sched_param;
  848. api->malloc = init_state->malloc;
  849. api->free = init_state->free;
  850. api->old_log_printf = init_state->log_printf;
  851. api->fatal_error = init_state->fatal_error;
  852. api->security_valid = init_state->security_valid;
  853. api->service_available = init_state->service_available;
  854. api->private_data_size_get = init_state->private_data_size_get;
  855. api->serialize_lock = init_state->serialize_lock;
  856. api->serialize_unlock = init_state->serialize_unlock;
  857. api->sending_allowed = init_state->sending_allowed;
  858. api->sending_allowed_release = init_state->sending_allowed_release;
  859. api->poll_accept_add = init_state->poll_accept_add;
  860. api->poll_dispatch_add = init_state->poll_dispatch_add;
  861. api->poll_dispatch_modify = init_state->poll_dispatch_modify;
  862. api->init_fn_get = init_state->init_fn_get;
  863. api->exit_fn_get = init_state->exit_fn_get;
  864. api->handler_fn_get = init_state->handler_fn_get;
  865. log_printf (LOGSYS_LEVEL_DEBUG, "you are using ipc api v1\n");
  866. _corosync_ipc_init ();
  867. }
  868. static void _corosync_ipc_init(void)
  869. {
  870. int server_fd;
  871. struct sockaddr_un un_addr;
  872. int res;
  873. /*
  874. * Create socket for IPC clients, name socket, listen for connections
  875. */
  876. #if defined(COROSYNC_SOLARIS)
  877. server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  878. #else
  879. server_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  880. #endif
  881. if (server_fd == -1) {
  882. log_printf (LOGSYS_LEVEL_CRIT, "Cannot create client connections socket.\n");
  883. api->fatal_error ("Can't create library listen socket");
  884. }
  885. res = fcntl (server_fd, F_SETFL, O_NONBLOCK);
  886. if (res == -1) {
  887. LOGSYS_PERROR (errno, LOGSYS_LEVEL_CRIT,
  888. "Could not set non-blocking operation on server socket");
  889. api->fatal_error ("Could not set non-blocking operation on server socket");
  890. }
  891. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  892. un_addr.sun_family = AF_UNIX;
  893. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  894. un_addr.sun_len = SUN_LEN(&un_addr);
  895. #endif
  896. #if defined(COROSYNC_LINUX)
  897. sprintf (un_addr.sun_path + 1, "%s", api->socket_name);
  898. #else
  899. {
  900. struct stat stat_out;
  901. res = stat (SOCKETDIR, &stat_out);
  902. if (res == -1 || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  903. log_printf (LOGSYS_LEVEL_CRIT, "Required directory not present %s\n", SOCKETDIR);
  904. api->fatal_error ("Please create required directory.");
  905. }
  906. sprintf (un_addr.sun_path, "%s/%s", SOCKETDIR, api->socket_name);
  907. unlink (un_addr.sun_path);
  908. }
  909. #endif
  910. res = bind (server_fd, (struct sockaddr *)&un_addr, COROSYNC_SUN_LEN(&un_addr));
  911. if (res) {
  912. LOGSYS_PERROR (errno, LOGSYS_LEVEL_CRIT,
  913. "Could not bind AF_UNIX (%s)", un_addr.sun_path);
  914. api->fatal_error ("Could not bind to AF_UNIX socket\n");
  915. }
  916. /*
  917. * Allow eveyrone to write to the socket since the IPC layer handles
  918. * security automatically
  919. */
  920. #if !defined(COROSYNC_LINUX)
  921. res = chmod (un_addr.sun_path, S_IRWXU|S_IRWXG|S_IRWXO);
  922. #endif
  923. listen (server_fd, SERVER_BACKLOG);
  924. /*
  925. * Setup connection dispatch routine
  926. */
  927. api->poll_accept_add (server_fd);
  928. }
  929. void coroipcs_ipc_exit (void)
  930. {
  931. struct list_head *list;
  932. struct conn_info *conn_info;
  933. unsigned int res;
  934. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  935. list = list->next) {
  936. conn_info = list_entry (list, struct conn_info, list);
  937. if (conn_info->state != CONN_STATE_THREAD_ACTIVE)
  938. continue;
  939. ipc_disconnect (conn_info);
  940. #if _POSIX_THREAD_PROCESS_SHARED > 0
  941. sem_destroy (&conn_info->control_buffer->sem_request_or_flush_or_exit);
  942. sem_destroy (&conn_info->control_buffer->sem_request);
  943. sem_destroy (&conn_info->control_buffer->sem_response);
  944. sem_destroy (&conn_info->control_buffer->sem_dispatch);
  945. #else
  946. semctl (conn_info->control_buffer->semid, 0, IPC_RMID);
  947. #endif
  948. /*
  949. * Unmap memory segments
  950. */
  951. res = munmap ((void *)conn_info->control_buffer,
  952. conn_info->control_size);
  953. res = munmap ((void *)conn_info->request_buffer,
  954. conn_info->request_size);
  955. res = munmap ((void *)conn_info->response_buffer,
  956. conn_info->response_size);
  957. res = circular_memory_unmap (conn_info->dispatch_buffer,
  958. conn_info->dispatch_size);
  959. }
  960. }
  961. int coroipcs_ipc_service_exit (unsigned int service)
  962. {
  963. struct list_head *list, *list_next;
  964. struct conn_info *conn_info;
  965. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  966. list = list_next) {
  967. list_next = list->next;
  968. conn_info = list_entry (list, struct conn_info, list);
  969. if (conn_info->service != service ||
  970. (conn_info->state != CONN_STATE_THREAD_ACTIVE && conn_info->state != CONN_STATE_THREAD_REQUEST_EXIT)) {
  971. continue;
  972. }
  973. ipc_disconnect (conn_info);
  974. api->poll_dispatch_destroy (conn_info->fd, NULL);
  975. while (conn_info_destroy (conn_info) != -1)
  976. ;
  977. /*
  978. * We will return to prevent token loss. Schedwrk will call us again.
  979. */
  980. return (-1);
  981. }
  982. /*
  983. * No conn info left in active list. We will traverse thru exit list. If there is any
  984. * conn_info->service == service, we will wait to proper end -> return -1
  985. */
  986. for (list = conn_info_exit_list_head.next; list != &conn_info_exit_list_head; list = list->next) {
  987. conn_info = list_entry (list, struct conn_info, list);
  988. if (conn_info->service == service) {
  989. return (-1);
  990. }
  991. }
  992. return (0);
  993. }
  994. /*
  995. * Get the conn info private data
  996. */
  997. void *coroipcs_private_data_get (void *conn)
  998. {
  999. struct conn_info *conn_info = (struct conn_info *)conn;
  1000. return (conn_info->private_data);
  1001. }
  1002. int coroipcs_response_send (void *conn, const void *msg, size_t mlen)
  1003. {
  1004. struct conn_info *conn_info = (struct conn_info *)conn;
  1005. memcpy (conn_info->response_buffer, msg, mlen);
  1006. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_RESPONSE);
  1007. api->stats_increment_value (conn_info->stats_handle, "responses");
  1008. return (0);
  1009. }
  1010. int coroipcs_response_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  1011. {
  1012. struct conn_info *conn_info = (struct conn_info *)conn;
  1013. int write_idx = 0;
  1014. int i;
  1015. for (i = 0; i < iov_len; i++) {
  1016. memcpy (&conn_info->response_buffer[write_idx],
  1017. iov[i].iov_base, iov[i].iov_len);
  1018. write_idx += iov[i].iov_len;
  1019. }
  1020. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_RESPONSE);
  1021. api->stats_increment_value (conn_info->stats_handle, "responses");
  1022. return (0);
  1023. }
  1024. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info)
  1025. {
  1026. unsigned int n_read;
  1027. unsigned int n_write;
  1028. unsigned int bytes_left;
  1029. n_read = conn_info->control_buffer->read;
  1030. n_write = conn_info->control_buffer->write;
  1031. if (n_read <= n_write) {
  1032. bytes_left = conn_info->dispatch_size - n_write + n_read;
  1033. } else {
  1034. bytes_left = n_read - n_write;
  1035. }
  1036. if (bytes_left > 0) {
  1037. bytes_left--;
  1038. }
  1039. return (bytes_left);
  1040. }
  1041. static void memcpy_dwrap (struct conn_info *conn_info, void *msg, unsigned int len)
  1042. {
  1043. unsigned int write_idx;
  1044. write_idx = conn_info->control_buffer->write;
  1045. memcpy (&conn_info->dispatch_buffer[write_idx], msg, len);
  1046. conn_info->control_buffer->write = ((write_idx + len + 7) & 0xFFFFFFF8) % conn_info->dispatch_size;
  1047. }
  1048. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  1049. int locked)
  1050. {
  1051. struct conn_info *conn_info = (struct conn_info *)conn;
  1052. int res;
  1053. int i;
  1054. char buf;
  1055. for (i = 0; i < iov_len; i++) {
  1056. memcpy_dwrap (conn_info, iov[i].iov_base, iov[i].iov_len);
  1057. }
  1058. buf = list_empty (&conn_info->outq_head);
  1059. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1060. if (res != 1) {
  1061. conn_info->pending_semops += 1;
  1062. if (conn_info->poll_state == POLL_STATE_IN) {
  1063. conn_info->poll_state = POLL_STATE_INOUT;
  1064. api->poll_dispatch_modify (conn_info->fd,
  1065. POLLIN|POLLOUT|POLLNVAL);
  1066. }
  1067. }
  1068. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_DISPATCH);
  1069. }
  1070. static void outq_flush (struct conn_info *conn_info) {
  1071. struct list_head *list, *list_next;
  1072. struct outq_item *outq_item;
  1073. unsigned int bytes_left;
  1074. struct iovec iov;
  1075. int32_t q_size_dec = 0;
  1076. int32_t i;
  1077. int32_t fc_set;
  1078. pthread_mutex_lock (&conn_info->mutex);
  1079. if (list_empty (&conn_info->outq_head)) {
  1080. fc_set = flow_control_state_set (conn_info, 0);
  1081. pthread_mutex_unlock (&conn_info->mutex);
  1082. if (fc_set) {
  1083. flow_control_stats_update (conn_info->stats_handle, 0);
  1084. }
  1085. return;
  1086. }
  1087. for (list = conn_info->outq_head.next;
  1088. list != &conn_info->outq_head; list = list_next) {
  1089. list_next = list->next;
  1090. outq_item = list_entry (list, struct outq_item, list);
  1091. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  1092. if (bytes_left > outq_item->mlen) {
  1093. iov.iov_base = outq_item->msg;
  1094. iov.iov_len = outq_item->mlen;
  1095. msg_send (conn_info, &iov, 1, MSG_SEND_UNLOCKED);
  1096. list_del (list);
  1097. api->free (iov.iov_base);
  1098. api->free (outq_item);
  1099. q_size_dec++;
  1100. } else {
  1101. break;
  1102. }
  1103. }
  1104. pthread_mutex_unlock (&conn_info->mutex);
  1105. /*
  1106. * these need to be sent out of the conn_info->mutex
  1107. */
  1108. for (i = 0; i < q_size_dec; i++) {
  1109. api->stats_decrement_value (conn_info->stats_handle, "queue_size");
  1110. api->stats_increment_value (conn_info->stats_handle, "dispatched");
  1111. }
  1112. }
  1113. static int priv_change (struct conn_info *conn_info)
  1114. {
  1115. mar_req_priv_change req_priv_change;
  1116. unsigned int res;
  1117. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1118. union semun semun;
  1119. struct semid_ds ipc_set;
  1120. int i;
  1121. #endif
  1122. retry_recv:
  1123. res = recv (conn_info->fd, &req_priv_change,
  1124. sizeof (mar_req_priv_change),
  1125. MSG_NOSIGNAL);
  1126. if (res == -1 && errno == EINTR) {
  1127. api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
  1128. goto retry_recv;
  1129. }
  1130. if (res == -1 && errno == EAGAIN) {
  1131. api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
  1132. goto retry_recv;
  1133. }
  1134. if (res == -1 && errno != EAGAIN) {
  1135. return (-1);
  1136. }
  1137. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1138. /* Error on socket, EOF is detected when recv return 0
  1139. */
  1140. if (res == 0) {
  1141. return (-1);
  1142. }
  1143. #endif
  1144. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1145. ipc_set.sem_perm.uid = req_priv_change.euid;
  1146. ipc_set.sem_perm.gid = req_priv_change.egid;
  1147. ipc_set.sem_perm.mode = 0600;
  1148. semun.buf = &ipc_set;
  1149. for (i = 0; i < 3; i++) {
  1150. res = semctl (conn_info->control_buffer->semid, 0, IPC_SET, semun);
  1151. if (res == -1) {
  1152. return (-1);
  1153. }
  1154. }
  1155. #endif
  1156. return (0);
  1157. }
  1158. static void msg_send_or_queue (void *conn, const struct iovec *iov, unsigned int iov_len)
  1159. {
  1160. struct conn_info *conn_info = (struct conn_info *)conn;
  1161. unsigned int bytes_left;
  1162. unsigned int bytes_msg = 0;
  1163. int i;
  1164. struct outq_item *outq_item;
  1165. char *write_buf = 0;
  1166. /*
  1167. * Exit transmission if the connection is dead
  1168. */
  1169. if (ipc_thread_active (conn) == 0) {
  1170. return;
  1171. }
  1172. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  1173. for (i = 0; i < iov_len; i++) {
  1174. bytes_msg += iov[i].iov_len;
  1175. }
  1176. if (bytes_left < bytes_msg || list_empty (&conn_info->outq_head) == 0) {
  1177. if (flow_control_state_set (conn_info, 1)) {
  1178. flow_control_stats_update(conn_info->stats_handle, 1);
  1179. }
  1180. outq_item = api->malloc (sizeof (struct outq_item));
  1181. if (outq_item == NULL) {
  1182. ipc_disconnect (conn);
  1183. return;
  1184. }
  1185. outq_item->msg = api->malloc (bytes_msg);
  1186. if (outq_item->msg == 0) {
  1187. api->free (outq_item);
  1188. ipc_disconnect (conn);
  1189. return;
  1190. }
  1191. write_buf = outq_item->msg;
  1192. for (i = 0; i < iov_len; i++) {
  1193. memcpy (write_buf, iov[i].iov_base, iov[i].iov_len);
  1194. write_buf += iov[i].iov_len;
  1195. }
  1196. outq_item->mlen = bytes_msg;
  1197. list_init (&outq_item->list);
  1198. pthread_mutex_lock (&conn_info->mutex);
  1199. list_add_tail (&outq_item->list, &conn_info->outq_head);
  1200. pthread_mutex_unlock (&conn_info->mutex);
  1201. api->stats_increment_value (conn_info->stats_handle, "queue_size");
  1202. return;
  1203. }
  1204. msg_send (conn, iov, iov_len, MSG_SEND_LOCKED);
  1205. api->stats_increment_value (conn_info->stats_handle, "dispatched");
  1206. }
  1207. void coroipcs_refcount_inc (void *conn)
  1208. {
  1209. struct conn_info *conn_info = (struct conn_info *)conn;
  1210. pthread_mutex_lock (&conn_info->mutex);
  1211. conn_info->refcount++;
  1212. pthread_mutex_unlock (&conn_info->mutex);
  1213. }
  1214. void coroipcs_refcount_dec (void *conn)
  1215. {
  1216. struct conn_info *conn_info = (struct conn_info *)conn;
  1217. pthread_mutex_lock (&conn_info->mutex);
  1218. conn_info->refcount--;
  1219. pthread_mutex_unlock (&conn_info->mutex);
  1220. }
  1221. int coroipcs_dispatch_send (void *conn, const void *msg, size_t mlen)
  1222. {
  1223. struct iovec iov;
  1224. iov.iov_base = (void *)msg;
  1225. iov.iov_len = mlen;
  1226. msg_send_or_queue (conn, &iov, 1);
  1227. return (0);
  1228. }
  1229. int coroipcs_dispatch_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  1230. {
  1231. msg_send_or_queue (conn, iov, iov_len);
  1232. return (0);
  1233. }
  1234. int coroipcs_handler_accept (
  1235. int fd,
  1236. int revent,
  1237. void *data)
  1238. {
  1239. socklen_t addrlen;
  1240. struct sockaddr_un un_addr;
  1241. int new_fd;
  1242. #ifdef COROSYNC_LINUX
  1243. int on = 1;
  1244. #endif
  1245. int res;
  1246. addrlen = sizeof (struct sockaddr_un);
  1247. retry_accept:
  1248. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  1249. if (new_fd == -1 && errno == EINTR) {
  1250. goto retry_accept;
  1251. }
  1252. if (new_fd == -1) {
  1253. LOGSYS_PERROR (errno, LOGSYS_LEVEL_ERROR,
  1254. "Could not accept Library connection");
  1255. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1256. }
  1257. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  1258. if (res == -1) {
  1259. LOGSYS_PERROR (errno, LOGSYS_LEVEL_ERROR,
  1260. "Could not set non-blocking operation on library connection");
  1261. close (new_fd);
  1262. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1263. }
  1264. /*
  1265. * Valid accept
  1266. */
  1267. /*
  1268. * Request credentials of sender provided by kernel
  1269. */
  1270. #ifdef COROSYNC_LINUX
  1271. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  1272. #endif
  1273. res = conn_info_create (new_fd);
  1274. if (res != 0) {
  1275. close (new_fd);
  1276. }
  1277. return (0);
  1278. }
  1279. static char * pid_to_name (pid_t pid, char *out_name, size_t name_len)
  1280. {
  1281. char *name;
  1282. char *rest;
  1283. FILE *fp;
  1284. char fname[32];
  1285. char buf[256];
  1286. snprintf (fname, 32, "/proc/%d/stat", pid);
  1287. fp = fopen (fname, "r");
  1288. if (!fp) {
  1289. return NULL;
  1290. }
  1291. if (fgets (buf, sizeof (buf), fp) == NULL) {
  1292. fclose (fp);
  1293. return NULL;
  1294. }
  1295. fclose (fp);
  1296. name = strrchr (buf, '(');
  1297. if (!name) {
  1298. return NULL;
  1299. }
  1300. /* move past the bracket */
  1301. name++;
  1302. rest = strrchr (buf, ')');
  1303. if (rest == NULL || rest[1] != ' ') {
  1304. return NULL;
  1305. }
  1306. *rest = '\0';
  1307. /* move past the NULL and space */
  1308. rest += 2;
  1309. /* copy the name */
  1310. strncpy (out_name, name, name_len);
  1311. out_name[name_len - 1] = '\0';
  1312. return out_name;
  1313. }
  1314. static void coroipcs_init_conn_stats (
  1315. struct conn_info *conn)
  1316. {
  1317. char conn_name[CS_MAX_NAME_LENGTH];
  1318. char proc_name[CS_MAX_NAME_LENGTH];
  1319. char int_str[4];
  1320. if (conn->client_pid > 0) {
  1321. if (pid_to_name (conn->client_pid, proc_name, sizeof(proc_name))) {
  1322. snprintf (conn_name, sizeof(conn_name),
  1323. "%s:%s:%d:%d", proc_name,
  1324. short_service_name_get(conn->service, int_str, 4),
  1325. conn->client_pid, conn->fd);
  1326. } else {
  1327. snprintf (conn_name, sizeof(conn_name),
  1328. "proc:%s:%d:%d",
  1329. short_service_name_get(conn->service, int_str, 4),
  1330. conn->client_pid,
  1331. conn->fd);
  1332. }
  1333. } else {
  1334. snprintf (conn_name, sizeof(conn_name),
  1335. "proc:%s:pid:%d",
  1336. short_service_name_get(conn->service, int_str, 4),
  1337. conn->fd);
  1338. }
  1339. conn->stats_handle = api->stats_create_connection (conn_name, conn->client_pid, conn->fd);
  1340. api->stats_update_value (conn->stats_handle, "service_id",
  1341. &conn->service, sizeof(conn->service));
  1342. }
  1343. int coroipcs_handler_dispatch (
  1344. int fd,
  1345. int revent,
  1346. void *context)
  1347. {
  1348. mar_req_setup_t *req_setup;
  1349. struct conn_info *conn_info = (struct conn_info *)context;
  1350. int res;
  1351. char buf = 0;
  1352. if (ipc_thread_exiting (conn_info)) {
  1353. return conn_info_destroy (conn_info);
  1354. }
  1355. /*
  1356. * If an error occurs, request exit
  1357. */
  1358. if (revent & (POLLERR|POLLHUP)) {
  1359. ipc_disconnect (conn_info);
  1360. return (0);
  1361. }
  1362. /*
  1363. * Read the header and process it
  1364. */
  1365. if (conn_info->service == SOCKET_SERVICE_INIT && (revent & POLLIN)) {
  1366. pthread_attr_t thread_attr;
  1367. /*
  1368. * Receive in a nonblocking fashion the request
  1369. * IF security invalid, send ERR_SECURITY, otherwise
  1370. * send OK
  1371. */
  1372. res = req_setup_recv (conn_info);
  1373. if (res != CS_OK && res != CS_ERR_LIBRARY) {
  1374. req_setup_send (conn_info, res);
  1375. }
  1376. if (res != CS_OK) {
  1377. return (0);
  1378. }
  1379. pthread_mutex_init (&conn_info->mutex, NULL);
  1380. req_setup = (mar_req_setup_t *)conn_info->setup_msg;
  1381. /*
  1382. * Is the service registered ?
  1383. * Has service init function ?
  1384. */
  1385. if (api->service_available (req_setup->service) == 0 ||
  1386. api->init_fn_get (req_setup->service) == NULL) {
  1387. req_setup_send (conn_info, CS_ERR_NOT_EXIST);
  1388. ipc_disconnect (conn_info);
  1389. return (0);
  1390. }
  1391. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1392. conn_info->semkey = req_setup->semkey;
  1393. #endif
  1394. res = memory_map (
  1395. req_setup->control_file,
  1396. req_setup->control_size,
  1397. (void *)&conn_info->control_buffer);
  1398. if (res == -1) {
  1399. goto send_setup_response;
  1400. }
  1401. conn_info->control_size = req_setup->control_size;
  1402. res = memory_map (
  1403. req_setup->request_file,
  1404. req_setup->request_size,
  1405. (void *)&conn_info->request_buffer);
  1406. if (res == -1) {
  1407. goto send_setup_response;
  1408. }
  1409. conn_info->request_size = req_setup->request_size;
  1410. res = memory_map (
  1411. req_setup->response_file,
  1412. req_setup->response_size,
  1413. (void *)&conn_info->response_buffer);
  1414. if (res == -1) {
  1415. goto send_setup_response;
  1416. }
  1417. conn_info->response_size = req_setup->response_size;
  1418. res = circular_memory_map (
  1419. req_setup->dispatch_file,
  1420. req_setup->dispatch_size,
  1421. (void *)&conn_info->dispatch_buffer);
  1422. if (res == -1) {
  1423. goto send_setup_response;
  1424. }
  1425. conn_info->dispatch_size = req_setup->dispatch_size;
  1426. send_setup_response:
  1427. if (res == 0) {
  1428. req_setup_send (conn_info, CS_OK);
  1429. } else {
  1430. req_setup_send (conn_info, CS_ERR_LIBRARY);
  1431. ipc_disconnect (conn_info);
  1432. return (0);
  1433. }
  1434. conn_info->service = req_setup->service;
  1435. conn_info->refcount = 0;
  1436. conn_info->setup_bytes_read = 0;
  1437. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1438. conn_info->control_buffer->semid = semget (conn_info->semkey, 3, 0600);
  1439. #endif
  1440. conn_info->pending_semops = 0;
  1441. /*
  1442. * ipc thread is the only reference at startup
  1443. */
  1444. conn_info->refcount = 1;
  1445. conn_info->state = CONN_STATE_THREAD_ACTIVE;
  1446. conn_info->private_data = api->malloc (api->private_data_size_get (conn_info->service));
  1447. memset (conn_info->private_data, 0,
  1448. api->private_data_size_get (conn_info->service));
  1449. api->init_fn_get (conn_info->service) (conn_info);
  1450. /* create stats objects */
  1451. coroipcs_init_conn_stats (conn_info);
  1452. pthread_attr_init (&thread_attr);
  1453. /*
  1454. * IA64 needs more stack space then other arches
  1455. */
  1456. #if defined(__ia64__)
  1457. pthread_attr_setstacksize (&thread_attr, 400000);
  1458. #else
  1459. pthread_attr_setstacksize (&thread_attr, 200000);
  1460. #endif
  1461. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_JOINABLE);
  1462. res = pthread_create (&conn_info->thread,
  1463. &thread_attr,
  1464. pthread_ipc_consumer,
  1465. conn_info);
  1466. pthread_attr_destroy (&thread_attr);
  1467. /*
  1468. * Security check - disallow multiple configurations of
  1469. * the ipc connection
  1470. */
  1471. if (conn_info->service == SOCKET_SERVICE_INIT) {
  1472. conn_info->service = SOCKET_SERVICE_SECURITY_VIOLATION;
  1473. }
  1474. } else
  1475. if (revent & POLLIN) {
  1476. coroipcs_refcount_inc (conn_info);
  1477. res = recv (fd, &buf, 1, MSG_NOSIGNAL);
  1478. if (res == 1) {
  1479. switch (buf) {
  1480. case MESSAGE_REQ_CHANGE_EUID:
  1481. if (priv_change (conn_info) == -1) {
  1482. ipc_disconnect (conn_info);
  1483. }
  1484. break;
  1485. default:
  1486. res = 0;
  1487. break;
  1488. }
  1489. }
  1490. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1491. /* On many OS poll never return POLLHUP or POLLERR.
  1492. * EOF is detected when recvmsg return 0.
  1493. */
  1494. if (res == 0) {
  1495. ipc_disconnect (conn_info);
  1496. coroipcs_refcount_dec (conn_info);
  1497. return (0);
  1498. }
  1499. #endif
  1500. coroipcs_refcount_dec (conn_info);
  1501. }
  1502. if (revent & POLLOUT) {
  1503. int psop = conn_info->pending_semops;
  1504. int i;
  1505. assert (psop != 0);
  1506. for (i = 0; i < psop; i++) {
  1507. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1508. if (res != 1) {
  1509. return (0);
  1510. } else {
  1511. conn_info->pending_semops -= 1;
  1512. }
  1513. }
  1514. if (conn_info->poll_state == POLL_STATE_INOUT) {
  1515. conn_info->poll_state = POLL_STATE_IN;
  1516. api->poll_dispatch_modify (conn_info->fd, POLLIN|POLLNVAL);
  1517. }
  1518. }
  1519. return (0);
  1520. }