7b13190edd71daf221e55e330d42d30df59765fa
[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(int (*uxsock_trigger)(char *, char **, int *, void *),
123                         void * trigger_data)
124 {
125         int ux_sock;
126         size_t len;
127         int rlen, timeout;
128         char *inbuf;
129         char *reply;
130         sigset_t mask;
131
132         ux_sock = ux_socket_listen(DEFAULT_SOCKET);
133
134         if (ux_sock == -1) {
135                 condlog(1, "could not create uxsock: %d", errno);
136                 return NULL;
137         }
138
139         if (!conf) {
140                 condlog(1, "configuration changed");
141                 return NULL;
142         }
143
144         timeout = conf->uxsock_timeout;
145
146         pthread_cleanup_push(uxsock_cleanup, NULL);
147
148         polls = (struct pollfd *)MALLOC(0);
149         pthread_sigmask(SIG_SETMASK, NULL, &mask);
150         sigdelset(&mask, SIGHUP);
151         sigdelset(&mask, SIGUSR1);
152         while (1) {
153                 struct client *c, *tmp;
154                 int i, poll_count;
155
156                 /*
157                  * Store configuration timeout;
158                  * configuration might change during
159                  * the call to 'reconfigure'.
160                  */
161                 if (conf)
162                         timeout = conf->uxsock_timeout;
163
164                 /* setup for a poll */
165                 polls = REALLOC(polls, (1+num_clients) * sizeof(*polls));
166                 polls[0].fd = ux_sock;
167                 polls[0].events = POLLIN;
168
169                 /* setup the clients */
170                 i = 1;
171                 list_for_each_entry(c, &clients, node) {
172                         polls[i].fd = c->fd;
173                         polls[i].events = POLLIN;
174                         if (i > num_clients)
175                                 break;
176                         i++;
177                 }
178
179                 /* most of our life is spent in this call */
180                 poll_count = ppoll(polls, i, &sleep_time, &mask);
181
182                 if (poll_count == -1) {
183                         if (errno == EINTR) {
184                                 handle_signals();
185                                 continue;
186                         }
187
188                         /* something went badly wrong! */
189                         condlog(0, "poll");
190                         pthread_exit(NULL);
191                 }
192
193                 if (poll_count == 0)
194                         continue;
195
196                 /* see if a client wants to speak to us */
197                 for (i = 1; i < num_clients + 1; i++) {
198                         if (polls[i].revents & POLLIN) {
199                                 struct timeval start_time;
200
201                                 c = NULL;
202                                 list_for_each_entry(tmp, &clients, node) {
203                                         if (tmp->fd == polls[i].fd) {
204                                                 c = tmp;
205                                                 break;
206                                         }
207                                 }
208                                 if (!c) {
209                                         condlog(3, "cli%d: invalid fd %d",
210                                                 i, polls[i].fd);
211                                         continue;
212                                 }
213                                 if (gettimeofday(&start_time, NULL) != 0)
214                                         start_time.tv_sec = 0;
215
216                                 if (recv_packet(c->fd, &inbuf, &len,
217                                                 timeout) != 0) {
218                                         dead_client(c);
219                                 } else {
220                                         inbuf[len - 1] = 0;
221                                         condlog(4, "Got request [%s]", inbuf);
222                                         uxsock_trigger(inbuf, &reply, &rlen,
223                                                        trigger_data);
224                                         if (reply) {
225                                                 if (send_packet(c->fd, reply,
226                                                                 rlen) != 0) {
227                                                         dead_client(c);
228                                                 }
229                                                 condlog(4, "Reply [%d bytes]",
230                                                         rlen);
231                                                 FREE(reply);
232                                                 reply = NULL;
233                                         }
234                                         check_timeout(start_time, inbuf,
235                                                       timeout);
236                                         FREE(inbuf);
237                                 }
238                         }
239                 }
240
241                 /* see if we got a new client */
242                 if (polls[0].revents & POLLIN) {
243                         new_client(ux_sock);
244                 }
245         }
246
247         pthread_cleanup_pop(1);
248         close(ux_sock);
249         return NULL;
250 }