coroipcs.c 43 KB

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