hammer2 - Early messaging infrastructure
[dragonfly.git] / sbin / hammer2 / cmd_node.c
... / ...
CommitLineData
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
38static void *node_master_accept(void *data);
39static void *node_master_service(void *data);
40static void node_master_recv(hammer2_iocom_t *iocom);
41static void node_master_send(hammer2_iocom_t *iocom);
42
43/*
44 * Start-up the master listener daemon for the machine.
45 *
46 * The master listener serves as a rendezvous point in the cluster, accepting
47 * connections, performing registrations and authentications, maintaining
48 * the spanning tree, and keeping track of message state so disconnects can
49 * be handled properly.
50 *
51 * Once authenticated only low-level messaging protocols (which includes
52 * tracking persistent messages) are handled by this daemon. This daemon
53 * does not run the higher level quorum or locking protocols.
54 *
55 * This daemon can also be told to maintain connections to other nodes,
56 * forming a messaging backbone, which in turn allows PFS's (if desired) to
57 * simply connect to the master daemon via localhost if desired.
58 * Backbones are specified via /etc/hammer2.conf.
59 */
60int
61cmd_node(void)
62{
63 struct sockaddr_in lsin;
64 int on;
65 int lfd;
66
67 /*
68 * Acquire socket and set options
69 */
70 if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
71 fprintf(stderr, "node_master_listen: socket(): %s\n",
72 strerror(errno));
73 return 1;
74 }
75 on = 1;
76 setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
77
78 /*
79 * Setup listen port and try to bind. If the bind fails we assume
80 * that a master listener process is already running and silently
81 * fail.
82 */
83 bzero(&lsin, sizeof(lsin));
84 lsin.sin_family = AF_INET;
85 lsin.sin_addr.s_addr = INADDR_ANY;
86 lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
87 if (bind(lfd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
88 close(lfd);
89 fprintf(stderr, "master listen: daemon already running\n");
90 return 0;
91 }
92 fprintf(stderr, "master listen: startup\n");
93 listen(lfd, 50);
94
95 /*
96 * Fork and disconnect the controlling terminal and parent process,
97 * executing the specified function as a pthread.
98 *
99 * Returns to the original process which can then continue running.
100 * In debug mode this call will create the pthread without forking
101 * and set NormalExit to 0, instead of fork.
102 */
103 hammer2_demon(node_master_accept, (void *)(intptr_t)lfd);
104 if (NormalExit)
105 close(lfd);
106 return 0;
107}
108
109/*
110 * Master listen/accept thread. Accept connections on the master socket,
111 * starting a pthread for each one.
112 */
113static
114void *
115node_master_accept(void *data)
116{
117 struct sockaddr_in asin;
118 socklen_t alen;
119 pthread_t thread;
120 int lfd = (int)(intptr_t)data;
121 int fd;
122
123 /*
124 * Nobody waits for us
125 */
126 setproctitle("hammer2 master listen");
127 pthread_detach(pthread_self());
128
129 /*
130 * Accept connections and create pthreads to handle them after
131 * validating the IP.
132 */
133 for (;;) {
134 alen = sizeof(asin);
135 fd = accept(lfd, (struct sockaddr *)&asin, &alen);
136 if (fd < 0) {
137 if (errno == EINTR)
138 continue;
139 break;
140 }
141 thread = NULL;
142 fprintf(stderr, "node_master_accept: accept fd %d\n", fd);
143 pthread_create(&thread, NULL,
144 node_master_service, (void *)(intptr_t)fd);
145 }
146 return (NULL);
147}
148
149/*
150 * Service an accepted connection (runs as a pthread)
151 */
152static
153void *
154node_master_service(void *data)
155{
156 hammer2_iocom_t iocom;
157 int fd;
158
159 fd = (int)(intptr_t)data;
160 hammer2_iocom_init(&iocom, fd, -1);
161 hammer2_iocom_core(&iocom, node_master_recv, node_master_send, NULL);
162
163 fprintf(stderr,
164 "iocom on fd %d terminated error rx=%d, tx=%d\n",
165 fd, iocom.ioq_rx.error, iocom.ioq_tx.error);
166 close(fd);
167
168 return (NULL);
169}
170
171/*
172 * Callback from hammer2_iocom_core() when messages might be present
173 * on the socket.
174 */
175static
176void
177node_master_recv(hammer2_iocom_t *iocom)
178{
179 hammer2_msg_t *msg;
180
181 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
182 (msg = hammer2_ioq_read(iocom)) != NULL) {
183 fprintf(stderr, "MSG RECEIVED: %08x error %d\n",
184 msg->any.head.cmd, msg->any.head.error);
185 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
186 case HAMMER2_LNK_ERROR:
187 break;
188 case HAMMER2_DBG_SHELL:
189 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
190 hammer2_debug_remote(iocom, msg);
191 break;
192 default:
193 hammer2_ioq_reply_term(iocom, msg,
194 HAMMER2_MSG_ERR_UNKNOWN);
195 break;
196 }
197 }
198 if (iocom->ioq_rx.error) {
199 fprintf(stderr,
200 "node_master_recv: comm error %d\n",
201 iocom->ioq_rx.error);
202 }
203}
204
205/*
206 * Callback from hammer2_iocom_core() when messages might be transmittable
207 * to the socket.
208 */
209static
210void
211node_master_send(hammer2_iocom_t *iocom)
212{
213 hammer2_ioq_write(iocom, NULL);
214}