2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
36 #include "dmsg_local.h"
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);
44 * Service an accepted connection (runs as a pthread)
46 * (also called from a couple of other places)
49 dmsg_master_service(void *data)
51 dmsg_master_service_info_t *info = data;
55 pthread_detach(pthread_self());
57 dmsg_iocom_init(&iocom,
59 (info->altmsg_callback ? info->altfd : -1),
62 info->dbgmsg_callback,
63 info->altmsg_callback);
65 dmsg_iocom_label(&iocom, "%s", info->label);
69 dmsg_iocom_core(&iocom);
70 dmsg_iocom_done(&iocom);
73 "iocom on fd %d terminated error rx=%d, tx=%d\n",
74 info->fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
76 info->fd = -1; /* safety */
77 if (info->exit_callback)
78 info->exit_callback(info->handle);
84 /************************************************************************
86 ************************************************************************
88 * Callback via dmsg_iocom_core().
90 * Additional messaging-based authentication must occur before normal
91 * message operation. The connection has already been encrypted at
94 static void master_auth_conn_rx(dmsg_msg_t *msg);
98 master_auth_signal(dmsg_iocom_t *iocom)
103 * Transmit LNK_CONN, enabling the SPAN protocol if both sides
106 * XXX put additional authentication states here?
108 msg = dmsg_msg_alloc(&iocom->circuit0, 0,
109 DMSG_LNK_CONN | DMSGF_CREATE,
110 master_auth_conn_rx, NULL);
111 msg->any.lnk_conn.peer_mask = (uint64_t)-1;
112 msg->any.lnk_conn.peer_type = DMSG_PEER_CLUSTER;
113 msg->any.lnk_conn.pfs_mask = (uint64_t)-1;
117 dmsg_iocom_restate(iocom,
120 iocom->altmsg_callback);
125 master_auth_conn_rx(dmsg_msg_t *msg)
127 if (msg->any.head.cmd & DMSGF_DELETE)
128 dmsg_msg_reply(msg, 0);
133 master_auth_rxmsg(dmsg_msg_t *msg __unused)
137 /************************************************************************
138 * POST-AUTHENTICATION SERVICE MSGS *
139 ************************************************************************
141 * Callback via dmsg_iocom_core().
145 master_link_signal(dmsg_iocom_t *iocom)
147 dmsg_msg_lnk_signal(iocom);
152 master_link_rxmsg(dmsg_msg_t *msg)
158 * If the message state has a function established we just
159 * call the function, otherwise we call the appropriate
160 * link-level protocol related to the original command and
161 * let it sort it out.
163 * Non-transactional one-off messages, on the otherhand,
164 * might have REPLY set.
167 cmd = state ? state->icmd : msg->any.head.cmd;
169 if (state && state->func) {
170 assert(state->func != NULL);
173 switch(cmd & DMSGF_PROTOS) {
181 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
188 * This is called from the master node to process a received debug
189 * shell command. We process the command, outputting the results,
190 * then finish up by outputting another prompt.
193 dmsg_msg_dbg(dmsg_msg_t *msg)
195 switch(msg->any.head.cmd & DMSGF_CMDSWMASK) {
198 * This is a command which we must process.
199 * When we are finished we generate a final reply.
202 msg->aux_data[msg->aux_size - 1] = 0;
203 msg->iocom->dbgmsg_callback(msg);
204 dmsg_msg_reply(msg, 0); /* XXX send prompt instead */
206 case DMSG_DBG_SHELL | DMSGF_REPLY:
208 * A reply just prints out the string. No newline is added
209 * (it is expected to be embedded if desired).
212 msg->aux_data[msg->aux_size - 1] = 0;
214 write(2, msg->aux_data, strlen(msg->aux_data));
217 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
223 * Returns text debug output to the original defined by (msg). (msg) is
224 * not modified and stays intact. We use a one-way message with REPLY set
225 * to distinguish between a debug command and debug terminal output.
227 * To prevent loops circuit_printf() can filter the message (cmd) related
228 * to the circuit_printf(). We filter out DBG messages.
231 dmsg_circuit_printf(dmsg_circuit_t *circuit, const char *ctl, ...)
239 vsnprintf(buf, sizeof(buf), ctl, va);
241 len = strlen(buf) + 1;
243 rmsg = dmsg_msg_alloc(circuit, len,
244 DMSG_DBG_SHELL | DMSGF_REPLY,
246 bcopy(buf, rmsg->aux_data, len);
248 dmsg_msg_write(rmsg);