coroipcs.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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 <netinet/in.h>
  52. #include <arpa/inet.h>
  53. #include <unistd.h>
  54. #include <fcntl.h>
  55. #include <stdlib.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <signal.h>
  59. #include <sched.h>
  60. #include <time.h>
  61. #if defined(HAVE_GETPEERUCRED)
  62. #include <ucred.h>
  63. #endif
  64. #include <sys/shm.h>
  65. #include <sys/sem.h>
  66. #include <corosync/corotypes.h>
  67. #include <corosync/list.h>
  68. #include "coroipcs.h"
  69. #include <corosync/ipc_gen.h>
  70. #ifndef MSG_NOSIGNAL
  71. #define MSG_NOSIGNAL 0
  72. #endif
  73. #define SERVER_BACKLOG 5
  74. #define MSG_SEND_LOCKED 0
  75. #define MSG_SEND_UNLOCKED 1
  76. static struct coroipcs_init_state *api;
  77. DECLARE_LIST_INIT (conn_info_list_head);
  78. struct outq_item {
  79. void *msg;
  80. size_t mlen;
  81. struct list_head list;
  82. };
  83. #if defined(_SEM_SEMUN_UNDEFINED)
  84. union semun {
  85. int val;
  86. struct semid_ds *buf;
  87. unsigned short int *array;
  88. struct seminfo *__buf;
  89. };
  90. #endif
  91. enum conn_state {
  92. CONN_STATE_THREAD_INACTIVE = 0,
  93. CONN_STATE_THREAD_ACTIVE = 1,
  94. CONN_STATE_THREAD_REQUEST_EXIT = 2,
  95. CONN_STATE_THREAD_DESTROYED = 3,
  96. CONN_STATE_LIB_EXIT_CALLED = 4,
  97. CONN_STATE_DISCONNECT_INACTIVE = 5
  98. };
  99. struct conn_info {
  100. int fd;
  101. pthread_t thread;
  102. pthread_attr_t thread_attr;
  103. unsigned int service;
  104. enum conn_state state;
  105. int notify_flow_control_enabled;
  106. int refcount;
  107. key_t shmkey;
  108. key_t semkey;
  109. int shmid;
  110. int semid;
  111. unsigned int pending_semops;
  112. pthread_mutex_t mutex;
  113. struct shared_memory *mem;
  114. char *dispatch_buffer;
  115. struct list_head outq_head;
  116. void *private_data;
  117. struct list_head list;
  118. char setup_msg[sizeof (mar_req_setup_t)];
  119. unsigned int setup_bytes_read;
  120. char *sending_allowed_private_data[64];
  121. };
  122. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info);
  123. static void outq_flush (struct conn_info *conn_info);
  124. static int priv_change (struct conn_info *conn_info);
  125. static void ipc_disconnect (struct conn_info *conn_info);
  126. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  127. int locked);
  128. static inline int
  129. coroipcs_circular_memory_unmap (void *buf, size_t bytes)
  130. {
  131. int res;
  132. res = munmap (buf, bytes << 1);
  133. return (res);
  134. }
  135. static int ipc_thread_active (void *conn)
  136. {
  137. struct conn_info *conn_info = (struct conn_info *)conn;
  138. int retval = 0;
  139. pthread_mutex_lock (&conn_info->mutex);
  140. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  141. retval = 1;
  142. }
  143. pthread_mutex_unlock (&conn_info->mutex);
  144. return (retval);
  145. }
  146. static int ipc_thread_exiting (void *conn)
  147. {
  148. struct conn_info *conn_info = (struct conn_info *)conn;
  149. int retval = 1;
  150. pthread_mutex_lock (&conn_info->mutex);
  151. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  152. retval = 0;
  153. } else
  154. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  155. retval = 0;
  156. }
  157. pthread_mutex_unlock (&conn_info->mutex);
  158. return (retval);
  159. }
  160. /*
  161. * returns 0 if should be called again, -1 if finished
  162. */
  163. static inline int conn_info_destroy (struct conn_info *conn_info)
  164. {
  165. unsigned int res;
  166. void *retval;
  167. list_del (&conn_info->list);
  168. list_init (&conn_info->list);
  169. if (conn_info->state == CONN_STATE_THREAD_REQUEST_EXIT) {
  170. res = pthread_join (conn_info->thread, &retval);
  171. conn_info->state = CONN_STATE_THREAD_DESTROYED;
  172. return (0);
  173. }
  174. if (conn_info->state == CONN_STATE_THREAD_INACTIVE ||
  175. conn_info->state == CONN_STATE_DISCONNECT_INACTIVE) {
  176. list_del (&conn_info->list);
  177. close (conn_info->fd);
  178. api->free (conn_info);
  179. return (-1);
  180. }
  181. if (conn_info->state == CONN_STATE_THREAD_ACTIVE) {
  182. pthread_kill (conn_info->thread, SIGUSR1);
  183. return (0);
  184. }
  185. api->serialize_lock ();
  186. /*
  187. * Retry library exit function if busy
  188. */
  189. if (conn_info->state == CONN_STATE_THREAD_DESTROYED) {
  190. res = api->exit_fn_get (conn_info->service) (conn_info);
  191. if (res == -1) {
  192. api->serialize_unlock ();
  193. return (0);
  194. } else {
  195. conn_info->state = CONN_STATE_LIB_EXIT_CALLED;
  196. }
  197. }
  198. pthread_mutex_lock (&conn_info->mutex);
  199. if (conn_info->refcount > 0) {
  200. pthread_mutex_unlock (&conn_info->mutex);
  201. api->serialize_unlock ();
  202. return (0);
  203. }
  204. list_del (&conn_info->list);
  205. pthread_mutex_unlock (&conn_info->mutex);
  206. /*
  207. * Destroy shared memory segment and semaphore
  208. */
  209. shmdt (conn_info->mem);
  210. res = shmctl (conn_info->shmid, IPC_RMID, NULL);
  211. semctl (conn_info->semid, 0, IPC_RMID);
  212. /*
  213. * Free allocated data needed to retry exiting library IPC connection
  214. */
  215. if (conn_info->private_data) {
  216. api->free (conn_info->private_data);
  217. }
  218. close (conn_info->fd);
  219. res = coroipcs_circular_memory_unmap (conn_info->dispatch_buffer, DISPATCH_SIZE);
  220. api->free (conn_info);
  221. api->serialize_unlock ();
  222. return (-1);
  223. }
  224. struct res_overlay {
  225. mar_res_header_t header __attribute__((aligned(8)));
  226. char buf[4096];
  227. };
  228. static void *pthread_ipc_consumer (void *conn)
  229. {
  230. struct conn_info *conn_info = (struct conn_info *)conn;
  231. struct sembuf sop;
  232. int res;
  233. mar_req_header_t *header;
  234. struct res_overlay res_overlay;
  235. int send_ok;
  236. if (api->sched_priority != 0) {
  237. struct sched_param sched_param;
  238. sched_param.sched_priority = api->sched_priority;
  239. res = pthread_setschedparam (conn_info->thread, SCHED_RR, &sched_param);
  240. }
  241. for (;;) {
  242. sop.sem_num = 0;
  243. sop.sem_op = -1;
  244. sop.sem_flg = 0;
  245. retry_semop:
  246. if (ipc_thread_active (conn_info) == 0) {
  247. coroipcs_refcount_dec (conn_info);
  248. pthread_exit (0);
  249. }
  250. res = semop (conn_info->semid, &sop, 1);
  251. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  252. goto retry_semop;
  253. } else
  254. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  255. coroipcs_refcount_dec (conn_info);
  256. pthread_exit (0);
  257. }
  258. coroipcs_refcount_inc (conn_info);
  259. header = (mar_req_header_t *)conn_info->mem->req_buffer;
  260. send_ok = api->sending_allowed (conn_info->service,
  261. header->id,
  262. header,
  263. conn_info->sending_allowed_private_data);
  264. if (send_ok) {
  265. api->serialize_lock();
  266. api->handler_fn_get (conn_info->service, header->id) (conn_info, header);
  267. api->serialize_unlock();
  268. } else {
  269. /*
  270. * Overload, tell library to retry
  271. */
  272. res_overlay.header.size =
  273. api->response_size_get (conn_info->service, header->id);
  274. res_overlay.header.id =
  275. api->response_id_get (conn_info->service, header->id);
  276. res_overlay.header.error = CS_ERR_TRY_AGAIN;
  277. coroipcs_response_send (conn_info, &res_overlay,
  278. res_overlay.header.size);
  279. }
  280. api->sending_allowed_release (conn_info->sending_allowed_private_data);
  281. coroipcs_refcount_dec (conn);
  282. }
  283. pthread_exit (0);
  284. }
  285. static int
  286. req_setup_send (
  287. struct conn_info *conn_info,
  288. int error)
  289. {
  290. mar_res_setup_t res_setup;
  291. unsigned int res;
  292. res_setup.error = error;
  293. retry_send:
  294. res = send (conn_info->fd, &res_setup, sizeof (mar_res_setup_t), MSG_WAITALL);
  295. if (res == -1 && errno == EINTR) {
  296. goto retry_send;
  297. } else
  298. if (res == -1 && errno == EAGAIN) {
  299. goto retry_send;
  300. }
  301. return (0);
  302. }
  303. static int
  304. req_setup_recv (
  305. struct conn_info *conn_info)
  306. {
  307. int res;
  308. struct msghdr msg_recv;
  309. struct iovec iov_recv;
  310. #ifdef COROSYNC_LINUX
  311. struct cmsghdr *cmsg;
  312. char cmsg_cred[CMSG_SPACE (sizeof (struct ucred))];
  313. struct ucred *cred;
  314. int off = 0;
  315. int on = 1;
  316. #endif
  317. msg_recv.msg_iov = &iov_recv;
  318. msg_recv.msg_iovlen = 1;
  319. msg_recv.msg_name = 0;
  320. msg_recv.msg_namelen = 0;
  321. #ifdef COROSYNC_LINUX
  322. msg_recv.msg_control = (void *)cmsg_cred;
  323. msg_recv.msg_controllen = sizeof (cmsg_cred);
  324. #endif
  325. #ifdef PORTABILITY_WORK_TODO
  326. #ifdef COROSYNC_SOLARIS
  327. msg_recv.msg_flags = 0;
  328. uid_t euid;
  329. gid_t egid;
  330. euid = -1;
  331. egid = -1;
  332. if (getpeereid(conn_info->fd, &euid, &egid) != -1 &&
  333. (api->security_valid (euid, egid)) {
  334. if (conn_info->state == CONN_IO_STATE_INITIALIZING) {
  335. api->log_printf ("Invalid security authentication\n");
  336. return (-1);
  337. }
  338. }
  339. msg_recv.msg_accrights = 0;
  340. msg_recv.msg_accrightslen = 0;
  341. #else /* COROSYNC_SOLARIS */
  342. #ifdef HAVE_GETPEERUCRED
  343. ucred_t *uc;
  344. uid_t euid = -1;
  345. gid_t egid = -1;
  346. if (getpeerucred (conn_info->fd, &uc) == 0) {
  347. euid = ucred_geteuid (uc);
  348. egid = ucred_getegid (uc);
  349. if (api->security_valid (euid, egid) {
  350. conn_info->authenticated = 1;
  351. }
  352. ucred_free(uc);
  353. }
  354. if (conn_info->authenticated == 0) {
  355. api->log_printf ("Invalid security authentication\n");
  356. }
  357. #else /* HAVE_GETPEERUCRED */
  358. api->log_printf (LOGSYS_LEVEL_SECURITY, "Connection not authenticated "
  359. "because platform does not support "
  360. "authentication with sockets, continuing "
  361. "with a fake authentication\n");
  362. #endif /* HAVE_GETPEERUCRED */
  363. #endif /* COROSYNC_SOLARIS */
  364. #endif
  365. iov_recv.iov_base = &conn_info->setup_msg[conn_info->setup_bytes_read];
  366. iov_recv.iov_len = sizeof (mar_req_setup_t) - conn_info->setup_bytes_read;
  367. #ifdef COROSYNC_LINUX
  368. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  369. #endif
  370. retry_recv:
  371. res = recvmsg (conn_info->fd, &msg_recv, MSG_NOSIGNAL);
  372. if (res == -1 && errno == EINTR) {
  373. goto retry_recv;
  374. } else
  375. if (res == -1 && errno != EAGAIN) {
  376. return (0);
  377. } else
  378. if (res == 0) {
  379. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  380. /* On many OS poll never return POLLHUP or POLLERR.
  381. * EOF is detected when recvmsg return 0.
  382. */
  383. ipc_disconnect (conn_info);
  384. #endif
  385. return (-1);
  386. }
  387. conn_info->setup_bytes_read += res;
  388. #ifdef COROSYNC_LINUX
  389. cmsg = CMSG_FIRSTHDR (&msg_recv);
  390. assert (cmsg);
  391. cred = (struct ucred *)CMSG_DATA (cmsg);
  392. if (cred) {
  393. if (api->security_valid (cred->uid, cred->gid)) {
  394. } else {
  395. ipc_disconnect (conn_info);
  396. api->log_printf ("Invalid security authentication\n");
  397. return (-1);
  398. }
  399. }
  400. #endif
  401. if (conn_info->setup_bytes_read == sizeof (mar_req_setup_t)) {
  402. #ifdef COROSYNC_LINUX
  403. setsockopt(conn_info->fd, SOL_SOCKET, SO_PASSCRED,
  404. &off, sizeof (off));
  405. #endif
  406. return (1);
  407. }
  408. return (0);
  409. }
  410. static void ipc_disconnect (struct conn_info *conn_info)
  411. {
  412. if (conn_info->state == CONN_STATE_THREAD_INACTIVE) {
  413. conn_info->state = CONN_STATE_DISCONNECT_INACTIVE;
  414. return;
  415. }
  416. if (conn_info->state != CONN_STATE_THREAD_ACTIVE) {
  417. return;
  418. }
  419. pthread_mutex_lock (&conn_info->mutex);
  420. conn_info->state = CONN_STATE_THREAD_REQUEST_EXIT;
  421. pthread_mutex_unlock (&conn_info->mutex);
  422. pthread_kill (conn_info->thread, SIGUSR1);
  423. }
  424. static int conn_info_create (int fd)
  425. {
  426. struct conn_info *conn_info;
  427. conn_info = api->malloc (sizeof (struct conn_info));
  428. if (conn_info == NULL) {
  429. return (-1);
  430. }
  431. memset (conn_info, 0, sizeof (struct conn_info));
  432. conn_info->fd = fd;
  433. conn_info->service = SOCKET_SERVICE_INIT;
  434. conn_info->state = CONN_STATE_THREAD_INACTIVE;
  435. list_init (&conn_info->outq_head);
  436. list_init (&conn_info->list);
  437. list_add (&conn_info->list, &conn_info_list_head);
  438. api->poll_dispatch_add (fd, conn_info);
  439. return (0);
  440. }
  441. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  442. /* SUN_LEN is broken for abstract namespace
  443. */
  444. #define COROSYNC_SUN_LEN(a) sizeof(*(a))
  445. #else
  446. #define COROSYNC_SUN_LEN(a) SUN_LEN(a)
  447. #endif
  448. /*
  449. * Exported functions
  450. */
  451. extern void coroipcs_ipc_init (
  452. struct coroipcs_init_state *init_state)
  453. {
  454. int server_fd;
  455. struct sockaddr_un un_addr;
  456. int res;
  457. api = init_state;
  458. /*
  459. * Create socket for IPC clients, name socket, listen for connections
  460. */
  461. server_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  462. if (server_fd == -1) {
  463. api->log_printf ("Cannot create client connections socket.\n");
  464. api->fatal_error ("Can't create library listen socket");
  465. };
  466. res = fcntl (server_fd, F_SETFL, O_NONBLOCK);
  467. if (res == -1) {
  468. api->log_printf ("Could not set non-blocking operation on server socket: %s\n", strerror (errno));
  469. api->fatal_error ("Could not set non-blocking operation on server socket");
  470. }
  471. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  472. un_addr.sun_family = AF_UNIX;
  473. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  474. un_addr.sun_len = sizeof(struct sockaddr_un);
  475. #endif
  476. #if defined(COROSYNC_LINUX)
  477. sprintf (un_addr.sun_path + 1, "%s", api->socket_name);
  478. #else
  479. sprintf (un_addr.sun_path, "%s/%s", SOCKETDIR, api->socket_name);
  480. unlink (un_addr.sun_path);
  481. #endif
  482. res = bind (server_fd, (struct sockaddr *)&un_addr, COROSYNC_SUN_LEN(&un_addr));
  483. if (res) {
  484. api->log_printf ("Could not bind AF_UNIX: %s.\n", strerror (errno));
  485. api->fatal_error ("Could not bind to AF_UNIX socket\n");
  486. }
  487. listen (server_fd, SERVER_BACKLOG);
  488. /*
  489. * Setup connection dispatch routine
  490. */
  491. api->poll_accept_add (server_fd);
  492. }
  493. void coroipcs_ipc_exit (void)
  494. {
  495. struct list_head *list;
  496. struct conn_info *conn_info;
  497. for (list = conn_info_list_head.next; list != &conn_info_list_head;
  498. list = list->next) {
  499. conn_info = list_entry (list, struct conn_info, list);
  500. shmdt (conn_info->mem);
  501. shmctl (conn_info->shmid, IPC_RMID, NULL);
  502. semctl (conn_info->semid, 0, IPC_RMID);
  503. pthread_kill (conn_info->thread, SIGUSR1);
  504. }
  505. }
  506. /*
  507. * Get the conn info private data
  508. */
  509. void *coroipcs_private_data_get (void *conn)
  510. {
  511. struct conn_info *conn_info = (struct conn_info *)conn;
  512. return (conn_info->private_data);
  513. }
  514. int coroipcs_response_send (void *conn, const void *msg, size_t mlen)
  515. {
  516. struct conn_info *conn_info = (struct conn_info *)conn;
  517. struct sembuf sop;
  518. int res;
  519. memcpy (conn_info->mem->res_buffer, msg, mlen);
  520. sop.sem_num = 1;
  521. sop.sem_op = 1;
  522. sop.sem_flg = 0;
  523. retry_semop:
  524. res = semop (conn_info->semid, &sop, 1);
  525. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  526. goto retry_semop;
  527. } else
  528. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  529. return (0);
  530. }
  531. return (0);
  532. }
  533. int coroipcs_response_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  534. {
  535. struct conn_info *conn_info = (struct conn_info *)conn;
  536. struct sembuf sop;
  537. int res;
  538. int write_idx = 0;
  539. int i;
  540. for (i = 0; i < iov_len; i++) {
  541. memcpy (&conn_info->mem->res_buffer[write_idx], iov[i].iov_base, iov[i].iov_len);
  542. write_idx += iov[i].iov_len;
  543. }
  544. sop.sem_num = 1;
  545. sop.sem_op = 1;
  546. sop.sem_flg = 0;
  547. retry_semop:
  548. res = semop (conn_info->semid, &sop, 1);
  549. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  550. goto retry_semop;
  551. } else
  552. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  553. return (0);
  554. }
  555. return (0);
  556. }
  557. static int shared_mem_dispatch_bytes_left (const struct conn_info *conn_info)
  558. {
  559. unsigned int n_read;
  560. unsigned int n_write;
  561. unsigned int bytes_left;
  562. n_read = conn_info->mem->read;
  563. n_write = conn_info->mem->write;
  564. if (n_read <= n_write) {
  565. bytes_left = DISPATCH_SIZE - n_write + n_read;
  566. } else {
  567. bytes_left = n_read - n_write;
  568. }
  569. return (bytes_left);
  570. }
  571. static void memcpy_dwrap (struct conn_info *conn_info, void *msg, unsigned int len)
  572. {
  573. unsigned int write_idx;
  574. write_idx = conn_info->mem->write;
  575. memcpy (&conn_info->dispatch_buffer[write_idx], msg, len);
  576. conn_info->mem->write = (write_idx + len) % (DISPATCH_SIZE);
  577. }
  578. static void msg_send (void *conn, const struct iovec *iov, unsigned int iov_len,
  579. int locked)
  580. {
  581. struct conn_info *conn_info = (struct conn_info *)conn;
  582. struct sembuf sop;
  583. int res;
  584. int i;
  585. char buf;
  586. for (i = 0; i < iov_len; i++) {
  587. memcpy_dwrap (conn_info, iov[i].iov_base, iov[i].iov_len);
  588. }
  589. buf = !list_empty (&conn_info->outq_head);
  590. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  591. if (res == -1 && errno == EAGAIN) {
  592. if (locked == 0) {
  593. pthread_mutex_lock (&conn_info->mutex);
  594. }
  595. conn_info->pending_semops += 1;
  596. if (locked == 0) {
  597. pthread_mutex_unlock (&conn_info->mutex);
  598. }
  599. api->poll_dispatch_modify (conn_info->fd,
  600. POLLIN|POLLOUT|POLLNVAL);
  601. } else
  602. if (res == -1) {
  603. ipc_disconnect (conn_info);
  604. }
  605. sop.sem_num = 2;
  606. sop.sem_op = 1;
  607. sop.sem_flg = 0;
  608. retry_semop:
  609. res = semop (conn_info->semid, &sop, 1);
  610. if ((res == -1) && (errno == EINTR || errno == EAGAIN)) {
  611. goto retry_semop;
  612. } else
  613. if ((res == -1) && (errno == EINVAL || errno == EIDRM)) {
  614. return;
  615. }
  616. }
  617. static void outq_flush (struct conn_info *conn_info) {
  618. struct list_head *list, *list_next;
  619. struct outq_item *outq_item;
  620. unsigned int bytes_left;
  621. struct iovec iov;
  622. char buf;
  623. int res;
  624. pthread_mutex_lock (&conn_info->mutex);
  625. if (list_empty (&conn_info->outq_head)) {
  626. buf = 3;
  627. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  628. pthread_mutex_unlock (&conn_info->mutex);
  629. return;
  630. }
  631. for (list = conn_info->outq_head.next;
  632. list != &conn_info->outq_head; list = list_next) {
  633. list_next = list->next;
  634. outq_item = list_entry (list, struct outq_item, list);
  635. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  636. if (bytes_left > outq_item->mlen) {
  637. iov.iov_base = outq_item->msg;
  638. iov.iov_len = outq_item->mlen;
  639. msg_send (conn_info, &iov, 1, MSG_SEND_UNLOCKED);
  640. list_del (list);
  641. api->free (iov.iov_base);
  642. api->free (outq_item);
  643. } else {
  644. break;
  645. }
  646. }
  647. pthread_mutex_unlock (&conn_info->mutex);
  648. }
  649. static int priv_change (struct conn_info *conn_info)
  650. {
  651. mar_req_priv_change req_priv_change;
  652. unsigned int res;
  653. union semun semun;
  654. struct semid_ds ipc_set;
  655. int i;
  656. retry_recv:
  657. res = recv (conn_info->fd, &req_priv_change,
  658. sizeof (mar_req_priv_change),
  659. MSG_NOSIGNAL);
  660. if (res == -1 && errno == EINTR) {
  661. goto retry_recv;
  662. }
  663. if (res == -1 && errno == EAGAIN) {
  664. goto retry_recv;
  665. }
  666. if (res == -1 && errno != EAGAIN) {
  667. return (-1);
  668. }
  669. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  670. /* Error on socket, EOF is detected when recv return 0
  671. */
  672. if (res == 0) {
  673. return (-1);
  674. }
  675. #endif
  676. ipc_set.sem_perm.uid = req_priv_change.euid;
  677. ipc_set.sem_perm.gid = req_priv_change.egid;
  678. ipc_set.sem_perm.mode = 0600;
  679. semun.buf = &ipc_set;
  680. for (i = 0; i < 3; i++) {
  681. res = semctl (conn_info->semid, 0, IPC_SET, semun);
  682. if (res == -1) {
  683. return (-1);
  684. }
  685. }
  686. return (0);
  687. }
  688. static void msg_send_or_queue (void *conn, const struct iovec *iov, unsigned int iov_len)
  689. {
  690. struct conn_info *conn_info = (struct conn_info *)conn;
  691. unsigned int bytes_left;
  692. unsigned int bytes_msg = 0;
  693. int i;
  694. struct outq_item *outq_item;
  695. char *write_buf = 0;
  696. /*
  697. * Exit transmission if the connection is dead
  698. */
  699. if (ipc_thread_active (conn) == 0) {
  700. return;
  701. }
  702. bytes_left = shared_mem_dispatch_bytes_left (conn_info);
  703. for (i = 0; i < iov_len; i++) {
  704. bytes_msg += iov[i].iov_len;
  705. }
  706. if (bytes_left < bytes_msg || list_empty (&conn_info->outq_head) == 0) {
  707. outq_item = api->malloc (sizeof (struct outq_item));
  708. if (outq_item == NULL) {
  709. ipc_disconnect (conn);
  710. return;
  711. }
  712. outq_item->msg = api->malloc (bytes_msg);
  713. if (outq_item->msg == 0) {
  714. api->free (outq_item);
  715. ipc_disconnect (conn);
  716. return;
  717. }
  718. write_buf = outq_item->msg;
  719. for (i = 0; i < iov_len; i++) {
  720. memcpy (write_buf, iov[i].iov_base, iov[i].iov_len);
  721. write_buf += iov[i].iov_len;
  722. }
  723. outq_item->mlen = bytes_msg;
  724. list_init (&outq_item->list);
  725. pthread_mutex_lock (&conn_info->mutex);
  726. if (list_empty (&conn_info->outq_head)) {
  727. conn_info->notify_flow_control_enabled = 1;
  728. api->poll_dispatch_modify (conn_info->fd,
  729. POLLIN|POLLOUT|POLLNVAL);
  730. }
  731. list_add_tail (&outq_item->list, &conn_info->outq_head);
  732. pthread_mutex_unlock (&conn_info->mutex);
  733. return;
  734. }
  735. msg_send (conn, iov, iov_len, MSG_SEND_LOCKED);
  736. }
  737. void coroipcs_refcount_inc (void *conn)
  738. {
  739. struct conn_info *conn_info = (struct conn_info *)conn;
  740. pthread_mutex_lock (&conn_info->mutex);
  741. conn_info->refcount++;
  742. pthread_mutex_unlock (&conn_info->mutex);
  743. }
  744. void coroipcs_refcount_dec (void *conn)
  745. {
  746. struct conn_info *conn_info = (struct conn_info *)conn;
  747. pthread_mutex_lock (&conn_info->mutex);
  748. conn_info->refcount--;
  749. pthread_mutex_unlock (&conn_info->mutex);
  750. }
  751. int coroipcs_dispatch_send (void *conn, const void *msg, size_t mlen)
  752. {
  753. struct iovec iov;
  754. iov.iov_base = (void *)msg;
  755. iov.iov_len = mlen;
  756. msg_send_or_queue (conn, &iov, 1);
  757. return (0);
  758. }
  759. int coroipcs_dispatch_iov_send (void *conn, const struct iovec *iov, unsigned int iov_len)
  760. {
  761. msg_send_or_queue (conn, iov, iov_len);
  762. return (0);
  763. }
  764. int coroipcs_handler_accept (
  765. int fd,
  766. int revent,
  767. void *data)
  768. {
  769. socklen_t addrlen;
  770. struct sockaddr_un un_addr;
  771. int new_fd;
  772. #ifdef COROSYNC_LINUX
  773. int on = 1;
  774. #endif
  775. int res;
  776. addrlen = sizeof (struct sockaddr_un);
  777. retry_accept:
  778. new_fd = accept (fd, (struct sockaddr *)&un_addr, &addrlen);
  779. if (new_fd == -1 && errno == EINTR) {
  780. goto retry_accept;
  781. }
  782. if (new_fd == -1) {
  783. api->log_printf ("Could not accept Library connection: %s\n", strerror (errno));
  784. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  785. }
  786. res = fcntl (new_fd, F_SETFL, O_NONBLOCK);
  787. if (res == -1) {
  788. api->log_printf ("Could not set non-blocking operation on library connection: %s\n", strerror (errno));
  789. close (new_fd);
  790. return (0); /* This is an error, but -1 would indicate disconnect from poll loop */
  791. }
  792. /*
  793. * Valid accept
  794. */
  795. /*
  796. * Request credentials of sender provided by kernel
  797. */
  798. #ifdef COROSYNC_LINUX
  799. setsockopt(new_fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on));
  800. #endif
  801. res = conn_info_create (new_fd);
  802. if (res != 0) {
  803. close (new_fd);
  804. }
  805. return (0);
  806. }
  807. static int
  808. coroipcs_memory_map (char *path, void **buf, size_t bytes)
  809. {
  810. int fd;
  811. void *addr_orig;
  812. void *addr;
  813. int res;
  814. fd = open (path, O_RDWR, 0600);
  815. unlink (path);
  816. res = ftruncate (fd, bytes);
  817. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  818. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  819. if (addr_orig == MAP_FAILED) {
  820. return (-1);
  821. }
  822. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  823. MAP_FIXED | MAP_SHARED, fd, 0);
  824. if (addr != addr_orig) {
  825. return (-1);
  826. }
  827. addr = mmap (((char *)addr_orig) + bytes,
  828. bytes, PROT_READ | PROT_WRITE,
  829. MAP_FIXED | MAP_SHARED, fd, 0);
  830. res = close (fd);
  831. if (res) {
  832. return (-1);
  833. }
  834. *buf = addr_orig;
  835. return (0);
  836. }
  837. int coroipcs_handler_dispatch (
  838. int fd,
  839. int revent,
  840. void *context)
  841. {
  842. mar_req_setup_t *req_setup;
  843. struct conn_info *conn_info = (struct conn_info *)context;
  844. int res;
  845. char buf;
  846. if (ipc_thread_exiting (conn_info)) {
  847. return conn_info_destroy (conn_info);
  848. }
  849. /*
  850. * If an error occurs, request exit
  851. */
  852. if (revent & (POLLERR|POLLHUP)) {
  853. ipc_disconnect (conn_info);
  854. return (0);
  855. }
  856. /*
  857. * Read the header and process it
  858. */
  859. if (conn_info->service == SOCKET_SERVICE_INIT && (revent & POLLIN)) {
  860. /*
  861. * Receive in a nonblocking fashion the request
  862. * IF security invalid, send TRY_AGAIN, otherwise
  863. * send OK
  864. */
  865. res = req_setup_recv (conn_info);
  866. if (res == -1) {
  867. req_setup_send (conn_info, CS_ERR_TRY_AGAIN);
  868. }
  869. if (res != 1) {
  870. return (0);
  871. }
  872. req_setup_send (conn_info, CS_OK);
  873. pthread_mutex_init (&conn_info->mutex, NULL);
  874. req_setup = (mar_req_setup_t *)conn_info->setup_msg;
  875. /*
  876. * Is the service registered ?
  877. */
  878. if (api->service_available (req_setup->service) == 0) {
  879. ipc_disconnect (conn_info);
  880. return (0);
  881. }
  882. conn_info->shmkey = req_setup->shmkey;
  883. conn_info->semkey = req_setup->semkey;
  884. res = coroipcs_memory_map (
  885. req_setup->dispatch_file,
  886. (void *)&conn_info->dispatch_buffer,
  887. DISPATCH_SIZE);
  888. conn_info->service = req_setup->service;
  889. conn_info->refcount = 0;
  890. conn_info->notify_flow_control_enabled = 0;
  891. conn_info->setup_bytes_read = 0;
  892. conn_info->shmid = shmget (conn_info->shmkey,
  893. sizeof (struct shared_memory), 0600);
  894. conn_info->mem = shmat (conn_info->shmid, NULL, 0);
  895. conn_info->semid = semget (conn_info->semkey, 3, 0600);
  896. conn_info->pending_semops = 0;
  897. /*
  898. * ipc thread is the only reference at startup
  899. */
  900. conn_info->refcount = 1;
  901. conn_info->state = CONN_STATE_THREAD_ACTIVE;
  902. conn_info->private_data = api->malloc (api->private_data_size_get (conn_info->service));
  903. memset (conn_info->private_data, 0,
  904. api->private_data_size_get (conn_info->service));
  905. api->init_fn_get (conn_info->service) (conn_info);
  906. pthread_attr_init (&conn_info->thread_attr);
  907. /*
  908. * IA64 needs more stack space then other arches
  909. */
  910. #if defined(__ia64__)
  911. pthread_attr_setstacksize (&conn_info->thread_attr, 400000);
  912. #else
  913. pthread_attr_setstacksize (&conn_info->thread_attr, 200000);
  914. #endif
  915. pthread_attr_setdetachstate (&conn_info->thread_attr, PTHREAD_CREATE_JOINABLE);
  916. res = pthread_create (&conn_info->thread,
  917. &conn_info->thread_attr,
  918. pthread_ipc_consumer,
  919. conn_info);
  920. /*
  921. * Security check - disallow multiple configurations of
  922. * the ipc connection
  923. */
  924. if (conn_info->service == SOCKET_SERVICE_INIT) {
  925. conn_info->service = -1;
  926. }
  927. } else
  928. if (revent & POLLIN) {
  929. coroipcs_refcount_inc (conn_info);
  930. res = recv (fd, &buf, 1, MSG_NOSIGNAL);
  931. if (res == 1) {
  932. switch (buf) {
  933. case MESSAGE_REQ_OUTQ_FLUSH:
  934. outq_flush (conn_info);
  935. break;
  936. case MESSAGE_REQ_CHANGE_EUID:
  937. if (priv_change (conn_info) == -1) {
  938. ipc_disconnect (conn_info);
  939. }
  940. break;
  941. default:
  942. res = 0;
  943. break;
  944. }
  945. coroipcs_refcount_dec (conn_info);
  946. }
  947. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  948. /* On many OS poll never return POLLHUP or POLLERR.
  949. * EOF is detected when recvmsg return 0.
  950. */
  951. if (res == 0) {
  952. ipc_disconnect (conn_info);
  953. return (0);
  954. }
  955. #endif
  956. }
  957. coroipcs_refcount_inc (conn_info);
  958. pthread_mutex_lock (&conn_info->mutex);
  959. if ((conn_info->state == CONN_STATE_THREAD_ACTIVE) && (revent & POLLOUT)) {
  960. buf = !list_empty (&conn_info->outq_head);
  961. for (; conn_info->pending_semops;) {
  962. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  963. if (res == 1) {
  964. conn_info->pending_semops--;
  965. } else {
  966. break;
  967. }
  968. }
  969. if (conn_info->notify_flow_control_enabled) {
  970. buf = 2;
  971. res = send (conn_info->fd, &buf, 1, MSG_NOSIGNAL);
  972. if (res == 1) {
  973. conn_info->notify_flow_control_enabled = 0;
  974. }
  975. }
  976. if (conn_info->notify_flow_control_enabled == 0 &&
  977. conn_info->pending_semops == 0) {
  978. api->poll_dispatch_modify (conn_info->fd,
  979. POLLIN|POLLNVAL);
  980. }
  981. }
  982. pthread_mutex_unlock (&conn_info->mutex);
  983. coroipcs_refcount_dec (conn_info);
  984. return (0);
  985. }