coroipcs.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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. #ifndef MSG_NOSIGNAL
  82. #define MSG_NOSIGNAL 0
  83. #endif
  84. #define SERVER_BACKLOG 5
  85. #define MSG_SEND_LOCKED 0
  86. #define MSG_SEND_UNLOCKED 1
  87. #define POLL_STATE_IN 1
  88. #define POLL_STATE_INOUT 2
  89. static struct coroipcs_init_state_v2 *api = NULL;
  90. DECLARE_LIST_INIT (conn_info_list_head);
  91. DECLARE_LIST_INIT (conn_info_exit_list_head);
  92. struct outq_item {
  93. void *msg;
  94. size_t mlen;
  95. struct list_head list;
  96. };
  97. struct zcb_mapped {
  98. struct list_head list;
  99. void *addr;
  100. size_t size;
  101. };
  102. #if _POSIX_THREAD_PROCESS_SHARED < 1
  103. #if defined(_SEM_SEMUN_UNDEFINED)
  104. union semun {
  105. int val;
  106. struct semid_ds *buf;
  107. unsigned short int *array;
  108. struct seminfo *__buf;
  109. };
  110. #endif
  111. #endif
  112. enum conn_state {
  113. CONN_STATE_THREAD_INACTIVE = 0,
  114. CONN_STATE_THREAD_ACTIVE = 1,
  115. CONN_STATE_THREAD_REQUEST_EXIT = 2,
  116. CONN_STATE_THREAD_DESTROYED = 3,
  117. CONN_STATE_LIB_EXIT_CALLED = 4,
  118. CONN_STATE_DISCONNECT_INACTIVE = 5
  119. };
  120. struct conn_info {
  121. int fd;
  122. pthread_t thread;
  123. pid_t client_pid;
  124. pthread_attr_t thread_attr;
  125. unsigned int service;
  126. enum conn_state state;
  127. int refcount;
  128. hdb_handle_t stats_handle;
  129. #if _POSIX_THREAD_PROCESS_SHARED < 1
  130. key_t semkey;
  131. #endif
  132. unsigned int pending_semops;
  133. pthread_mutex_t mutex;
  134. struct control_buffer *control_buffer;
  135. char *request_buffer;
  136. char *response_buffer;
  137. char *dispatch_buffer;
  138. size_t control_size;
  139. size_t request_size;
  140. size_t response_size;
  141. size_t dispatch_size;
  142. struct list_head outq_head;
  143. void *private_data;
  144. struct list_head list;
  145. char setup_msg[sizeof (mar_req_setup_t)];
  146. unsigned int setup_bytes_read;
  147. struct list_head zcb_mapped_list_head;
  148. char *sending_allowed_private_data[64];
  149. int poll_state;
  150. };
  151. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info);
  152. static void outq_flush (struct conn_info *conn_info);
  153. static int priv_change (struct conn_info *conn_info);
  154. static void ipc_disconnect (struct conn_info *conn_info);
  155. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  156. int locked);
  157. static void _corosync_ipc_init(void);
  158. #define log_printf(level, format, args...) \
  159. do { \
  160. if (api->log_printf) \
  161. api->log_printf ( \
  162. LOGSYS_ENCODE_RECID(level, \
  163. api->log_subsys_id, \
  164. LOGSYS_RECID_LOG), \
  165. __FUNCTION__, __FILE__, __LINE__, \
  166. (const char *)format, ##args); \
  167. else \
  168. api->old_log_printf ((const char *)format, ##args); \
  169. } while (0)
  170. static hdb_handle_t dummy_stats_create_connection (
  171. const char *name,
  172. pid_t pid,
  173. int fd)
  174. {
  175. return (0ULL);
  176. }
  177. static void dummy_stats_destroy_connection (
  178. hdb_handle_t handle)
  179. {
  180. }
  181. static void dummy_stats_update_value (
  182. hdb_handle_t handle,
  183. const char *name,
  184. const void *value,
  185. size_t value_size)
  186. {
  187. }
  188. static void dummy_stats_increment_value (
  189. hdb_handle_t handle,
  190. const char *name)
  191. {
  192. }
  193. static int
  194. memory_map (
  195. const char *path,
  196. size_t bytes,
  197. void **buf)
  198. {
  199. int32_t fd;
  200. void *addr_orig;
  201. void *addr;
  202. int32_t res;
  203. fd = open (path, O_RDWR, 0600);
  204. unlink (path);
  205. if (fd == -1) {
  206. return (-1);
  207. }
  208. res = ftruncate (fd, bytes);
  209. if (res == -1) {
  210. goto error_close_unlink;
  211. }
  212. addr_orig = mmap (NULL, bytes, PROT_NONE,
  213. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  214. if (addr_orig == MAP_FAILED) {
  215. goto error_close_unlink;
  216. }
  217. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  218. MAP_FIXED | MAP_SHARED, fd, 0);
  219. if (addr != addr_orig) {
  220. munmap(addr_orig, bytes);
  221. goto error_close_unlink;
  222. }
  223. #ifdef COROSYNC_BSD
  224. madvise(addr, bytes, MADV_NOSYNC);
  225. #endif
  226. res = close (fd);
  227. if (res) {
  228. return (-1);
  229. }
  230. *buf = addr_orig;
  231. return (0);
  232. error_close_unlink:
  233. close (fd);
  234. unlink(path);
  235. return -1;
  236. }
  237. static int
  238. circular_memory_map (
  239. const char *path,
  240. size_t bytes,
  241. void **buf)
  242. {
  243. int32_t fd;
  244. void *addr_orig;
  245. void *addr;
  246. int32_t res;
  247. fd = open (path, O_RDWR, 0600);
  248. unlink (path);
  249. if (fd == -1) {
  250. return (-1);
  251. }
  252. res = ftruncate (fd, bytes);
  253. if (res == -1) {
  254. goto error_close_unlink;
  255. }
  256. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  257. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  258. if (addr_orig == MAP_FAILED) {
  259. munmap(addr_orig, bytes);
  260. goto error_close_unlink;
  261. }
  262. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  263. MAP_FIXED | MAP_SHARED, fd, 0);
  264. if (addr != addr_orig) {
  265. munmap(addr_orig, bytes);
  266. goto error_close_unlink;
  267. }
  268. #ifdef COROSYNC_BSD
  269. madvise(addr_orig, bytes, MADV_NOSYNC);
  270. #endif
  271. addr = mmap (((char *)addr_orig) + bytes,
  272. bytes, PROT_READ | PROT_WRITE,
  273. MAP_FIXED | MAP_SHARED, fd, 0);
  274. if (addr == MAP_FAILED) {
  275. munmap(addr_orig, bytes);
  276. munmap(addr, bytes);
  277. goto error_close_unlink;
  278. }
  279. #ifdef COROSYNC_BSD
  280. madvise(((char *)addr_orig) + bytes, bytes, MADV_NOSYNC);
  281. #endif
  282. res = close (fd);
  283. if (res) {
  284. munmap(addr_orig, bytes);
  285. munmap(addr, bytes);
  286. return (-1);
  287. }
  288. *buf = addr_orig;
  289. return (0);
  290. error_close_unlink:
  291. close (fd);
  292. unlink(path);
  293. return (-1);
  294. }
  295. static inline int
  296. circular_memory_unmap (void *buf, size_t bytes)
  297. {
  298. int res;
  299. res = munmap (buf, bytes << 1);
  300. return (res);
  301. }
  302. static void flow_control_state_set (
  303. struct conn_info *conn_info,
  304. int flow_control_state)
  305. {
  306. if (conn_info->control_buffer->flow_control_enabled == flow_control_state) {
  307. return;
  308. }
  309. if (flow_control_state == 0) {
  310. log_printf (LOGSYS_LEVEL_DEBUG,
  311. "Disabling flow control for %d\n",
  312. conn_info->client_pid);
  313. } else
  314. if (flow_control_state == 1) {
  315. log_printf (LOGSYS_LEVEL_DEBUG,
  316. "Enabling flow control for %d\n",
  317. conn_info->client_pid);
  318. }
  319. conn_info->control_buffer->flow_control_enabled = flow_control_state;
  320. api->stats_update_value (conn_info->stats_handle,
  321. "flow_control",
  322. &flow_control_state,
  323. sizeof(flow_control_state));
  324. api->stats_increment_value (conn_info->stats_handle,
  325. "flow_control_count");
  326. }
  327. static inline int zcb_free (struct zcb_mapped *zcb_mapped)
  328. {
  329. unsigned int res;
  330. res = munmap (zcb_mapped->addr, zcb_mapped->size);
  331. list_del (&zcb_mapped->list);
  332. free (zcb_mapped);
  333. return (res);
  334. }
  335. static inline int zcb_by_addr_free (struct conn_info *conn_info, void *addr)
  336. {
  337. struct list_head *list;
  338. struct zcb_mapped *zcb_mapped;
  339. unsigned int res = 0;
  340. for (list = conn_info->zcb_mapped_list_head.next;
  341. list != &conn_info->zcb_mapped_list_head; list = list->next) {
  342. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  343. if (zcb_mapped->addr == addr) {
  344. res = zcb_free (zcb_mapped);
  345. break;
  346. }
  347. }
  348. return (res);
  349. }
  350. static inline int zcb_all_free (
  351. struct conn_info *conn_info)
  352. {
  353. struct list_head *list;
  354. struct zcb_mapped *zcb_mapped;
  355. for (list = conn_info->zcb_mapped_list_head.next;
  356. list != &conn_info->zcb_mapped_list_head;) {
  357. zcb_mapped = list_entry (list, struct zcb_mapped, list);
  358. list = list->next;
  359. zcb_free (zcb_mapped);
  360. }
  361. return (0);
  362. }
  363. static inline int zcb_alloc (
  364. struct conn_info *conn_info,
  365. const char *path_to_file,
  366. size_t size,
  367. void **addr)
  368. {
  369. struct zcb_mapped *zcb_mapped;
  370. unsigned int res;
  371. zcb_mapped = malloc (sizeof (struct zcb_mapped));
  372. if (zcb_mapped == NULL) {
  373. return (-1);
  374. }
  375. res = memory_map (
  376. path_to_file,
  377. size,
  378. addr);
  379. if (res == -1) {
  380. free (zcb_mapped);
  381. return (-1);
  382. }
  383. list_init (&zcb_mapped->list);
  384. zcb_mapped->addr = *addr;
  385. zcb_mapped->size = size;
  386. list_add_tail (&zcb_mapped->list, &conn_info->zcb_mapped_list_head);
  387. return (0);
  388. }
  389. static int ipc_thread_active (void *conn)
  390. {
  391. struct conn_info *conn_info = (struct conn_info *)conn;
  392. int retval = 0;
  393. pthread_mutex_lock (&conn_info->mutex);
  394. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  395. retval = 1;
  396. }
  397. pthread_mutex_unlock (&conn_info->mutex);
  398. return (retval);
  399. }
  400. static int ipc_thread_exiting (void *conn)
  401. {
  402. struct conn_info *conn_info = (struct conn_info *)conn;
  403. int retval = 1;
  404. pthread_mutex_lock (&conn_info->mutex);
  405. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  406. retval = 0;
  407. } else
  408. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  409. retval = 0;
  410. }
  411. pthread_mutex_unlock (&conn_info->mutex);
  412. return (retval);
  413. }
  414. /*
  415. * returns 0 if should be called again, -1 if finished
  416. */
  417. static inline int conn_info_destroy (struct conn_info *conn_info)
  418. {
  419. unsigned int res;
  420. void *retval;
  421. list_del (&conn_info->list);
  422. list_init (&conn_info->list);
  423. list_add (&conn_info->list, &conn_info_exit_list_head);
  424. if (conn_info->state == CONN_STATE_THREAD_REQUEST_EXIT) {
  425. res = pthread_join (conn_info->thread, &retval);
  426. conn_info->state = CONN_STATE_THREAD_DESTROYED;
  427. return (0);
  428. }
  429. if (conn_info->state == CONN_STATE_THREAD_INACTIVE ||
  430. conn_info->state == CONN_STATE_DISCONNECT_INACTIVE) {
  431. list_del (&conn_info->list);
  432. close (conn_info->fd);
  433. api->free (conn_info);
  434. return (-1);
  435. }
  436. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  437. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT);
  438. return (0);
  439. }
  440. api->serialize_lock ();
  441. /*
  442. * Retry library exit function if busy
  443. */
  444. if (conn_info->state == CONN_STATE_THREAD_DESTROYED) {
  445. api->stats_destroy_connection (conn_info->stats_handle);
  446. res = api->exit_fn_get (conn_info->service) (conn_info);
  447. if (res == -1) {
  448. api->serialize_unlock ();
  449. return (0);
  450. } else {
  451. conn_info->state = CONN_STATE_LIB_EXIT_CALLED;
  452. }
  453. }
  454. pthread_mutex_lock (&conn_info->mutex);
  455. if (conn_info->refcount > 0) {
  456. pthread_mutex_unlock (&conn_info->mutex);
  457. api->serialize_unlock ();
  458. return (0);
  459. }
  460. list_del (&conn_info->list);
  461. pthread_mutex_unlock (&conn_info->mutex);
  462. #if _POSIX_THREAD_PROCESS_SHARED > 0
  463. sem_destroy (&conn_info->control_buffer->sem_request_or_flush_or_exit);
  464. sem_destroy (&conn_info->control_buffer->sem_request);
  465. sem_destroy (&conn_info->control_buffer->sem_response);
  466. sem_destroy (&conn_info->control_buffer->sem_dispatch);
  467. #else
  468. semctl (conn_info->control_buffer->semid, 0, IPC_RMID);
  469. #endif
  470. /*
  471. * Destroy shared memory segment and semaphore
  472. */
  473. res = munmap ((void *)conn_info->control_buffer, conn_info->control_size);
  474. res = munmap ((void *)conn_info->request_buffer, conn_info->request_size);
  475. res = munmap ((void *)conn_info->response_buffer, conn_info->response_size);
  476. /*
  477. * Free allocated data needed to retry exiting library IPC connection
  478. */
  479. if (conn_info->private_data) {
  480. api->free (conn_info->private_data);
  481. }
  482. close (conn_info->fd);
  483. res = circular_memory_unmap (conn_info->dispatch_buffer, conn_info->dispatch_size);
  484. zcb_all_free (conn_info);
  485. api->free (conn_info);
  486. api->serialize_unlock ();
  487. return (-1);
  488. }
  489. union u {
  490. uint64_t server_addr;
  491. void *server_ptr;
  492. };
  493. static uint64_t void2serveraddr (void *server_ptr)
  494. {
  495. union u u;
  496. u.server_ptr = server_ptr;
  497. return (u.server_addr);
  498. }
  499. static void *serveraddr2void (uint64_t server_addr)
  500. {
  501. union u u;
  502. u.server_addr = server_addr;
  503. return (u.server_ptr);
  504. };
  505. static inline void zerocopy_operations_process (
  506. struct conn_info *conn_info,
  507. coroipc_request_header_t **header_out,
  508. unsigned int *new_message)
  509. {
  510. coroipc_request_header_t *header;
  511. header = (coroipc_request_header_t *)conn_info->request_buffer;
  512. if (header->id == ZC_ALLOC_HEADER) {
  513. mar_req_coroipcc_zc_alloc_t *hdr = (mar_req_coroipcc_zc_alloc_t *)header;
  514. coroipc_response_header_t res_header;
  515. void *addr = NULL;
  516. struct coroipcs_zc_header *zc_header;
  517. unsigned int res;
  518. res = zcb_alloc (conn_info, hdr->path_to_file, hdr->map_size,
  519. &addr);
  520. zc_header = (struct coroipcs_zc_header *)addr;
  521. zc_header->server_address = void2serveraddr(addr);
  522. res_header.size = sizeof (coroipc_response_header_t);
  523. res_header.id = 0;
  524. coroipcs_response_send (
  525. conn_info, &res_header,
  526. res_header.size);
  527. *new_message = 0;
  528. return;
  529. } else
  530. if (header->id == ZC_FREE_HEADER) {
  531. mar_req_coroipcc_zc_free_t *hdr = (mar_req_coroipcc_zc_free_t *)header;
  532. coroipc_response_header_t res_header;
  533. void *addr = NULL;
  534. addr = serveraddr2void (hdr->server_address);
  535. zcb_by_addr_free (conn_info, addr);
  536. res_header.size = sizeof (coroipc_response_header_t);
  537. res_header.id = 0;
  538. coroipcs_response_send (
  539. conn_info, &res_header,
  540. res_header.size);
  541. *new_message = 0;
  542. return;
  543. } else
  544. if (header->id == ZC_EXECUTE_HEADER) {
  545. mar_req_coroipcc_zc_execute_t *hdr = (mar_req_coroipcc_zc_execute_t *)header;
  546. header = (coroipc_request_header_t *)(((char *)serveraddr2void(hdr->server_address) + sizeof (struct coroipcs_zc_header)));
  547. }
  548. *header_out = header;
  549. *new_message = 1;
  550. }
  551. static void *pthread_ipc_consumer (void *conn)
  552. {
  553. struct conn_info *conn_info = (struct conn_info *)conn;
  554. int res;
  555. coroipc_request_header_t *header;
  556. coroipc_response_header_t coroipc_response_header;
  557. int send_ok;
  558. unsigned int new_message;
  559. int sem_value = 0;
  560. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
  561. if (api->sched_policy != 0) {
  562. res = pthread_setschedparam (conn_info->thread,
  563. api->sched_policy, api->sched_param);
  564. }
  565. #endif
  566. for (;;) {
  567. ipc_sem_wait (conn_info->control_buffer, SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT, IPC_SEMWAIT_NOFILE);
  568. if (ipc_thread_active (conn_info) == 0) {
  569. coroipcs_refcount_dec (conn_info);
  570. pthread_exit (0);
  571. }
  572. outq_flush (conn_info);
  573. ipc_sem_getvalue (conn_info->control_buffer, SEMAPHORE_REQUEST, &sem_value);
  574. if (sem_value > 0) {
  575. res = ipc_sem_wait (conn_info->control_buffer, SEMAPHORE_REQUEST, IPC_SEMWAIT_NOFILE);
  576. } else {
  577. continue;
  578. }
  579. zerocopy_operations_process (conn_info, &header, &new_message);
  580. /*
  581. * There is no new message to process, continue for loop
  582. */
  583. if (new_message == 0) {
  584. continue;
  585. }
  586. coroipcs_refcount_inc (conn);
  587. send_ok = api->sending_allowed (conn_info->service,
  588. header->id,
  589. header,
  590. conn_info->sending_allowed_private_data);
  591. /*
  592. * This happens when the message contains some kind of invalid
  593. * parameter, such as an invalid size
  594. */
  595. if (send_ok == -1) {
  596. coroipc_response_header.size = sizeof (coroipc_response_header_t);
  597. coroipc_response_header.id = 0;
  598. coroipc_response_header.error = CS_ERR_INVALID_PARAM;
  599. coroipcs_response_send (conn_info,
  600. &coroipc_response_header,
  601. sizeof (coroipc_response_header_t));
  602. } else
  603. if (send_ok) {
  604. api->serialize_lock();
  605. api->stats_increment_value (conn_info->stats_handle, "requests");
  606. api->handler_fn_get (conn_info->service, header->id) (conn_info, header);
  607. api->serialize_unlock();
  608. } else {
  609. /*
  610. * Overload, tell library to retry
  611. */
  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 int
  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. int authenticated = 0;
  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 (0);
  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 0;
  694. #else
  695. return (-1);
  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. authenticated = 1;
  717. }
  718. ucred_free(uc);
  719. }
  720. }
  721. #elif HAVE_GETPEEREID
  722. /*
  723. * Usually MacOSX systems
  724. */
  725. {
  726. uid_t euid;
  727. gid_t egid;
  728. /*
  729. * TODO get the peer's pid.
  730. * conn_info->client_pid = ?;
  731. */
  732. euid = -1;
  733. egid = -1;
  734. if (getpeereid (conn_info->fd, &euid, &egid) == 0) {
  735. if (api->security_valid (euid, egid)) {
  736. authenticated = 1;
  737. }
  738. }
  739. }
  740. #elif SO_PASSCRED
  741. /*
  742. * Usually Linux systems
  743. */
  744. cmsg = CMSG_FIRSTHDR (&msg_recv);
  745. assert (cmsg);
  746. cred = (struct ucred *)CMSG_DATA (cmsg);
  747. if (cred) {
  748. conn_info->client_pid = cred->pid;
  749. if (api->security_valid (cred->uid, cred->gid)) {
  750. authenticated = 1;
  751. }
  752. }
  753. #else /* no credentials */
  754. authenticated = 1;
  755. log_printf (LOGSYS_LEVEL_ERROR, "Platform does not support IPC authentication. Using no authentication\n");
  756. #endif /* no credentials */
  757. if (authenticated == 0) {
  758. log_printf (LOGSYS_LEVEL_ERROR, "Invalid IPC credentials.\n");
  759. ipc_disconnect (conn_info);
  760. return (-1);
  761. }
  762. if (conn_info->setup_bytes_read == sizeof (mar_req_setup_t)) {
  763. #ifdef COROSYNC_LINUX
  764. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED,
  765. &off, sizeof (off));
  766. #endif
  767. return (1);
  768. }
  769. return (0);
  770. }
  771. static void ipc_disconnect (struct conn_info *conn_info)
  772. {
  773. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  774. conn_info->state = CONN_STATE_DISCONNECT_INACTIVE;
  775. return;
  776. }
  777. if (conn_info->state != CONN_STATE_THREAD_ACTIVE) {
  778. return;
  779. }
  780. pthread_mutex_lock (&conn_info->mutex);
  781. conn_info->state = CONN_STATE_THREAD_REQUEST_EXIT;
  782. pthread_mutex_unlock (&conn_info->mutex);
  783. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_REQUEST_OR_FLUSH_OR_EXIT);
  784. }
  785. static int conn_info_create (int fd)
  786. {
  787. struct conn_info *conn_info;
  788. conn_info = api->malloc (sizeof (struct conn_info));
  789. if (conn_info == NULL) {
  790. return (-1);
  791. }
  792. memset (conn_info, 0, sizeof (struct conn_info));
  793. conn_info->fd = fd;
  794. conn_info->client_pid = 0;
  795. conn_info->service = SOCKET_SERVICE_INIT;
  796. conn_info->state = CONN_STATE_THREAD_INACTIVE;
  797. conn_info->poll_state = POLL_STATE_IN;
  798. list_init (&conn_info->outq_head);
  799. list_init (&conn_info->list);
  800. list_init (&conn_info->zcb_mapped_list_head);
  801. list_add (&conn_info->list, &conn_info_list_head);
  802. api->poll_dispatch_add (fd, conn_info);
  803. return (0);
  804. }
  805. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  806. /* SUN_LEN is broken for abstract namespace
  807. */
  808. #define COROSYNC_SUN_LEN(a) sizeof(*(a))
  809. #else
  810. #define COROSYNC_SUN_LEN(a) SUN_LEN(a)
  811. #endif
  812. /*
  813. * Exported functions
  814. */
  815. extern void coroipcs_ipc_init_v2 (
  816. struct coroipcs_init_state_v2 *init_state_v2)
  817. {
  818. api = init_state_v2;
  819. api->old_log_printf = NULL;
  820. log_printf (LOGSYS_LEVEL_DEBUG, "you are using ipc api v2\n");
  821. _corosync_ipc_init ();
  822. }
  823. extern void coroipcs_ipc_init (
  824. struct coroipcs_init_state *init_state)
  825. {
  826. api = calloc (sizeof(struct coroipcs_init_state_v2), 1);
  827. /* v2 api */
  828. api->stats_create_connection = dummy_stats_create_connection;
  829. api->stats_destroy_connection = dummy_stats_destroy_connection;
  830. api->stats_update_value = dummy_stats_update_value;
  831. api->stats_increment_value = dummy_stats_increment_value;
  832. api->log_printf = NULL;
  833. /* v1 api */
  834. api->socket_name = init_state->socket_name;
  835. api->sched_policy = init_state->sched_policy;
  836. api->sched_param = init_state->sched_param;
  837. api->malloc = init_state->malloc;
  838. api->free = init_state->free;
  839. api->old_log_printf = init_state->log_printf;
  840. api->fatal_error = init_state->fatal_error;
  841. api->security_valid = init_state->security_valid;
  842. api->service_available = init_state->service_available;
  843. api->private_data_size_get = init_state->private_data_size_get;
  844. api->serialize_lock = init_state->serialize_lock;
  845. api->serialize_unlock = init_state->serialize_unlock;
  846. api->sending_allowed = init_state->sending_allowed;
  847. api->sending_allowed_release = init_state->sending_allowed_release;
  848. api->poll_accept_add = init_state->poll_accept_add;
  849. api->poll_dispatch_add = init_state->poll_dispatch_add;
  850. api->poll_dispatch_modify = init_state->poll_dispatch_modify;
  851. api->init_fn_get = init_state->init_fn_get;
  852. api->exit_fn_get = init_state->exit_fn_get;
  853. api->handler_fn_get = init_state->handler_fn_get;
  854. log_printf (LOGSYS_LEVEL_DEBUG, "you are using ipc api v1\n");
  855. _corosync_ipc_init ();
  856. }
  857. static void _corosync_ipc_init(void)
  858. {
  859. int server_fd;
  860. struct sockaddr_un un_addr;
  861. int res;
  862. /*
  863. * Create socket for IPC clients, name socket, listen for connections
  864. */
  865. #if defined(COROSYNC_SOLARIS)
  866. server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  867. #else
  868. server_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  869. #endif
  870. if (server_fd == -1) {
  871. log_printf (LOGSYS_LEVEL_CRIT, "Cannot create client connections socket.\n");
  872. api->fatal_error ("Can't create library listen socket");
  873. }
  874. res = fcntl (server_fd, F_SETFL, O_NONBLOCK);
  875. if (res == -1) {
  876. char error_str[100];
  877. strerror_r (errno, error_str, 100);
  878. log_printf (LOGSYS_LEVEL_CRIT, "Could not set non-blocking operation on server socket: %s\n", error_str);
  879. api->fatal_error ("Could not set non-blocking operation on server socket");
  880. }
  881. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  882. un_addr.sun_family = AF_UNIX;
  883. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  884. un_addr.sun_len = SUN_LEN(&un_addr);
  885. #endif
  886. #if defined(COROSYNC_LINUX)
  887. sprintf (un_addr.sun_path + 1, "%s", api->socket_name);
  888. #else
  889. {
  890. struct stat stat_out;
  891. res = stat (SOCKETDIR, &stat_out);
  892. if (res == -1 || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  893. log_printf (LOGSYS_LEVEL_CRIT, "Required directory not present %s\n", SOCKETDIR);
  894. api->fatal_error ("Please create required directory.");
  895. }
  896. sprintf (un_addr.sun_path, "%s/%s", SOCKETDIR, api->socket_name);
  897. unlink (un_addr.sun_path);
  898. }
  899. #endif
  900. res = bind (server_fd, (struct sockaddr *)&un_addr, COROSYNC_SUN_LEN(&un_addr));
  901. if (res) {
  902. char error_str[100];
  903. strerror_r (errno, error_str, 100);
  904. log_printf (LOGSYS_LEVEL_CRIT, "Could not bind AF_UNIX (%s): %s.\n", un_addr.sun_path, error_str);
  905. api->fatal_error ("Could not bind to AF_UNIX socket\n");
  906. }
  907. /*
  908. * Allow eveyrone to write to the socket since the IPC layer handles
  909. * security automatically
  910. */
  911. #if !defined(COROSYNC_LINUX)
  912. res = chmod (un_addr.sun_path, S_IRWXU|S_IRWXG|S_IRWXO);
  913. #endif
  914. listen (server_fd, SERVER_BACKLOG);
  915. /*
  916. * Setup connection dispatch routine
  917. */
  918. api->poll_accept_add (server_fd);
  919. }
  920. void coroipcs_ipc_exit (void)
  921. {
  922. struct list_head *list;
  923. struct conn_info *conn_info;
  924. unsigned int res;
  925. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  926. list = list->next) {
  927. conn_info = list_entry (list, struct conn_info, list);
  928. if (conn_info->state != CONN_STATE_THREAD_ACTIVE)
  929. continue;
  930. ipc_disconnect (conn_info);
  931. #if _POSIX_THREAD_PROCESS_SHARED > 0
  932. sem_destroy (&conn_info->control_buffer->sem_request_or_flush_or_exit);
  933. sem_destroy (&conn_info->control_buffer->sem_request);
  934. sem_destroy (&conn_info->control_buffer->sem_response);
  935. sem_destroy (&conn_info->control_buffer->sem_dispatch);
  936. #else
  937. semctl (conn_info->control_buffer->semid, 0, IPC_RMID);
  938. #endif
  939. /*
  940. * Unmap memory segments
  941. */
  942. res = munmap ((void *)conn_info->control_buffer,
  943. conn_info->control_size);
  944. res = munmap ((void *)conn_info->request_buffer,
  945. conn_info->request_size);
  946. res = munmap ((void *)conn_info->response_buffer,
  947. conn_info->response_size);
  948. res = circular_memory_unmap (conn_info->dispatch_buffer,
  949. conn_info->dispatch_size);
  950. }
  951. }
  952. int coroipcs_ipc_service_exit (unsigned int service)
  953. {
  954. struct list_head *list, *list_next;
  955. struct conn_info *conn_info;
  956. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  957. list = list_next) {
  958. list_next = list->next;
  959. conn_info = list_entry (list, struct conn_info, list);
  960. if (conn_info->service != service ||
  961. (conn_info->state != CONN_STATE_THREAD_ACTIVE && conn_info->state != CONN_STATE_THREAD_REQUEST_EXIT)) {
  962. continue;
  963. }
  964. ipc_disconnect (conn_info);
  965. api->poll_dispatch_destroy (conn_info->fd, NULL);
  966. while (conn_info_destroy (conn_info) != -1)
  967. ;
  968. /*
  969. * We will return to prevent token loss. Schedwrk will call us again.
  970. */
  971. return (-1);
  972. }
  973. /*
  974. * No conn info left in active list. We will traverse thru exit list. If there is any
  975. * conn_info->service == service, we will wait to proper end -> return -1
  976. */
  977. for (list = conn_info_exit_list_head.next; list != &conn_info_exit_list_head; list = list->next) {
  978. conn_info = list_entry (list, struct conn_info, list);
  979. if (conn_info->service == service) {
  980. return (-1);
  981. }
  982. }
  983. return (0);
  984. }
  985. /*
  986. * Get the conn info private data
  987. */
  988. void *coroipcs_private_data_get (void *conn)
  989. {
  990. struct conn_info *conn_info = (struct conn_info *)conn;
  991. return (conn_info->private_data);
  992. }
  993. int coroipcs_response_send (void *conn, const void *msg, size_t mlen)
  994. {
  995. struct conn_info *conn_info = (struct conn_info *)conn;
  996. memcpy (conn_info->response_buffer, msg, mlen);
  997. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_RESPONSE);
  998. api->stats_increment_value (conn_info->stats_handle, "responses");
  999. return (0);
  1000. }
  1001. int coroipcs_response_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  1002. {
  1003. struct conn_info *conn_info = (struct conn_info *)conn;
  1004. int write_idx = 0;
  1005. int i;
  1006. for (i = 0; i < iov_len; i++) {
  1007. memcpy (&conn_info->response_buffer[write_idx],
  1008. iov[i].iov_base, iov[i].iov_len);
  1009. write_idx += iov[i].iov_len;
  1010. }
  1011. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_RESPONSE);
  1012. api->stats_increment_value (conn_info->stats_handle, "responses");
  1013. return (0);
  1014. }
  1015. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info)
  1016. {
  1017. unsigned int n_read;
  1018. unsigned int n_write;
  1019. unsigned int bytes_left;
  1020. n_read = conn_info->control_buffer->read;
  1021. n_write = conn_info->control_buffer->write;
  1022. if (n_read <= n_write) {
  1023. bytes_left = conn_info->dispatch_size - n_write + n_read;
  1024. } else {
  1025. bytes_left = n_read - n_write;
  1026. }
  1027. if (bytes_left > 0) {
  1028. bytes_left--;
  1029. }
  1030. return (bytes_left);
  1031. }
  1032. static void memcpy_dwrap (struct conn_info *conn_info, void *msg, unsigned int len)
  1033. {
  1034. unsigned int write_idx;
  1035. write_idx = conn_info->control_buffer->write;
  1036. memcpy (&conn_info->dispatch_buffer[write_idx], msg, len);
  1037. conn_info->control_buffer->write = (write_idx + len) % conn_info->dispatch_size;
  1038. }
  1039. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  1040. int locked)
  1041. {
  1042. struct conn_info *conn_info = (struct conn_info *)conn;
  1043. int res;
  1044. int i;
  1045. char buf;
  1046. for (i = 0; i < iov_len; i++) {
  1047. memcpy_dwrap (conn_info, iov[i].iov_base, iov[i].iov_len);
  1048. }
  1049. buf = list_empty (&conn_info->outq_head);
  1050. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1051. if (res != 1) {
  1052. conn_info->pending_semops += 1;
  1053. if (conn_info->poll_state == POLL_STATE_IN) {
  1054. conn_info->poll_state = POLL_STATE_INOUT;
  1055. api->poll_dispatch_modify (conn_info->fd,
  1056. POLLIN|POLLOUT|POLLNVAL);
  1057. }
  1058. }
  1059. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_DISPATCH);
  1060. api->stats_increment_value (conn_info->stats_handle, "dispatched");
  1061. }
  1062. static void outq_flush (struct conn_info *conn_info) {
  1063. struct list_head *list, *list_next;
  1064. struct outq_item *outq_item;
  1065. unsigned int bytes_left;
  1066. struct iovec iov;
  1067. pthread_mutex_lock (&conn_info->mutex);
  1068. if (list_empty (&conn_info->outq_head)) {
  1069. flow_control_state_set (conn_info, 0);
  1070. pthread_mutex_unlock (&conn_info->mutex);
  1071. return;
  1072. }
  1073. for (list = conn_info->outq_head.next;
  1074. list != &conn_info->outq_head; list = list_next) {
  1075. list_next = list->next;
  1076. outq_item = list_entry (list, struct outq_item, list);
  1077. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  1078. if (bytes_left > outq_item->mlen) {
  1079. iov.iov_base = outq_item->msg;
  1080. iov.iov_len = outq_item->mlen;
  1081. msg_send (conn_info, &iov, 1, MSG_SEND_UNLOCKED);
  1082. list_del (list);
  1083. api->free (iov.iov_base);
  1084. api->free (outq_item);
  1085. api->stats_decrement_value (conn_info->stats_handle, "queue_size");
  1086. } else {
  1087. break;
  1088. }
  1089. }
  1090. pthread_mutex_unlock (&conn_info->mutex);
  1091. }
  1092. static int priv_change (struct conn_info *conn_info)
  1093. {
  1094. mar_req_priv_change req_priv_change;
  1095. unsigned int res;
  1096. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1097. union semun semun;
  1098. struct semid_ds ipc_set;
  1099. int i;
  1100. #endif
  1101. retry_recv:
  1102. res = recv (conn_info->fd, &req_priv_change,
  1103. sizeof (mar_req_priv_change),
  1104. MSG_NOSIGNAL);
  1105. if (res == -1 && errno == EINTR) {
  1106. api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
  1107. goto retry_recv;
  1108. }
  1109. if (res == -1 && errno == EAGAIN) {
  1110. api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
  1111. goto retry_recv;
  1112. }
  1113. if (res == -1 && errno != EAGAIN) {
  1114. return (-1);
  1115. }
  1116. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1117. /* Error on socket, EOF is detected when recv return 0
  1118. */
  1119. if (res == 0) {
  1120. return (-1);
  1121. }
  1122. #endif
  1123. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1124. ipc_set.sem_perm.uid = req_priv_change.euid;
  1125. ipc_set.sem_perm.gid = req_priv_change.egid;
  1126. ipc_set.sem_perm.mode = 0600;
  1127. semun.buf = &ipc_set;
  1128. for (i = 0; i < 3; i++) {
  1129. res = semctl (conn_info->control_buffer->semid, 0, IPC_SET, semun);
  1130. if (res == -1) {
  1131. return (-1);
  1132. }
  1133. }
  1134. #endif
  1135. return (0);
  1136. }
  1137. static void msg_send_or_queue (void *conn, const struct iovec *iov, unsigned int iov_len)
  1138. {
  1139. struct conn_info *conn_info = (struct conn_info *)conn;
  1140. unsigned int bytes_left;
  1141. unsigned int bytes_msg = 0;
  1142. int i;
  1143. struct outq_item *outq_item;
  1144. char *write_buf = 0;
  1145. /*
  1146. * Exit transmission if the connection is dead
  1147. */
  1148. if (ipc_thread_active (conn) == 0) {
  1149. return;
  1150. }
  1151. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  1152. for (i = 0; i < iov_len; i++) {
  1153. bytes_msg += iov[i].iov_len;
  1154. }
  1155. if (bytes_left < bytes_msg || list_empty (&conn_info->outq_head) == 0) {
  1156. flow_control_state_set (conn_info, 1);
  1157. outq_item = api->malloc (sizeof (struct outq_item));
  1158. if (outq_item == NULL) {
  1159. ipc_disconnect (conn);
  1160. return;
  1161. }
  1162. outq_item->msg = api->malloc (bytes_msg);
  1163. if (outq_item->msg == 0) {
  1164. api->free (outq_item);
  1165. ipc_disconnect (conn);
  1166. return;
  1167. }
  1168. write_buf = outq_item->msg;
  1169. for (i = 0; i < iov_len; i++) {
  1170. memcpy (write_buf, iov[i].iov_base, iov[i].iov_len);
  1171. write_buf += iov[i].iov_len;
  1172. }
  1173. outq_item->mlen = bytes_msg;
  1174. list_init (&outq_item->list);
  1175. pthread_mutex_lock (&conn_info->mutex);
  1176. list_add_tail (&outq_item->list, &conn_info->outq_head);
  1177. pthread_mutex_unlock (&conn_info->mutex);
  1178. api->stats_increment_value (conn_info->stats_handle, "queue_size");
  1179. return;
  1180. }
  1181. msg_send (conn, iov, iov_len, MSG_SEND_LOCKED);
  1182. }
  1183. void coroipcs_refcount_inc (void *conn)
  1184. {
  1185. struct conn_info *conn_info = (struct conn_info *)conn;
  1186. pthread_mutex_lock (&conn_info->mutex);
  1187. conn_info->refcount++;
  1188. pthread_mutex_unlock (&conn_info->mutex);
  1189. }
  1190. void coroipcs_refcount_dec (void *conn)
  1191. {
  1192. struct conn_info *conn_info = (struct conn_info *)conn;
  1193. pthread_mutex_lock (&conn_info->mutex);
  1194. conn_info->refcount--;
  1195. pthread_mutex_unlock (&conn_info->mutex);
  1196. }
  1197. int coroipcs_dispatch_send (void *conn, const void *msg, size_t mlen)
  1198. {
  1199. struct iovec iov;
  1200. iov.iov_base = (void *)msg;
  1201. iov.iov_len = mlen;
  1202. msg_send_or_queue (conn, &iov, 1);
  1203. return (0);
  1204. }
  1205. int coroipcs_dispatch_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  1206. {
  1207. msg_send_or_queue (conn, iov, iov_len);
  1208. return (0);
  1209. }
  1210. int coroipcs_handler_accept (
  1211. int fd,
  1212. int revent,
  1213. void *data)
  1214. {
  1215. socklen_t addrlen;
  1216. struct sockaddr_un un_addr;
  1217. int new_fd;
  1218. #ifdef COROSYNC_LINUX
  1219. int on = 1;
  1220. #endif
  1221. int res;
  1222. addrlen = sizeof (struct sockaddr_un);
  1223. retry_accept:
  1224. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  1225. if (new_fd == -1 && errno == EINTR) {
  1226. goto retry_accept;
  1227. }
  1228. if (new_fd == -1) {
  1229. char error_str[100];
  1230. strerror_r (errno, error_str, 100);
  1231. log_printf (LOGSYS_LEVEL_ERROR,
  1232. "Could not accept Library connection: %s\n", error_str);
  1233. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1234. }
  1235. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  1236. if (res == -1) {
  1237. char error_str[100];
  1238. strerror_r (errno, error_str, 100);
  1239. log_printf (LOGSYS_LEVEL_ERROR,
  1240. "Could not set non-blocking operation on library connection: %s\n",
  1241. error_str);
  1242. close (new_fd);
  1243. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1244. }
  1245. /*
  1246. * Valid accept
  1247. */
  1248. /*
  1249. * Request credentials of sender provided by kernel
  1250. */
  1251. #ifdef COROSYNC_LINUX
  1252. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  1253. #endif
  1254. res = conn_info_create (new_fd);
  1255. if (res != 0) {
  1256. close (new_fd);
  1257. }
  1258. return (0);
  1259. }
  1260. static char * pid_to_name (pid_t pid, char *out_name, size_t name_len)
  1261. {
  1262. char *name;
  1263. char *rest;
  1264. FILE *fp;
  1265. char fname[32];
  1266. char buf[256];
  1267. snprintf (fname, 32, "/proc/%d/stat", pid);
  1268. fp = fopen (fname, "r");
  1269. if (!fp) {
  1270. return NULL;
  1271. }
  1272. if (fgets (buf, sizeof (buf), fp) == NULL) {
  1273. fclose (fp);
  1274. return NULL;
  1275. }
  1276. fclose (fp);
  1277. name = strrchr (buf, '(');
  1278. if (!name) {
  1279. return NULL;
  1280. }
  1281. /* move past the bracket */
  1282. name++;
  1283. rest = strrchr (buf, ')');
  1284. if (rest == NULL || rest[1] != ' ') {
  1285. return NULL;
  1286. }
  1287. *rest = '\0';
  1288. /* move past the NULL and space */
  1289. rest += 2;
  1290. /* copy the name */
  1291. strncpy (out_name, name, name_len);
  1292. out_name[name_len - 1] = '\0';
  1293. return out_name;
  1294. }
  1295. static void coroipcs_init_conn_stats (
  1296. struct conn_info *conn)
  1297. {
  1298. char conn_name[42];
  1299. char proc_name[32];
  1300. if (conn->client_pid > 0) {
  1301. if (pid_to_name (conn->client_pid, proc_name, sizeof(proc_name)))
  1302. snprintf (conn_name, sizeof(conn_name), "%s:%d:%d", proc_name, conn->client_pid, conn->fd);
  1303. else
  1304. snprintf (conn_name, sizeof(conn_name), "%d:%d", conn->client_pid, conn->fd);
  1305. } else
  1306. snprintf (conn_name, sizeof(conn_name), "%d", conn->fd);
  1307. conn->stats_handle = api->stats_create_connection (conn_name, conn->client_pid, conn->fd);
  1308. api->stats_update_value (conn->stats_handle, "service_id",
  1309. &conn->service, sizeof(conn->service));
  1310. }
  1311. int coroipcs_handler_dispatch (
  1312. int fd,
  1313. int revent,
  1314. void *context)
  1315. {
  1316. mar_req_setup_t *req_setup;
  1317. struct conn_info *conn_info = (struct conn_info *)context;
  1318. int res;
  1319. char buf;
  1320. if (ipc_thread_exiting (conn_info)) {
  1321. return conn_info_destroy (conn_info);
  1322. }
  1323. /*
  1324. * If an error occurs, request exit
  1325. */
  1326. if (revent & (POLLERR|POLLHUP)) {
  1327. ipc_disconnect (conn_info);
  1328. return (0);
  1329. }
  1330. /*
  1331. * Read the header and process it
  1332. */
  1333. if (conn_info->service == SOCKET_SERVICE_INIT && (revent & POLLIN)) {
  1334. /*
  1335. * Receive in a nonblocking fashion the request
  1336. * IF security invalid, send ERR_SECURITY, otherwise
  1337. * send OK
  1338. */
  1339. res = req_setup_recv (conn_info);
  1340. if (res == -1) {
  1341. req_setup_send (conn_info, CS_ERR_SECURITY);
  1342. }
  1343. if (res != 1) {
  1344. return (0);
  1345. }
  1346. pthread_mutex_init (&conn_info->mutex, NULL);
  1347. req_setup = (mar_req_setup_t *)conn_info->setup_msg;
  1348. /*
  1349. * Is the service registered ?
  1350. */
  1351. if (api->service_available (req_setup->service) == 0) {
  1352. req_setup_send (conn_info, CS_ERR_NOT_EXIST);
  1353. ipc_disconnect (conn_info);
  1354. return (0);
  1355. }
  1356. req_setup_send (conn_info, CS_OK);
  1357. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1358. conn_info->semkey = req_setup->semkey;
  1359. #endif
  1360. res = memory_map (
  1361. req_setup->control_file,
  1362. req_setup->control_size,
  1363. (void *)&conn_info->control_buffer);
  1364. conn_info->control_size = req_setup->control_size;
  1365. res = memory_map (
  1366. req_setup->request_file,
  1367. req_setup->request_size,
  1368. (void *)&conn_info->request_buffer);
  1369. conn_info->request_size = req_setup->request_size;
  1370. res = memory_map (
  1371. req_setup->response_file,
  1372. req_setup->response_size,
  1373. (void *)&conn_info->response_buffer);
  1374. conn_info->response_size = req_setup->response_size;
  1375. res = circular_memory_map (
  1376. req_setup->dispatch_file,
  1377. req_setup->dispatch_size,
  1378. (void *)&conn_info->dispatch_buffer);
  1379. conn_info->dispatch_size = req_setup->dispatch_size;
  1380. conn_info->service = req_setup->service;
  1381. conn_info->refcount = 0;
  1382. conn_info->setup_bytes_read = 0;
  1383. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1384. conn_info->control_buffer->semid = semget (conn_info->semkey, 3, 0600);
  1385. #endif
  1386. conn_info->pending_semops = 0;
  1387. /*
  1388. * ipc thread is the only reference at startup
  1389. */
  1390. conn_info->refcount = 1;
  1391. conn_info->state = CONN_STATE_THREAD_ACTIVE;
  1392. conn_info->private_data = api->malloc (api->private_data_size_get (conn_info->service));
  1393. memset (conn_info->private_data, 0,
  1394. api->private_data_size_get (conn_info->service));
  1395. api->init_fn_get (conn_info->service) (conn_info);
  1396. /* create stats objects */
  1397. coroipcs_init_conn_stats (conn_info);
  1398. pthread_attr_init (&conn_info->thread_attr);
  1399. /*
  1400. * IA64 needs more stack space then other arches
  1401. */
  1402. #if defined(__ia64__)
  1403. pthread_attr_setstacksize (&conn_info->thread_attr, 400000);
  1404. #else
  1405. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  1406. #endif
  1407. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_JOINABLE);
  1408. res = pthread_create (&conn_info->thread,
  1409. &conn_info->thread_attr,
  1410. pthread_ipc_consumer,
  1411. conn_info);
  1412. /*
  1413. * Security check - disallow multiple configurations of
  1414. * the ipc connection
  1415. */
  1416. if (conn_info->service == SOCKET_SERVICE_INIT) {
  1417. conn_info->service = -1;
  1418. }
  1419. } else
  1420. if (revent & POLLIN) {
  1421. coroipcs_refcount_inc (conn_info);
  1422. res = recv (fd, &buf, 1, MSG_NOSIGNAL);
  1423. if (res == 1) {
  1424. switch (buf) {
  1425. case MESSAGE_REQ_CHANGE_EUID:
  1426. if (priv_change (conn_info) == -1) {
  1427. ipc_disconnect (conn_info);
  1428. }
  1429. break;
  1430. default:
  1431. res = 0;
  1432. break;
  1433. }
  1434. }
  1435. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1436. /* On many OS poll never return POLLHUP or POLLERR.
  1437. * EOF is detected when recvmsg return 0.
  1438. */
  1439. if (res == 0) {
  1440. ipc_disconnect (conn_info);
  1441. coroipcs_refcount_dec (conn_info);
  1442. return (0);
  1443. }
  1444. #endif
  1445. coroipcs_refcount_dec (conn_info);
  1446. }
  1447. if (revent & POLLOUT) {
  1448. int psop = conn_info->pending_semops;
  1449. int i;
  1450. assert (psop != 0);
  1451. for (i = 0; i < psop; i++) {
  1452. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1453. if (res != 1) {
  1454. return (0);
  1455. } else {
  1456. conn_info->pending_semops -= 1;
  1457. }
  1458. }
  1459. if (conn_info->poll_state == POLL_STATE_INOUT) {
  1460. conn_info->poll_state = POLL_STATE_IN;
  1461. api->poll_dispatch_modify (conn_info->fd, POLLIN|POLLNVAL);
  1462. }
  1463. }
  1464. return (0);
  1465. }