Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / sbin / hammer2 / cmd_service.c
1 /*
2  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "hammer2.h"
37
38 static void *master_accept(void *data);
39 static void *master_service(void *data);
40 static void master_auth_state(hammer2_iocom_t *iocom);
41 static void master_auth_rxmsg(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
42 static void master_link_state(hammer2_iocom_t *iocom);
43 static void master_link_rxmsg(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
44
45 /*
46  * Start-up the master listener daemon for the machine.
47  *
48  * The master listener serves as a rendezvous point in the cluster, accepting
49  * connections, performing registrations and authentications, maintaining
50  * the spanning tree, and keeping track of message state so disconnects can
51  * be handled properly.
52  *
53  * Once authenticated only low-level messaging protocols (which includes
54  * tracking persistent messages) are handled by this daemon.  This daemon
55  * does not run the higher level quorum or locking protocols.
56  *
57  * This daemon can also be told to maintain connections to other nodes,
58  * forming a messaging backbone, which in turn allows PFS's (if desired) to
59  * simply connect to the master daemon via localhost if desired.
60  * Backbones are specified via /etc/hammer2.conf.
61  */
62 int
63 cmd_service(void)
64 {
65         struct sockaddr_in lsin;
66         int on;
67         int lfd;
68
69         /*
70          * Acquire socket and set options
71          */
72         if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
73                 fprintf(stderr, "master_listen: socket(): %s\n",
74                         strerror(errno));
75                 return 1;
76         }
77         on = 1;
78         setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
79
80         /*
81          * Setup listen port and try to bind.  If the bind fails we assume
82          * that a master listener process is already running and silently
83          * fail.
84          */
85         bzero(&lsin, sizeof(lsin));
86         lsin.sin_family = AF_INET;
87         lsin.sin_addr.s_addr = INADDR_ANY;
88         lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
89         if (bind(lfd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
90                 close(lfd);
91                 if (QuietOpt == 0) {
92                         fprintf(stderr,
93                                 "master listen: daemon already running\n");
94                 }
95                 return 0;
96         }
97         if (QuietOpt == 0)
98                 fprintf(stderr, "master listen: startup\n");
99         listen(lfd, 50);
100
101         /*
102          * Fork and disconnect the controlling terminal and parent process,
103          * executing the specified function as a pthread.
104          *
105          * Returns to the original process which can then continue running.
106          * In debug mode this call will create the pthread without forking
107          * and set NormalExit to 0, instead of fork.
108          */
109         hammer2_demon(master_accept, (void *)(intptr_t)lfd);
110         if (NormalExit)
111                 close(lfd);
112         return 0;
113 }
114
115 /*
116  * Master listen/accept thread.  Accept connections on the master socket,
117  * starting a pthread for each one.
118  */
119 static
120 void *
121 master_accept(void *data)
122 {
123         struct sockaddr_in asin;
124         socklen_t alen;
125         pthread_t thread;
126         int lfd = (int)(intptr_t)data;
127         int fd;
128
129         /*
130          * Nobody waits for us
131          */
132         setproctitle("hammer2 master listen");
133         pthread_detach(pthread_self());
134
135         /*
136          * Accept connections and create pthreads to handle them after
137          * validating the IP.
138          */
139         for (;;) {
140                 alen = sizeof(asin);
141                 fd = accept(lfd, (struct sockaddr *)&asin, &alen);
142                 if (fd < 0) {
143                         if (errno == EINTR)
144                                 continue;
145                         break;
146                 }
147                 thread = NULL;
148                 fprintf(stderr, "master_accept: accept fd %d\n", fd);
149                 pthread_create(&thread, NULL,
150                                master_service, (void *)(intptr_t)fd);
151         }
152         return (NULL);
153 }
154
155 /*
156  * Service an accepted connection (runs as a pthread)
157  */
158 static
159 void *
160 master_service(void *data)
161 {
162         hammer2_iocom_t iocom;
163         int fd;
164
165         fd = (int)(intptr_t)data;
166         hammer2_iocom_init(&iocom, fd, -1,
167                            master_auth_state, master_auth_rxmsg, NULL);
168         hammer2_iocom_core(&iocom);
169
170         fprintf(stderr,
171                 "iocom on fd %d terminated error rx=%d, tx=%d\n",
172                 fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
173         close(fd);
174
175         return (NULL);
176 }
177
178 /************************************************************************
179  *                          AUTHENTICATION                              *
180  ************************************************************************
181  *
182  * Callback via hammer2_iocom_core().
183  *
184  * Additional messaging-based authentication must occur before normal
185  * message operation.  The connection has already been encrypted at
186  * this point.
187  */
188 static void master_auth_conn_rx(hammer2_state_t *state, hammer2_msg_t *msg);
189
190 static
191 void
192 master_auth_state(hammer2_iocom_t *iocom __unused)
193 {
194         hammer2_msg_t *msg;
195
196         /*
197          * Transmit LNK_CONN, enabling the SPAN protocol if both sides
198          * agree.
199          *
200          * XXX put additional authentication states here
201          */
202         msg = hammer2_msg_alloc(iocom, 0, HAMMER2_LNK_CONN |
203                                           HAMMER2_MSGF_CREATE);
204         snprintf(msg->any.lnk_conn.label, sizeof(msg->any.lnk_conn.label), "*");
205         hammer2_msg_write(iocom, msg, master_auth_conn_rx, NULL, NULL);
206
207         hammer2_iocom_restate(iocom,
208                               master_link_state, master_link_rxmsg, NULL);
209 }
210
211 static
212 void
213 master_auth_conn_rx(hammer2_state_t *state, hammer2_msg_t *msg)
214 {
215         if (msg->any.head.cmd & HAMMER2_MSGF_DELETE)
216                 hammer2_msg_reply(state->iocom, msg, 0);
217 }
218
219 static
220 void
221 master_auth_rxmsg(hammer2_iocom_t *iocom __unused, hammer2_msg_t *msg __unused)
222 {
223 }
224
225 /************************************************************************
226  *                      POST-AUTHENTICATION SERVICE MSGS                *
227  ************************************************************************
228  *
229  * Callback via hammer2_iocom_core().
230  */
231 static
232 void
233 master_link_state(hammer2_iocom_t *iocom __unused)
234 {
235 }
236
237 static
238 void
239 master_link_rxmsg(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
240 {
241         hammer2_state_t *state;
242         uint32_t cmd;
243
244         /*
245          * If the message state has a function established we just
246          * call the function, otherwise we call the appropriate
247          * link-level protocol related to the original command and
248          * let it sort it out.
249          *
250          * Non-transactional one-off messages, on the otherhand,
251          * might have REPLY set.
252          */
253         state = msg->state;
254         if (state) {
255                 cmd = state->msg->any.head.cmd;
256                 fprintf(stderr,
257                         "MSGRX persist=%08x cmd=%08x error %d\n",
258                         cmd, msg->any.head.cmd, msg->any.head.error);
259         } else {
260                 cmd = msg->any.head.cmd;
261                 fprintf(stderr,
262                         "MSGRX persist=-------- cmd=%08x error %d\n",
263                         cmd, msg->any.head.error);
264         }
265         if (state && state->func) {
266                 assert(state->func != NULL);
267                 state->func(state, msg);
268         } else {
269                 switch(cmd & HAMMER2_MSGF_PROTOS) {
270                 case HAMMER2_MSG_PROTO_LNK:
271                         hammer2_msg_lnk(iocom, msg);
272                         break;
273                 case HAMMER2_MSG_PROTO_DBG:
274                         hammer2_msg_dbg(iocom, msg);
275                         break;
276                 default:
277                         hammer2_msg_reply(iocom, msg,
278                                           HAMMER2_MSG_ERR_UNKNOWN);
279                         break;
280                 }
281         }
282 }