uis.c 4.7 KB

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