Allowing CTRL + c functionality in C++ wrapper program [duplicate] - c++

This question already has answers here:
How can I catch a ctrl-c event?
(4 answers)
Catch Ctrl-C in C
(9 answers)
Closed last month.
I am writing a wrapper program in C++ for Linux, which calls several other binaries in sequence by use of the system() function.
While testing, I noticed that if I enter Ctrl + C in the terminal during execution of one of the child processes invoked by system(), only that child process gets halted. In other words, that subprocess stops, but the wrapper program is unaffected, and continues the subsequent binary calls.
This results in me having to continue inputting Ctrl + C on all children until the C++ binary runs to completion. I would like to allow my user to halt the entire wrapper program with Ctrl + C.
After researching other similar questions, it seems clear that one must leverage process groups on Linux to properly distribute the halt signal. However, I am unsure of how to relay a stop signal from a child process called by system() to the parent wrapper.

Related

how to kill a program using c++ when that same program is opened twice?

say, I have a C++ program, Named "A.exe"
If someone opened it twice,
The program should kill it and then run as One single program.
RULE:It should be killed by that program (NOT by using any other program).
My problem is
if I use
system("taskkill /im A.exe /t");
but It kills itself too.
Use a named mutex, this can only be held by one thread at at time, create it at the beginning of your program, if you fail to acquire the mutex, it is because there is another copy of your program already running (and owning the mutex), you can then safely terminate the program (without having to call any external programs, relying looking for particular file names etc)

Limit execution time of system() call in C++ [duplicate]

I am using a system() to call an executable program(A server) . Now after a certain time I want to terminate this program from my c program itself. Does anyone know how to do this?
OS running:(http://rcn-ee.net/deb/rootfs/precise/ubuntu-12.04-r4-minimal-armhf-2012-07-16.tar.xz)
The best way to do this is to use a function that gives you more control over the resulting process than system() does. However, this would be platform specific.
For Windows, use CreateProcess() which returns a HANDLE which you can use later in TerminateProcess() to kill the process.
For Unix, use fork() and exec() which gives you the pid of the child process, which you can use later in kill() to kill the process.

How to run a c++ executable file apart from my main execution [duplicate]

This question already has answers here:
How do I open an .exe from another C++ .exe?
(7 answers)
How do I execute an external program within C code in Linux with arguments?
(7 answers)
Closed 6 years ago.
I've got a compiled executable file called "starTracking", and I need to use another c++ program to run this file.
Right now I'm using:
system("./starTracking");
But I can't wait it to finish before calling it again, so I'd like it to run apart from my main program so I can call it several times without making my main program wait.
Could someone tell me if there's a way to do it?
I'd be so grateful :D
I need to use another c++ program to run this file
If running your C++ on Windows read up on this guide Creating Processes.
I can't wait it to finish before calling it again, so I'd like it to
run apart from my main program so I can call it several times without
making my main program wait.
That's what running a process will do. To send/receive data to and from the external program make sure you involve stdin and stdout. See this section of the guide: Creating a Child Process with Redirected Input and Output.

Catch process kills in c++ under Unix

Is it possible with a C++ program to monitor which processes gets killed (either by the user or by the OS), or if the process terminates for some other reasons which are not segmentation fault or illegal operations, and perform some actions afterwards?
Short answer, yes it's possible.
Long answer:
You will need to implement signal handlers for the different signals that may kill a process. You can't necessarily catch EVERY type of signal (in particular, SIGKILL is not possible to catch since that would potentially make a process unkillable).
Use the sigaction function call to set up your signal handlers.
There is a decent list of which signals do what here (about 1/3 down from the top):
http://pubs.opengroup.org/onlinepubs/7908799/xsh/signal.h.html
Edit: Sorry, thought you meant within the process, not from outside of the process. If you "own" the process, you can use ptrace and it's PTRACE_GETSIGINFO to get what the signal was.
To generally "find processes killed" would be quite difficult - or at least to tell the difference between processes just exiting on their own, as opposed to those that exit because they are killed for some other reason.

PTRACE_SYSCALL and orig_eax

I want to kill a child process if it does other system calls than read and write (and even filter these calls as well, but it's a different story) but there some system calls done by default.
I have compiled an empty test child (exits instantly) program and I also have a parent process which forks, enables ptracing and executes the child program. Parent process uses PTRACE_SYSCALL and checks orig_eax every time. My test program reports that the child was stopped 49 times (which, I assume, means 48 / 2 + 1 system calls).
I wanted to know whether the system calls sequence is always the same (initialization) and/or it's possible to know when I can start and when to stop kill-on-syscall in my parent?
I had a similar problem once (see my question on the topic). When a program starts, it executes a lot of system calls when initializing the application (such as loading shared libraries) before calling main(). What I did is to simply allow somewhat more system calls and use another means of security (such as chroot) to prevent the application from accessing undesired files.
A better option would be to somehow find the entry point of the main() function of the program (see this tutorial for writing debugging code) and disable system calls after that point. I don't know if it's possible to do in general case, but that's the way I would start to search.
After finding the entry point, there is another way of restricting the program from making certain system calls. Instead of using PTRACE_SYSCALL to check each system call done by the program, inject a prctl(PR_SET_SECCOMP, ...) call to the program (using ptrace()) then just leave the program running.