coroipcs.c 27 KB

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