diff --git a/etc/codeTemplates/codeStream/fixedValueFvPatchScalarFieldTemplate.C b/etc/codeTemplates/codeStream/fixedValueFvPatchScalarFieldTemplate.C index 417b77ee68..76a31e9fb0 100644 --- a/etc/codeTemplates/codeStream/fixedValueFvPatchScalarFieldTemplate.C +++ b/etc/codeTemplates/codeStream/fixedValueFvPatchScalarFieldTemplate.C @@ -35,6 +35,20 @@ ${codeInclude} namespace Foam { +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // + +extern "C" +{ + // unique function name that can be checked if the correct library version + // has been loaded + bool ${typeName}_${SHA1sum}() + { + return true; + } +} + + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // ${typeName}FixedValueFvPatchScalarField:: diff --git a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C index f2bc4c3f10..6144653367 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C @@ -35,7 +35,8 @@ License #include "dlLibraryTable.H" #include "OSspecific.H" #include "Time.H" -#include "Pstream.H" +#include "PstreamReduceOps.H" + // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -70,18 +71,11 @@ bool Foam::functionEntries::codeStream::execute Istream& is ) { - if (isAdministrator()) - { - FatalIOErrorIn - ( - "functionEntries::codeStream::execute(..)", - parentDict - ) << "This code should not be executed by someone with administrator" - << " rights due to security reasons." << endl - << "(it writes a shared library which then gets loaded " - << "using dlopen)" - << exit(FatalIOError); - } + codeStreamTools::checkSecurity + ( + "functionEntries::codeStream::execute(..)", + parentDict + ); // get code dictionary // must reference parent for stringOps::expand to work nicely @@ -175,6 +169,7 @@ bool Foam::functionEntries::codeStream::execute copyFiles[0].file() = fileCsrc; copyFiles[0].set("codeInclude", codeInclude); copyFiles[0].set("code", code); + copyFiles[0].set("SHA1sum", sha.str()); List filesContents(2); @@ -218,8 +213,9 @@ bool Foam::functionEntries::codeStream::execute } } -// bool dummy = true; -// reduce(dummy, orOp()); + // all processes must wait for compile + bool dummy = true; + reduce(dummy, orOp()); if (!dlLibraryTable::open(libPath, false)) { diff --git a/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.C b/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.C index 1eaa2a4e9d..cf252d1f8b 100644 --- a/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.C +++ b/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.C @@ -49,6 +49,28 @@ const Foam::fileName Foam::codeStreamTools::codeTemplateDirName // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // +void Foam::codeStreamTools::checkSecurity +( + const char* title, + const dictionary& context +) +{ + if (isAdministrator()) + { + FatalIOErrorIn + ( + title, + context + ) << "This code should not be executed by someone with administrator" + << " rights due to security reasons." << nl + << "(it writes a shared library which then gets loaded " + << "using dlopen)" + << exit(FatalIOError); + } +} + + + Foam::fileName Foam::codeStreamTools::codePath(const word& subDirName) { return stringOps::expand("$FOAM_CASE/codeStream/" + subDirName); @@ -253,6 +275,13 @@ bool Foam::codeStreamTools::copyFilesContents(const fileName& dir) const // variables mapping HashTable mapping(copyFiles_[i]); mapping.set("typeName", name_); + + // provide a zero digest if not otherwise specified + if (!mapping.found("SHA1sum")) + { + mapping.insert("SHA1sum", SHA1Digest().str()); + } + copyAndExpand(is, os, mapping); } diff --git a/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.H b/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.H index 460eb7e0fc..8df397cc15 100644 --- a/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.H +++ b/src/OpenFOAM/db/dynamicLibrary/codeStream/codeStreamTools.H @@ -145,6 +145,13 @@ public: // Member functions + //- Check security for creating dynamic code + static void checkSecurity + ( + const char* title, + const dictionary& context + ); + //- Local path for specified code name // Expanded from \$FOAM_CASE/codeStream static fileName codePath(const word& subDirName); diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchScalarField.C b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchScalarField.C index 51f88bf131..8b19852db8 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchScalarField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchScalarField.C @@ -124,7 +124,9 @@ void Foam::codedFixedValueFvPatchScalarField::writeLibrary // <<"new SHA1: " << sha << endl; - // (void) codeStreamTools::upToDate(codePath, sha) + // only use side-effect of writing SHA1Digest for now + (void) codeStreamTools::upToDate(codePath, sha); + // TODO: compile on-demand if (true) { @@ -152,6 +154,7 @@ void Foam::codedFixedValueFvPatchScalarField::writeLibrary copyFiles[0].file() = fileCsrc; copyFiles[0].set("codeInclude", codeInclude); copyFiles[0].set("code", code); + copyFiles[0].set("SHA1sum", sha.str()); copyFiles[1].file() = fileHsrc; @@ -190,18 +193,11 @@ void Foam::codedFixedValueFvPatchScalarField::writeLibrary void Foam::codedFixedValueFvPatchScalarField::updateLibrary() { - if (isAdministrator()) - { - FatalIOErrorIn - ( - "codedFixedValueFvPatchScalarField::updateLibrary()", - dict_ - ) << "This code should not be executed by someone with administrator" - << " rights due to security reasons." << endl - << "(it writes a shared library which then gets loaded " - << "using dlopen)" - << exit(FatalIOError); - } + codeStreamTools::checkSecurity + ( + "codedFixedValueFvPatchScalarField::updateLibrary()", + dict_ + ); // write code into redirectType_ subdir const fileName codePath = codeStreamTools::codePath(redirectType_); @@ -220,6 +216,16 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary() void* lib = dlLibraryTable::findLibrary(libPath); + + // TODO: + // calculate old/new SHA1 for code + // check if the correct library version was already loaded. + // Find the library handle. + // + // string signatureName(redirectType_ + "_" + sha().str()); + // void (*signatureFunction)(); + // signatureFunction = reinterpret_cast(dlSym(lib, signatureName)); + if (dict_.found("code")) { if (!lib) @@ -274,6 +280,7 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary() } } + // all processes must wait for compile bool dummy = true; reduce(dummy, orOp());