Apply suggestion from Andrew for srcdir functionality.
[openais.git] / lcr / uic.c
blob603e417c1cb2b253e918cbe020714cbe468cad59
1 /*
2 * Copyright (c) 2006 Steven Dake (sdake@mvista.com)
3 * Copyright (c) 2006 Sun Microsystems, Inc.
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:
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.
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.
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>
52 #if defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS)
53 /* SUN_LEN is broken for abstract namespace
55 #define AIS_SUN_LEN(a) sizeof(*(a))
56 #else
57 #define AIS_SUN_LEN(a) SUN_LEN(a)
58 #endif
60 #ifdef OPENAIS_LINUX
61 static char *socketname = "lcr.socket";
62 #else
63 static char *socketname = "/var/run/lcr.socket";
64 #endif
66 int uic_connect (int *fd)
68 int res;
69 struct sockaddr_un addr;
71 memset (&addr, 0, sizeof (struct sockaddr_un));
72 #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
73 addr.sun_len = sizeof(struct sockaddr_un);
74 #endif
75 addr.sun_family = PF_UNIX;
76 #if defined(OPENAIS_LINUX)
77 strcpy (addr.sun_path + 1, socketname);
78 #else
79 strcpy (addr.sun_path, socketname);
80 #endif
81 *fd = socket (PF_UNIX, SOCK_STREAM, 0);
82 if (*fd == -1) {
83 return -errno;
85 res = connect (*fd, (struct sockaddr *)&addr, AIS_SUN_LEN(&addr));
86 if (res == -1) {
87 return -errno;
89 return 0;
92 struct req_msg {
93 int len;
94 char msg[0];
97 int uic_msg_send (int fd, char *msg)
99 struct msghdr msg_send;
100 struct iovec iov_send[2];
101 struct req_msg req_msg;
102 int res;
104 req_msg.len = strlen (msg) + 1;
105 iov_send[0].iov_base = (void *)&req_msg;
106 iov_send[0].iov_len = sizeof (struct req_msg);
107 iov_send[1].iov_base = (void *)msg;
108 iov_send[1].iov_len = req_msg.len;
110 msg_send.msg_iov = iov_send;
111 msg_send.msg_iovlen = 2;
112 msg_send.msg_name = 0;
113 msg_send.msg_namelen = 0;
114 #ifndef OPENAIS_SOLARIS
115 msg_send.msg_control = 0;
116 msg_send.msg_controllen = 0;
117 msg_send.msg_flags = 0;
118 #else
119 msg_send.msg_accrights = NULL;
120 msg_send.msg_accrightslen = 0;
121 #endif
123 retry_send:
124 res = sendmsg (fd, &msg_send, 0);
125 if (res == -1 && errno == EINTR) {
126 goto retry_send;
128 if (res == -1) {
129 res = -errno;
131 return (res);
136 int main (void)
138 int client_fd;
139 int res;
141 res = uic_connect (&client_fd);
142 if (res != 0) {
143 printf ("Couldn't connect to live replacement service\n");
145 uic_msg_send (client_fd, "livereplace ckpt version 2");
147 return 0;