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-------------------------------------------------------------------------------
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"
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
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::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// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
static void scatter(const List< commsStruct > &comms, T &value, const int tag, const label comm)
Broadcast data: Distribute without modification.
virtual bool read()
Re-read model coefficients if they have changed.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:166
Abstract base-class for Time/database function objects.
Executes system calls, entered in the form of string lists.
Definition: systemCall.H:143
label dispatch(const stringList &calls)
Dispatch specified calls.
Definition: systemCall.C:55
bool masterOnly_
Perform system calls on the master only.
Definition: systemCall.H:158
virtual bool read(const dictionary &dict)
Read the system calls.
Definition: systemCall.C:104
virtual bool execute()
Execute the "executeCalls" at each time-step.
Definition: systemCall.C:152
virtual bool write()
Write, execute the "writeCalls".
Definition: systemCall.C:159
virtual bool end()
Execute the "endCalls" at the final time-loop.
Definition: systemCall.C:166
splitCell * master() const
Definition: splitCell.H:113
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
int system(const std::string &command, const bool bg=false)
Execute the specified command via the shell.
Definition: MSwindows.C:1158
bool isAdministrator()
Is the current user the administrator (root)
Definition: MSwindows.C:450
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict