ipc.c 27 KB

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