coroipcs.c 43 KB

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