ipc.c 28 KB

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