multipathd: lock cli client list
[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 pthread_mutex_t client_lock = PTHREAD_MUTEX_INITIALIZER;
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         c = (struct client *)MALLOC(sizeof(*c));
68         memset(c, 0, sizeof(*c));
69         INIT_LIST_HEAD(&c->node);
70         c->fd = fd;
71
72         /* put it in our linked list */
73         pthread_mutex_lock(&client_lock);
74         list_add_tail(&c->node, &clients);
75         pthread_mutex_unlock(&client_lock);
76 }
77
78 /*
79  * kill off a dead client
80  */
81 static void dead_client(struct client *c)
82 {
83         pthread_mutex_lock(&client_lock);
84         list_del_init(&c->node);
85         pthread_mutex_unlock(&client_lock);
86         close(c->fd);
87         c->fd = -1;
88         FREE(c);
89 }
90
91 void free_polls (void)
92 {
93         if (polls)
94                 FREE(polls);
95 }
96
97 void check_timeout(struct timeval start_time, char *inbuf,
98                    unsigned int timeout)
99 {
100         struct timeval diff_time, end_time;
101
102         if (start_time.tv_sec && gettimeofday(&end_time, NULL) == 0) {
103                 timersub(&end_time, &start_time, &diff_time);
104                 unsigned long msecs;
105
106                 msecs = diff_time.tv_sec * 1000 +
107                         diff_time.tv_usec / 1000;
108                 if (msecs > timeout)
109                         condlog(2, "cli cmd '%s' timeout reached "
110                                 "after %lu.%06lu secs", inbuf,
111                                 diff_time.tv_sec, diff_time.tv_usec);
112         }
113 }
114
115 void uxsock_cleanup(void *arg)
116 {
117         cli_exit();
118         free_polls();
119 }
120
121 /*
122  * entry point
123  */
124 void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
125 {
126         int ux_sock;
127         size_t len;
128         int rlen, timeout;
129         char *inbuf;
130         char *reply;
131         sigset_t mask;
132
133         ux_sock = ux_socket_listen(DEFAULT_SOCKET);
134
135         if (ux_sock == -1) {
136                 condlog(1, "could not create uxsock: %d", errno);
137                 return NULL;
138         }
139
140         if (!conf) {
141                 condlog(1, "configuration changed");
142                 return NULL;
143         }
144
145         timeout = conf->uxsock_timeout;
146
147         pthread_cleanup_push(uxsock_cleanup, NULL);
148
149         polls = (struct pollfd *)MALLOC(0);
150         pthread_sigmask(SIG_SETMASK, NULL, &mask);
151         sigdelset(&mask, SIGHUP);
152         sigdelset(&mask, SIGUSR1);
153         while (1) {
154                 struct client *c, *tmp;
155                 int i, poll_count, num_clients;
156
157                 /*
158                  * Store configuration timeout;
159                  * configuration might change during
160                  * the call to 'reconfigure'.
161                  */
162                 if (conf)
163                         timeout = conf->uxsock_timeout;
164
165                 /* setup for a poll */
166                 pthread_mutex_lock(&client_lock);
167                 num_clients = 0;
168                 list_for_each_entry(c, &clients, node) {
169                         num_clients++;
170                 }
171                 polls = REALLOC(polls, (1+num_clients) * sizeof(*polls));
172                 polls[0].fd = ux_sock;
173                 polls[0].events = POLLIN;
174
175                 /* setup the clients */
176                 i = 1;
177                 list_for_each_entry(c, &clients, node) {
178                         polls[i].fd = c->fd;
179                         polls[i].events = POLLIN;
180                         i++;
181                 }
182                 pthread_mutex_unlock(&client_lock);
183
184                 /* most of our life is spent in this call */
185                 poll_count = ppoll(polls, i, &sleep_time, &mask);
186
187                 if (poll_count == -1) {
188                         if (errno == EINTR) {
189                                 handle_signals();
190                                 continue;
191                         }
192
193                         /* something went badly wrong! */
194                         condlog(0, "poll");
195                         pthread_exit(NULL);
196                 }
197
198                 if (poll_count == 0)
199                         continue;
200
201                 /* see if a client wants to speak to us */
202                 for (i = 1; i < num_clients + 1; i++) {
203                         if (polls[i].revents & POLLIN) {
204                                 struct timeval start_time;
205
206                                 c = NULL;
207                                 pthread_mutex_lock(&client_lock);
208                                 list_for_each_entry(tmp, &clients, node) {
209                                         if (tmp->fd == polls[i].fd) {
210                                                 c = tmp;
211                                                 break;
212                                         }
213                                 }
214                                 pthread_mutex_unlock(&client_lock);
215                                 if (!c) {
216                                         condlog(3, "cli%d: invalid fd %d",
217                                                 i, polls[i].fd);
218                                         continue;
219                                 }
220                                 if (gettimeofday(&start_time, NULL) != 0)
221                                         start_time.tv_sec = 0;
222
223                                 if (recv_packet(c->fd, &inbuf, &len,
224                                                 timeout) != 0) {
225                                         dead_client(c);
226                                 } else {
227                                         inbuf[len - 1] = 0;
228                                         condlog(4, "Got request [%s]", inbuf);
229                                         uxsock_trigger(inbuf, &reply, &rlen,
230                                                        trigger_data);
231                                         if (reply) {
232                                                 if (send_packet(c->fd, reply,
233                                                                 rlen) != 0) {
234                                                         dead_client(c);
235                                                 }
236                                                 condlog(4, "Reply [%d bytes]",
237                                                         rlen);
238                                                 FREE(reply);
239                                                 reply = NULL;
240                                         }
241                                         check_timeout(start_time, inbuf,
242                                                       timeout);
243                                         FREE(inbuf);
244                                 }
245                         }
246                 }
247
248                 /* see if we got a new client */
249                 if (polls[0].revents & POLLIN) {
250                         new_client(ux_sock);
251                 }
252         }
253
254         pthread_cleanup_pop(1);
255         close(ux_sock);
256         return NULL;
257 }