ipc.c 28 KB

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