From 44adf21c834ca8624b47dc6dbc2794bc429463ad Mon Sep 17 00:00:00 2001 From: David Steele Date: Mon, 10 Feb 2020 21:30:43 -0700 Subject: [PATCH] Consolidate archive async exec code. Move duplicated code to the common module. This will reduce copy and paste between the get and push modules when changes are made. --- src/Makefile.in | 6 +++--- src/command/archive/common.c | 30 ++++++++++++++++++++++++++++++ src/command/archive/common.h | 3 +++ src/command/archive/get/get.c | 17 ++--------------- src/command/archive/push/push.c | 17 ++--------------- test/define.yaml | 2 ++ 6 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/Makefile.in b/src/Makefile.in index 4010b0e81..9c5fbd7bc 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -212,13 +212,13 @@ clean: #################################################################################################################################### # Compile rules #################################################################################################################################### -command/archive/common.o: command/archive/common.c build.auto.h command/archive/common.h common/assert.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/regExp.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h common/wait.h config/config.auto.h config/config.h config/define.auto.h config/define.h postgres/version.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h +command/archive/common.o: command/archive/common.c build.auto.h command/archive/common.h common/assert.h common/debug.h common/error.auto.h common/error.h common/fork.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/regExp.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h common/wait.h config/config.auto.h config/config.h config/define.auto.h config/define.h postgres/version.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h $(CC) $(CPPFLAGS) $(CFLAGS) $(CMAKE) -c command/archive/common.c -o command/archive/common.o command/archive/get/file.o: command/archive/get/file.c build.auto.h command/archive/common.h command/archive/get/file.h command/control/common.h common/assert.h common/compress/gzip/common.h common/compress/gzip/decompress.h common/crypto/cipherBlock.h common/crypto/common.h common/debug.h common/error.auto.h common/error.h common/ini.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h config/config.auto.h config/config.h config/define.auto.h config/define.h info/info.h info/infoArchive.h info/infoPg.h postgres/interface.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h $(CC) $(CPPFLAGS) $(CFLAGS) $(CMAKE) -c command/archive/get/file.c -o command/archive/get/file.o -command/archive/get/get.o: command/archive/get/get.c build.auto.h command/archive/common.h command/archive/get/file.h command/archive/get/protocol.h command/command.h common/assert.h common/crypto/common.h common/debug.h common/error.auto.h common/error.h common/fork.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/regExp.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h common/wait.h config/config.auto.h config/config.h config/define.auto.h config/define.h config/exec.h postgres/interface.h protocol/client.h protocol/command.h protocol/helper.h protocol/parallel.h protocol/parallelJob.h protocol/server.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h +command/archive/get/get.o: command/archive/get/get.c build.auto.h command/archive/common.h command/archive/get/file.h command/archive/get/protocol.h command/command.h common/assert.h common/crypto/common.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/regExp.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h common/wait.h config/config.auto.h config/config.h config/define.auto.h config/define.h config/exec.h postgres/interface.h protocol/client.h protocol/command.h protocol/helper.h protocol/parallel.h protocol/parallelJob.h protocol/server.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h $(CC) $(CPPFLAGS) $(CFLAGS) $(CMAKE) -c command/archive/get/get.c -o command/archive/get/get.o command/archive/get/protocol.o: command/archive/get/protocol.c build.auto.h command/archive/get/file.h command/archive/get/protocol.h common/assert.h common/crypto/common.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/group.h common/io/io.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h config/config.auto.h config/config.h config/define.auto.h config/define.h protocol/server.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h @@ -230,7 +230,7 @@ command/archive/push/file.o: command/archive/push/file.c build.auto.h command/ar command/archive/push/protocol.o: command/archive/push/protocol.c build.auto.h command/archive/push/file.h command/archive/push/protocol.h common/assert.h common/crypto/common.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/group.h common/io/io.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h config/config.auto.h config/config.h config/define.auto.h config/define.h protocol/server.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h $(CC) $(CPPFLAGS) $(CFLAGS) $(CMAKE) -c command/archive/push/protocol.c -o command/archive/push/protocol.o -command/archive/push/push.o: command/archive/push/push.c build.auto.h command/archive/common.h command/archive/push/file.h command/archive/push/protocol.h command/command.h command/control/common.h common/assert.h common/crypto/common.h common/debug.h common/error.auto.h common/error.h common/fork.h common/ini.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h common/wait.h config/config.auto.h config/config.h config/define.auto.h config/define.h config/exec.h info/info.h info/infoArchive.h info/infoPg.h postgres/interface.h protocol/client.h protocol/command.h protocol/helper.h protocol/parallel.h protocol/parallelJob.h protocol/server.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h +command/archive/push/push.o: command/archive/push/push.c build.auto.h command/archive/common.h command/archive/push/file.h command/archive/push/protocol.h command/command.h command/control/common.h common/assert.h common/crypto/common.h common/debug.h common/error.auto.h common/error.h common/ini.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h common/wait.h config/config.auto.h config/config.h config/define.auto.h config/define.h config/exec.h info/info.h info/infoArchive.h info/infoPg.h postgres/interface.h protocol/client.h protocol/command.h protocol/helper.h protocol/parallel.h protocol/parallelJob.h protocol/server.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h $(CC) $(CPPFLAGS) $(CFLAGS) $(CMAKE) -c command/archive/push/push.c -o command/archive/push/push.o command/backup/backup.o: command/backup/backup.c build.auto.h command/archive/common.h command/backup/backup.h command/backup/common.h command/backup/file.h command/backup/protocol.h command/check/common.h command/control/common.h command/stanza/common.h common/assert.h common/compress/gzip/common.h common/compress/gzip/compress.h common/compress/gzip/decompress.h common/crypto/cipherBlock.h common/crypto/common.h common/crypto/hash.h common/debug.h common/error.auto.h common/error.h common/ini.h common/io/filter/filter.h common/io/filter/group.h common/io/filter/size.h common/io/read.h common/io/write.h common/lock.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/param.h common/type/string.h common/type/stringList.h common/type/stringz.h common/type/variant.h common/type/variantList.h config/config.auto.h config/config.h config/define.auto.h config/define.h db/db.h db/helper.h info/info.h info/infoArchive.h info/infoBackup.h info/infoPg.h info/manifest.h postgres/client.h postgres/interface.h postgres/version.h protocol/client.h protocol/command.h protocol/helper.h protocol/parallel.h protocol/parallelJob.h protocol/server.h storage/helper.h storage/info.h storage/read.h storage/storage.h storage/write.h version.h diff --git a/src/command/archive/common.c b/src/command/archive/common.c index 57b0e8306..87bf6c1b6 100644 --- a/src/command/archive/common.c +++ b/src/command/archive/common.c @@ -10,6 +10,7 @@ Archive Common #include "command/archive/common.h" #include "common/debug.h" +#include "common/fork.h" #include "common/log.h" #include "common/memContext.h" #include "common/regExp.h" @@ -229,6 +230,35 @@ archiveAsyncStatusOkWrite(ArchiveMode archiveMode, const String *walSegment, con FUNCTION_LOG_RETURN_VOID(); } +/**********************************************************************************************************************************/ +void +archiveAsyncExec(ArchiveMode archiveMode, const StringList *commandExec) +{ + FUNCTION_LOG_BEGIN(logLevelDebug); + FUNCTION_LOG_PARAM(ENUM, archiveMode); + FUNCTION_LOG_PARAM(STRING_LIST, commandExec); + FUNCTION_LOG_END(); + + ASSERT(commandExec != NULL); + + // Fork off the async process + if (forkSafe() == 0) + { + // Disable logging and close log file + logClose(); + + // Detach from parent process + forkDetach(); + + // Execute the binary. This statement will not return if it is successful. + THROW_ON_SYS_ERROR_FMT( + execvp(strPtr(strLstGet(commandExec, 0)), (char ** const)strLstPtr(commandExec)) == -1, ExecuteError, + "unable to execute asynchronous '%s'", archiveMode == archiveModeGet ? CFGCMD_ARCHIVE_GET : CFGCMD_ARCHIVE_PUSH); + } + + FUNCTION_LOG_RETURN_VOID(); +} + /*********************************************************************************************************************************** Is the segment partial? ***********************************************************************************************************************************/ diff --git a/src/command/archive/common.h b/src/command/archive/common.h index 8e92885ea..a1588f6e3 100644 --- a/src/command/archive/common.h +++ b/src/command/archive/common.h @@ -66,6 +66,9 @@ bool archiveAsyncStatus(ArchiveMode archiveMode, const String *walSegment, bool void archiveAsyncStatusOkWrite(ArchiveMode archiveMode, const String *walSegment, const String *warning); void archiveAsyncStatusErrorWrite(ArchiveMode archiveMode, const String *walSegment, int code, const String *message); +// Execute the async process. This function will only return in the calling process and the implementation is platform depedent. +void archiveAsyncExec(ArchiveMode archiveMode, const StringList *commandExec); + bool walIsPartial(const String *walSegment); bool walIsSegment(const String *walSegment); String *walPath(const String *walFile, const String *pgPath, const String *command); diff --git a/src/command/archive/get/get.c b/src/command/archive/get/get.c index 0dc98f999..8f425c4d0 100644 --- a/src/command/archive/get/get.c +++ b/src/command/archive/get/get.c @@ -14,7 +14,6 @@ Archive Get Command #include "command/archive/get/protocol.h" #include "command/command.h" #include "common/debug.h" -#include "common/fork.h" #include "common/log.h" #include "common/memContext.h" #include "common/regExp.h" @@ -235,20 +234,8 @@ cmdArchiveGet(void) // Release the lock so the child process can acquire it lockRelease(true); - // Fork off the async process - if (forkSafe() == 0) - { - // Disable logging and close log file - logClose(); - - // Detach from parent process - forkDetach(); - - // Execute the binary. This statement will not return if it is successful. - THROW_ON_SYS_ERROR( - execvp(strPtr(cfgExe()), (char ** const)strLstPtr(commandExec)) == -1, ExecuteError, - "unable to execute asynchronous '" CFGCMD_ARCHIVE_GET "'"); - } + // Execute the async process + archiveAsyncExec(archiveModeGet, commandExec); // Mark the async process as forked so it doesn't get forked again. A single run of the async process should be // enough to do the job, running it again won't help anything. diff --git a/src/command/archive/push/push.c b/src/command/archive/push/push.c index 5ba106e2c..d08d630f0 100644 --- a/src/command/archive/push/push.c +++ b/src/command/archive/push/push.c @@ -12,7 +12,6 @@ Archive Push Command #include "command/command.h" #include "command/control/common.h" #include "common/debug.h" -#include "common/fork.h" #include "common/log.h" #include "common/memContext.h" #include "common/wait.h" @@ -311,20 +310,8 @@ cmdArchivePush(void) // Release the lock so the child process can acquire it lockRelease(true); - // Fork off the async process - if (forkSafe() == 0) - { - // Disable logging and close log file - logClose(); - - // Detach from parent process - forkDetach(); - - // Execute the binary. This statement will not return if it is successful. - THROW_ON_SYS_ERROR( - execvp(strPtr(cfgExe()), (char ** const)strLstPtr(commandExec)) == -1, ExecuteError, - "unable to execute asynchronous '" CFGCMD_ARCHIVE_PUSH "'"); - } + // Execute the async process + archiveAsyncExec(archiveModePush, commandExec); // Mark the async process as forked so it doesn't get forked again. A single run of the async process should be // enough to do the job, running it again won't help anything. diff --git a/test/define.yaml b/test/define.yaml index 28a2d0dac..fbaf2d9f7 100644 --- a/test/define.yaml +++ b/test/define.yaml @@ -525,6 +525,7 @@ unit: binReq: true coverage: + command/archive/common: full command/archive/get/file: full command/archive/get/get: full command/archive/get/protocol: full @@ -535,6 +536,7 @@ unit: binReq: true coverage: + command/archive/common: full command/archive/push/file: full command/archive/push/protocol: full command/archive/push/push: full