uxlsnr: use typedef for trigger function
[multipath-tools/.git] / multipathd / uxlsnr.c
1 /*
2  * Original author : tridge@samba.org, January 2002
3  *
4  * Copyright (c) 2005 Christophe Varoqui
5  * Copyright (c) 2005 Benjamin Marzinski, Redhat
6  */
7
8 /*
9  * A simple domain socket listener
10  */
11 #define _GNU_SOURCE
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdarg.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <sys/poll.h>
23 #include <sys/time.h>
24 #include <signal.h>
25 #include <checkers.h>
26 #include <memory.h>
27 #include <debug.h>
28 #include <vector.h>
29 #include <structs.h>
30 #include <structs_vec.h>
31 #include <uxsock.h>
32 #include <defaults.h>
33 #include <config.h>
34
35 #include "main.h"
36 #include "cli.h"
37 #include "uxlsnr.h"
38
39 struct timespec sleep_time = {5, 0};
40
41 struct client {
42         struct list_head node;
43         int fd;
44 };
45
46 LIST_HEAD(clients);
47 static unsigned num_clients;
48 struct pollfd *polls;
49 volatile sig_atomic_t reconfig_sig = 0;
50 volatile sig_atomic_t log_reset_sig = 0;
51
52 /*
53  * handle a new client joining
54  */
55 static void new_client(int ux_sock)
56 {
57         struct client *c;
58         struct sockaddr addr;
59         socklen_t len = sizeof(addr);
60         int fd;
61
62         fd = accept(ux_sock, &addr, &len);
63
64         if (fd == -1)
65                 return;
66
67         /* put it in our linked list */
68         c = (struct client *)MALLOC(sizeof(*c));
69         memset(c, 0, sizeof(*c));
70         INIT_LIST_HEAD(&c->node);
71         c->fd = fd;
72
73         list_add_tail(&c->node, &clients);
74         num_clients++;
75 }
76
77 /*
78  * kill off a dead client
79  */
80 static void dead_client(struct client *c)
81 {
82         close(c->fd);
83         c->fd = -1;
84         list_del_init(&c->node);
85         FREE(c);
86         num_clients--;
87 }
88
89 void free_polls (void)
90 {
91         if (polls)
92                 FREE(polls);
93 }
94
95 void check_timeout(struct timeval start_time, char *inbuf,
96                    unsigned int timeout)
97 {
98         struct timeval diff_time, end_time;
99
100         if (start_time.tv_sec && gettimeofday(&end_time, NULL) == 0) {
101                 timersub(&end_time, &start_time, &diff_time);
102                 unsigned long msecs;
103
104                 msecs = diff_time.tv_sec * 1000 +
105                         diff_time.tv_usec / 1000;
106                 if (msecs > timeout)
107                         condlog(2, "cli cmd '%s' timeout reached "
108                                 "after %lu.%06lu secs", inbuf,
109                                 diff_time.tv_sec, diff_time.tv_usec);
110         }
111 }
112
113 void uxsock_cleanup(void *arg)
114 {
115         cli_exit();
116         free_polls();
117 }
118
119 /*
120  * entry point
121  */
122 void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
123 {
124         int ux_sock;
125         size_t len;
126         int rlen, timeout;
127         char *inbuf;
128         char *reply;
129         sigset_t mask;
130
131         ux_sock = ux_socket_listen(DEFAULT_SOCKET);
132
133         if (ux_sock == -1) {
134                 condlog(1, "could not create uxsock: %d", errno);
135                 return NULL;
136         }
137
138         if (!conf) {
139                 condlog(1, "configuration changed");
140                 return NULL;
141         }
142
143         timeout = conf->uxsock_timeout;
144
145         pthread_cleanup_push(uxsock_cleanup, NULL);
146
147         polls = (struct pollfd *)MALLOC(0);
148         pthread_sigmask(SIG_SETMASK, NULL, &mask);
149         sigdelset(&mask, SIGHUP);
150         sigdelset(&mask, SIGUSR1);
151         while (1) {
152                 struct client *c, *tmp;
153                 int i, poll_count;
154
155                 /*
156                  * Store configuration timeout;
157                  * configuration might change during
158                  * the call to 'reconfigure'.
159                  */
160                 if (conf)
161                         timeout = conf->uxsock_timeout;
162
163                 /* setup for a poll */
164                 polls = REALLOC(polls, (1+num_clients) * sizeof(*polls));
165                 polls[0].fd = ux_sock;
166                 polls[0].events = POLLIN;
167
168                 /* setup the clients */
169                 i = 1;
170                 list_for_each_entry(c, &clients, node) {
171                         polls[i].fd = c->fd;
172                         polls[i].events = POLLIN;
173                         if (i > num_clients)
174                                 break;
175                         i++;
176                 }
177
178                 /* most of our life is spent in this call */
179                 poll_count = ppoll(polls, i, &sleep_time, &mask);
180
181                 if (poll_count == -1) {
182                         if (errno == EINTR) {
183                                 handle_signals();
184                                 continue;
185                         }
186
187                         /* something went badly wrong! */
188                         condlog(0, "poll");
189                         pthread_exit(NULL);
190                 }
191
192                 if (poll_count == 0)
193                         continue;
194
195                 /* see if a client wants to speak to us */
196                 for (i = 1; i < num_clients + 1; i++) {
197                         if (polls[i].revents & POLLIN) {
198                                 struct timeval start_time;
199
200                                 c = NULL;
201                                 list_for_each_entry(tmp, &clients, node) {
202                                         if (tmp->fd == polls[i].fd) {
203                                                 c = tmp;
204                                                 break;
205                                         }
206                                 }
207                                 if (!c) {
208                                         condlog(3, "cli%d: invalid fd %d",
209                                                 i, polls[i].fd);
210                                         continue;
211                                 }
212                                 if (gettimeofday(&start_time, NULL) != 0)
213                                         start_time.tv_sec = 0;
214
215                                 if (recv_packet(c->fd, &inbuf, &len,
216                                                 timeout) != 0) {
217                                         dead_client(c);
218                                 } else {
219                                         inbuf[len - 1] = 0;
220                                         condlog(4, "Got request [%s]", inbuf);
221                                         uxsock_trigger(inbuf, &reply, &rlen,
222                                                        trigger_data);
223                                         if (reply) {
224                                                 if (send_packet(c->fd, reply,
225                                                                 rlen) != 0) {
226                                                         dead_client(c);
227                                                 }
228                                                 condlog(4, "Reply [%d bytes]",
229                                                         rlen);
230                                                 FREE(reply);
231                                                 reply = NULL;
232                                         }
233                                         check_timeout(start_time, inbuf,
234                                                       timeout);
235                                         FREE(inbuf);
236                                 }
237                         }
238                 }
239
240                 /* see if we got a new client */
241                 if (polls[0].revents & POLLIN) {
242                         new_client(ux_sock);
243                 }
244         }
245
246         pthread_cleanup_pop(1);
247         close(ux_sock);
248         return NULL;
249 }