coroipcs.c 42 KB

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