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-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace functionObjects
40 {
41  defineTypeNameAndDebug(systemCall, 0);
42 
44  (
45  functionObject,
46  systemCall,
47  dictionary
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 
64  if (!masterOnly_ || Pstream::master())
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::scatter(nCalls);
77  }
78 
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)
97 {
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
145  << exit(FatalError);
146  }
147 
148  return true;
149 }
150 
151 
153 {
154  dispatch(executeCalls_);
155  return true;
156 }
157 
158 
160 {
161  dispatch(writeCalls_);
162  return true;
163 }
164 
165 
167 {
168  dispatch(endCalls_);
169  return true;
170 }
171 
172 
173 // ************************************************************************* //
Foam::functionObjects::systemCall::end
virtual bool end()
Execute the "endCalls" at the final time-loop.
Definition: systemCall.C:166
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
systemCall.H
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::system
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1150
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:150
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:332
Foam::dynamicCode::allowSystemOperations
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:166
Foam::isAdministrator
bool isAdministrator()
Is the current user the administrator (root)
Definition: MSwindows.C:442
Foam::foamVersion::api
const int api
Foam::functionObjects::systemCall::dispatch
label dispatch(const stringList &calls)
Dispatch specified calls.
Definition: systemCall.C:55
dynamicCode.H
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: functionObject.C:163
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::functionObjects::systemCall::systemCall
systemCall(const systemCall &)=delete
No copy construct.
Foam::functionObjects::systemCall::read
virtual bool read(const dictionary &dict)
Read the system calls.
Definition: systemCall.C:104
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::functionObjects::systemCall::write
virtual bool write()
Write, execute the "writeCalls".
Definition: systemCall.C:159
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::List< string >
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::systemCall::execute
virtual bool execute()
Execute the "executeCalls" at each time-step.
Definition: systemCall.C:152
Foam::functionObjects::systemCall::masterOnly_
bool masterOnly_
Perform system calls on the master only.
Definition: systemCall.H:158
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
foamVersion.H