coroipcs.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  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. api->stats_increment_value (conn_info->stats_handle, "invalid_request");
  596. coroipc_response_header.size = sizeof (coroipc_response_header_t);
  597. coroipc_response_header.id = 0;
  598. coroipc_response_header.error = CS_ERR_INVALID_PARAM;
  599. coroipcs_response_send (conn_info,
  600. &coroipc_response_header,
  601. sizeof (coroipc_response_header_t));
  602. } else
  603. if (send_ok) {
  604. api->stats_increment_value (conn_info->stats_handle, "requests");
  605. api->serialize_lock();
  606. api->handler_fn_get (conn_info->service, header->id) (conn_info, header);
  607. api->serialize_unlock();
  608. } else {
  609. /*
  610. * Overload, tell library to retry
  611. */
  612. api->stats_increment_value (conn_info->stats_handle, "overload");
  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. LOGSYS_PERROR (errno, LOGSYS_LEVEL_CRIT,
  889. "Could not set non-blocking operation on server socket");
  890. api->fatal_error ("Could not set non-blocking operation on server socket");
  891. }
  892. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  893. un_addr.sun_family = AF_UNIX;
  894. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  895. un_addr.sun_len = SUN_LEN(&un_addr);
  896. #endif
  897. #if defined(COROSYNC_LINUX)
  898. sprintf (un_addr.sun_path + 1, "%s", api->socket_name);
  899. #else
  900. {
  901. struct stat stat_out;
  902. res = stat (SOCKETDIR, &stat_out);
  903. if (res == -1 || (res == 0 && !S_ISDIR(stat_out.st_mode))) {
  904. log_printf (LOGSYS_LEVEL_CRIT, "Required directory not present %s\n", SOCKETDIR);
  905. api->fatal_error ("Please create required directory.");
  906. }
  907. sprintf (un_addr.sun_path, "%s/%s", SOCKETDIR, api->socket_name);
  908. unlink (un_addr.sun_path);
  909. }
  910. #endif
  911. res = bind (server_fd, (struct sockaddr *)&un_addr, COROSYNC_SUN_LEN(&un_addr));
  912. if (res) {
  913. LOGSYS_PERROR (errno, LOGSYS_LEVEL_CRIT,
  914. "Could not bind AF_UNIX (%s)", un_addr.sun_path);
  915. api->fatal_error ("Could not bind to AF_UNIX socket\n");
  916. }
  917. /*
  918. * Allow eveyrone to write to the socket since the IPC layer handles
  919. * security automatically
  920. */
  921. #if !defined(COROSYNC_LINUX)
  922. res = chmod (un_addr.sun_path, S_IRWXU|S_IRWXG|S_IRWXO);
  923. #endif
  924. listen (server_fd, SERVER_BACKLOG);
  925. /*
  926. * Setup connection dispatch routine
  927. */
  928. api->poll_accept_add (server_fd);
  929. }
  930. void coroipcs_ipc_exit (void)
  931. {
  932. struct list_head *list;
  933. struct conn_info *conn_info;
  934. unsigned int res;
  935. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  936. list = list->next) {
  937. conn_info = list_entry (list, struct conn_info, list);
  938. if (conn_info->state != CONN_STATE_THREAD_ACTIVE)
  939. continue;
  940. ipc_disconnect (conn_info);
  941. #if _POSIX_THREAD_PROCESS_SHARED > 0
  942. sem_destroy (&conn_info->control_buffer->sem_request_or_flush_or_exit);
  943. sem_destroy (&conn_info->control_buffer->sem_request);
  944. sem_destroy (&conn_info->control_buffer->sem_response);
  945. sem_destroy (&conn_info->control_buffer->sem_dispatch);
  946. #else
  947. semctl (conn_info->control_buffer->semid, 0, IPC_RMID);
  948. #endif
  949. /*
  950. * Unmap memory segments
  951. */
  952. res = munmap ((void *)conn_info->control_buffer,
  953. conn_info->control_size);
  954. res = munmap ((void *)conn_info->request_buffer,
  955. conn_info->request_size);
  956. res = munmap ((void *)conn_info->response_buffer,
  957. conn_info->response_size);
  958. res = circular_memory_unmap (conn_info->dispatch_buffer,
  959. conn_info->dispatch_size);
  960. }
  961. }
  962. int coroipcs_ipc_service_exit (unsigned int service)
  963. {
  964. struct list_head *list, *list_next;
  965. struct conn_info *conn_info;
  966. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  967. list = list_next) {
  968. list_next = list->next;
  969. conn_info = list_entry (list, struct conn_info, list);
  970. if (conn_info->service != service ||
  971. (conn_info->state != CONN_STATE_THREAD_ACTIVE && conn_info->state != CONN_STATE_THREAD_REQUEST_EXIT)) {
  972. continue;
  973. }
  974. ipc_disconnect (conn_info);
  975. api->poll_dispatch_destroy (conn_info->fd, NULL);
  976. while (conn_info_destroy (conn_info) != -1)
  977. ;
  978. /*
  979. * We will return to prevent token loss. Schedwrk will call us again.
  980. */
  981. return (-1);
  982. }
  983. /*
  984. * No conn info left in active list. We will traverse thru exit list. If there is any
  985. * conn_info->service == service, we will wait to proper end -> return -1
  986. */
  987. for (list = conn_info_exit_list_head.next; list != &conn_info_exit_list_head; list = list->next) {
  988. conn_info = list_entry (list, struct conn_info, list);
  989. if (conn_info->service == service) {
  990. return (-1);
  991. }
  992. }
  993. return (0);
  994. }
  995. /*
  996. * Get the conn info private data
  997. */
  998. void *coroipcs_private_data_get (void *conn)
  999. {
  1000. struct conn_info *conn_info = (struct conn_info *)conn;
  1001. return (conn_info->private_data);
  1002. }
  1003. int coroipcs_response_send (void *conn, const void *msg, size_t mlen)
  1004. {
  1005. struct conn_info *conn_info = (struct conn_info *)conn;
  1006. memcpy (conn_info->response_buffer, msg, mlen);
  1007. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_RESPONSE);
  1008. api->stats_increment_value (conn_info->stats_handle, "responses");
  1009. return (0);
  1010. }
  1011. int coroipcs_response_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  1012. {
  1013. struct conn_info *conn_info = (struct conn_info *)conn;
  1014. int write_idx = 0;
  1015. int i;
  1016. for (i = 0; i < iov_len; i++) {
  1017. memcpy (&conn_info->response_buffer[write_idx],
  1018. iov[i].iov_base, iov[i].iov_len);
  1019. write_idx += iov[i].iov_len;
  1020. }
  1021. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_RESPONSE);
  1022. api->stats_increment_value (conn_info->stats_handle, "responses");
  1023. return (0);
  1024. }
  1025. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info)
  1026. {
  1027. unsigned int n_read;
  1028. unsigned int n_write;
  1029. unsigned int bytes_left;
  1030. n_read = conn_info->control_buffer->read;
  1031. n_write = conn_info->control_buffer->write;
  1032. if (n_read <= n_write) {
  1033. bytes_left = conn_info->dispatch_size - n_write + n_read;
  1034. } else {
  1035. bytes_left = n_read - n_write;
  1036. }
  1037. if (bytes_left > 0) {
  1038. bytes_left--;
  1039. }
  1040. return (bytes_left);
  1041. }
  1042. static void memcpy_dwrap (struct conn_info *conn_info, void *msg, unsigned int len)
  1043. {
  1044. unsigned int write_idx;
  1045. write_idx = conn_info->control_buffer->write;
  1046. memcpy (&conn_info->dispatch_buffer[write_idx], msg, len);
  1047. conn_info->control_buffer->write = ((write_idx + len + 7) & 0xFFFFFFF8) % conn_info->dispatch_size;
  1048. }
  1049. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  1050. int locked)
  1051. {
  1052. struct conn_info *conn_info = (struct conn_info *)conn;
  1053. int res;
  1054. int i;
  1055. char buf;
  1056. for (i = 0; i < iov_len; i++) {
  1057. memcpy_dwrap (conn_info, iov[i].iov_base, iov[i].iov_len);
  1058. }
  1059. buf = list_empty (&conn_info->outq_head);
  1060. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1061. if (res != 1) {
  1062. conn_info->pending_semops += 1;
  1063. if (conn_info->poll_state == POLL_STATE_IN) {
  1064. conn_info->poll_state = POLL_STATE_INOUT;
  1065. api->poll_dispatch_modify (conn_info->fd,
  1066. POLLIN|POLLOUT|POLLNVAL);
  1067. }
  1068. }
  1069. ipc_sem_post (conn_info->control_buffer, SEMAPHORE_DISPATCH);
  1070. }
  1071. static void outq_flush (struct conn_info *conn_info) {
  1072. struct list_head *list, *list_next;
  1073. struct outq_item *outq_item;
  1074. unsigned int bytes_left;
  1075. struct iovec iov;
  1076. int32_t q_size_dec = 0;
  1077. int32_t i;
  1078. int32_t fc_set;
  1079. pthread_mutex_lock (&conn_info->mutex);
  1080. if (list_empty (&conn_info->outq_head)) {
  1081. fc_set = flow_control_state_set (conn_info, 0);
  1082. pthread_mutex_unlock (&conn_info->mutex);
  1083. if (fc_set) {
  1084. flow_control_stats_update (conn_info->stats_handle, 0);
  1085. }
  1086. return;
  1087. }
  1088. for (list = conn_info->outq_head.next;
  1089. list != &conn_info->outq_head; list = list_next) {
  1090. list_next = list->next;
  1091. outq_item = list_entry (list, struct outq_item, list);
  1092. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  1093. if (bytes_left > outq_item->mlen) {
  1094. iov.iov_base = outq_item->msg;
  1095. iov.iov_len = outq_item->mlen;
  1096. msg_send (conn_info, &iov, 1, MSG_SEND_UNLOCKED);
  1097. list_del (list);
  1098. api->free (iov.iov_base);
  1099. api->free (outq_item);
  1100. q_size_dec++;
  1101. } else {
  1102. break;
  1103. }
  1104. }
  1105. pthread_mutex_unlock (&conn_info->mutex);
  1106. /*
  1107. * these need to be sent out of the conn_info->mutex
  1108. */
  1109. for (i = 0; i < q_size_dec; i++) {
  1110. api->stats_decrement_value (conn_info->stats_handle, "queue_size");
  1111. api->stats_increment_value (conn_info->stats_handle, "dispatched");
  1112. }
  1113. }
  1114. static int priv_change (struct conn_info *conn_info)
  1115. {
  1116. mar_req_priv_change req_priv_change;
  1117. unsigned int res;
  1118. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1119. union semun semun;
  1120. struct semid_ds ipc_set;
  1121. int i;
  1122. #endif
  1123. retry_recv:
  1124. res = recv (conn_info->fd, &req_priv_change,
  1125. sizeof (mar_req_priv_change),
  1126. MSG_NOSIGNAL);
  1127. if (res == -1 && errno == EINTR) {
  1128. api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
  1129. goto retry_recv;
  1130. }
  1131. if (res == -1 && errno == EAGAIN) {
  1132. api->stats_increment_value (conn_info->stats_handle, "recv_retry_count");
  1133. goto retry_recv;
  1134. }
  1135. if (res == -1 && errno != EAGAIN) {
  1136. return (-1);
  1137. }
  1138. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1139. /* Error on socket, EOF is detected when recv return 0
  1140. */
  1141. if (res == 0) {
  1142. return (-1);
  1143. }
  1144. #endif
  1145. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1146. ipc_set.sem_perm.uid = req_priv_change.euid;
  1147. ipc_set.sem_perm.gid = req_priv_change.egid;
  1148. ipc_set.sem_perm.mode = 0600;
  1149. semun.buf = &ipc_set;
  1150. for (i = 0; i < 3; i++) {
  1151. res = semctl (conn_info->control_buffer->semid, 0, IPC_SET, semun);
  1152. if (res == -1) {
  1153. return (-1);
  1154. }
  1155. }
  1156. #endif
  1157. return (0);
  1158. }
  1159. static void msg_send_or_queue (void *conn, const struct iovec *iov, unsigned int iov_len)
  1160. {
  1161. struct conn_info *conn_info = (struct conn_info *)conn;
  1162. unsigned int bytes_left;
  1163. unsigned int bytes_msg = 0;
  1164. int i;
  1165. struct outq_item *outq_item;
  1166. char *write_buf = 0;
  1167. /*
  1168. * Exit transmission if the connection is dead
  1169. */
  1170. if (ipc_thread_active (conn) == 0) {
  1171. return;
  1172. }
  1173. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  1174. for (i = 0; i < iov_len; i++) {
  1175. bytes_msg += iov[i].iov_len;
  1176. }
  1177. if (bytes_left < bytes_msg || list_empty (&conn_info->outq_head) == 0) {
  1178. if (flow_control_state_set (conn_info, 1)) {
  1179. flow_control_stats_update(conn_info->stats_handle, 1);
  1180. }
  1181. outq_item = api->malloc (sizeof (struct outq_item));
  1182. if (outq_item == NULL) {
  1183. ipc_disconnect (conn);
  1184. return;
  1185. }
  1186. outq_item->msg = api->malloc (bytes_msg);
  1187. if (outq_item->msg == 0) {
  1188. api->free (outq_item);
  1189. ipc_disconnect (conn);
  1190. return;
  1191. }
  1192. write_buf = outq_item->msg;
  1193. for (i = 0; i < iov_len; i++) {
  1194. memcpy (write_buf, iov[i].iov_base, iov[i].iov_len);
  1195. write_buf += iov[i].iov_len;
  1196. }
  1197. outq_item->mlen = bytes_msg;
  1198. list_init (&outq_item->list);
  1199. pthread_mutex_lock (&conn_info->mutex);
  1200. list_add_tail (&outq_item->list, &conn_info->outq_head);
  1201. pthread_mutex_unlock (&conn_info->mutex);
  1202. api->stats_increment_value (conn_info->stats_handle, "queue_size");
  1203. return;
  1204. }
  1205. msg_send (conn, iov, iov_len, MSG_SEND_LOCKED);
  1206. api->stats_increment_value (conn_info->stats_handle, "dispatched");
  1207. }
  1208. void coroipcs_refcount_inc (void *conn)
  1209. {
  1210. struct conn_info *conn_info = (struct conn_info *)conn;
  1211. pthread_mutex_lock (&conn_info->mutex);
  1212. conn_info->refcount++;
  1213. pthread_mutex_unlock (&conn_info->mutex);
  1214. }
  1215. void coroipcs_refcount_dec (void *conn)
  1216. {
  1217. struct conn_info *conn_info = (struct conn_info *)conn;
  1218. pthread_mutex_lock (&conn_info->mutex);
  1219. conn_info->refcount--;
  1220. pthread_mutex_unlock (&conn_info->mutex);
  1221. }
  1222. int coroipcs_dispatch_send (void *conn, const void *msg, size_t mlen)
  1223. {
  1224. struct iovec iov;
  1225. iov.iov_base = (void *)msg;
  1226. iov.iov_len = mlen;
  1227. msg_send_or_queue (conn, &iov, 1);
  1228. return (0);
  1229. }
  1230. int coroipcs_dispatch_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  1231. {
  1232. msg_send_or_queue (conn, iov, iov_len);
  1233. return (0);
  1234. }
  1235. int coroipcs_handler_accept (
  1236. int fd,
  1237. int revent,
  1238. void *data)
  1239. {
  1240. socklen_t addrlen;
  1241. struct sockaddr_un un_addr;
  1242. int new_fd;
  1243. #ifdef COROSYNC_LINUX
  1244. int on = 1;
  1245. #endif
  1246. int res;
  1247. addrlen = sizeof (struct sockaddr_un);
  1248. retry_accept:
  1249. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  1250. if (new_fd == -1 && errno == EINTR) {
  1251. goto retry_accept;
  1252. }
  1253. if (new_fd == -1) {
  1254. LOGSYS_PERROR (errno, LOGSYS_LEVEL_ERROR,
  1255. "Could not accept Library connection");
  1256. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1257. }
  1258. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  1259. if (res == -1) {
  1260. LOGSYS_PERROR (errno, LOGSYS_LEVEL_ERROR,
  1261. "Could not set non-blocking operation on library connection");
  1262. close (new_fd);
  1263. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  1264. }
  1265. /*
  1266. * Valid accept
  1267. */
  1268. /*
  1269. * Request credentials of sender provided by kernel
  1270. */
  1271. #ifdef COROSYNC_LINUX
  1272. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  1273. #endif
  1274. res = conn_info_create (new_fd);
  1275. if (res != 0) {
  1276. close (new_fd);
  1277. }
  1278. return (0);
  1279. }
  1280. static char * pid_to_name (pid_t pid, char *out_name, size_t name_len)
  1281. {
  1282. char *name;
  1283. char *rest;
  1284. FILE *fp;
  1285. char fname[32];
  1286. char buf[256];
  1287. snprintf (fname, 32, "/proc/%d/stat", pid);
  1288. fp = fopen (fname, "r");
  1289. if (!fp) {
  1290. return NULL;
  1291. }
  1292. if (fgets (buf, sizeof (buf), fp) == NULL) {
  1293. fclose (fp);
  1294. return NULL;
  1295. }
  1296. fclose (fp);
  1297. name = strrchr (buf, '(');
  1298. if (!name) {
  1299. return NULL;
  1300. }
  1301. /* move past the bracket */
  1302. name++;
  1303. rest = strrchr (buf, ')');
  1304. if (rest == NULL || rest[1] != ' ') {
  1305. return NULL;
  1306. }
  1307. *rest = '\0';
  1308. /* move past the NULL and space */
  1309. rest += 2;
  1310. /* copy the name */
  1311. strncpy (out_name, name, name_len);
  1312. out_name[name_len - 1] = '\0';
  1313. return out_name;
  1314. }
  1315. static void coroipcs_init_conn_stats (
  1316. struct conn_info *conn)
  1317. {
  1318. char conn_name[CS_MAX_NAME_LENGTH];
  1319. char proc_name[CS_MAX_NAME_LENGTH];
  1320. char int_str[4];
  1321. if (conn->client_pid > 0) {
  1322. if (pid_to_name (conn->client_pid, proc_name, sizeof(proc_name))) {
  1323. snprintf (conn_name, sizeof(conn_name),
  1324. "%s:%s:%d:%d", proc_name,
  1325. short_service_name_get(conn->service, int_str, 4),
  1326. conn->client_pid, conn->fd);
  1327. } else {
  1328. snprintf (conn_name, sizeof(conn_name),
  1329. "proc:%s:%d:%d",
  1330. short_service_name_get(conn->service, int_str, 4),
  1331. conn->client_pid,
  1332. conn->fd);
  1333. }
  1334. } else {
  1335. snprintf (conn_name, sizeof(conn_name),
  1336. "proc:%s:pid:%d",
  1337. short_service_name_get(conn->service, int_str, 4),
  1338. conn->fd);
  1339. }
  1340. conn->stats_handle = api->stats_create_connection (conn_name, conn->client_pid, conn->fd);
  1341. api->stats_update_value (conn->stats_handle, "service_id",
  1342. &conn->service, sizeof(conn->service));
  1343. }
  1344. int coroipcs_handler_dispatch (
  1345. int fd,
  1346. int revent,
  1347. void *context)
  1348. {
  1349. mar_req_setup_t *req_setup;
  1350. struct conn_info *conn_info = (struct conn_info *)context;
  1351. int res;
  1352. char buf = 0;
  1353. if (ipc_thread_exiting (conn_info)) {
  1354. return conn_info_destroy (conn_info);
  1355. }
  1356. /*
  1357. * If an error occurs, request exit
  1358. */
  1359. if (revent & (POLLERR|POLLHUP)) {
  1360. ipc_disconnect (conn_info);
  1361. return (0);
  1362. }
  1363. /*
  1364. * Read the header and process it
  1365. */
  1366. if (conn_info->service == SOCKET_SERVICE_INIT && (revent & POLLIN)) {
  1367. /*
  1368. * Receive in a nonblocking fashion the request
  1369. * IF security invalid, send ERR_SECURITY, otherwise
  1370. * send OK
  1371. */
  1372. res = req_setup_recv (conn_info);
  1373. if (res != CS_OK && res != CS_ERR_LIBRARY) {
  1374. req_setup_send (conn_info, res);
  1375. }
  1376. if (res != CS_OK) {
  1377. return (0);
  1378. }
  1379. pthread_mutex_init (&conn_info->mutex, NULL);
  1380. req_setup = (mar_req_setup_t *)conn_info->setup_msg;
  1381. /*
  1382. * Is the service registered ?
  1383. * Has service init function ?
  1384. */
  1385. if (api->service_available (req_setup->service) == 0 ||
  1386. api->init_fn_get (req_setup->service) == NULL) {
  1387. req_setup_send (conn_info, CS_ERR_NOT_EXIST);
  1388. ipc_disconnect (conn_info);
  1389. return (0);
  1390. }
  1391. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1392. conn_info->semkey = req_setup->semkey;
  1393. #endif
  1394. res = memory_map (
  1395. req_setup->control_file,
  1396. req_setup->control_size,
  1397. (void *)&conn_info->control_buffer);
  1398. if (res == -1) {
  1399. goto send_setup_response;
  1400. }
  1401. conn_info->control_size = req_setup->control_size;
  1402. res = memory_map (
  1403. req_setup->request_file,
  1404. req_setup->request_size,
  1405. (void *)&conn_info->request_buffer);
  1406. if (res == -1) {
  1407. goto send_setup_response;
  1408. }
  1409. conn_info->request_size = req_setup->request_size;
  1410. res = memory_map (
  1411. req_setup->response_file,
  1412. req_setup->response_size,
  1413. (void *)&conn_info->response_buffer);
  1414. if (res == -1) {
  1415. goto send_setup_response;
  1416. }
  1417. conn_info->response_size = req_setup->response_size;
  1418. res = circular_memory_map (
  1419. req_setup->dispatch_file,
  1420. req_setup->dispatch_size,
  1421. (void *)&conn_info->dispatch_buffer);
  1422. if (res == -1) {
  1423. goto send_setup_response;
  1424. }
  1425. conn_info->dispatch_size = req_setup->dispatch_size;
  1426. send_setup_response:
  1427. if (res == 0) {
  1428. req_setup_send (conn_info, CS_OK);
  1429. } else {
  1430. req_setup_send (conn_info, CS_ERR_LIBRARY);
  1431. ipc_disconnect (conn_info);
  1432. return (0);
  1433. }
  1434. conn_info->service = req_setup->service;
  1435. conn_info->refcount = 0;
  1436. conn_info->setup_bytes_read = 0;
  1437. #if _POSIX_THREAD_PROCESS_SHARED < 1
  1438. conn_info->control_buffer->semid = semget (conn_info->semkey, 3, 0600);
  1439. #endif
  1440. conn_info->pending_semops = 0;
  1441. /*
  1442. * ipc thread is the only reference at startup
  1443. */
  1444. conn_info->refcount = 1;
  1445. conn_info->state = CONN_STATE_THREAD_ACTIVE;
  1446. conn_info->private_data = api->malloc (api->private_data_size_get (conn_info->service));
  1447. memset (conn_info->private_data, 0,
  1448. api->private_data_size_get (conn_info->service));
  1449. api->init_fn_get (conn_info->service) (conn_info);
  1450. /* create stats objects */
  1451. coroipcs_init_conn_stats (conn_info);
  1452. pthread_attr_init (&conn_info->thread_attr);
  1453. /*
  1454. * IA64 needs more stack space then other arches
  1455. */
  1456. #if defined(__ia64__)
  1457. pthread_attr_setstacksize (&conn_info->thread_attr, 400000);
  1458. #else
  1459. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  1460. #endif
  1461. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_JOINABLE);
  1462. res = pthread_create (&conn_info->thread,
  1463. &conn_info->thread_attr,
  1464. pthread_ipc_consumer,
  1465. conn_info);
  1466. /*
  1467. * Security check - disallow multiple configurations of
  1468. * the ipc connection
  1469. */
  1470. if (conn_info->service == SOCKET_SERVICE_INIT) {
  1471. conn_info->service = SOCKET_SERVICE_SECURITY_VIOLATION;
  1472. }
  1473. } else
  1474. if (revent & POLLIN) {
  1475. coroipcs_refcount_inc (conn_info);
  1476. res = recv (fd, &buf, 1, MSG_NOSIGNAL);
  1477. if (res == 1) {
  1478. switch (buf) {
  1479. case MESSAGE_REQ_CHANGE_EUID:
  1480. if (priv_change (conn_info) == -1) {
  1481. ipc_disconnect (conn_info);
  1482. }
  1483. break;
  1484. default:
  1485. res = 0;
  1486. break;
  1487. }
  1488. }
  1489. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  1490. /* On many OS poll never return POLLHUP or POLLERR.
  1491. * EOF is detected when recvmsg return 0.
  1492. */
  1493. if (res == 0) {
  1494. ipc_disconnect (conn_info);
  1495. coroipcs_refcount_dec (conn_info);
  1496. return (0);
  1497. }
  1498. #endif
  1499. coroipcs_refcount_dec (conn_info);
  1500. }
  1501. if (revent & POLLOUT) {
  1502. int psop = conn_info->pending_semops;
  1503. int i;
  1504. assert (psop != 0);
  1505. for (i = 0; i < psop; i++) {
  1506. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  1507. if (res != 1) {
  1508. return (0);
  1509. } else {
  1510. conn_info->pending_semops -= 1;
  1511. }
  1512. }
  1513. if (conn_info->poll_state == POLL_STATE_INOUT) {
  1514. conn_info->poll_state = POLL_STATE_IN;
  1515. api->poll_dispatch_modify (conn_info->fd, POLLIN|POLLNVAL);
  1516. }
  1517. }
  1518. return (0);
  1519. }