Allocate pathname buffers dynamically
[openafs.git] / src / vol / daemon_com.c
index a530c21..9b5149a 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
-
-#include <sys/types.h>
-#include <stdio.h>
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#include <time.h>
-#else
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <sys/time.h>
-#include <unistd.h>
-#endif
-#include <errno.h>
-#include <assert.h>
-#include <signal.h>
-#include <string.h>
-
+#include <roken.h>
+#include <afs/opr.h>
 
 #include <rx/xdr.h>
 #include <afs/afsint.h>
-#include "nfs.h"
 #include <afs/errors.h>
+#include <rx/rx_queue.h>
+
+#include "nfs.h"
 #include "daemon_com.h"
 #include "lwp.h"
 #include "lock.h"
@@ -96,17 +81,13 @@ static int SYNC_ask_internal(SYNC_client_state * state, SYNC_command * com, SYNC
 void
 SYNC_getAddr(SYNC_endpoint_t * endpoint, SYNC_sockaddr_t * addr)
 {
-#ifdef USE_UNIX_SOCKETS
-    char tbuffer[AFSDIR_PATH_MAX];
-#endif /* USE_UNIX_SOCKETS */
-
     memset(addr, 0, sizeof(*addr));
 
 #ifdef USE_UNIX_SOCKETS
-    strcompose(tbuffer, AFSDIR_PATH_MAX, AFSDIR_SERVER_LOCAL_DIRPATH, "/",
-               endpoint->un, NULL);
     addr->sun_family = AF_UNIX;
-    strncpy(addr->sun_path, tbuffer, (sizeof(struct sockaddr_un) - sizeof(short)));
+    snprintf(addr->sun_path, sizeof(addr->sun_path), "%s/%s",
+            AFSDIR_SERVER_LOCAL_DIRPATH, endpoint->un);
+    addr->sun_path[sizeof(addr->sun_path) - 1] = '\0';
 #else  /* !USE_UNIX_SOCKETS */
 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
     addr->sin_len = sizeof(struct sockaddr_in);
@@ -131,7 +112,7 @@ osi_socket
 SYNC_getSock(SYNC_endpoint_t * endpoint)
 {
     osi_socket sd;
-    assert((sd = socket(endpoint->domain, SOCK_STREAM, 0)) >= 0);
+    opr_Verify((sd = socket(endpoint->domain, SOCK_STREAM, 0)) >= 0);
     return sd;
 }
 
@@ -156,7 +137,7 @@ SYNC_connect(SYNC_client_state * state)
        { 3, 3, 3, 5, 5, 5, 7, 15, 16, 24, 32, 40, 48, 0 };
     time_t *timeout = &backoff[0];
 
-    if (state->fd >= 0) {
+    if (state->fd != OSI_NULLSOCKET) {
        return 1;
     }
 
@@ -189,12 +170,8 @@ SYNC_connect(SYNC_client_state * state)
 int
 SYNC_disconnect(SYNC_client_state * state)
 {
-#ifdef AFS_NT40_ENV
-    closesocket(state->fd);
-#else
-    close(state->fd);
-#endif
-    state->fd = -1;
+    rk_closesocket(state->fd);
+    state->fd = OSI_NULLSOCKET;
     return 0;
 }
 
@@ -213,7 +190,7 @@ SYNC_closeChannel(SYNC_client_state * state)
     SYNC_response res;
     SYNC_PROTO_BUF_DECL(ores);
 
-    if (state->fd == -1)
+    if (state->fd == OSI_NULLSOCKET)
        return SYNC_OK;
 
     memset(&com, 0, sizeof(com));
@@ -275,16 +252,11 @@ SYNC_ask(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
     int tries;
     afs_uint32 now, timeout, code=SYNC_OK;
 
-    if (state->fatal_error) {
-       return SYNC_COM_ERROR;
-    }
-
-    if (state->fd == -1) {
+    if (state->fd == OSI_NULLSOCKET) {
        SYNC_connect(state);
     }
 
-    if (state->fd == -1) {
-       state->fatal_error = 1;
+    if (state->fd == OSI_NULLSOCKET) {
        return SYNC_COM_ERROR;
     }
 
@@ -320,10 +292,9 @@ SYNC_ask(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
     }
 
     if (code == SYNC_COM_ERROR) {
-       Log("SYNC_ask: fatal protocol error on circuit '%s'; disabling sync "
-           "protocol until next server restart\n",
-           state->proto_name);
-       state->fatal_error = 1;
+       Log("SYNC_ask: too many / too latent fatal protocol errors on circuit "
+           "'%s'; giving up (tries %d timeout %d)\n",
+           state->proto_name, tries, timeout);
     }
 
     return code;
@@ -352,7 +323,7 @@ SYNC_ask_internal(SYNC_client_state * state, SYNC_command * com, SYNC_response *
     struct iovec iov[2];
 #endif
 
-    if (state->fd == -1) {
+    if (state->fd == OSI_NULLSOCKET) {
        Log("SYNC_ask:  invalid sync file descriptor on circuit '%s'\n",
            state->proto_name);
        res->hdr.response = SYNC_COM_ERROR;
@@ -630,7 +601,7 @@ SYNC_verifyProtocolString(char * buf, size_t len)
 {
     size_t s_len;
 
-    s_len = afs_strnlen(buf, len);
+    s_len = strnlen(buf, len);
 
     return (s_len == len) ? 1 : 0;
 }