coroipcs.c 44 KB

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