| 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_rx(hammer2_iocom_t *iocom); |
| 41 | static void master_auth_tx(hammer2_iocom_t *iocom); |
| 42 | static void master_link_rx(hammer2_iocom_t *iocom); |
| 43 | static void master_link_tx(hammer2_iocom_t *iocom); |
| 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 | fprintf(stderr, "master listen: daemon already running\n"); |
| 92 | return 0; |
| 93 | } |
| 94 | fprintf(stderr, "master listen: startup\n"); |
| 95 | listen(lfd, 50); |
| 96 | |
| 97 | /* |
| 98 | * Fork and disconnect the controlling terminal and parent process, |
| 99 | * executing the specified function as a pthread. |
| 100 | * |
| 101 | * Returns to the original process which can then continue running. |
| 102 | * In debug mode this call will create the pthread without forking |
| 103 | * and set NormalExit to 0, instead of fork. |
| 104 | */ |
| 105 | hammer2_demon(master_accept, (void *)(intptr_t)lfd); |
| 106 | if (NormalExit) |
| 107 | close(lfd); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Master listen/accept thread. Accept connections on the master socket, |
| 113 | * starting a pthread for each one. |
| 114 | */ |
| 115 | static |
| 116 | void * |
| 117 | master_accept(void *data) |
| 118 | { |
| 119 | struct sockaddr_in asin; |
| 120 | socklen_t alen; |
| 121 | pthread_t thread; |
| 122 | int lfd = (int)(intptr_t)data; |
| 123 | int fd; |
| 124 | |
| 125 | /* |
| 126 | * Nobody waits for us |
| 127 | */ |
| 128 | setproctitle("hammer2 master listen"); |
| 129 | pthread_detach(pthread_self()); |
| 130 | |
| 131 | /* |
| 132 | * Accept connections and create pthreads to handle them after |
| 133 | * validating the IP. |
| 134 | */ |
| 135 | for (;;) { |
| 136 | alen = sizeof(asin); |
| 137 | fd = accept(lfd, (struct sockaddr *)&asin, &alen); |
| 138 | if (fd < 0) { |
| 139 | if (errno == EINTR) |
| 140 | continue; |
| 141 | break; |
| 142 | } |
| 143 | thread = NULL; |
| 144 | fprintf(stderr, "master_accept: accept fd %d\n", fd); |
| 145 | pthread_create(&thread, NULL, |
| 146 | master_service, (void *)(intptr_t)fd); |
| 147 | } |
| 148 | return (NULL); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Service an accepted connection (runs as a pthread) |
| 153 | */ |
| 154 | static |
| 155 | void * |
| 156 | master_service(void *data) |
| 157 | { |
| 158 | hammer2_iocom_t iocom; |
| 159 | int fd; |
| 160 | |
| 161 | fd = (int)(intptr_t)data; |
| 162 | hammer2_iocom_init(&iocom, fd, -1); |
| 163 | hammer2_iocom_core(&iocom, master_auth_rx, master_auth_tx, NULL); |
| 164 | |
| 165 | fprintf(stderr, |
| 166 | "iocom on fd %d terminated error rx=%d, tx=%d\n", |
| 167 | fd, iocom.ioq_rx.error, iocom.ioq_tx.error); |
| 168 | close(fd); |
| 169 | |
| 170 | return (NULL); |
| 171 | } |
| 172 | |
| 173 | /************************************************************************ |
| 174 | * AUTHENTICATION * |
| 175 | ************************************************************************ |
| 176 | * |
| 177 | * Additional messaging-based authentication must occur before normal |
| 178 | * message operation. The connection has already been encrypted at |
| 179 | * this point. |
| 180 | */ |
| 181 | static |
| 182 | void |
| 183 | master_auth_rx(hammer2_iocom_t *iocom __unused) |
| 184 | { |
| 185 | printf("AUTHRX\n"); |
| 186 | iocom->recvmsg_callback = master_link_rx; |
| 187 | iocom->sendmsg_callback = master_link_tx; |
| 188 | } |
| 189 | |
| 190 | static |
| 191 | void |
| 192 | master_auth_tx(hammer2_iocom_t *iocom __unused) |
| 193 | { |
| 194 | printf("AUTHTX\n"); |
| 195 | iocom->recvmsg_callback = master_link_rx; |
| 196 | iocom->sendmsg_callback = master_link_tx; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Callback from hammer2_iocom_core() when messages might be present |
| 201 | * on the socket. |
| 202 | */ |
| 203 | static |
| 204 | void |
| 205 | master_link_rx(hammer2_iocom_t *iocom) |
| 206 | { |
| 207 | hammer2_msg_t *msg; |
| 208 | |
| 209 | while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 && |
| 210 | (msg = hammer2_ioq_read(iocom)) != NULL) { |
| 211 | fprintf(stderr, "MSG RECEIVED: %08x error %d\n", |
| 212 | msg->any.head.cmd, msg->any.head.error); |
| 213 | switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) { |
| 214 | case HAMMER2_LNK_ERROR: |
| 215 | break; |
| 216 | case HAMMER2_DBG_SHELL: |
| 217 | case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY: |
| 218 | hammer2_shell_remote(msg); |
| 219 | break; |
| 220 | default: |
| 221 | hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN); |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | if (iocom->ioq_rx.error) { |
| 226 | fprintf(stderr, |
| 227 | "master_recv: comm error %d\n", |
| 228 | iocom->ioq_rx.error); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Callback from hammer2_iocom_core() when messages might be transmittable |
| 234 | * to the socket. |
| 235 | */ |
| 236 | static |
| 237 | void |
| 238 | master_link_tx(hammer2_iocom_t *iocom) |
| 239 | { |
| 240 | hammer2_iocom_flush(iocom); |
| 241 | } |