coroipcs.c 26 KB

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