coroipcs.c 44 KB

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