coroipcs.c 45 KB

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