coroipcs.c 26 KB

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