ipc.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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. #include <config.h>
  35. #ifndef _GNU_SOURCE
  36. #define _GNU_SOURCE 1
  37. #endif
  38. #include <pthread.h>
  39. #include <assert.h>
  40. #include <pwd.h>
  41. #include <grp.h>
  42. #include <sys/types.h>
  43. #include <sys/poll.h>
  44. #include <sys/uio.h>
  45. #include <sys/mman.h>
  46. #include <sys/socket.h>
  47. #include <sys/un.h>
  48. #include <sys/time.h>
  49. #include <sys/resource.h>
  50. #include <sys/wait.h>
  51. #include <netinet/in.h>
  52. #include <arpa/inet.h>
  53. #include <unistd.h>
  54. #include <fcntl.h>
  55. #include <stdlib.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <signal.h>
  59. #include <sched.h>
  60. #include <time.h>
  61. #if defined(HAVE_GETPEERUCRED)
  62. #include <ucred.h>
  63. #endif
  64. #include <sys/shm.h>
  65. #include <sys/sem.h>
  66. #include <corosync/swab.h>
  67. #include <corosync/corotypes.h>
  68. #include <corosync/list.h>
  69. #include <corosync/queue.h>
  70. #include <corosync/lcr/lcr_ifact.h>
  71. #include <corosync/totem/coropoll.h>
  72. #include <corosync/totem/totempg.h>
  73. #include <corosync/engine/objdb.h>
  74. #include <corosync/engine/config.h>
  75. #include <corosync/engine/logsys.h>
  76. #include "quorum.h"
  77. #include "poll.h"
  78. #include "totemsrp.h"
  79. #include "mempool.h"
  80. #include "mainconfig.h"
  81. #include "totemconfig.h"
  82. #include "main.h"
  83. #include "tlist.h"
  84. #include "ipc.h"
  85. #include "sync.h"
  86. #include <corosync/engine/coroapi.h>
  87. #include "service.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. }