Executing external program via system() does not run properly - c++

I try to call a program (ncbi blast, for those who need to know) from my code, via calling the command in a system() call.
If I execute the string directly in the shell, it works as intended, but if I try the same string via system(), the program returns much faster, without the intended results. The output file is created, but the file size is 0. The returned error code is also 0. I even tried appending "> output.log 2> error.log" but these files are not created.
I guess it has something to do with environment variables or the path...
The output file name is given via -o command line parameter, not output redirection.
I read something about the popen command being possibly better suited for my use-case, but I can not find it, which library is that from?

The most usual cause of such problems is incorrect environment variable setting in ones ~/.bashrc.
You should be able to see what ncbi is unhappy about by executing
$SHELL -c '<exact string you pass to system()>'
Another common way to debug this is with strace. Execute:
strace -fo /tmp/strace.out ./myProgram
and look in /tmp/strace.out for clues.

Is there any reason that you do not want to fork and exec? This is a common idiom for executing one process from another.

popen is in the standard C library
See the man page

Some quick questions:
How do you execute in in the shell?
How do you execute in the system command?
What is the value returned by system?
Exact copy and paste from you terminal is preferable then an English description.

Related

system() executes shell command differently C++

I need to run this shell command in a C++ script:
"/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so –w /usr/local/www" -b"
This command launches an application which broadcasts a video feed. When I execute this command via system() in C++ the application doesn't start properly.
I use:
system("/usr/local/bin/mjpg_streamer -i \"/usr/local/lib/input_uvc.so\" -o \"/usr/local/lib/output_http.so –w /usr/local/www\" -b");
When I try to access the video stream after I started it with the C++ application the webpage returns:
501: Not Implemented!
no www-folder configured
I can't expect you guys to give me an application related solution, but I'm wondering if there's a difference in the way commands from a C++ application using system() and commands directly entered in a terminal are executed.
EDIT: The application broadcasts the video stream on IP:8080. I access it by going to that IP in my browser. Usually it opens a webpage with the stream in it but when I execute the command with the C++ application I get that error.
Edit: The old idea of mis-placed quotes was wrong; I realize that -w is actually an option to output_http.so, so the whole shebang must be passed as a single parameter to the -o option, as shown here or here etc.
In that case, check file permissions etc. Does /usr/local/www exist? Is it possible that you are running the shell command from a root shell?
Hey, I have a book recommendation, too, "one of the best tech books ever published": Stevens' Advanced Programming in the Unix Environment. The guy knows -- sorry: knew -- what he was talking about.
I would avoid using the system(3) library function, or at the very least, check its returning error code. I don't understand why you are using " inside your command (I believe that in your particular case, you don't need them; but in general beware of code injection!). Read about globbing
You could use popen(3) to at least get the output of the command.
Even better, code yourself the running of the mjpg_streamer program using the fork(2) & execve(2) & waitpid(2) and other syscalls(2) (perhaps pipe(2), poll(2), dup2(2) etc...). Read Advanced Lnux Programming for more.

How to generate and compile C++ code while the program is running?

So basically what I need isn't a specific code (of course that would be great), but just an idea and methods on how to achieve my goal.
1) I have to create a program in C++ , which generates a little example of C++ code, that is each time a bit different. (This causes no problems for me, I will use a template and randomize some variables in the code, which will make it unique every time.)
2) I will display the generated code and the user will have to type in, what he thinks the code prints out.
And here is where the problems start:
3) I have to take the generated code and compile it somehow to get a string with the text that the program would have printed out.
4) And then compare the string with what the user has typed in.
So the step 3) is where I stop and can't figure it out without help... I was thinking to write the generated code in a function of a .cpp file and then call that function, but I couldn't get it to work, so I started to think, I should ask an expert, maybe there are some other methods or ideas how to achieve this.
Write the .cpp file
invoke the compiler (or make or equivalent build system)
if compilation fails, presumably display the errors?
if compilation succeeds, run the resulting program and display the output
It's probably simplest to wrap 2/3/4 into a script, and invoke that with system or popen. The script can make sure the filenames are unique, fold stderr into stdout, etc. etc.
Your running program isn't really interacting with the compiled code, just reading the output, so keeping it as a seperate process is probably easiest. The script can add some markup to help you distinguish compiler output/errors from the program output.
I haven't written a batch file for years, but once you know how to run your compiler from the command line (ref), you can write a script to:
compile a .cpp file
execute the resulting .exe
redirect its output to a file
then, in C++ you just need to save the code to the .cpp file the script expects, execute the script like system("myScript.bat"), and then read the output file.
If you don't want to write a seperate batch script, you can just call system once to invoke the compiler, and again to execute the resulting .exe.
I think you are looking for a way to script c++. Have a look at http://www.softintegration.com/ or this stackoverflow question Any tutorial for embedding Clang as script interpreter into C++ Code?.
I have not used this but it is free and also does not compile but interpret. Very suitable for your problem, worth a shot...
http://root.cern.ch/drupal/content/cint
why dont you compile the generated code standalone using system() call?
system("g++ temp.cpp -o temp.exe); --something of this sort and then based on the return value
you can run temp.exe again like system("temp.exe"); Ofcourse you can print the output from temp.exe to a file and read that file to get the output of temp.exe to your current program..
Would something like Geordi work?
You can invoke the c++ compiler just like you'd invoke any external tool. E.g. system("g++ test.cpp") or use popen or whatever else is offered by your platform.
You could also look into integrating a library that implements a compiler into your program. For that you might want to look into clang and llvm.

Hooking output from the console

I was wondering if there is any way to read the output of a console command, from executing it in code. OK that's probably not the clearest way I could have put that, so let's have an example:
My project PingSweepr is, as the name implies, a simple network ping sweeper that uses the C++ system() command to automate ping sweeping with the Windows shell ping command.
The only problem is, there is no way to sort the results (btw, this would be used in more than just that program, in case you were wondering), which would involve parsing the command-line output of the ping program. So basically my question is: is there any way to read the output from the shell into the program? Maybe through a system message hook or something, or is it just not possible?
Thanks!
Have you tried looking at the popen function? This older question has some discussion:
Capturing stdout from a system() command optimally
Here is the answer: How to execute a command and get output of command within C++ using POSIX?

Can I globally redirect the output of system() calls?

I have a program that executes various shell commands via system() and occasionally prints to cout. I want to redirect all output coming from system() calls to a log file so they don't clutter up the normal output. Can I do this without having to append > log to all my system commands?
Looks like you can use popen
Close the stdio file descriptors (0, 1, and 2) and re-open them on whatever output device you like.
Using system is just a bad idea, period. If you use fork and execve or posix_spawn, you can easily make the necessary redirections and avoid all sorts of vulnerabilities from shell quoting issues.
If you can use a library that wrap process call. It is hard to code from posix. I use boost.process, it works fine. you can simply tell the lib how you want the output to be redirected...
my2c

How to find out where all a closed source application is writing to?

I have an application (the source for which I don't have), which can be invoked from command line like this
$ ./notmyapp
I want to know all the locations where the application is writing to. It outputs some files in the directory it is being called from, but I need to make sure that those are the only files that are created.
So, I need to isolate the application to find out which all files it created/edited while it was running.
How can I do this?
Some way using Perl or C or C++? Do any of the standard libraries in these languages have ways to do this?
strace, ktrace/kdump, truss, dtruss, or whatever other program your platform provides for tracing system calls is probably what you're looking for.
Expect lots of output from any of those. To figure out what files the application is reading and writing to, you might want to limit the output to just a few syscalls. strace -eopen ./notmyapp, for example.
The application might also fork off child processes to do some of its work. With most system call tracers, you'll have to be specific about tracing those child processes as well. With strace, that'd be strace -f ./notmyapp.
In unix systems, you can use strace to print out a trace of all the system calls made and signals received by a process:
$ strace ./notmyapp
grep can be used to limit the output to subset of system calls:
$ strace ./notmyapp 2>&1 | egrep '(open|write)'
you could use strace.
You say in response to rafl's answer that notmyapp is supposed to produce a prompt and wait for [...] inputs before doing something.
Put your inputs in advance into a plain text file (say, responses.txt), one input per line. Then use strace, as suggested to track calls to open() or write() piping in the contents of responses.txt:
$ strace -eopen -ewrite ./notmyapp < responses.txt
If you're expecting a lot of file access, then you may want to pipe the output to your favourite pager or editor:
$ strace -eopen -ewrite ./notmyapp < responses.txt | vim -R -
strace is a powerful tool. For more information, consult man strace.
You could try running it as a user which has no rights to write anywhere on any drive. Then you get an error message when it tries to create/write the first file. Log that directory/file and give write rights to it, then repeat until there are no more error messages.