ipc.c 29 KB

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