coroipcs.c 44 KB

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