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>
32 #define SLEEP_TIME 5000
36 struct client *next, *prev;
39 static struct client *clients;
40 static unsigned num_clients;
43 * handle a new client joining
45 static void new_client(int ux_sock)
49 socklen_t len = sizeof(addr);
52 fd = accept(ux_sock, &addr, &len);
57 /* put it in our linked list */
58 c = (struct client *)MALLOC(sizeof(*c));
59 memset(c, 0, sizeof(*c));
62 if (c->next) c->next->prev = c;
68 * kill off a dead client
70 static void dead_client(struct client *c)
73 if (c->prev) c->prev->next = c->next;
74 if (c->next) c->next->prev = c->prev;
75 if (c == clients) clients = c->next;
80 void free_polls (void)
88 void * uxsock_listen(int (*uxsock_trigger)(char *, char **, int *, void *),
97 ux_sock = ux_socket_listen(DEFAULT_SOCKET);
100 condlog(0, "ux_socket_listen error");
104 polls = (struct pollfd *)MALLOC(0);
110 /* setup for a poll */
111 polls = REALLOC(polls, (1+num_clients) * sizeof(*polls));
112 polls[0].fd = ux_sock;
113 polls[0].events = POLLIN;
115 /* setup the clients */
116 for (i=1, c = clients; c; i++, c = c->next) {
118 polls[i].events = POLLIN;
121 /* most of our life is spent in this call */
122 poll_count = poll(polls, i, SLEEP_TIME);
124 if (poll_count == -1) {
128 /* something went badly wrong! */
136 /* see if a client wants to speak to us */
137 for (i=1, c = clients; c; i++) {
138 struct client *next = c->next;
140 if (polls[i].revents & POLLIN) {
141 if (recv_packet(c->fd, &inbuf, &len) != 0) {
145 condlog(4, "Got request [%s]", inbuf);
146 uxsock_trigger(inbuf, &reply, &rlen,
150 if (send_packet(c->fd, reply,
163 /* see if we got a new client */
164 if (polls[0].revents & POLLIN) {