Skip to content

Commit

Permalink
Added argument to System.systemCall: output file (redirect stdout and…
Browse files Browse the repository at this point in the history
… stderr)

- Fixes some issues with OSX not redirecting segmentation faults
- Also added this option to Scripting.system()


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18280 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 25, 2013
1 parent e991939 commit d00cafa
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 57 deletions.
16 changes: 8 additions & 8 deletions Compiler/BackEnd/OpenTURNS.mo
Expand Up @@ -157,8 +157,7 @@ algorithm
compileWrapperCommand := System.readFile(getFullShareFileName(cStrWrapperCompileCmd));
compileWrapperCommand := System.stringReplace(compileWrapperCommand,"<%wrapperName%>", lastClassName +& cStrWrapperSuffix);
compileWrapperCommand := System.stringReplace(compileWrapperCommand,"<%currentDirectory%>", System.pwd());
compileWrapperCommand := compileWrapperCommand +& " > " +& lastClassName +& cStrWrapperSuffix +& ".log" +& " 2>&1";
runCommand(compileWrapperCommand);
runCommand(compileWrapperCommand, lastClassName +& cStrWrapperSuffix +& ".log");
end generateWrapperLibrary;

protected function generateXMLFile "generates the xml file for the OpenTURNS wrapper"
Expand Down Expand Up @@ -713,23 +712,24 @@ algorithm
cmdFile = inStrPythonScriptFile +& ".bat";
System.writeFile(cmdFile, cmdContents);
logFile = inStrPythonScriptFile +& ".log";
runCommand(cmdFile +& " > " +& logFile +& " 2>&1");
runCommand(cmdFile, logFile);
then
logFile;
end match;
end runPythonScript;

protected function runCommand
input String cmd;
input String logFile;
algorithm
_ := matchcontinue(cmd)
case _
_ := matchcontinue(cmd,logFile)
case (_,_)
equation
print("running: " +& cmd +& "\n");
0 = System.systemCall(cmd);
print("running: " +& cmd +& " to logFile: " +& logFile +& "\n");
0 = System.systemCall(cmd,logFile);
then
();
case _
else
equation
print("running: " +& cmd +& "\n\tfailed!\nCheck the log file!\n");
then
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/SimCodeMain.mo
Expand Up @@ -457,7 +457,7 @@ algorithm
case (_, _, "Dump") equation
// Yes, do this better later on...
str = Tpl.tplString2(SimCodeDump.dumpSimCode, simCode, true);
0 = System.systemCall("saxonb-xslt -o '" +& str +& ".html' '" +& str +& ".xml' '" +& Settings.getInstallationDirectoryPath() +& "/share/omc/scripts/simcodedump.xsl'");
0 = System.systemCall("saxonb-xslt -o '" +& str +& ".html' '" +& str +& ".xml' '" +& Settings.getInstallationDirectoryPath() +& "/share/omc/scripts/simcodedump.xsl'", "");
print("User-friendly html output to " +& str +& ".html - please enable javascript\n");
then ();

Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/ConnectionGraph.mo
Expand Up @@ -1515,7 +1515,7 @@ algorithm
fileNameGraphViz +& "','file',null,null);txtview('off');";
Debug.traceln("Running command: " +& "lefty -e " +& leftyCMD +& " > " +& fileNameTraceRemovedConnections);
// execute lefty
leftyExitStatus = System.systemCall("lefty -e " +& leftyCMD +& " > " +& fileNameTraceRemovedConnections);
leftyExitStatus = System.systemCall("lefty -e " +& leftyCMD, fileNameTraceRemovedConnections);
// show the exit status
Debug.traceln("GraphViz *lefty* exited with status:" +& intString(leftyExitStatus));
brokenConnects = System.readFile(fileNameTraceRemovedConnections);
Expand Down
1 change: 1 addition & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -966,6 +966,7 @@ end loadFileInteractive;

function system "Similar to system(3). Executes the given command in the system shell."
input String callStr "String to call: sh -c $callStr";
input String filename := "" "The output is redirected to this file (unless already done by callStr)";
output Integer retval "Return value of the system call; usually 0 on success";
external "builtin" annotation(__OpenModelica_Impure=true);
annotation(preferredView="text");
Expand Down
28 changes: 14 additions & 14 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -1435,10 +1435,10 @@ algorithm
// we should really have different log files for simulation/compilation!
// as the buildModel log file will be deleted here and that gives less information to the user!
0 = Debug.bcallret1(System.regularFileExists(logFile),System.removeFile,logFile,0);
sim_call = stringAppendList({cit,exeDir,executableSuffixedExe,cit," ",simflags2," > ",logFile," 2>&1"});
sim_call = stringAppendList({cit,exeDir,executableSuffixedExe,cit," ",simflags2});
System.realtimeTick(GlobalScript.RT_CLOCK_SIMULATE_SIMULATION);
SimulationResults.close() "Windows cannot handle reading and writing to the same file from different processes like any real OS :(";
resI = System.systemCall(sim_call);
resI = System.systemCall(sim_call,logFile);
timeSimulation = System.realtimeTock(GlobalScript.RT_CLOCK_SIMULATE_SIMULATION);
timeTotal = System.realtimeTock(GlobalScript.RT_CLOCK_SIMULATE_TOTAL);
(cache,simValue,newst) = createSimulationResultFromcallModelExecutable(resI,timeTotal,timeSimulation,resultValues,cache,className,vals,st,result_file,logFile);
Expand Down Expand Up @@ -1525,10 +1525,10 @@ algorithm
logFile = stringAppend(executable1,".log");
0 = Debug.bcallret1(System.regularFileExists(logFile),System.removeFile,logFile,0);
strlinearizeTime = realString(linearizeTime);
sim_call = stringAppendList({cit,compileDir,executableSuffixedExe,cit," ","-l=",strlinearizeTime," ",simflags," > ",logFile," 2>&1"});
sim_call = stringAppendList({cit,compileDir,executableSuffixedExe,cit," ","-l=",strlinearizeTime," ",simflags});
System.realtimeTick(GlobalScript.RT_CLOCK_SIMULATE_SIMULATION);
SimulationResults.close() "Windows cannot handle reading and writing to the same file from different processes like any real OS :(";
0 = System.systemCall(sim_call);
0 = System.systemCall(sim_call,logFile);

result_file = stringAppendList(List.consOnTrue(not Config.getRunningTestsuite(),compileDir,{executable,"_res.",outputFormat_str}));
timeSimulation = System.realtimeTock(GlobalScript.RT_CLOCK_SIMULATE_SIMULATION);
Expand Down Expand Up @@ -1588,7 +1588,7 @@ algorithm
workdir = Util.if_(System.directoryExists(workdir), workdir, System.pwd());
// create the list of arguments for fmigenerator
call = str +& " " +& "--fmufile=\"" +& filename +& "\" --outputdir=\"" +& workdir +& "\"";
0 = System.systemCall(call);
0 = System.systemCall(call,"");
then
(cache,Values.BOOL(true),st);

Expand Down Expand Up @@ -1789,9 +1789,9 @@ algorithm
then
(cache,Values.STRING(str_1),st);

case (cache,env,"system",{Values.STRING(str)},st,_)
case (cache,env,"system",{Values.STRING(str),Values.STRING(filename)},st,_)
equation
resI = System.systemCall(str);
resI = System.systemCall(str,filename);
then
(cache,Values.INTEGER(resI),st);

Expand Down Expand Up @@ -2405,17 +2405,17 @@ algorithm
touch_file = "omc.checksettings.create_file_test";
usercflags = Util.makeValueOrDefault(System.readEnv,"MODELICAUSERCFLAGS","");
workdir = System.pwd();
touch_res = 0 == System.systemCall("touch " +& touch_file);
uname_res = 0 == System.systemCall("uname -a > " +& touch_file);
touch_res = 0 == System.systemCall("touch " +& touch_file, "");
uname_res = 0 == System.systemCall("uname -a", touch_file);
uname = System.readFile(touch_file);
rm_res = 0 == System.systemCall("rm " +& touch_file);
rm_res = 0 == System.systemCall("rm " +& touch_file, "");
platform = System.platform();
senddata = System.getRTLibs();
gcc = System.getCCompiler();
have_corba = Corba.haveCorba();
gcc_res = 0 == System.systemCall(gcc +& " -v > " +& touch_file +& " 2>&1");
gcc_res = 0 == System.systemCall(gcc +& " -v", touch_file);
gccVersion = System.readFile(touch_file);
_ = System.systemCall("rm " +& touch_file);
_ = System.systemCall("rm " +& touch_file, "");
confcmd = System.configureCommandLine();
vals = {Values.STRING(omhome),
Values.STRING(omlib),
Expand Down Expand Up @@ -4055,7 +4055,7 @@ algorithm
make = System.getMakeCommand();
numParallel = Util.if_(Config.getRunningTestsuite(), 1, Config.noProc());
numParallelStr = intString(numParallel);
make_call = stringAppendList({make," -j",numParallelStr," -f ",fileprefix,".makefile > ",fileprefix,".log 2>&1"});
make_call = stringAppendList({make," -j",numParallelStr," -f ",fileprefix,".makefile"});
s_call = Util.if_(isWindows, win_call, make_call);
Debug.fprintln(Flags.DYN_LOAD, "compileModel: running " +& s_call);

Expand All @@ -4068,7 +4068,7 @@ algorithm
0 = Debug.bcallret1(System.regularFileExists(fileLOG),System.removeFile,fileLOG,0);

// call the system command to compile the model!
0 = System.systemCall(s_call);
0 = System.systemCall(s_call,Util.if_(isWindows,"",fileLOG));
Debug.bcall2(Config.getRunningTestsuite(), System.appendFile, Config.getRunningTestsuiteFile(),
fileEXE +& "\n" +& fileDLL +& "\n" +& fileLOG +& "\n" +& fileprefix +& ".o\n" +& fileprefix +& ".libs\n" +&
fileprefix +& "_records.o\n" +& fileprefix +& "_res.mat\n");
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenC.tpl
Expand Up @@ -542,7 +542,7 @@ template simulationFile(SimCode simCode, String guid)
extern const char* <%symbolName(modelNamePrefixStr,"linear_model_frame")%>(void);
struct OpenModelicaGeneratedFunctionCallbacks <%symbolName(modelNamePrefixStr,"callback")%> = {
<%symbolName(modelNamePrefixStr,"performSimulation")%>,
(int (*)(DATA *, void *)) <%symbolName(modelNamePrefixStr,"performSimulation")%>,
<%symbolName(modelNamePrefixStr,"callExternalObjectConstructors")%>,
<%symbolName(modelNamePrefixStr,"callExternalObjectDestructors")%>,
<%symbolName(modelNamePrefixStr,"initialNonLinearSystem")%>,
Expand Down
5 changes: 3 additions & 2 deletions Compiler/Util/System.mo
Expand Up @@ -301,9 +301,10 @@ public function readFile
end readFile;

public function systemCall
input String inString;
input String command;
input String outFile := "" "empty file means no redirection unless it is part of the command";
output Integer outInteger;
external "C" outInteger=SystemImpl__systemCall(inString) annotation(Library = "omcruntime");
external "C" outInteger=SystemImpl__systemCall(command,outFile) annotation(Library = "omcruntime");
end systemCall;

public function systemCallParallel
Expand Down
23 changes: 0 additions & 23 deletions Compiler/Util/Util.mo
Expand Up @@ -2487,29 +2487,6 @@ algorithm
end matchcontinue;
end writeFileOrErrorMsg;

public function systemCallWithErrorMsg "
This function executes a command with System.systemCall
if System.systemCall does not return 0 then the msg
is outputed to errorBuf and the function fails."
input String inString1;
input String inString2;
algorithm
_:=
matchcontinue (inString1,inString2)
local String s_call,e_msg;
case (s_call,_) /* command errorMsg to errorBuf if fail */
equation
0 = System.systemCall(s_call);
then
();
case (_,e_msg)
equation
Print.printErrorBuf(e_msg);
then
fail();
end matchcontinue;
end systemCallWithErrorMsg;

public function strncmp "Compare two strings up to the nth character
Returns true if they are equal."
input String inString1;
Expand Down
3 changes: 2 additions & 1 deletion Compiler/runtime/System_rml.c
Expand Up @@ -792,7 +792,8 @@ RML_END_LABEL
RML_BEGIN_LABEL(System__systemCall)
{
const char* str = RML_STRINGDATA(rmlA0);
rmlA0 = (void*) mk_icon(SystemImpl__systemCall(str));
const char* outFile = RML_STRINGDATA(rmlA1);
rmlA0 = (void*) mk_icon(SystemImpl__systemCall(str,outFile));
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL
Expand Down
33 changes: 28 additions & 5 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -82,6 +82,7 @@ typedef void* iconv_t;
#define getFunctionPointerFromDLL dlsym
#define FreeLibraryFromHandle dlclose
#define GetLastError(X) 1L
#include <fcntl.h>
#endif

/*
Expand Down Expand Up @@ -514,7 +515,7 @@ const char* SystemImpl__basename(const char *str)
return res;
}

int SystemImpl__systemCall(const char* str)
int SystemImpl__systemCall(const char* str, const char* outFile)
{
int status = -1,ret_val = -1;
const int debug = 0;
Expand All @@ -524,11 +525,33 @@ int SystemImpl__systemCall(const char* str)

fflush(NULL); /* flush output so the testsuite is deterministic */
#if defined(__MINGW32__) || defined(_MSC_VER)
status = system(str);
if (*outFile) {
const char *command = malloc(strlen(str) + strlen(outFile) + 9):
sprintf(command, "%s > %s 2>&1");
status = system(command);
free(command);
} else {
status = system(command);
}
#else
pid_t pID = vfork();
if (pID == 0) { // child
execl("/bin/sh", "/bin/sh", "-c", str, NULL);
if (*outFile) {
/* redirect stdout, stderr in the fork'ed process */
int fd = open(outFile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
dup2(fd, 1);
dup2(fd, 2);
#if defined(__APPLE_CC__)
/* OSX likes to not redirect the Segmentation Fault: messages unless the command is in a subshell */
char command[strlen(str)+3];
sprintf(command, "(%s)", str);
execl("/bin/sh", "/bin/sh", "-c", command, NULL);
#else
execl("/bin/sh", "/bin/sh", "-c", str, NULL);
#endif
} else {
execl("/bin/sh", "/bin/sh", "-c", str, NULL);
}
if (debug) {
fprintf(stderr, "System.systemCall: execl failed %s\n", strerror(errno));
fflush(NULL);
Expand Down Expand Up @@ -612,7 +635,7 @@ static void* systemCallWorkerThread(void *argVoid)
*arg->current+=1;
pthread_mutex_unlock(arg->mutex);
if (i >= arg->size) break;
arg->results[i] = SystemImpl__systemCall(arg->calls[i]);
arg->results[i] = SystemImpl__systemCall(arg->calls[i],"");
};
return NULL;
}
Expand Down Expand Up @@ -642,7 +665,7 @@ void* SystemImpl__systemCallParallel(void *lst, int numThreads)
tmp = RML_CDR(tmp);
}
if (sz == 1) {
results[i] = SystemImpl__systemCall(calls[0]);
results[i] = SystemImpl__systemCall(calls[0],"");
} else {
int index = 0;
pthread_mutex_t mutex;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/runtime/systemimpl.h
Expand Up @@ -86,7 +86,7 @@ extern char* SystemImpl__pwd(void);
extern int SystemImpl__regularFileExists(const char* str);
extern int SystemImpl__removeFile(const char* filename);
extern const char* SystemImpl__basename(const char *str);
extern int SystemImpl__systemCall(const char* str);
extern int SystemImpl__systemCall(const char* str, const char* outFile);
extern void* SystemImpl__systemCallParallel(void *lst, int numThreads);
extern int SystemImpl__spawnCall(const char* path, const char* str);
extern double SystemImpl__time(void);
Expand Down

0 comments on commit d00cafa

Please sign in to comment.