uis.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2006 Steven Dake (sdake@redhat.com)
  3. *
  4. * This software licensed under BSD license, the text of which follows:
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from this
  16. * software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  28. * THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <config.h>
  31. #include <sys/uio.h>
  32. #include <sys/mman.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <sys/un.h>
  36. #include <sys/time.h>
  37. #include <sys/resource.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <errno.h>
  45. #include <sched.h>
  46. #include <time.h>
  47. #include <pthread.h>
  48. #include <sys/poll.h>
  49. #include <string.h>
  50. #define SERVER_BACKLOG 5
  51. #if defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS)
  52. /* SUN_LEN is broken for abstract namespace
  53. */
  54. #define AIS_SUN_LEN(a) sizeof(*(a))
  55. #else
  56. #define AIS_SUN_LEN(a) SUN_LEN(a)
  57. #endif
  58. #ifdef COROSYNC_LINUX
  59. static const char *socketname = "lcr.socket";
  60. #else
  61. static const char *socketname = SOCKETDIR "/lcr.socket";
  62. #endif
  63. static void uis_lcr_bind (int *server_fd)
  64. {
  65. int fd;
  66. struct sockaddr_un un_addr;
  67. int res;
  68. /*
  69. * Create socket for lcr clients, name socket, listen for connections
  70. */
  71. fd = socket (PF_UNIX, SOCK_STREAM, 0);
  72. if (fd == -1) {
  73. perror ("uis_lcr_bind failed");
  74. *server_fd = -1;
  75. return;
  76. };
  77. #if !defined(COROSYNC_LINUX)
  78. unlink(socketname);
  79. #endif
  80. memset (&un_addr, 0, sizeof (struct sockaddr_un));
  81. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  82. un_addr.sun_len = sizeof(struct sockaddr_un);
  83. #endif
  84. un_addr.sun_family = AF_UNIX;
  85. #if defined(COROSYNC_LINUX)
  86. strcpy (un_addr.sun_path + 1, socketname);
  87. #else
  88. strcpy (un_addr.sun_path, socketname);
  89. #endif
  90. res = bind (fd, (struct sockaddr *)&un_addr, AIS_SUN_LEN(&un_addr));
  91. if (res) {
  92. char error_str[100];
  93. const char *error_ptr;
  94. #ifdef _GNU_SOURCE
  95. /* The GNU version of strerror_r returns a (char*) that *must* be used */
  96. error_ptr = strerror_r(errno, error_str, sizeof(error_str));
  97. #else
  98. /* The XSI-compliant strerror_r() return 0 or -1 (in case the buffer is full) */
  99. strerror_r(errno, error_str, sizeof(error_str));
  100. error_ptr = error_str;
  101. #endif
  102. printf ("Could not bind AF_UNIX: %s\n", error_ptr);
  103. }
  104. listen (fd, SERVER_BACKLOG);
  105. *server_fd = fd;
  106. }
  107. struct uis_commands {
  108. const char *command;
  109. void (*cmd_handler) (char *);
  110. };
  111. static void cmd1 (char *cmd) {
  112. printf ("cmd1 executed with cmd line %s\n", cmd);
  113. }
  114. struct uis_commands uis_commands[] = {
  115. {
  116. "cmd1", cmd1
  117. }
  118. };
  119. struct uis_req_msg {
  120. int len;
  121. char msg[0];
  122. };
  123. static void lcr_uis_dispatch (int fd)
  124. {
  125. struct uis_req_msg header;
  126. char msg_contents[512];
  127. ssize_t readsize;
  128. /*
  129. * TODO this doesn't handle short reads
  130. */
  131. readsize = read (fd, &header, sizeof (header));
  132. if (readsize == -1) {
  133. return;
  134. }
  135. readsize = read (fd, msg_contents, sizeof (msg_contents));
  136. if (readsize == -1) {
  137. return;
  138. }
  139. printf ("msg contents %s\n", msg_contents);
  140. }
  141. static void *lcr_uis_server (void *data)
  142. {
  143. struct pollfd ufds[2];
  144. struct sockaddr_un un_addr;
  145. socklen_t addrlen;
  146. int nfds = 1;
  147. #ifdef COROSYNC_LINUX
  148. int on = 1;
  149. #endif
  150. /*
  151. * Main acceptance and dispatch loop
  152. */
  153. uis_lcr_bind (&ufds[0].fd);
  154. printf ("UIS server thread started %d\n", ufds[0].fd);
  155. ufds[0].events = POLLIN;
  156. ufds[1].events = POLLOUT;
  157. for (;;) {
  158. int res = poll (ufds, nfds, -1);
  159. if (res == 0 || (res < 0 && errno == EINTR))
  160. continue;
  161. if (res < 0)
  162. return NULL;
  163. if (nfds == 1 && ufds[0].revents & POLLIN) {
  164. ufds[1].fd = accept (ufds[0].fd,
  165. (struct sockaddr *)&un_addr, &addrlen);
  166. #ifdef COROSYNC_LINUX
  167. if (ufds[1].fd >= 0) {
  168. setsockopt(ufds[1].fd, SOL_SOCKET, SO_PASSCRED,
  169. &on, sizeof (on));
  170. }
  171. #endif
  172. nfds = 2;
  173. }
  174. if (ufds[1].fd >= 0 && (ufds[0].revents & POLLIN)) {
  175. lcr_uis_dispatch (ufds[1].fd);
  176. }
  177. }
  178. }
  179. __attribute__ ((constructor)) static int lcr_uis_ctors (void)
  180. {
  181. pthread_t thread;
  182. pthread_create (&thread, NULL, lcr_uis_server, NULL);
  183. return (0);
  184. }