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:
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.
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <sys/resource.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
52 #define SERVER_BACKLOG 5
54 #if defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS)
55 /* SUN_LEN is broken for abstract namespace
57 #define AIS_SUN_LEN(a) sizeof(*(a))
59 #define AIS_SUN_LEN(a) SUN_LEN(a)
63 static char *socketname
= "lcr.socket";
65 static char *socketname
= "/var/run/lcr.socket";
68 static void uis_lcr_bind (int *server_fd
)
71 struct sockaddr_un un_addr
;
75 * Create socket for lcr clients, name socket, listen for connections
77 fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
79 printf ("lcr_bind failed\n");
82 #if !defined(OPENAIS_LINUX)
85 memset (&un_addr
, 0, sizeof (struct sockaddr_un
));
86 #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
87 un_addr
.sun_len
= sizeof(struct sockaddr_un
);
89 un_addr
.sun_family
= AF_UNIX
;
90 #if defined(OPENAIS_LINUX)
91 strcpy (un_addr
.sun_path
+ 1, socketname
);
93 strcpy (un_addr
.sun_path
, socketname
);
96 res
= bind (fd
, (struct sockaddr
*)&un_addr
, AIS_SUN_LEN(&un_addr
));
98 printf ("Could not bind AF_UNIX: %s\n", strerror (errno
));
100 listen (fd
, SERVER_BACKLOG
);
104 struct uis_commands
{
106 void (*cmd_handler
) (char *);
109 void cmd1 (char *cmd
) {
110 printf ("cmd1 executed with cmd line %s\n", cmd
);
113 struct uis_commands uis_commands
[] = {
124 static void lcr_uis_dispatch (int fd
)
126 struct req_msg header
;
127 char msg_contents
[512];
131 * TODO this doesn't handle short reads
133 readsize
= read (fd
, &header
, sizeof (header
));
134 if (readsize
== -1) {
137 readsize
= read (fd
, msg_contents
, sizeof (msg_contents
));
138 if (readsize
== -1) {
142 printf ("msg contents %s\n", msg_contents
);
145 static void *lcr_uis_server (void *data
)
147 struct pollfd ufds
[2];
148 struct sockaddr_un un_addr
;
157 * Main acceptance and dispatch loop
159 uis_lcr_bind (&ufds
[0].fd
);
160 printf ("UIS server thread started %d\n", ufds
[0].fd
);
161 ufds
[0].events
= POLLIN
;
162 ufds
[1].events
= POLLOUT
;
164 res
= poll (ufds
, nfds
, -1);
165 if (nfds
== 1 && ufds
[0].revents
& POLLIN
) {
166 ufds
[1].fd
= accept (ufds
[0].fd
,
167 (struct sockaddr
*)&un_addr
, &addrlen
);
169 setsockopt(ufds
[1].fd
, SOL_SOCKET
, SO_PASSCRED
,
174 if (ufds
[0].revents
& POLLIN
) {
175 lcr_uis_dispatch (ufds
[1].fd
);
183 __attribute__ ((constructor
)) static int lcr_uis_ctors (void)
187 pthread_create (&thread
, NULL
, lcr_uis_server
, NULL
);