ipc.c 29 KB

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