ipc.c 27 KB

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