coroipcs.c 44 KB

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