coroipcs.c 43 KB

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