coroipcs.c 44 KB

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