ipc.c 28 KB

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