Loading...
Searching...
No Matches
systemCall.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2018-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "systemCall.H"
30#include "Time.H"
31#include "dynamicCode.H"
32#include "foamVersion.H"
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37namespace Foam
38{
39namespace functionObjects
40{
42
44 (
48 );
49}
50}
51
52
53// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
54
56{
57 if (calls.empty())
58 {
59 return 0;
60 }
61
62 label nCalls = 0;
63
65 {
66 for (const string& call : calls)
67 {
68 Foam::system(call); // Handles empty command as a successful no-op.
69 ++nCalls;
70 }
71 }
72
73 // MPI barrier
74 if (masterOnly_)
75 {
76 Pstream::broadcast(nCalls);
77 }
79 return nCalls;
80}
81
82
83// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
84
86(
87 const word& name,
88 const Time&,
89 const dictionary& dict
90)
91:
93 executeCalls_(),
94 writeCalls_(),
95 endCalls_(),
96 masterOnly_(false)
98 read(dict);
99}
100
101
102// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
103
105{
107
108 executeCalls_.clear();
109 writeCalls_.clear();
110 endCalls_.clear();
111
112 dict.readIfPresent("executeCalls", executeCalls_);
113 dict.readIfPresent("writeCalls", writeCalls_);
114 dict.readIfPresent("endCalls", endCalls_);
115 masterOnly_ = dict.getOrDefault("master", false);
116
117 if (executeCalls_.empty() && endCalls_.empty() && writeCalls_.empty())
118 {
120 << "No executeCalls, endCalls or writeCalls defined."
121 << endl;
122 }
123 else if (isAdministrator())
124 {
126 << "System calls should not be executed by someone"
127 << " with administrator rights for security reasons." << nl
128 << nl << endl
129 << exit(FatalError);
130 }
132 {
134 << "Executing user-supplied system calls may have been disabled"
135 << " by default" << nl
136 << "for security reasons." << nl
137 << "If you trust the code, you may enable this by adding"
138 << nl << nl
139 << " allowSystemOperations 1" << nl << nl
140 << "to the InfoSwitches setting in the system controlDict." << nl
141 << "The system controlDict is any of" << nl << nl
142 << " ~/.OpenFOAM/" << foamVersion::api << "/controlDict" << nl
143 << " ~/.OpenFOAM/controlDict" << nl
144 << " $WM_PROJECT_DIR/etc/controlDict" << nl << endl
146 }
147
148 return true;
149}
150
151
157
158
160{
162 return true;
163}
164
165
167{
168 dispatch(endCalls_);
169 return true;
170}
171
172
173// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
bool empty() const noexcept
True if List is empty (ie, size() is zero).
Definition UList.H:701
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
@ broadcast
broadcast [MPI]
Definition UPstream.H:189
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
static int allowSystemOperations
Flag if system operations are allowed.
Abstract base-class for Time/database function objects.
const word & name() const noexcept
Return the name of this functionObject.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
functionObject(const word &name, const bool withNamePrefix=defaultUseNamePrefix)
Construct from components.
Executes system calls, entered in the form of string lists.
Definition systemCall.H:157
label dispatch(const stringList &calls)
Dispatch specified calls.
Definition systemCall.C:48
bool masterOnly_
Perform system calls on the master only.
Definition systemCall.H:180
stringList writeCalls_
List of calls to execute - write steps.
Definition systemCall.H:170
systemCall(const systemCall &)=delete
No copy construct.
stringList executeCalls_
List of calls to execute - every step.
Definition systemCall.H:165
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
Definition systemCall.C:97
stringList endCalls_
List of calls to execute when exiting the time-loop.
Definition systemCall.H:175
virtual bool execute()
Execute the function-object operations.
Definition systemCall.C:145
virtual bool write()
Write the function-object results.
Definition systemCall.C:152
virtual bool end()
Execute the "endCalls" at the final time-loop.
Definition systemCall.C:159
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
#define WarningInFunction
Report a warning using Foam::Warning.
const int api
OpenFOAM api number (integer) corresponding to the value of OPENFOAM at the time of compilation.
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition POSIX.C:1704
bool isAdministrator()
Is the current user the administrator (root).
Definition POSIX.C:436
List< string > stringList
List of string.
Definition stringList.H:32
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict