coroipcc.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. * vi: set autoindent tabstop=4 shiftwidth=4 :
  3. *
  4. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  5. * Copyright (c) 2006-2009 Red Hat, Inc.
  6. *
  7. * All rights reserved.
  8. *
  9. * Author: Steven Dake (sdake@redhat.com)
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <config.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <unistd.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. #include <fcntl.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/types.h>
  46. #include <sys/uio.h>
  47. #include <sys/socket.h>
  48. #include <sys/select.h>
  49. #include <sys/time.h>
  50. #include <sys/un.h>
  51. #include <net/if.h>
  52. #include <arpa/inet.h>
  53. #include <netinet/in.h>
  54. #include <assert.h>
  55. #include <sys/shm.h>
  56. #include <sys/sem.h>
  57. #include <sys/mman.h>
  58. #include <corosync/corotypes.h>
  59. #include <corosync/coroipc_types.h>
  60. #include <corosync/coroipc_ipc.h>
  61. #include <corosync/coroipcc.h>
  62. enum SA_HANDLE_STATE {
  63. SA_HANDLE_STATE_EMPTY,
  64. SA_HANDLE_STATE_PENDINGREMOVAL,
  65. SA_HANDLE_STATE_ACTIVE
  66. };
  67. struct saHandle {
  68. int state;
  69. void *instance;
  70. int refCount;
  71. uint32_t check;
  72. };
  73. struct ipc_segment {
  74. int fd;
  75. int shmid;
  76. int semid;
  77. int flow_control_state;
  78. struct control_buffer *control_buffer;
  79. char *request_buffer;
  80. char *response_buffer;
  81. char *dispatch_buffer;
  82. size_t control_size;
  83. size_t request_size;
  84. size_t response_size;
  85. size_t dispatch_size;
  86. uid_t euid;
  87. };
  88. #if defined(COROSYNC_LINUX)
  89. /* SUN_LEN is broken for abstract namespace
  90. */
  91. #define AIS_SUN_LEN(a) sizeof(*(a))
  92. #else
  93. #define AIS_SUN_LEN(a) SUN_LEN(a)
  94. #endif
  95. #ifdef SO_NOSIGPIPE
  96. void socket_nosigpipe(int s)
  97. {
  98. int on = 1;
  99. setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
  100. }
  101. #endif
  102. #ifndef MSG_NOSIGNAL
  103. #define MSG_NOSIGNAL 0
  104. #endif
  105. static int
  106. coroipcc_send (
  107. int s,
  108. void *msg,
  109. size_t len)
  110. {
  111. int result;
  112. struct msghdr msg_send;
  113. struct iovec iov_send;
  114. char *rbuf = msg;
  115. int processed = 0;
  116. msg_send.msg_iov = &iov_send;
  117. msg_send.msg_iovlen = 1;
  118. msg_send.msg_name = 0;
  119. msg_send.msg_namelen = 0;
  120. msg_send.msg_control = 0;
  121. msg_send.msg_controllen = 0;
  122. msg_send.msg_flags = 0;
  123. retry_send:
  124. iov_send.iov_base = &rbuf[processed];
  125. iov_send.iov_len = len - processed;
  126. result = sendmsg (s, &msg_send, MSG_NOSIGNAL);
  127. /*
  128. * return immediately on any kind of syscall error that maps to
  129. * CS_ERR if no part of message has been sent
  130. */
  131. if (result == -1 && processed == 0) {
  132. if (errno == EINTR) {
  133. goto error_exit;
  134. }
  135. if (errno == EAGAIN) {
  136. goto error_exit;
  137. }
  138. if (errno == EFAULT) {
  139. goto error_exit;
  140. }
  141. }
  142. /*
  143. * retry read operations that are already started except
  144. * for fault in that case, return ERR_LIBRARY
  145. */
  146. if (result == -1 && processed > 0) {
  147. if (errno == EINTR) {
  148. goto retry_send;
  149. }
  150. if (errno == EAGAIN) {
  151. goto retry_send;
  152. }
  153. if (errno == EFAULT) {
  154. goto error_exit;
  155. }
  156. }
  157. /*
  158. * return ERR_LIBRARY on any other syscall error
  159. */
  160. if (result == -1) {
  161. goto error_exit;
  162. }
  163. processed += result;
  164. if (processed != len) {
  165. goto retry_send;
  166. }
  167. return (0);
  168. error_exit:
  169. return (-1);
  170. }
  171. static int
  172. coroipcc_recv (
  173. int s,
  174. void *msg,
  175. size_t len)
  176. {
  177. int error = 0;
  178. int result;
  179. struct msghdr msg_recv;
  180. struct iovec iov_recv;
  181. char *rbuf = msg;
  182. int processed = 0;
  183. msg_recv.msg_iov = &iov_recv;
  184. msg_recv.msg_iovlen = 1;
  185. msg_recv.msg_name = 0;
  186. msg_recv.msg_namelen = 0;
  187. msg_recv.msg_control = 0;
  188. msg_recv.msg_controllen = 0;
  189. msg_recv.msg_flags = 0;
  190. retry_recv:
  191. iov_recv.iov_base = (void *)&rbuf[processed];
  192. iov_recv.iov_len = len - processed;
  193. result = recvmsg (s, &msg_recv, MSG_NOSIGNAL|MSG_WAITALL);
  194. if (result == -1 && errno == EINTR) {
  195. goto retry_recv;
  196. }
  197. if (result == -1 && errno == EAGAIN) {
  198. goto retry_recv;
  199. }
  200. #if defined(COROSYNC_SOLARIS) || defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  201. /* On many OS poll never return POLLHUP or POLLERR.
  202. * EOF is detected when recvmsg return 0.
  203. */
  204. if (result == 0) {
  205. error = -1;
  206. goto error_exit;
  207. }
  208. #endif
  209. if (result == -1 || result == 0) {
  210. error = -1;
  211. goto error_exit;
  212. }
  213. processed += result;
  214. if (processed != len) {
  215. goto retry_recv;
  216. }
  217. assert (processed == len);
  218. error_exit:
  219. return (0);
  220. }
  221. static int
  222. priv_change_send (struct ipc_segment *ipc_segment)
  223. {
  224. char buf_req;
  225. mar_req_priv_change req_priv_change;
  226. unsigned int res;
  227. req_priv_change.euid = geteuid();
  228. /*
  229. * Don't resend request unless euid has changed
  230. */
  231. if (ipc_segment->euid == req_priv_change.euid) {
  232. return (0);
  233. }
  234. req_priv_change.egid = getegid();
  235. buf_req = MESSAGE_REQ_CHANGE_EUID;
  236. res = coroipcc_send (ipc_segment->fd, &buf_req, 1);
  237. if (res == -1) {
  238. return (-1);
  239. }
  240. res = coroipcc_send (ipc_segment->fd, &req_priv_change,
  241. sizeof (req_priv_change));
  242. if (res == -1) {
  243. return (-1);
  244. }
  245. ipc_segment->euid = req_priv_change.euid;
  246. return (0);
  247. }
  248. #if defined(_SEM_SEMUN_UNDEFINED)
  249. union semun {
  250. int val;
  251. struct semid_ds *buf;
  252. unsigned short int *array;
  253. struct seminfo *__buf;
  254. };
  255. #endif
  256. static int
  257. circular_memory_map (char *path, const char *file, void **buf, size_t bytes)
  258. {
  259. int fd;
  260. void *addr_orig;
  261. void *addr;
  262. int res;
  263. sprintf (path, "/dev/shm/%s", file);
  264. fd = mkstemp (path);
  265. if (fd == -1) {
  266. sprintf (path, "/var/run/%s", file);
  267. fd = mkstemp (path);
  268. if (fd == -1) {
  269. return (-1);
  270. }
  271. }
  272. res = ftruncate (fd, bytes);
  273. addr_orig = mmap (NULL, bytes << 1, PROT_NONE,
  274. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  275. if (addr_orig == MAP_FAILED) {
  276. return (-1);
  277. }
  278. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  279. MAP_FIXED | MAP_SHARED, fd, 0);
  280. if (addr != addr_orig) {
  281. return (-1);
  282. }
  283. addr = mmap (((char *)addr_orig) + bytes,
  284. bytes, PROT_READ | PROT_WRITE,
  285. MAP_FIXED | MAP_SHARED, fd, 0);
  286. res = close (fd);
  287. if (res) {
  288. return (-1);
  289. }
  290. *buf = addr_orig;
  291. return (0);
  292. }
  293. static void
  294. memory_unmap (void *addr, size_t bytes)
  295. {
  296. int res;
  297. res = munmap (addr, bytes);
  298. }
  299. static int
  300. memory_map (char *path, const char *file, void **buf, size_t bytes)
  301. {
  302. int fd;
  303. void *addr_orig;
  304. void *addr;
  305. int res;
  306. sprintf (path, "/dev/shm/%s", file);
  307. fd = mkstemp (path);
  308. if (fd == -1) {
  309. sprintf (path, "/var/run/%s", file);
  310. fd = mkstemp (path);
  311. if (fd == -1) {
  312. return (-1);
  313. }
  314. }
  315. res = ftruncate (fd, bytes);
  316. addr_orig = mmap (NULL, bytes, PROT_NONE,
  317. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  318. if (addr_orig == MAP_FAILED) {
  319. return (-1);
  320. }
  321. addr = mmap (addr_orig, bytes, PROT_READ | PROT_WRITE,
  322. MAP_FIXED | MAP_SHARED, fd, 0);
  323. if (addr != addr_orig) {
  324. return (-1);
  325. }
  326. res = close (fd);
  327. if (res) {
  328. return (-1);
  329. }
  330. *buf = addr_orig;
  331. return (0);
  332. }
  333. extern cs_error_t
  334. coroipcc_service_connect (
  335. const char *socket_name,
  336. unsigned int service,
  337. size_t request_size,
  338. size_t response_size,
  339. size_t dispatch_size,
  340. void **ipc_context)
  341. {
  342. int request_fd;
  343. struct sockaddr_un address;
  344. cs_error_t error;
  345. struct ipc_segment *ipc_segment;
  346. key_t semkey = 0;
  347. int res;
  348. mar_req_setup_t req_setup;
  349. mar_res_setup_t res_setup;
  350. union semun semun;
  351. char control_map_path[128];
  352. char request_map_path[128];
  353. char response_map_path[128];
  354. char dispatch_map_path[128];
  355. res_setup.error = CS_ERR_LIBRARY;
  356. request_fd = socket (PF_UNIX, SOCK_STREAM, 0);
  357. if (request_fd == -1) {
  358. return (-1);
  359. }
  360. memset (&address, 0, sizeof (struct sockaddr_un));
  361. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  362. address.sun_len = sizeof(struct sockaddr_un);
  363. #endif
  364. address.sun_family = PF_UNIX;
  365. #if defined(COROSYNC_LINUX)
  366. sprintf (address.sun_path + 1, "%s", socket_name);
  367. #else
  368. sprintf (address.sun_path, "%s/%s", SOCKETDIR, socket_name);
  369. #endif
  370. res = connect (request_fd, (struct sockaddr *)&address,
  371. AIS_SUN_LEN(&address));
  372. if (res == -1) {
  373. close (request_fd);
  374. return (CS_ERR_TRY_AGAIN);
  375. }
  376. ipc_segment = malloc (sizeof (struct ipc_segment));
  377. if (ipc_segment == NULL) {
  378. close (request_fd);
  379. return (-1);
  380. }
  381. bzero (ipc_segment, sizeof (struct ipc_segment));
  382. /*
  383. * Allocate a semaphore segment
  384. */
  385. while (1) {
  386. semkey = random();
  387. ipc_segment->euid = geteuid ();
  388. if ((ipc_segment->semid
  389. = semget (semkey, 3, IPC_CREAT|IPC_EXCL|0600)) != -1) {
  390. break;
  391. }
  392. if (errno != EEXIST) {
  393. goto error_exit;
  394. }
  395. }
  396. semun.val = 0;
  397. res = semctl (ipc_segment->semid, 0, SETVAL, semun);
  398. if (res != 0) {
  399. goto error_exit;
  400. }
  401. res = semctl (ipc_segment->semid, 1, SETVAL, semun);
  402. if (res != 0) {
  403. goto error_exit;
  404. }
  405. res = memory_map (
  406. control_map_path,
  407. "control_buffer-XXXXXX",
  408. (void *)&ipc_segment->control_buffer,
  409. 8192);
  410. res = memory_map (
  411. request_map_path,
  412. "request_buffer-XXXXXX",
  413. (void *)&ipc_segment->request_buffer,
  414. request_size);
  415. res = memory_map (
  416. response_map_path,
  417. "response_buffer-XXXXXX",
  418. (void *)&ipc_segment->response_buffer,
  419. response_size);
  420. res = circular_memory_map (
  421. dispatch_map_path,
  422. "dispatch_buffer-XXXXXX",
  423. (void *)&ipc_segment->dispatch_buffer,
  424. dispatch_size);
  425. /*
  426. * Initialize IPC setup message
  427. */
  428. req_setup.service = service;
  429. strcpy (req_setup.control_file, control_map_path);
  430. strcpy (req_setup.request_file, request_map_path);
  431. strcpy (req_setup.response_file, response_map_path);
  432. strcpy (req_setup.dispatch_file, dispatch_map_path);
  433. req_setup.control_size = 8192;
  434. req_setup.request_size = request_size;
  435. req_setup.response_size = response_size;
  436. req_setup.dispatch_size = dispatch_size;
  437. req_setup.semkey = semkey;
  438. error = coroipcc_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
  439. if (error != 0) {
  440. goto error_exit;
  441. }
  442. error = coroipcc_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
  443. if (error != 0) {
  444. goto error_exit;
  445. }
  446. ipc_segment->fd = request_fd;
  447. ipc_segment->flow_control_state = 0;
  448. if (res_setup.error == CS_ERR_TRY_AGAIN) {
  449. goto error_exit;
  450. }
  451. ipc_segment->control_size = 8192;
  452. ipc_segment->request_size = request_size;
  453. ipc_segment->response_size = response_size;
  454. ipc_segment->dispatch_size = dispatch_size;
  455. *ipc_context = ipc_segment;
  456. return (res_setup.error);
  457. error_exit:
  458. close (request_fd);
  459. if (ipc_segment->semid > 0)
  460. semctl (ipc_segment->semid, 0, IPC_RMID);
  461. return (res_setup.error);
  462. }
  463. cs_error_t
  464. coroipcc_service_disconnect (
  465. void *ipc_context)
  466. {
  467. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  468. shutdown (ipc_segment->fd, SHUT_RDWR);
  469. close (ipc_segment->fd);
  470. /*
  471. * << 1 (or multiplied by 2) because this is a wrapped memory buffer
  472. */
  473. memory_unmap (ipc_segment->control_buffer, ipc_segment->control_size);
  474. memory_unmap (ipc_segment->request_buffer, ipc_segment->request_size);
  475. memory_unmap (ipc_segment->response_buffer, ipc_segment->response_size);
  476. memory_unmap (ipc_segment->dispatch_buffer, (ipc_segment->dispatch_size) << 1);
  477. free (ipc_segment);
  478. return (CS_OK);
  479. }
  480. int
  481. coroipcc_dispatch_flow_control_get (
  482. void *ipc_context)
  483. {
  484. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  485. return (ipc_segment->flow_control_state);
  486. }
  487. int
  488. coroipcc_fd_get (void *ipc_ctx)
  489. {
  490. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  491. return (ipc_segment->fd);
  492. }
  493. int
  494. coroipcc_dispatch_get (void *ipc_ctx, void **data, int timeout)
  495. {
  496. struct pollfd ufds;
  497. int poll_events;
  498. char buf;
  499. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  500. int res;
  501. char buf_two = 1;
  502. char *data_addr;
  503. ufds.fd = ipc_segment->fd;
  504. ufds.events = POLLIN;
  505. ufds.revents = 0;
  506. retry_poll:
  507. poll_events = poll (&ufds, 1, timeout);
  508. if (poll_events == -1 && errno == EINTR) {
  509. goto retry_poll;
  510. } else
  511. if (poll_events == -1) {
  512. return (-1);
  513. } else
  514. if (poll_events == 0) {
  515. return (0);
  516. }
  517. if (poll_events == 1 && (ufds.revents & (POLLERR|POLLHUP))) {
  518. return (-1);
  519. }
  520. retry_recv:
  521. res = recv (ipc_segment->fd, &buf, 1, 0);
  522. if (res == -1 && errno == EINTR) {
  523. goto retry_recv;
  524. } else
  525. if (res == -1) {
  526. return (-1);
  527. }
  528. if (res == 0) {
  529. return (-1);
  530. }
  531. ipc_segment->flow_control_state = 0;
  532. if (buf == 1 || buf == 2) {
  533. ipc_segment->flow_control_state = 1;
  534. }
  535. /*
  536. * Notify executive to flush any pending dispatch messages
  537. */
  538. if (ipc_segment->flow_control_state) {
  539. buf_two = MESSAGE_REQ_OUTQ_FLUSH;
  540. res = coroipcc_send (ipc_segment->fd, &buf_two, 1);
  541. assert (res == 0); //TODO
  542. }
  543. /*
  544. * This is just a notification of flow control starting at the addition
  545. * of a new pending message, not a message to dispatch
  546. */
  547. if (buf == 2) {
  548. return (0);
  549. }
  550. if (buf == 3) {
  551. return (0);
  552. }
  553. data_addr = ipc_segment->dispatch_buffer;
  554. data_addr = &data_addr[ipc_segment->control_buffer->read];
  555. *data = (void *)data_addr;
  556. return (1);
  557. }
  558. int
  559. coroipcc_dispatch_put (void *ipc_ctx)
  560. {
  561. struct sembuf sop;
  562. coroipc_response_header_t *header;
  563. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_ctx;
  564. int res;
  565. char *addr;
  566. unsigned int read_idx;
  567. sop.sem_num = 2;
  568. sop.sem_op = -1;
  569. sop.sem_flg = 0;
  570. retry_semop:
  571. res = semop (ipc_segment->semid, &sop, 1);
  572. if (res == -1 && errno == EINTR) {
  573. goto retry_semop;
  574. } else
  575. if (res == -1 && errno == EACCES) {
  576. priv_change_send (ipc_segment);
  577. goto retry_semop;
  578. } else
  579. if (res == -1) {
  580. return (-1);
  581. }
  582. addr = ipc_segment->dispatch_buffer;
  583. read_idx = ipc_segment->control_buffer->read;
  584. header = (coroipc_response_header_t *) &addr[read_idx];
  585. ipc_segment->control_buffer->read =
  586. (read_idx + header->size) % ipc_segment->dispatch_size;
  587. return (0);
  588. }
  589. static cs_error_t
  590. coroipcc_msg_send (
  591. void *ipc_context,
  592. const struct iovec *iov,
  593. unsigned int iov_len)
  594. {
  595. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  596. struct sembuf sop;
  597. int i;
  598. int res;
  599. int req_buffer_idx = 0;
  600. for (i = 0; i < iov_len; i++) {
  601. memcpy (&ipc_segment->request_buffer[req_buffer_idx],
  602. iov[i].iov_base,
  603. iov[i].iov_len);
  604. req_buffer_idx += iov[i].iov_len;
  605. }
  606. /*
  607. * Signal semaphore #0 indicting a new message from client
  608. * to server request queue
  609. */
  610. sop.sem_num = 0;
  611. sop.sem_op = 1;
  612. sop.sem_flg = 0;
  613. retry_semop:
  614. res = semop (ipc_segment->semid, &sop, 1);
  615. if (res == -1 && errno == EINTR) {
  616. goto retry_semop;
  617. } else
  618. if (res == -1 && errno == EACCES) {
  619. priv_change_send (ipc_segment);
  620. goto retry_semop;
  621. } else
  622. if (res == -1) {
  623. return (CS_ERR_LIBRARY);
  624. }
  625. return (CS_OK);
  626. }
  627. static cs_error_t
  628. coroipcc_reply_receive (
  629. void *ipc_context,
  630. void *res_msg, size_t res_len)
  631. {
  632. struct sembuf sop;
  633. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  634. int res;
  635. /*
  636. * Wait for semaphore #1 indicating a new message from server
  637. * to client in the response queue
  638. */
  639. sop.sem_num = 1;
  640. sop.sem_op = -1;
  641. sop.sem_flg = 0;
  642. retry_semop:
  643. res = semop (ipc_segment->semid, &sop, 1);
  644. if (res == -1 && errno == EINTR) {
  645. goto retry_semop;
  646. } else
  647. if (res == -1 && errno == EACCES) {
  648. priv_change_send (ipc_segment);
  649. goto retry_semop;
  650. } else
  651. if (res == -1) {
  652. return (CS_ERR_LIBRARY);
  653. }
  654. memcpy (res_msg, ipc_segment->response_buffer, res_len);
  655. return (CS_OK);
  656. }
  657. static cs_error_t
  658. coroipcc_reply_receive_in_buf (
  659. void *ipc_context,
  660. void **res_msg)
  661. {
  662. struct sembuf sop;
  663. struct ipc_segment *ipc_segment = (struct ipc_segment *)ipc_context;
  664. int res;
  665. /*
  666. * Wait for semaphore #1 indicating a new message from server
  667. * to client in the response queue
  668. */
  669. sop.sem_num = 1;
  670. sop.sem_op = -1;
  671. sop.sem_flg = 0;
  672. retry_semop:
  673. res = semop (ipc_segment->semid, &sop, 1);
  674. if (res == -1 && errno == EINTR) {
  675. goto retry_semop;
  676. } else
  677. if (res == -1 && errno == EACCES) {
  678. priv_change_send (ipc_segment);
  679. goto retry_semop;
  680. } else
  681. if (res == -1) {
  682. return (CS_ERR_LIBRARY);
  683. }
  684. *res_msg = (char *)ipc_segment->response_buffer;
  685. return (CS_OK);
  686. }
  687. cs_error_t
  688. coroipcc_msg_send_reply_receive (
  689. void *ipc_context,
  690. const struct iovec *iov,
  691. unsigned int iov_len,
  692. void *res_msg,
  693. size_t res_len)
  694. {
  695. cs_error_t res;
  696. res = coroipcc_msg_send (ipc_context, iov, iov_len);
  697. if (res != CS_OK) {
  698. return (res);
  699. }
  700. res = coroipcc_reply_receive (ipc_context, res_msg, res_len);
  701. if (res != CS_OK) {
  702. return (res);
  703. }
  704. return (CS_OK);
  705. }
  706. cs_error_t
  707. coroipcc_msg_send_reply_receive_in_buf (
  708. void *ipc_context,
  709. const struct iovec *iov,
  710. unsigned int iov_len,
  711. void **res_msg)
  712. {
  713. unsigned int res;
  714. res = coroipcc_msg_send (ipc_context, iov, iov_len);
  715. if (res != CS_OK) {
  716. return (res);
  717. }
  718. res = coroipcc_reply_receive_in_buf (ipc_context, res_msg);
  719. if (res != CS_OK) {
  720. return (res);
  721. }
  722. return (CS_OK);
  723. }
  724. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  725. static void hdb_lock (struct saHandleDatabase *hdb)
  726. {
  727. pthread_spin_lock (&hdb->lock);
  728. }
  729. static void hdb_unlock (struct saHandleDatabase *hdb)
  730. {
  731. pthread_spin_unlock (&hdb->lock);
  732. }
  733. void saHandleDatabaseLock_init (struct saHandleDatabase *hdb)
  734. {
  735. pthread_spin_init (&hdb->lock, 0);
  736. }
  737. #else
  738. static void hdb_lock (struct saHandleDatabase *hdb)
  739. {
  740. pthread_mutex_lock (&hdb->lock);
  741. }
  742. static void hdb_unlock (struct saHandleDatabase *hdb)
  743. {
  744. pthread_mutex_unlock (&hdb->lock);
  745. }
  746. void saHandleDatabaseLock_init (struct saHandleDatabase *hdb)
  747. {
  748. pthread_mutex_init (&hdb->lock, NULL);
  749. }
  750. #endif
  751. cs_error_t
  752. coroipcc_zcb_alloc (
  753. void *ipc_context,
  754. void **buffer,
  755. size_t size,
  756. size_t header_size)
  757. {
  758. void *buf = NULL;
  759. char path[128];
  760. unsigned int res;
  761. mar_req_coroipcc_zc_alloc_t req_coroipcc_zc_alloc;
  762. coroipc_response_header_t res_coroipcs_zc_alloc;
  763. size_t map_size;
  764. struct iovec iovec;
  765. struct coroipcs_zc_header *hdr;
  766. map_size = size + header_size + sizeof (struct coroipcs_zc_header);
  767. res = memory_map (path, "cpg_zc-XXXXXX", &buf, size);
  768. assert (res != -1);
  769. req_coroipcc_zc_alloc.header.size = sizeof (mar_req_coroipcc_zc_alloc_t);
  770. req_coroipcc_zc_alloc.header.id = ZC_ALLOC_HEADER;
  771. req_coroipcc_zc_alloc.map_size = map_size;
  772. strcpy (req_coroipcc_zc_alloc.path_to_file, path);
  773. iovec.iov_base = &req_coroipcc_zc_alloc;
  774. iovec.iov_len = sizeof (mar_req_coroipcc_zc_alloc_t);
  775. res = coroipcc_msg_send_reply_receive (
  776. ipc_context,
  777. &iovec,
  778. 1,
  779. &res_coroipcs_zc_alloc,
  780. sizeof (coroipc_response_header_t));
  781. hdr = (struct coroipcs_zc_header *)buf;
  782. hdr->map_size = map_size;
  783. *buffer = ((char *)buf) + sizeof (struct coroipcs_zc_header);
  784. return (CS_OK);
  785. }
  786. cs_error_t
  787. coroipcc_zcb_free (
  788. void *ipc_context,
  789. void *buffer)
  790. {
  791. mar_req_coroipcc_zc_free_t req_coroipcc_zc_free;
  792. coroipc_response_header_t res_coroipcs_zc_free;
  793. struct iovec iovec;
  794. unsigned int res;
  795. struct coroipcs_zc_header *header = (struct coroipcs_zc_header *)((char *)buffer - sizeof (struct coroipcs_zc_header));
  796. req_coroipcc_zc_free.header.size = sizeof (mar_req_coroipcc_zc_free_t);
  797. req_coroipcc_zc_free.header.id = ZC_FREE_HEADER;
  798. req_coroipcc_zc_free.map_size = header->map_size;
  799. req_coroipcc_zc_free.server_address = header->server_address;
  800. iovec.iov_base = &req_coroipcc_zc_free;
  801. iovec.iov_len = sizeof (mar_req_coroipcc_zc_free_t);
  802. res = coroipcc_msg_send_reply_receive (
  803. ipc_context,
  804. &iovec,
  805. 1,
  806. &res_coroipcs_zc_free,
  807. sizeof (coroipc_response_header_t));
  808. munmap (header, header->map_size);
  809. return (CS_OK);
  810. }
  811. cs_error_t
  812. coroipcc_zcb_msg_send_reply_receive (
  813. void *ipc_context,
  814. void *msg,
  815. void *res_msg,
  816. size_t res_len)
  817. {
  818. mar_req_coroipcc_zc_execute_t req_coroipcc_zc_execute;
  819. struct coroipcs_zc_header *hdr;
  820. struct iovec iovec;
  821. cs_error_t res;
  822. hdr = (struct coroipcs_zc_header *)(((char *)msg) - sizeof (struct coroipcs_zc_header));
  823. req_coroipcc_zc_execute.header.size = sizeof (mar_req_coroipcc_zc_execute_t);
  824. req_coroipcc_zc_execute.header.id = ZC_EXECUTE_HEADER;
  825. req_coroipcc_zc_execute.server_address = hdr->server_address;
  826. iovec.iov_base = &req_coroipcc_zc_execute;
  827. iovec.iov_len = sizeof (mar_req_coroipcc_zc_execute_t);
  828. res = coroipcc_msg_send_reply_receive (
  829. ipc_context,
  830. &iovec,
  831. 1,
  832. res_msg,
  833. res_len);
  834. return (res);
  835. }
  836. cs_error_t
  837. saHandleCreate (
  838. struct saHandleDatabase *handleDatabase,
  839. int instanceSize,
  840. uint64_t *handleOut)
  841. {
  842. uint32_t handle;
  843. uint32_t check;
  844. void *newHandles = NULL;
  845. int found = 0;
  846. void *instance;
  847. int i;
  848. hdb_lock (handleDatabase);
  849. for (handle = 0; handle < handleDatabase->handleCount; handle++) {
  850. if (handleDatabase->handles[handle].state == SA_HANDLE_STATE_EMPTY) {
  851. found = 1;
  852. break;
  853. }
  854. }
  855. if (found == 0) {
  856. handleDatabase->handleCount += 1;
  857. newHandles = (struct saHandle *)realloc (handleDatabase->handles,
  858. sizeof (struct saHandle) * handleDatabase->handleCount);
  859. if (newHandles == NULL) {
  860. hdb_unlock (handleDatabase);
  861. return (CS_ERR_NO_MEMORY);
  862. }
  863. handleDatabase->handles = newHandles;
  864. }
  865. instance = malloc (instanceSize);
  866. if (instance == 0) {
  867. free (newHandles);
  868. hdb_unlock (handleDatabase);
  869. return (CS_ERR_NO_MEMORY);
  870. }
  871. /*
  872. * This code makes sure the random number isn't zero
  873. * We use 0 to specify an invalid handle out of the 1^64 address space
  874. * If we get 0 200 times in a row, the RNG may be broken
  875. */
  876. for (i = 0; i < 200; i++) {
  877. check = random();
  878. if (check != 0) {
  879. break;
  880. }
  881. }
  882. memset (instance, 0, instanceSize);
  883. handleDatabase->handles[handle].state = SA_HANDLE_STATE_ACTIVE;
  884. handleDatabase->handles[handle].instance = instance;
  885. handleDatabase->handles[handle].refCount = 1;
  886. handleDatabase->handles[handle].check = check;
  887. *handleOut = (uint64_t)((uint64_t)check << 32 | handle);
  888. hdb_unlock (handleDatabase);
  889. return (CS_OK);
  890. }
  891. cs_error_t
  892. saHandleDestroy (
  893. struct saHandleDatabase *handleDatabase,
  894. uint64_t inHandle)
  895. {
  896. cs_error_t error = CS_OK;
  897. uint32_t check = inHandle >> 32;
  898. uint32_t handle = inHandle & 0xffffffff;
  899. hdb_lock (handleDatabase);
  900. if (check != handleDatabase->handles[handle].check) {
  901. hdb_unlock (handleDatabase);
  902. error = CS_ERR_BAD_HANDLE;
  903. return (error);
  904. }
  905. handleDatabase->handles[handle].state = SA_HANDLE_STATE_PENDINGREMOVAL;
  906. hdb_unlock (handleDatabase);
  907. saHandleInstancePut (handleDatabase, inHandle);
  908. return (error);
  909. }
  910. cs_error_t
  911. saHandleInstanceGet (
  912. struct saHandleDatabase *handleDatabase,
  913. uint64_t inHandle,
  914. void **instance)
  915. {
  916. uint32_t check = inHandle >> 32;
  917. uint32_t handle = inHandle & 0xffffffff;
  918. cs_error_t error = CS_OK;
  919. hdb_lock (handleDatabase);
  920. if (handle >= (uint64_t)handleDatabase->handleCount) {
  921. error = CS_ERR_BAD_HANDLE;
  922. goto error_exit;
  923. }
  924. if (handleDatabase->handles[handle].state != SA_HANDLE_STATE_ACTIVE) {
  925. error = CS_ERR_BAD_HANDLE;
  926. goto error_exit;
  927. }
  928. if (check != handleDatabase->handles[handle].check) {
  929. error = CS_ERR_BAD_HANDLE;
  930. goto error_exit;
  931. }
  932. *instance = handleDatabase->handles[handle].instance;
  933. handleDatabase->handles[handle].refCount += 1;
  934. error_exit:
  935. hdb_unlock (handleDatabase);
  936. return (error);
  937. }
  938. cs_error_t
  939. saHandleInstancePut (
  940. struct saHandleDatabase *handleDatabase,
  941. uint64_t inHandle)
  942. {
  943. void *instance;
  944. cs_error_t error = CS_OK;
  945. uint32_t check = inHandle >> 32;
  946. uint32_t handle = inHandle & 0xffffffff;
  947. hdb_lock (handleDatabase);
  948. if (check != handleDatabase->handles[handle].check) {
  949. error = CS_ERR_BAD_HANDLE;
  950. goto error_exit;
  951. }
  952. handleDatabase->handles[handle].refCount -= 1;
  953. assert (handleDatabase->handles[handle].refCount >= 0);
  954. if (handleDatabase->handles[handle].refCount == 0) {
  955. instance = (handleDatabase->handles[handle].instance);
  956. handleDatabase->handleInstanceDestructor (instance);
  957. free (instance);
  958. memset (&handleDatabase->handles[handle], 0, sizeof (struct saHandle));
  959. }
  960. error_exit:
  961. hdb_unlock (handleDatabase);
  962. return (error);
  963. }