From ad9839f1538cae5f0f4c809ccdf98f2875453df4 Mon Sep 17 00:00:00 2001 From: Marcus Watts Date: Sat, 23 Jun 2007 14:46:53 +0000 Subject: [PATCH] STABLE14-fssync-unix-socket-handling-fixes-20070623 FIXES 63743 make unix domain socket behavior better --- src/vol/fssync.c | 73 ++++++++++++++++++++++++++++-------------------- src/vol/fssync.h | 2 +- src/vol/volume.c | 12 ++++---- src/vol/volume.h | 2 +- 4 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/vol/fssync.c b/src/vol/fssync.c index 9655600e5..68a6445b3 100644 --- a/src/vol/fssync.c +++ b/src/vol/fssync.c @@ -162,7 +162,7 @@ extern int LogLevel; struct Lock FSYNC_handler_lock; int -FSYNC_clientInit(void) +FSYNC_clientInit(int f) { #ifdef USE_UNIX_SOCKETS struct sockaddr_un addr; @@ -178,6 +178,10 @@ FSYNC_clientInit(void) FS_sd = getport(&addr); if (connect(FS_sd, (struct sockaddr *)&addr, sizeof(addr)) >= 0) return 1; + if (!f) { + FSYNC_clientFinis(); + return 0; + } if (!*timeout) break; if (!(*timeout & 1)) @@ -206,6 +210,7 @@ FSYNC_askfs(VolumeId volume, char *partName, int com, int reason) byte response; struct command command; int n; + int retrycount = 8; command.volume = volume; command.command = com; command.reason = reason; @@ -213,38 +218,42 @@ FSYNC_askfs(VolumeId volume, char *partName, int com, int reason) strcpy(command.partName, partName); else command.partName[0] = 0; - assert(FS_sd != -1); VFSYNC_LOCK; #ifdef AFS_NT40_ENV - if (send(FS_sd, (char *)&command, sizeof(command), 0) != sizeof(command)) { - printf("FSYNC_askfs: write to file server failed\n"); +#define FS_SEND(fd,cmd) send(fd, (char*)&(cmd), sizeof(cmd), 0) +#define FS_RECV(fd,resp) recv(fd, &(resp), 1, 0) +#define FS_PERROR(m) fprintf(stderr,"%s\n", m) +#define FS_INTERRUPTED(n) ((n) && WSAEINTR == WSAGetLastError()) +#else +#define FS_SEND(fd,cmd) write(fd,&(cmd), sizeof (cmd)) +#define FS_RECV(fd,resp) read(fd, &(resp), 1) +#define FS_PERROR(m) perror(m) +#define FS_INTERRUPTED(n) (n && errno == EINTR) +#endif + for (;;) { + if (FS_sd != -1) { + if (FS_SEND(FS_sd, command) == sizeof(command)) break; + FS_PERROR("FSYNC_askfs: write to file server failed"); + } + if (--retrycount > 0) { + FSYNC_clientFinis(); + if (FSYNC_clientInit(1)) continue; + } response = FSYNC_DENIED; goto done; } - while ((n = recv(FS_sd, &response, 1, 0)) != 1) { - if (n == 0 || WSAEINTR != WSAGetLastError()) { - printf("FSYNC_askfs: No response from file server\n"); - response = FSYNC_DENIED; - goto done; - } - } -#else - if (write(FS_sd, &command, sizeof(command)) != sizeof(command)) { - printf("FSYNC_askfs: write to file server failed\n"); + for (;;) { + errno = 0; + n = FS_RECV(FS_sd, response); + if (n == 1) break; + if (FS_INTERRUPTED(n)) continue; + fprintf(stderr,"FSYNC_askfs: No response from file server\n"); response = FSYNC_DENIED; goto done; } - while ((n = read(FS_sd, &response, 1)) != 1) { - if (n == 0 || errno != EINTR) { - printf("FSYNC_askfs: No response from file server\n"); - response = FSYNC_DENIED; - goto done; - } - } -#endif if (response == 0) { - printf - ("FSYNC_askfs: negative response from file server; volume %u, command %d\n", + fprintf(stderr, + "FSYNC_askfs: negative response from file server; volume %u, command %d\n", command.volume, (int)command.command); } done: @@ -338,13 +347,6 @@ FSYNC_sync() Log("Set thread id %d for FSYNC_sync\n", tid); #endif /* AFS_PTHREAD_ENV */ -#ifdef USE_UNIX_SOCKETS - strcompose(tbuffer, AFSDIR_PATH_MAX, AFSDIR_SERVER_LOCAL_DIRPATH, "/", - "fssync.sock", NULL); - /* ignore errors */ - remove(tbuffer); -#endif /* USE_UNIX_SOCKETS */ - while (!VInit) { /* Let somebody else run until level > 0. That doesn't mean that * all volumes have been attached. */ @@ -366,6 +368,15 @@ FSYNC_sync() if ((code = bind(AcceptSd, (struct sockaddr *)&addr, sizeof(addr))) == 0) break; +#ifdef USE_UNIX_SOCKETS + code = errno; + if (remove(addr.sun_path) == 0) { + Log("FSYNC_sync: bind failed with (%d), removed bogus %s\n", + code, addr.sun_path); + continue; + } + errno = code; +#endif Log("FSYNC_sync: bind failed with (%d), will sleep and retry\n", errno); sleep(5); diff --git a/src/vol/fssync.h b/src/vol/fssync.h index af5ab02c7..201df6cba 100644 --- a/src/vol/fssync.h +++ b/src/vol/fssync.h @@ -46,6 +46,6 @@ /* Prototypes from fssync.c */ void FSYNC_clientFinis(void); -int FSYNC_clientInit(void); +int FSYNC_clientInit(int); void FSYNC_fsInit(void); int FSYNC_askfs(VolumeId volume, char *partName, int com, int reason); diff --git a/src/vol/volume.c b/src/vol/volume.c index 5ecc94c36..ea07fda2c 100644 --- a/src/vol/volume.c +++ b/src/vol/volume.c @@ -379,9 +379,11 @@ VInitVolumePackage(ProgramType pt, int nLargeVnodes, int nSmallVnodes, VInit = 2; /* Initialized, and all volumes have been attached */ if (programType == volumeUtility && connect) { if (!VConnectFS()) { - Log("Unable to connect to file server; aborted\n"); + Log("Unable to connect to file server; will retry at need\n"); +#if 0 Lock_Destroy(&FSYNC_handler_lock); exit(1); +#endif } } return 0; @@ -457,17 +459,17 @@ VConnectFS(void) { int retVal; VOL_LOCK; - retVal = VConnectFS_r(); + retVal = VConnectFS_r(0); VOL_UNLOCK; return retVal; } int -VConnectFS_r(void) +VConnectFS_r(int f) { int rc; assert(VInit == 2 && programType == volumeUtility); - rc = FSYNC_clientInit(); + rc = FSYNC_clientInit(f); if (rc) VInit = 3; return rc; @@ -700,7 +702,7 @@ VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode) int isbusy = 0; *ec = 0; if (programType == volumeUtility) { - assert(VInit == 3); + assert(VInit == 3 || VConnectFS_r(1)); VLockPartition_r(partition); } if (programType == fileServer) { diff --git a/src/vol/volume.h b/src/vol/volume.h index 68fc0f98c..b5205a0f9 100644 --- a/src/vol/volume.h +++ b/src/vol/volume.h @@ -419,7 +419,7 @@ extern void VPutVolume_r(Volume *); extern void VOffline(Volume * vp, char *message); extern void VOffline_r(Volume * vp, char *message); extern int VConnectFS(void); -extern int VConnectFS_r(void); +extern int VConnectFS_r(int); extern Volume *VAttachVolume(Error * ec, VolumeId volumeId, int mode); extern Volume *VAttachVolume_r(Error * ec, VolumeId volumeId, int mode); extern Volume *VCreateVolume(Error * ec, char *partname, VolId volumeId, -- 2.39.5