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