cdafd82943e7e245203808529b9a165493fb22f8
[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 <stdbool.h>
26 #include "checkers.h"
27 #include "memory.h"
28 #include "debug.h"
29 #include "vector.h"
30 #include "structs.h"
31 #include "structs_vec.h"
32 #include "uxsock.h"
33 #include "defaults.h"
34 #include "config.h"
35 #include "mpath_cmd.h"
36 #include "time-util.h"
37
38 #include "main.h"
39 #include "cli.h"
40 #include "uxlsnr.h"
41
42 struct timespec sleep_time = {5, 0};
43
44 struct client {
45         struct list_head node;
46         int fd;
47 };
48
49 #define MIN_POLLS 1023
50
51 LIST_HEAD(clients);
52 pthread_mutex_t client_lock = PTHREAD_MUTEX_INITIALIZER;
53 struct pollfd *polls;
54
55 static bool _socket_client_is_root(int fd);
56
57 static bool _socket_client_is_root(int fd)
58 {
59         socklen_t len = 0;
60         struct ucred uc;
61
62         len = sizeof(struct ucred);
63         if ((fd >= 0) &&
64             (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &uc, &len) == 0) &&
65             (uc.uid == 0))
66                         return true;
67
68         /* Treat error as not root client */
69         return false;
70 }
71
72 /*
73  * handle a new client joining
74  */
75 static void new_client(int ux_sock)
76 {
77         struct client *c;
78         struct sockaddr addr;
79         socklen_t len = sizeof(addr);
80         int fd;
81
82         fd = accept(ux_sock, &addr, &len);
83
84         if (fd == -1)
85                 return;
86
87         c = (struct client *)MALLOC(sizeof(*c));
88         if (!c) {
89                 close(fd);
90                 return;
91         }
92         memset(c, 0, sizeof(*c));
93         INIT_LIST_HEAD(&c->node);
94         c->fd = fd;
95
96         /* put it in our linked list */
97         pthread_mutex_lock(&client_lock);
98         list_add_tail(&c->node, &clients);
99         pthread_mutex_unlock(&client_lock);
100 }
101
102 /*
103  * kill off a dead client
104  */
105 static void _dead_client(struct client *c)
106 {
107         int fd = c->fd;
108         list_del_init(&c->node);
109         c->fd = -1;
110         FREE(c);
111         close(fd);
112 }
113
114 static void dead_client(struct client *c)
115 {
116         pthread_cleanup_push(cleanup_lock, &client_lock);
117         pthread_mutex_lock(&client_lock);
118         _dead_client(c);
119         pthread_cleanup_pop(1);
120 }
121
122 void free_polls (void)
123 {
124         if (polls)
125                 FREE(polls);
126 }
127
128 void check_timeout(struct timespec start_time, char *inbuf,
129                    unsigned int timeout)
130 {
131         struct timespec diff_time, end_time;
132
133         if (start_time.tv_sec &&
134             clock_gettime(CLOCK_MONOTONIC, &end_time) == 0) {
135                 unsigned long msecs;
136
137                 timespecsub(&end_time, &start_time, &diff_time);
138                 msecs = diff_time.tv_sec * 1000 +
139                         diff_time.tv_nsec / (1000 * 1000);
140                 if (msecs > timeout)
141                         condlog(2, "cli cmd '%s' timeout reached "
142                                 "after %lu.%06lu secs", inbuf,
143                                 diff_time.tv_sec, diff_time.tv_nsec / 1000);
144         }
145 }
146
147 void uxsock_cleanup(void *arg)
148 {
149         struct client *client_loop;
150         struct client *client_tmp;
151         long ux_sock = (long)arg;
152
153         close(ux_sock);
154
155         pthread_mutex_lock(&client_lock);
156         list_for_each_entry_safe(client_loop, client_tmp, &clients, node) {
157                 _dead_client(client_loop);
158         }
159         pthread_mutex_unlock(&client_lock);
160
161         cli_exit();
162         free_polls();
163 }
164
165 /*
166  * entry point
167  */
168 void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
169 {
170         long ux_sock;
171         int rlen;
172         char *inbuf;
173         char *reply;
174         sigset_t mask;
175         int old_clients = MIN_POLLS;
176
177         ux_sock = ux_socket_listen(DEFAULT_SOCKET);
178
179         if (ux_sock == -1) {
180                 condlog(1, "could not create uxsock: %d", errno);
181                 return NULL;
182         }
183
184         pthread_cleanup_push(uxsock_cleanup, (void *)ux_sock);
185
186         condlog(3, "uxsock: startup listener");
187         polls = (struct pollfd *)MALLOC((MIN_POLLS + 1) * sizeof(struct pollfd));
188         if (!polls) {
189                 condlog(0, "uxsock: failed to allocate poll fds");
190                 return NULL;
191         }
192         sigfillset(&mask);
193         sigdelset(&mask, SIGINT);
194         sigdelset(&mask, SIGTERM);
195         sigdelset(&mask, SIGHUP);
196         sigdelset(&mask, SIGUSR1);
197         while (1) {
198                 struct client *c, *tmp;
199                 int i, poll_count, num_clients;
200
201                 /* setup for a poll */
202                 pthread_mutex_lock(&client_lock);
203                 num_clients = 0;
204                 list_for_each_entry(c, &clients, node) {
205                         num_clients++;
206                 }
207                 if (num_clients != old_clients) {
208                         struct pollfd *new;
209                         if (num_clients <= MIN_POLLS && old_clients > MIN_POLLS) {
210                                 new = REALLOC(polls, (1 + MIN_POLLS) *
211                                                 sizeof(struct pollfd));
212                         } else if (num_clients <= MIN_POLLS && old_clients <= MIN_POLLS) {
213                                 new = polls;
214                         } else {
215                                 new = REALLOC(polls, (1+num_clients) *
216                                                 sizeof(struct pollfd));
217                         }
218                         if (!new) {
219                                 pthread_mutex_unlock(&client_lock);
220                                 condlog(0, "%s: failed to realloc %d poll fds",
221                                         "uxsock", 1 + num_clients);
222                                 sched_yield();
223                                 continue;
224                         }
225                         old_clients = num_clients;
226                         polls = new;
227                 }
228                 polls[0].fd = ux_sock;
229                 polls[0].events = POLLIN;
230
231                 /* setup the clients */
232                 i = 1;
233                 list_for_each_entry(c, &clients, node) {
234                         polls[i].fd = c->fd;
235                         polls[i].events = POLLIN;
236                         i++;
237                 }
238                 pthread_mutex_unlock(&client_lock);
239
240                 /* most of our life is spent in this call */
241                 poll_count = ppoll(polls, i, &sleep_time, &mask);
242
243                 handle_signals(false);
244                 if (poll_count == -1) {
245                         if (errno == EINTR) {
246                                 handle_signals(true);
247                                 continue;
248                         }
249
250                         /* something went badly wrong! */
251                         condlog(0, "uxsock: poll failed with %d", errno);
252                         break;
253                 }
254
255                 if (poll_count == 0) {
256                         handle_signals(true);
257                         continue;
258                 }
259
260                 /* see if a client wants to speak to us */
261                 for (i = 1; i < num_clients + 1; i++) {
262                         if (polls[i].revents & POLLIN) {
263                                 struct timespec start_time;
264
265                                 c = NULL;
266                                 pthread_mutex_lock(&client_lock);
267                                 list_for_each_entry(tmp, &clients, node) {
268                                         if (tmp->fd == polls[i].fd) {
269                                                 c = tmp;
270                                                 break;
271                                         }
272                                 }
273                                 pthread_mutex_unlock(&client_lock);
274                                 if (!c) {
275                                         condlog(4, "cli%d: new fd %d",
276                                                 i, polls[i].fd);
277                                         continue;
278                                 }
279                                 if (clock_gettime(CLOCK_MONOTONIC, &start_time)
280                                     != 0)
281                                         start_time.tv_sec = 0;
282                                 if (recv_packet_from_client(c->fd, &inbuf,
283                                                             uxsock_timeout)
284                                     != 0) {
285                                         dead_client(c);
286                                         continue;
287                                 }
288                                 if (!inbuf) {
289                                         condlog(4, "recv_packet_from_client "
290                                                 "get null request");
291                                         continue;
292                                 }
293                                 condlog(4, "cli[%d]: Got request [%s]",
294                                         i, inbuf);
295                                 uxsock_trigger(inbuf, &reply, &rlen,
296                                                _socket_client_is_root(c->fd),
297                                                trigger_data);
298                                 if (reply) {
299                                         if (send_packet(c->fd,
300                                                         reply) != 0) {
301                                                 dead_client(c);
302                                         } else {
303                                                 condlog(4, "cli[%d]: "
304                                                         "Reply [%d bytes]",
305                                                         i, rlen);
306                                         }
307                                         FREE(reply);
308                                         reply = NULL;
309                                 }
310                                 check_timeout(start_time, inbuf,
311                                               uxsock_timeout);
312                                 FREE(inbuf);
313                         }
314                 }
315                 /* see if we got a non-fatal signal */
316                 handle_signals(true);
317
318                 /* see if we got a new client */
319                 if (polls[0].revents & POLLIN) {
320                         new_client(ux_sock);
321                 }
322         }
323
324         pthread_cleanup_pop(1);
325         return NULL;
326 }