ipc.c 27 KB

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