ipc.c 28 KB

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