coroipcs.c 44 KB

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