multipath-tools: replace <sys/poll.h> with <poll.h>
[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 <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 #include "mpath_cmd.h"
35
36 #include "main.h"
37 #include "cli.h"
38 #include "uxlsnr.h"
39
40 struct timespec sleep_time = {5, 0};
41
42 struct client {
43         struct list_head node;
44         int fd;
45 };
46
47 #define MIN_POLLS 1023
48
49 LIST_HEAD(clients);
50 pthread_mutex_t client_lock = PTHREAD_MUTEX_INITIALIZER;
51 struct pollfd *polls;
52 volatile sig_atomic_t reconfig_sig = 0;
53 volatile sig_atomic_t log_reset_sig = 0;
54
55 /*
56  * handle a new client joining
57  */
58 static void new_client(int ux_sock)
59 {
60         struct client *c;
61         struct sockaddr addr;
62         socklen_t len = sizeof(addr);
63         int fd;
64
65         fd = accept(ux_sock, &addr, &len);
66
67         if (fd == -1)
68                 return;
69
70         c = (struct client *)MALLOC(sizeof(*c));
71         if (!c) {
72                 close(fd);
73                 return;
74         }
75         memset(c, 0, sizeof(*c));
76         INIT_LIST_HEAD(&c->node);
77         c->fd = fd;
78
79         /* put it in our linked list */
80         pthread_mutex_lock(&client_lock);
81         list_add_tail(&c->node, &clients);
82         pthread_mutex_unlock(&client_lock);
83 }
84
85 /*
86  * kill off a dead client
87  */
88 static void dead_client(struct client *c)
89 {
90         pthread_mutex_lock(&client_lock);
91         list_del_init(&c->node);
92         pthread_mutex_unlock(&client_lock);
93         close(c->fd);
94         c->fd = -1;
95         FREE(c);
96 }
97
98 void free_polls (void)
99 {
100         if (polls)
101                 FREE(polls);
102 }
103
104 void check_timeout(struct timeval start_time, char *inbuf,
105                    unsigned int timeout)
106 {
107         struct timeval diff_time, end_time;
108
109         if (start_time.tv_sec && gettimeofday(&end_time, NULL) == 0) {
110                 timersub(&end_time, &start_time, &diff_time);
111                 unsigned long msecs;
112
113                 msecs = diff_time.tv_sec * 1000 +
114                         diff_time.tv_usec / 1000;
115                 if (msecs > timeout)
116                         condlog(2, "cli cmd '%s' timeout reached "
117                                 "after %lu.%06lu secs", inbuf,
118                                 diff_time.tv_sec, diff_time.tv_usec);
119         }
120 }
121
122 void uxsock_cleanup(void *arg)
123 {
124         cli_exit();
125         free_polls();
126 }
127
128 /*
129  * entry point
130  */
131 void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
132 {
133         int ux_sock;
134         int rlen;
135         char *inbuf;
136         char *reply;
137         sigset_t mask;
138         int old_clients = MIN_POLLS;
139
140         ux_sock = ux_socket_listen(DEFAULT_SOCKET);
141
142         if (ux_sock == -1) {
143                 condlog(1, "could not create uxsock: %d", errno);
144                 return NULL;
145         }
146
147         pthread_cleanup_push(uxsock_cleanup, NULL);
148
149         condlog(3, "uxsock: startup listener");
150         polls = (struct pollfd *)MALLOC(MIN_POLLS + 1);
151         if (!polls) {
152                 condlog(0, "uxsock: failed to allocate poll fds");
153                 return NULL;
154         }
155         pthread_sigmask(SIG_SETMASK, NULL, &mask);
156         sigdelset(&mask, SIGHUP);
157         sigdelset(&mask, SIGUSR1);
158         while (1) {
159                 struct client *c, *tmp;
160                 int i, poll_count, num_clients;
161
162                 /* setup for a poll */
163                 pthread_mutex_lock(&client_lock);
164                 num_clients = 0;
165                 list_for_each_entry(c, &clients, node) {
166                         num_clients++;
167                 }
168                 if (num_clients != old_clients) {
169                         struct pollfd *new;
170                         if (num_clients < MIN_POLLS) {
171                                 new = REALLOC(polls, (1 + MIN_POLLS) *
172                                                 sizeof(struct pollfd));
173                         } else {
174                                 new = REALLOC(polls, (1+num_clients) *
175                                                 sizeof(struct pollfd));
176                         }
177                         if (!new) {
178                                 pthread_mutex_unlock(&client_lock);
179                                 condlog(0, "%s: failed to realloc %d poll fds",
180                                         "uxsock", 1 + num_clients);
181                                 pthread_yield();
182                                 continue;
183                         }
184                         num_clients = old_clients;
185                         polls = new;
186                 }
187                 polls[0].fd = ux_sock;
188                 polls[0].events = POLLIN;
189
190                 /* setup the clients */
191                 i = 1;
192                 list_for_each_entry(c, &clients, node) {
193                         polls[i].fd = c->fd;
194                         polls[i].events = POLLIN;
195                         i++;
196                 }
197                 pthread_mutex_unlock(&client_lock);
198
199                 /* most of our life is spent in this call */
200                 poll_count = ppoll(polls, i, &sleep_time, &mask);
201
202                 if (poll_count == -1) {
203                         if (errno == EINTR) {
204                                 handle_signals();
205                                 continue;
206                         }
207
208                         /* something went badly wrong! */
209                         condlog(0, "uxsock: poll failed with %d", errno);
210                         break;
211                 }
212
213                 if (poll_count == 0)
214                         continue;
215
216                 /* see if a client wants to speak to us */
217                 for (i = 1; i < num_clients + 1; i++) {
218                         if (polls[i].revents & POLLIN) {
219                                 struct timeval start_time;
220
221                                 c = NULL;
222                                 pthread_mutex_lock(&client_lock);
223                                 list_for_each_entry(tmp, &clients, node) {
224                                         if (tmp->fd == polls[i].fd) {
225                                                 c = tmp;
226                                                 break;
227                                         }
228                                 }
229                                 pthread_mutex_unlock(&client_lock);
230                                 if (!c) {
231                                         condlog(4, "cli%d: new fd %d",
232                                                 i, polls[i].fd);
233                                         continue;
234                                 }
235                                 if (gettimeofday(&start_time, NULL) != 0)
236                                         start_time.tv_sec = 0;
237                                 if (recv_packet(c->fd, &inbuf,
238                                                 uxsock_timeout) != 0) {
239                                         dead_client(c);
240                                         continue;
241                                 }
242                                 condlog(4, "cli[%d]: Got request [%s]",
243                                         i, inbuf);
244                                 uxsock_trigger(inbuf, &reply, &rlen,
245                                                trigger_data);
246                                 if (reply) {
247                                         if (send_packet(c->fd,
248                                                         reply) != 0) {
249                                                 dead_client(c);
250                                         } else {
251                                                 condlog(4, "cli[%d]: "
252                                                         "Reply [%d bytes]",
253                                                         i, rlen);
254                                         }
255                                         FREE(reply);
256                                         reply = NULL;
257                                 }
258                                 check_timeout(start_time, inbuf,
259                                               uxsock_timeout);
260                                 FREE(inbuf);
261                         }
262                 }
263
264                 /* see if we got a new client */
265                 if (polls[0].revents & POLLIN) {
266                         new_client(ux_sock);
267                 }
268         }
269
270         pthread_cleanup_pop(1);
271         close(ux_sock);
272         return NULL;
273 }