coroipcs.c 26 KB

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