Return error when receiving CLI packet
[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 <signal.h>
24 #include <checkers.h>
25 #include <memory.h>
26 #include <debug.h>
27 #include <vector.h>
28 #include <structs.h>
29 #include <structs_vec.h>
30 #include <uxsock.h>
31 #include <defaults.h>
32
33 #include "main.h"
34 #include "cli.h"
35 #include "uxlsnr.h"
36
37 struct timespec sleep_time = {5, 0};
38
39 struct client {
40         int fd;
41         struct client *next, *prev;
42 };
43
44 static struct client *clients;
45 static unsigned num_clients;
46 struct pollfd *polls;
47 volatile sig_atomic_t reconfig_sig = 0;
48 volatile sig_atomic_t log_reset_sig = 0;
49
50 /*
51  * handle a new client joining
52  */
53 static void new_client(int ux_sock)
54 {
55         struct client *c;
56         struct sockaddr addr;
57         socklen_t len = sizeof(addr);
58         int fd;
59
60         fd = accept(ux_sock, &addr, &len);
61
62         if (fd == -1)
63                 return;
64
65         /* put it in our linked list */
66         c = (struct client *)MALLOC(sizeof(*c));
67         memset(c, 0, sizeof(*c));
68         c->fd = fd;
69         c->next = clients;
70         if (c->next) c->next->prev = c;
71         clients = c;
72         num_clients++;
73 }
74
75 /*
76  * kill off a dead client
77  */
78 static void dead_client(struct client *c)
79 {
80         close(c->fd);
81         if (c->prev) c->prev->next = c->next;
82         if (c->next) c->next->prev = c->prev;
83         if (c == clients) clients = c->next;
84         FREE(c);
85         num_clients--;
86 }
87
88 void free_polls (void)
89 {
90         if (polls)
91                 FREE(polls);
92 }
93
94 void uxsock_cleanup(void *arg)
95 {
96         cli_exit();
97         free_polls();
98 }
99
100 /*
101  * entry point
102  */
103 void * uxsock_listen(int (*uxsock_trigger)(char *, char **, int *, void *),
104                         void * trigger_data)
105 {
106         int ux_sock;
107         size_t len;
108         int rlen;
109         char *inbuf;
110         char *reply;
111         sigset_t mask;
112
113         ux_sock = ux_socket_listen(DEFAULT_SOCKET);
114
115         if (ux_sock == -1)
116                 exit(1);
117
118         pthread_cleanup_push(uxsock_cleanup, NULL);
119
120         polls = (struct pollfd *)MALLOC(0);
121         pthread_sigmask(SIG_SETMASK, NULL, &mask);
122         sigdelset(&mask, SIGHUP);
123         sigdelset(&mask, SIGUSR1);
124         while (1) {
125                 struct client *c;
126                 int i, poll_count;
127
128                 /* setup for a poll */
129                 polls = REALLOC(polls, (1+num_clients) * sizeof(*polls));
130                 polls[0].fd = ux_sock;
131                 polls[0].events = POLLIN;
132
133                 /* setup the clients */
134                 for (i=1, c = clients; c; i++, c = c->next) {
135                         polls[i].fd = c->fd;
136                         polls[i].events = POLLIN;
137                 }
138
139                 /* most of our life is spent in this call */
140                 poll_count = ppoll(polls, i, &sleep_time, &mask);
141
142                 if (poll_count == -1) {
143                         if (errno == EINTR) {
144                                 handle_signals();
145                                 continue;
146                         }
147
148                         /* something went badly wrong! */
149                         condlog(0, "poll");
150                         pthread_exit(NULL);
151                 }
152
153                 if (poll_count == 0)
154                         continue;
155
156                 /* see if a client wants to speak to us */
157                 for (i=1, c = clients; c; i++) {
158                         struct client *next = c->next;
159
160                         if (polls[i].revents & POLLIN) {
161                                 if (recv_packet(c->fd, &inbuf, &len,
162                                                 DEFAULT_UXSOCK_TIMEOUT) != 0) {
163                                         dead_client(c);
164                                 } else {
165                                         inbuf[len - 1] = 0;
166                                         condlog(4, "Got request [%s]", inbuf);
167                                         uxsock_trigger(inbuf, &reply, &rlen,
168                                                        trigger_data);
169                                         if (reply) {
170                                                 if (send_packet(c->fd, reply,
171                                                                 rlen) != 0) {
172                                                         dead_client(c);
173                                                 }
174                                                 condlog(4, "Reply [%d bytes]",
175                                                         rlen);
176                                                 FREE(reply);
177                                                 reply = NULL;
178                                         }
179                                         FREE(inbuf);
180                                 }
181                         }
182                         c = next;
183                 }
184
185                 /* see if we got a new client */
186                 if (polls[0].revents & POLLIN) {
187                         new_client(ux_sock);
188                 }
189         }
190
191         pthread_cleanup_pop(1);
192         close(ux_sock);
193         return NULL;
194 }