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 -------------------------------------------------------------------------------
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::broadcast(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
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 // ************************************************************************* //
bool masterOnly_
Perform system calls on the master only.
Definition: systemCall.H:162
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
virtual bool write()
Write, execute the "writeCalls".
Definition: systemCall.C:152
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:666
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Abstract base-class for Time/database function objects.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all communicator ranks. Does nothing in non-paral...
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
label dispatch(const stringList &calls)
Dispatch specified calls.
Definition: systemCall.C:48
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
virtual bool execute()
Execute the "executeCalls" at each time-step.
Definition: systemCall.C:145
A class for handling words, derived from Foam::string.
Definition: word.H:63
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:193
const int api
OpenFOAM api number (integer) corresponding to the value of OPENFOAM at the time of compilation...
virtual bool read(const dictionary &dict)
Read the system calls.
Definition: systemCall.C:97
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
systemCall(const systemCall &)=delete
No copy construct.
bool isAdministrator()
Is the current user the administrator (root)
Definition: POSIX.C:434
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
virtual bool end()
Execute the "endCalls" at the final time-loop.
Definition: systemCall.C:159
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: POSIX.C:1702
Namespace for OpenFOAM.