coroipcs.c 44 KB

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