system() executes shell command differently C++ - 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.

Related

Access data from terminal

I have to write a program that intercepts data from terminal and i have to parse it. After processing when the data, i have to parse it before it goes to stdout.
I can't use tee or commands like prog > file 2>&1 as the program is going to be interactive.
For example :
If the user types ls in the terminal i have to parse it then it should go operating system and then when I get the result after processing I ll have to again parse it before it's displayed in the terminal.
I did my research and I think I can achieve it through pseudo terminal interfaces ( pty ).
Please let me know if there is a better way to achieve it.
I am using cpp and bash and the platform is *nix.
Update:
I can also use libexpect from expect.
I am not sure what do you mean here - you mean interactive program as "working in another terminal communicating with user" or even displaying GUI?
How does it specify the terminal? It is probably important what is program layout here (which program starts which).
If your application uses GUI to communicate with user, then I would simply do it this way:
start bash with sdtin and stdout attached to pipes,
your program reads & writes to it's end's of those pipes, parses data, and reads/writes on it's own stdin&stdout - so it appears on it's terminal.
If you mean controlling different terminal than your application's, it gets though since system generally does not expect program operating on multiple terminals. I don't think it's possible to filter communication between terminal and already working application attached to it. Starting another process spawning another terminal might be an option - to have basically two terminals working in sync. But then you will have to synchronize both processes by some other means (named pipes, network connection or some other IPC).
If you provide more detail on your program I might provide more directed help.
PS Don't tell me that you are writing some terminal keylogger ')
EDIT:
Your program is probably GUI based then - what i would recommend would be something similar to answer linked by banuj.
Best option will probably be to create three pipes, then fork, and in child process assign corresponding ends of pipes to stdin, stdout and stderr. Then child process should exec into shell - probably bash, although I am not sure if other shells would sound better if read out loud ;) Main process will be able to read/write other ends of mentioned pipes, parsing both inputs and outputs to bash and programs it runs.
You could also exec directly to commands user specifies, but that forces you to take over tedious job of a shell - managing current directory, environment variables, job control and so on.
Using above method might however cause some trouble - some programs (usually in security related contexts - eg. su(do) asking for password) will try to bypass stdin/stdout anyway and read directly from terminal device. I am not sure what can you do in such case - programing your own terminal emulator would be an option, but I don't know if you want to go this deep into system programming for this.
If you want some code snippet's, if you don't know how to do above, just ask ;)

Launch new program using exec in new terminal

I've got a program called pgm1 which create a new process using fork.
Then in this process, I launch a new program (pgm2) using the following command:
execv( exec_path_name, argv ).
But the thing is that with this method I've got both output in the same terminal.
I've been searching for a while ans the only solution i found was this one:
Open a new terminal with a system call
Attach my pgm2 to the new terminal using this soft http://blog.nelhage.com/2011/01/reptyr-attach-a-running-process-to-a-new-terminal/comment-page-1/#comment-27264
So my question is really simple, is there a more simple way to do that ?
Thanks in advance !
PS: Distro - Ubuntu 11.10 32bit
I can think of two possible solutions:
Do The Right Thing(TM) and send your output to a file: Each process can use a different file, providing both clear separation of the output and better record-keeping. As a bonus, you are also bound to see a performance improvement - terminal output is computationally expensive, even nowadays...
Execute a terminal emulator with the proper arguments: Most terminal emulators provide a way to execute a specific program in place of the shell. For example xterm:
$ xterm top
This will launch top in an xterm instance, without a shell. Quiting top also terminates the xterm window.
If your terminal emulator of choice supports this, you can use it simply by modifying the arguments passed to execv(). Of course, in this case you will be actually executing the terminal emulator instead of your program, which will then call your own process.
Keep in mind that, depending on the terminal emulator, any open file descriptors may not be passed correctly to your program - the terminal will at least mangle the standard file descriptors.

Standard input/output in Pyclewn(GDB front end for vim)

I've just installed Pyclewn. It works and shows variables and etc. But it doesn't show my program's output and when my program wants to input something, it doesn't do anything(I can write ":C run output" and it works. but not with standard I/O.
There was something in its documentation: http://pyclewn.sourceforge.net/_static/pyclewn.html
But I didn't understand what it says.
P.S: I've done that. Now I want to map for example to run those commands. but because the "nn" in /dev/pts/nn may vary, I should manually enter the number(see it from the xterm opened). I also have another problem when I map a key to a sequence of gdb commands, it says gdb is busy, I can add ":sleep 100m" between commands and the problem will be soved. but in the documentation it says that I should enable async option. but when I run pyclewn from vim with :Pyclewn command I don't know how to enable the async option.
You should use inferior_tty.py to create a terminal to be used with the program being debugged.
Abridged summary (most relevant bits only) from the FAQ:
:Cshell setsid xterm -e inferior_tty.py &
Determine what the name of the tty to be used is from this newly spawned window, then:
:Cset inferior-tty /dev/pts/nn
Or just start pyclewn from a terminal and it will automatically grab that terminal for input and output.
E.g:
pyclewn -c "main.cc other.h other.cc"

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?

Bash: Execute script on file save?

I'd like to use Bash to run a test suite automatically when I save any file in a given directory.
Is there a mechanism for bash to execute a given script on save events?
Thanks.
::EDIT::
I should have mentioned that I'm on using OSX.
Edited: you (the OP) mentioned you use OSX. I'm not aware of any similar tools on OSX. There is a low-level system call (inherited from BSD) called "kqueue", but you'd have to implement your own user-level tool. There is a sample application from Apple, called "Watcher", but it's proof of concept only, and doesn't do what you want.
There is another thread about this on Stack Overflow (also inconclusive).
For lack of an appropriate tool, if you're using a specific programming language, I'd advise you to look for solutions already written for it. Otherwise, I think you're stuck to polling and managing the changes yourself...
Here's my original, Linux-based answer, for archival purposes:
If you're using Linux, you might want to take a look at inotify . More specifically, you can install inotify-tools, which include inotifywait.
With it, you can monitor files and directories for a number of events, such as access, modification, opening, closing and many others. inotifywait can exit once the specified event has been detected, and so a simple loop would get you what you want:
while :; do
inotifywait -e modify /some/directory
run_test_suite
done
By the way, many programming languages and environments already have their own continuous test runners (for instance, with Python you could use tdaemon, among others).
You can use incron to detect when a file has been closed.
Use dnotify only if inotify is not available in Your system (linux kernel < 2.6.13).
dnotify is standard linux method to watch directories:
http://en.wikipedia.org/wiki/Dnotify
Code example. Watch dir for changes:
dnotify WATCH_DIR -M -e SCRIPT_TO_EXECUTE
Please note SCRIPT_TO_EXECUTE will be executed every time, when any file in WATCH_DIR changes.
You can use inotify as explained here Inotify Example: Introduction to Inotify with a C Program Example