gdb - Local mods (compile)
[dragonfly.git] / lib / libdmsg / service.c
CommitLineData
0c3a8cd0 1/*
d30cab67 2 * Copyright (c) 2011-2014 The DragonFly Project. All rights reserved.
0c3a8cd0
MD
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
0d20ec8a 38static void master_auth_signal(dmsg_iocom_t *iocom);
0c3a8cd0 39static void master_auth_rxmsg(dmsg_msg_t *msg);
0d20ec8a 40static void master_link_signal(dmsg_iocom_t *iocom);
0c3a8cd0
MD
41static 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 */
48void *
49dmsg_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
2892d211
MD
57 dmsg_iocom_init(&iocom,
58 info->fd,
59 (info->altmsg_callback ? info->altfd : -1),
60 master_auth_signal,
61 master_auth_rxmsg,
01e43224 62 info->usrmsg_callback,
2892d211 63 info->altmsg_callback);
98126869
MD
64 if (info->noclosealt)
65 iocom.flags &= ~DMSG_IOCOMF_CLOSEALT;
f306de83
MD
66 if (info->label) {
67 dmsg_iocom_label(&iocom, "%s", info->label);
68 free(info->label);
69 info->label = NULL;
70 }
0c3a8cd0 71 dmsg_iocom_core(&iocom);
2892d211 72 dmsg_iocom_done(&iocom);
0c3a8cd0 73
5ab1caed
MD
74 dmio_printf(iocom, 1,
75 "iocom on fd %d terminated error rx=%d, tx=%d\n",
76 info->fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
0c3a8cd0
MD
77 close(info->fd);
78 info->fd = -1; /* safety */
185ace93
MD
79 if (info->exit_callback)
80 info->exit_callback(info->handle);
0c3a8cd0
MD
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 */
96static void master_auth_conn_rx(dmsg_msg_t *msg);
97
98static
99void
0d20ec8a 100master_auth_signal(dmsg_iocom_t *iocom)
0c3a8cd0
MD
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 */
1b8eded1 110 msg = dmsg_msg_alloc(&iocom->state0, 0,
0d20ec8a 111 DMSG_LNK_CONN | DMSGF_CREATE,
0c3a8cd0
MD
112 master_auth_conn_rx, NULL);
113 msg->any.lnk_conn.peer_mask = (uint64_t)-1;
7750fd72
MD
114 msg->any.lnk_conn.peer_type = DMSG_PEER_ROUTER;
115 msg->any.lnk_conn.peer_mask = (uint64_t)-1;
0c3a8cd0
MD
116
117 dmsg_msg_write(msg);
118
01e43224 119 dmsg_iocom_restate(iocom, master_link_signal, master_link_rxmsg);
0c3a8cd0
MD
120}
121
122static
123void
124master_auth_conn_rx(dmsg_msg_t *msg)
125{
126 if (msg->any.head.cmd & DMSGF_DELETE)
127 dmsg_msg_reply(msg, 0);
128}
129
130static
131void
132master_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 */
142static
143void
0d20ec8a 144master_link_signal(dmsg_iocom_t *iocom)
0c3a8cd0 145{
0d20ec8a 146 dmsg_msg_lnk_signal(iocom);
0c3a8cd0
MD
147}
148
149static
150void
151master_link_rxmsg(dmsg_msg_t *msg)
152{
153 dmsg_state_t *state;
1b8eded1 154 dmsg_iocom_t *iocom;
0c3a8cd0
MD
155 uint32_t cmd;
156
157 /*
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.
162 *
163 * Non-transactional one-off messages, on the otherhand,
164 * might have REPLY set.
165 */
166 state = msg->state;
1b8eded1
MD
167 iocom = state->iocom;
168 cmd = (state != &iocom->state0) ? state->icmd : msg->any.head.cmd;
0c3a8cd0 169
d30cab67
MD
170 if (state->func) {
171 /*
172 * Call function or router
173 */
174 assert(state != &iocom->state0);
0c3a8cd0
MD
175 state->func(msg);
176 } else {
d30cab67
MD
177 /*
178 * Top-level message
179 */
0c3a8cd0
MD
180 switch(cmd & DMSGF_PROTOS) {
181 case DMSG_PROTO_LNK:
182 dmsg_msg_lnk(msg);
183 break;
184 case DMSG_PROTO_DBG:
185 dmsg_msg_dbg(msg);
186 break;
187 default:
1b8eded1 188 iocom->usrmsg_callback(msg, 1);
0c3a8cd0
MD
189 break;
190 }
191 }
192}
193
194/*
195 * This is called from the master node to process a received debug
196 * shell command. We process the command, outputting the results,
197 * then finish up by outputting another prompt.
198 */
199void
200dmsg_msg_dbg(dmsg_msg_t *msg)
201{
1b8eded1
MD
202 dmsg_iocom_t *iocom = msg->state->iocom;
203
8e226bc8 204 switch(msg->tcmd & DMSGF_CMDSWMASK) {
0c3a8cd0
MD
205 case DMSG_DBG_SHELL:
206 /*
207 * This is a command which we must process.
208 * When we are finished we generate a final reply.
209 */
210 if (msg->aux_data)
211 msg->aux_data[msg->aux_size - 1] = 0;
1b8eded1 212 iocom->usrmsg_callback(msg, 0);
0c3a8cd0
MD
213 break;
214 case DMSG_DBG_SHELL | DMSGF_REPLY:
215 /*
216 * A reply just prints out the string. No newline is added
217 * (it is expected to be embedded if desired).
218 */
219 if (msg->aux_data)
220 msg->aux_data[msg->aux_size - 1] = 0;
221 if (msg->aux_data)
222 write(2, msg->aux_data, strlen(msg->aux_data));
223 break;
224 default:
1b8eded1 225 iocom->usrmsg_callback(msg, 1);
0c3a8cd0
MD
226 break;
227 }
228}
229
230/*
231 * Returns text debug output to the original defined by (msg). (msg) is
232 * not modified and stays intact. We use a one-way message with REPLY set
233 * to distinguish between a debug command and debug terminal output.
234 *
1b8eded1
MD
235 * To prevent loops dmsg_printf() can filter the message (cmd) related
236 * to the dmsg_printf(). We filter out DBG messages.
0c3a8cd0
MD
237 */
238void
1b8eded1 239dmsg_printf(dmsg_iocom_t *iocom, const char *ctl, ...)
0c3a8cd0
MD
240{
241 dmsg_msg_t *rmsg;
242 va_list va;
243 char buf[1024];
244 size_t len;
245
246 va_start(va, ctl);
247 vsnprintf(buf, sizeof(buf), ctl, va);
248 va_end(va);
249 len = strlen(buf) + 1;
250
1b8eded1 251 rmsg = dmsg_msg_alloc(&iocom->state0, len,
0d20ec8a
MD
252 DMSG_DBG_SHELL | DMSGF_REPLY,
253 NULL, NULL);
0c3a8cd0
MD
254 bcopy(buf, rmsg->aux_data, len);
255
256 dmsg_msg_write(rmsg);
257}