ENH: optionally limit systemCall function-object to master only (closes #729)

This commit is contained in:
Mark Olesen
2018-02-09 19:24:31 +01:00
parent 3e3c97397e
commit 88f4b6ca8d
19 changed files with 790 additions and 44 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -46,6 +46,36 @@ namespace functionObjects
}
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
Foam::label Foam::functionObjects::systemCall::dispatch(const stringList& calls)
{
if (calls.empty())
{
return 0;
}
label nCalls = 0;
if (!masterOnly_ || Pstream::master())
{
for (const string& call : calls)
{
Foam::system(call); // Handles empty command as a successful no-op.
++nCalls;
}
}
// MPI barrier
if (masterOnly_)
{
Pstream::scatter(nCalls);
}
return nCalls;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::systemCall::systemCall
@ -58,27 +88,27 @@ Foam::functionObjects::systemCall::systemCall
functionObject(name),
executeCalls_(),
endCalls_(),
writeCalls_()
writeCalls_(),
masterOnly_(false)
{
read(dict);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::systemCall::~systemCall()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::systemCall::read(const dictionary& dict)
{
functionObject::read(dict);
executeCalls_.clear();
writeCalls_.clear();
endCalls_.clear();
dict.readIfPresent("executeCalls", executeCalls_);
dict.readIfPresent("endCalls", endCalls_);
dict.readIfPresent("writeCalls", writeCalls_);
dict.readIfPresent("endCalls", endCalls_);
masterOnly_ = dict.lookupOrDefault("master", false);
if (executeCalls_.empty() && endCalls_.empty() && writeCalls_.empty())
{
@ -89,8 +119,8 @@ bool Foam::functionObjects::systemCall::read(const dictionary& dict)
else if (!dynamicCode::allowSystemOperations)
{
FatalErrorInFunction
<< "Executing user-supplied system calls is not enabled by "
<< "default because of " << nl
<< "Executing user-supplied system calls may not be enabled by "
<< "default due to potential " << nl
<< "security issues. If you trust the case you can enable this "
<< "facility by " << nl
<< "adding to the InfoSwitches setting in the system controlDict:"
@ -109,33 +139,21 @@ bool Foam::functionObjects::systemCall::read(const dictionary& dict)
bool Foam::functionObjects::systemCall::execute()
{
forAll(executeCalls_, calli)
{
Foam::system(executeCalls_[calli]);
}
return true;
}
bool Foam::functionObjects::systemCall::end()
{
forAll(endCalls_, calli)
{
Foam::system(endCalls_[calli]);
}
dispatch(executeCalls_);
return true;
}
bool Foam::functionObjects::systemCall::write()
{
forAll(writeCalls_, calli)
{
Foam::system(writeCalls_[calli]);
}
dispatch(writeCalls_);
return true;
}
bool Foam::functionObjects::systemCall::end()
{
dispatch(endCalls_);
return true;
}

View File

@ -28,7 +28,7 @@ Group
grpUtilitiesFunctionObjects
Description
Executes system calls, entered in the form of a string lists.
Executes system calls, entered in the form of string lists.
Calls can be made at the following points in the calculation:
- every time step
@ -36,7 +36,7 @@ Description
- end of the calculation
Usage
Example of function object specification:
Example of the function object specification:
\verbatim
systemCall1
{
@ -49,11 +49,11 @@ Usage
);
writeCalls
(
"echo \*\*\* writing data \*\*\*"
"echo === writing data ==="
);
endCalls
(
"echo \*\*\* writing .bashrc \*\*\*"
"echo === echoing .bashrc ==="
"cat ~/.bashrc"
"echo \*\*\* done \*\*\*"
);
@ -67,6 +67,7 @@ Usage
executeCalls | list of calls on execute | yes |
writeCalls | list of calls on write | yes |
endCalls | list of calls on end | yes |
master | execute on master only | no | false
\endtable
Note
@ -75,6 +76,9 @@ Note
\c allowSystemOperations must be set to '1'; otherwise, system calls will
not be allowed.
Additionally, since the system commands are normally sent via the shell,
special shell character may require backslash escaping.
See also
Foam::functionObject
Foam::functionObjects::timeControl
@ -118,16 +122,20 @@ protected:
//- List of calls to execute - write steps
stringList writeCalls_;
//- Perform system calls on the master only
bool masterOnly_;
private:
// Private member functions
// Protected Member Functions
//- Dispatch specified calls
label dispatch(const stringList& calls);
//- Disallow default bitwise copy construct
systemCall(const systemCall&);
systemCall(const systemCall&) = delete;
//- Disallow default bitwise assignment
void operator=(const systemCall&);
void operator=(const systemCall&) = delete;
public:
@ -148,22 +156,22 @@ public:
//- Destructor
virtual ~systemCall();
virtual ~systemCall() = default;
// Member Functions
//- Read the system calls
virtual bool read(const dictionary&);
virtual bool read(const dictionary& dict);
//- Execute the "executeCalls" at each time-step
virtual bool execute();
//- Execute the "endCalls" at the final time-loop
virtual bool end();
//- Write, execute the "writeCalls"
virtual bool write();
//- Execute the "endCalls" at the final time-loop
virtual bool end();
};