libdmsg - refactor remove hammer2-specific code
[dragonfly.git] / lib / libdmsg / 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 "dmsg_local.h"
37
38 static void master_auth_signal(dmsg_iocom_t *iocom);
39 static void master_auth_rxmsg(dmsg_msg_t *msg);
40 static void master_link_signal(dmsg_iocom_t *iocom);
41 static void master_link_rxmsg(dmsg_msg_t *msg);
42
43 /*
44  * Service an accepted connection (runs as a pthread)
45  *
46  * (also called from a couple of other places)
47  */
48 void *
49 dmsg_master_service(void *data)
50 {
51         dmsg_master_service_info_t *info = data;
52         dmsg_iocom_t iocom;
53
54         if (info->detachme)
55                 pthread_detach(pthread_self());
56
57         dmsg_iocom_init(&iocom,
58                         info->fd,
59                         (info->altmsg_callback ? info->altfd : -1),
60                         master_auth_signal,
61                         master_auth_rxmsg,
62                         info->usrmsg_callback,
63                         info->altmsg_callback);
64         if (info->noclosealt)
65                 iocom.flags &= ~DMSG_IOCOMF_CLOSEALT;
66         if (info->label) {
67                 dmsg_iocom_label(&iocom, "%s", info->label);
68                 free(info->label);
69                 info->label = NULL;
70         }
71         dmsg_iocom_core(&iocom);
72         dmsg_iocom_done(&iocom);
73
74         fprintf(stderr,
75                 "iocom on fd %d terminated error rx=%d, tx=%d\n",
76                 info->fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
77         close(info->fd);
78         info->fd = -1;  /* safety */
79         if (info->exit_callback)
80                 info->exit_callback(info->handle);
81         free(info);
82
83         return (NULL);
84 }
85
86 /************************************************************************
87  *                          AUTHENTICATION                              *
88  ************************************************************************
89  *
90  * Callback via dmsg_iocom_core().
91  *
92  * Additional messaging-based authentication must occur before normal
93  * message operation.  The connection has already been encrypted at
94  * this point.
95  */
96 static void master_auth_conn_rx(dmsg_msg_t *msg);
97
98 static
99 void
100 master_auth_signal(dmsg_iocom_t *iocom)
101 {
102         dmsg_msg_t *msg;
103
104         /*
105          * Transmit LNK_CONN, enabling the SPAN protocol if both sides
106          * agree.
107          *
108          * XXX put additional authentication states here?
109          */
110         msg = dmsg_msg_alloc(&iocom->circuit0, 0,
111                              DMSG_LNK_CONN | DMSGF_CREATE,
112                              master_auth_conn_rx, NULL);
113         msg->any.lnk_conn.peer_mask = (uint64_t)-1;
114         msg->any.lnk_conn.peer_type = DMSG_PEER_CLUSTER;
115         msg->any.lnk_conn.pfs_mask = (uint64_t)-1;
116
117         dmsg_msg_write(msg);
118
119         dmsg_iocom_restate(iocom, master_link_signal, master_link_rxmsg);
120 }
121
122 static
123 void
124 master_auth_conn_rx(dmsg_msg_t *msg)
125 {
126         if (msg->any.head.cmd & DMSGF_DELETE)
127                 dmsg_msg_reply(msg, 0);
128 }
129
130 static
131 void
132 master_auth_rxmsg(dmsg_msg_t *msg __unused)
133 {
134 }
135
136 /************************************************************************
137  *                      POST-AUTHENTICATION SERVICE MSGS                *
138  ************************************************************************
139  *
140  * Callback via dmsg_iocom_core().
141  */
142 static
143 void
144 master_link_signal(dmsg_iocom_t *iocom)
145 {
146         dmsg_msg_lnk_signal(iocom);
147 }
148
149 static
150 void
151 master_link_rxmsg(dmsg_msg_t *msg)
152 {
153         dmsg_state_t *state;
154         uint32_t cmd;
155
156         /*
157          * If the message state has a function established we just
158          * call the function, otherwise we call the appropriate
159          * link-level protocol related to the original command and
160          * let it sort it out.
161          *
162          * Non-transactional one-off messages, on the otherhand,
163          * might have REPLY set.
164          */
165         state = msg->state;
166         cmd = state ? state->icmd : msg->any.head.cmd;
167
168         if (state && state->func) {
169                 assert(state->func != NULL);
170                 state->func(msg);
171         } else {
172                 switch(cmd & DMSGF_PROTOS) {
173                 case DMSG_PROTO_LNK:
174                         dmsg_msg_lnk(msg);
175                         break;
176                 case DMSG_PROTO_DBG:
177                         dmsg_msg_dbg(msg);
178                         break;
179                 default:
180                         msg->iocom->usrmsg_callback(msg, 1);
181                         break;
182                 }
183         }
184 }
185
186 /*
187  * This is called from the master node to process a received debug
188  * shell command.  We process the command, outputting the results,
189  * then finish up by outputting another prompt.
190  */
191 void
192 dmsg_msg_dbg(dmsg_msg_t *msg)
193 {
194         switch(msg->any.head.cmd & DMSGF_CMDSWMASK) {
195         case DMSG_DBG_SHELL:
196                 /*
197                  * This is a command which we must process.
198                  * When we are finished we generate a final reply.
199                  */
200                 if (msg->aux_data)
201                         msg->aux_data[msg->aux_size - 1] = 0;
202                 msg->iocom->usrmsg_callback(msg, 0);
203                 dmsg_msg_reply(msg, 0); /* XXX send prompt instead */
204                 break;
205         case DMSG_DBG_SHELL | DMSGF_REPLY:
206                 /*
207                  * A reply just prints out the string.  No newline is added
208                  * (it is expected to be embedded if desired).
209                  */
210                 if (msg->aux_data)
211                         msg->aux_data[msg->aux_size - 1] = 0;
212                 if (msg->aux_data)
213                         write(2, msg->aux_data, strlen(msg->aux_data));
214                 break;
215         default:
216                 msg->iocom->usrmsg_callback(msg, 1);
217                 break;
218         }
219 }
220
221 /*
222  * Returns text debug output to the original defined by (msg).  (msg) is
223  * not modified and stays intact.  We use a one-way message with REPLY set
224  * to distinguish between a debug command and debug terminal output.
225  *
226  * To prevent loops circuit_printf() can filter the message (cmd) related
227  * to the circuit_printf().  We filter out DBG messages.
228  */
229 void
230 dmsg_circuit_printf(dmsg_circuit_t *circuit, const char *ctl, ...)
231 {
232         dmsg_msg_t *rmsg;
233         va_list va;
234         char buf[1024];
235         size_t len;
236
237         va_start(va, ctl);
238         vsnprintf(buf, sizeof(buf), ctl, va);
239         va_end(va);
240         len = strlen(buf) + 1;
241
242         rmsg = dmsg_msg_alloc(circuit, len,
243                               DMSG_DBG_SHELL | DMSGF_REPLY,
244                               NULL, NULL);
245         bcopy(buf, rmsg->aux_data, len);
246
247         dmsg_msg_write(rmsg);
248 }