coroipcs.c 44 KB

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