2 * Original author : tridge@samba.org, January 2002
4 * Copyright (c) 2005 Christophe Varoqui
5 * Copyright (c) 2005 Benjamin Marzinski, Redhat
9 * A simple domain socket listener
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
35 #define SLEEP_TIME 5000
39 struct client *next, *prev;
42 static struct client *clients;
43 static unsigned num_clients;
47 * handle a new client joining
49 static void new_client(int ux_sock)
53 socklen_t len = sizeof(addr);
56 fd = accept(ux_sock, &addr, &len);
61 /* put it in our linked list */
62 c = (struct client *)MALLOC(sizeof(*c));
63 memset(c, 0, sizeof(*c));
66 if (c->next) c->next->prev = c;
72 * kill off a dead client
74 static void dead_client(struct client *c)
77 if (c->prev) c->prev->next = c->next;
78 if (c->next) c->next->prev = c->prev;
79 if (c == clients) clients = c->next;
84 void free_polls (void)
90 void uxsock_cleanup(void *arg)
99 void * uxsock_listen(int (*uxsock_trigger)(char *, char **, int *, void *),
108 ux_sock = ux_socket_listen(DEFAULT_SOCKET);
111 condlog(0, "ux_socket_listen error");
115 pthread_cleanup_push(uxsock_cleanup, NULL);
117 polls = (struct pollfd *)MALLOC(0);
123 /* setup for a poll */
124 polls = REALLOC(polls, (1+num_clients) * sizeof(*polls));
125 polls[0].fd = ux_sock;
126 polls[0].events = POLLIN;
128 /* setup the clients */
129 for (i=1, c = clients; c; i++, c = c->next) {
131 polls[i].events = POLLIN;
134 /* most of our life is spent in this call */
135 poll_count = poll(polls, i, SLEEP_TIME);
137 if (poll_count == -1) {
141 /* something went badly wrong! */
149 /* see if a client wants to speak to us */
150 for (i=1, c = clients; c; i++) {
151 struct client *next = c->next;
153 if (polls[i].revents & POLLIN) {
154 if (recv_packet(c->fd, &inbuf, &len) != 0) {
158 condlog(4, "Got request [%s]", inbuf);
159 uxsock_trigger(inbuf, &reply, &rlen,
162 if (send_packet(c->fd, reply,
166 condlog(4, "Reply [%d bytes]",
177 /* see if we got a new client */
178 if (polls[0].revents & POLLIN) {
183 pthread_cleanup_pop(1);