uic.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <signal.h>
  46. #include <sched.h>
  47. #include <time.h>
  48. #include <pthread.h>
  49. #include <sys/poll.h>
  50. #include <string.h>
  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 int uic_connect (int *fd);
  64. static int uic_msg_send (int fd, void *msg);
  65. int uic_connect (int *fd)
  66. {
  67. int res;
  68. struct sockaddr_un addr;
  69. memset (&addr, 0, sizeof (struct sockaddr_un));
  70. #if defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  71. addr.sun_len = sizeof(struct sockaddr_un);
  72. #endif
  73. addr.sun_family = PF_UNIX;
  74. #if defined(COROSYNC_LINUX)
  75. strcpy (addr.sun_path + 1, socketname);
  76. #else
  77. strcpy (addr.sun_path, socketname);
  78. #endif
  79. *fd = socket (PF_UNIX, SOCK_STREAM, 0);
  80. if (*fd == -1) {
  81. return -errno;
  82. }
  83. res = connect (*fd, (struct sockaddr *)&addr, AIS_SUN_LEN(&addr));
  84. if (res == -1) {
  85. return -errno;
  86. }
  87. return 0;
  88. }
  89. struct uic_req_msg {
  90. int len;
  91. char msg[0];
  92. };
  93. static int uic_msg_send (int fd, void *msg)
  94. {
  95. struct msghdr msg_send;
  96. struct iovec iov_send[2];
  97. struct uic_req_msg req_msg;
  98. ssize_t send_res;
  99. int res;
  100. req_msg.len = strlen (msg) + 1;
  101. iov_send[0].iov_base = (void *)&req_msg;
  102. iov_send[0].iov_len = sizeof (struct uic_req_msg);
  103. iov_send[1].iov_base = msg;
  104. iov_send[1].iov_len = req_msg.len;
  105. msg_send.msg_iov = iov_send;
  106. msg_send.msg_iovlen = 2;
  107. msg_send.msg_name = 0;
  108. msg_send.msg_namelen = 0;
  109. #ifndef COROSYNC_SOLARIS
  110. msg_send.msg_control = 0;
  111. msg_send.msg_controllen = 0;
  112. msg_send.msg_flags = 0;
  113. #else
  114. msg_send.msg_accrights = NULL;
  115. msg_send.msg_accrightslen = 0;
  116. #endif
  117. retry_send:
  118. send_res = sendmsg (fd, &msg_send, 0);
  119. if (send_res == -1 && errno == EINTR) {
  120. goto retry_send;
  121. }
  122. if (send_res == -1) {
  123. res = -errno;
  124. } else {
  125. res = (int)send_res;
  126. }
  127. return (res);
  128. }
  129. int main (void)
  130. {
  131. int client_fd;
  132. int res;
  133. char command[128] = "livereplace ckpt version2";
  134. res = uic_connect (&client_fd);
  135. if (res != 0) {
  136. printf ("Couldn't connect to live replacement service\n");
  137. }
  138. uic_msg_send (client_fd, (void *)command);
  139. return 0;
  140. }