persistent: add a ctdb_db context to the ctdb_persistent_state struct.
[sahlberg/ctdb.git] / tcp / tcp_connect.c
index bd8af57e08a30c1676e927ece18ed173ce737a46..d7a0b3368773ff05d7dc5f1ed8b8c4dff5cd087b 100644 (file)
@@ -2,6 +2,7 @@
    ctdb over TCP
 
    Copyright (C) Andrew Tridgell  2006
+   Copyright (C) Ronnie Sahlberg  2008
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -18,7 +19,7 @@
 */
 
 #include "includes.h"
-#include "lib/events/events.h"
+#include "lib/tevent/tevent.h"
 #include "lib/tdb/include/tdb.h"
 #include "system/network.h"
 #include "system/filesys.h"
@@ -108,7 +109,7 @@ static void ctdb_node_connect_write(struct event_context *ev, struct fd_event *f
 static int ctdb_tcp_get_address(struct ctdb_context *ctdb,
                                const char *address, ctdb_sock_addr *addr)
 {
-       if (parse_ip(address, addr) == 0) {
+       if (parse_ip(address, NULL, 0, addr) == 0) {
                DEBUG(DEBUG_CRIT, (__location__ " Unparsable address : %s.\n", address));
                return -1;
        }
@@ -127,6 +128,8 @@ void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
                                                      struct ctdb_tcp_node);
        struct ctdb_context *ctdb = node->ctdb;
         ctdb_sock_addr sock_in;
+       int sockin_size;
+       int sockout_size;
         ctdb_sock_addr sock_out;
 
        ctdb_tcp_stop_connection(node);
@@ -151,11 +154,12 @@ void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
                return;
        }
 
-DEBUG(DEBUG_ERR,("create socket...\n"));
        tnode->fd = socket(sock_out.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
        set_nonblocking(tnode->fd);
        set_close_on_exec(tnode->fd);
 
+       DEBUG(DEBUG_DEBUG, (__location__ " Created TCP SOCKET FD:%d\n", tnode->fd));
+
        /* Bind our side of the socketpair to the same address we use to listen
         * on incoming CTDB traffic.
         * We must specify this address to make sure that the address we expose to
@@ -163,15 +167,39 @@ DEBUG(DEBUG_ERR,("create socket...\n"));
         * a dedicated non-routeable network.
         */
        ZERO_STRUCT(sock_in);
-#ifdef HAVE_SOCK_SIN_LEN
-       sock_in.ip.sin_len = sizeof(sock_in);
-#endif
        if (ctdb_tcp_get_address(ctdb, ctdb->address.address, &sock_in) != 0) {
+               DEBUG(DEBUG_ERR, (__location__ " Failed to find our address. Failing bind.\n"));
+               close(tnode->fd);
+               return;
+       }
+
+       /* AIX libs check to see if the socket address and length
+          arguments are consistent with each other on calls like
+          connect().   Can not get by with just sizeof(sock_in),
+          need sizeof(sock_in.ip).
+       */
+       switch (sock_in.sa.sa_family) {
+       case AF_INET:
+               sockin_size = sizeof(sock_in.ip);
+               sockout_size = sizeof(sock_out.ip);
+               break;
+       case AF_INET6:
+               sockin_size = sizeof(sock_in.ip6);
+               sockout_size = sizeof(sock_out.ip6);
+               break;
+       default:
+               DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
+                       sock_in.sa.sa_family));
+               close(tnode->fd);
                return;
        }
-       bind(tnode->fd, (struct sockaddr *)&sock_in, sizeof(sock_in));
+#ifdef HAVE_SOCK_SIN_LEN
+       sock_in.ip.sin_len = sockin_size;
+       sock_out.ip.sin_len = sockout_size;
+#endif
+       bind(tnode->fd, (struct sockaddr *)&sock_in, sockin_size);
 
-       if (connect(tnode->fd, (struct sockaddr *)&sock_out, sizeof(sock_out)) != 0 &&
+       if (connect(tnode->fd, (struct sockaddr *)&sock_out, sockout_size) != 0 &&
            errno != EINPROGRESS) {
                ctdb_tcp_stop_connection(node);
                tnode->connect_te = event_add_timed(ctdb->ev, tnode, 
@@ -230,10 +258,12 @@ static void ctdb_listen_event(struct event_context *ev, struct fd_event *fde,
        set_nonblocking(in->fd);
        set_close_on_exec(in->fd);
 
+       DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d to incoming ctdb connection\n", fd));
+
         setsockopt(in->fd,SOL_SOCKET,SO_KEEPALIVE,(char *)&one,sizeof(one));
 
        in->queue = ctdb_queue_setup(ctdb, in, in->fd, CTDB_TCP_ALIGNMENT, 
-                                    ctdb_tcp_read_cb, in);
+                                    ctdb_tcp_read_cb, in, "ctdbd-%s", incoming_node);
 }
 
 
@@ -249,6 +279,8 @@ static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
        const char *lock_path = "/tmp/.ctdb_socket_lock";
        struct flock lock;
        int one = 1;
+       int sock_size;
+       struct tevent_fd *fde;
 
        /* in order to ensure that we don't get two nodes with the
           same adddress, we must make the bind() and listen() calls
@@ -272,7 +304,11 @@ static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
                return -1;
        }
 
-       for (i=0;i<ctdb->num_nodes;i++) {
+       for (i=0; i < ctdb->num_nodes; i++) {
+               if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
+                       continue;
+               }
+
                /* if node_ip is specified we will only try to bind to that
                   ip.
                */
@@ -283,9 +319,6 @@ static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
                }
 
                ZERO_STRUCT(sock);
-#ifdef HAVE_SOCK_SIN_LEN
-               sock.ip.sin_len = sizeof(sock);
-#endif
                if (ctdb_tcp_get_address(ctdb,
                                ctdb->nodes[i]->address.address, 
                                &sock) != 0) {
@@ -295,15 +328,20 @@ static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
                switch (sock.sa.sa_family) {
                case AF_INET:
                        sock.ip.sin_port = htons(ctdb->nodes[i]->address.port);
+                       sock_size = sizeof(sock.ip);
                        break;
                case AF_INET6:
                        sock.ip6.sin6_port = htons(ctdb->nodes[i]->address.port);
+                       sock_size = sizeof(sock.ip6);
                        break;
                default:
                        DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
                                sock.sa.sa_family));
                        continue;
                }
+#ifdef HAVE_SOCK_SIN_LEN
+               sock.ip.sin_len = sock_size;
+#endif
 
                ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
                if (ctcp->listen_fd == -1) {
@@ -315,10 +353,17 @@ static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
 
                setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
 
-               if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, 
-                        sizeof(sock)) == 0) {
+               if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) == 0) {
                        break;
                }
+
+               if (errno == EADDRNOTAVAIL) {
+                       DEBUG(DEBUG_DEBUG,(__location__ " Failed to bind() to socket. %s(%d)\n",
+                                       strerror(errno), errno));
+               } else {
+                       DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n",
+                                       strerror(errno), errno));
+               }
        }
        
        if (i == ctdb->num_nodes) {
@@ -341,13 +386,19 @@ static int ctdb_tcp_listen_automatic(struct ctdb_context *ctdb)
                DEBUG(DEBUG_INFO, ("This node is configured to start in DISABLED state\n"));
                ctdb->nodes[i]->flags |= NODE_FLAGS_DISABLED;
        }
+       /* do we start out in STOPPED mode? */
+       if (ctdb->start_as_stopped != 0) {
+               DEBUG(DEBUG_INFO, ("This node is configured to start in STOPPED state\n"));
+               ctdb->nodes[i]->flags |= NODE_FLAGS_STOPPED;
+       }
        
        if (listen(ctcp->listen_fd, 10) == -1) {
                goto failed;
        }
 
-       event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE, 
-                    ctdb_listen_event, ctdb);  
+       fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
+                          ctdb_listen_event, ctdb);
+       tevent_fd_set_auto_close(fde);
 
        close(lock_fd);
        return 0;
@@ -368,7 +419,9 @@ int ctdb_tcp_listen(struct ctdb_context *ctdb)
        struct ctdb_tcp *ctcp = talloc_get_type(ctdb->private_data,
                                                struct ctdb_tcp);
         ctdb_sock_addr sock;
+       int sock_size;
        int one = 1;
+       struct tevent_fd *fde;
 
        /* we can either auto-bind to the first available address, or we can
           use a specified address */
@@ -377,9 +430,6 @@ int ctdb_tcp_listen(struct ctdb_context *ctdb)
        }
 
        ZERO_STRUCT(sock);
-#ifdef HAVE_SOCK_SIN_LEN
-       sock.ip.sin_len = sizeof(sock);
-#endif
        if (ctdb_tcp_get_address(ctdb, ctdb->address.address, 
                                 &sock) != 0) {
                goto failed;
@@ -388,15 +438,20 @@ int ctdb_tcp_listen(struct ctdb_context *ctdb)
        switch (sock.sa.sa_family) {
        case AF_INET:
                sock.ip.sin_port = htons(ctdb->address.port);
+               sock_size = sizeof(sock.ip);
                break;
        case AF_INET6:
                sock.ip6.sin6_port = htons(ctdb->address.port);
+               sock_size = sizeof(sock.ip6);
                break;
        default:
                DEBUG(DEBUG_ERR, (__location__ " unknown family %u\n",
                        sock.sa.sa_family));
                goto failed;
        }
+#ifdef HAVE_SOCK_SIN_LEN
+       sock.ip.sin_len = sock_size;
+#endif
 
        ctcp->listen_fd = socket(sock.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
        if (ctcp->listen_fd == -1) {
@@ -408,7 +463,8 @@ int ctdb_tcp_listen(struct ctdb_context *ctdb)
 
         setsockopt(ctcp->listen_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
 
-       if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sizeof(sock)) != 0) {
+       if (bind(ctcp->listen_fd, (struct sockaddr * )&sock, sock_size) != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n", strerror(errno), errno));
                goto failed;
        }
 
@@ -416,8 +472,9 @@ int ctdb_tcp_listen(struct ctdb_context *ctdb)
                goto failed;
        }
 
-       event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE, 
+       fde = event_add_fd(ctdb->ev, ctcp, ctcp->listen_fd, EVENT_FD_READ,
                     ctdb_listen_event, ctdb);  
+       tevent_fd_set_auto_close(fde);
 
        return 0;