coroipcs.c 45 KB

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