how to make execv reattach to a running process - c

I created a C program that is using the execv function to start a daemon process of another program and read its stdout.
I have achieved that by using the fork and setsid functions.
Everything works great. But if the program exit i want to be able to
reattach to a running process again and read the stdout.
I can store the pid of the process if that can be used.
Can someone please tell me if it even possible ? and how that can be done in C please.

Related

Know if process has been called by exec()

Is there any way to know if a process has started to run from a call of exec() or has started from the terminal by the user?
Helpful to you: child and parent process id;
getppid() returns the process ID of the parent of the calling
process. This will be either the ID of the process that created this
process using fork(), or, (!!!CARE!!!) if that process has already terminated, the
ID of the process to which this process has been reparented;
I would also consider adding additional program arg.
All programs are started by a call to exec family of functions.
When you type a command in the terminal, for example, it searches for the binary executable, forks and calls exec in the child process. This will substitute the binary image of the calling process (the terminal) for the binary image of the new program. The program will execute and the terminal process will wait.
There is this absolutely awesome answer by paxdiablo on the question Please explain exec() function and its family that will surely help you understand how exec works.
In Unix, all processes are created by using the fork system call, optionally followed by the exec system call, even those started by a user (they are fork/exec'd by the user's shell).
Depending on what you really want to do, the library function isatty() will tell you if stdin, stdout or stderr are file descriptors of a tty device. i.e. input comes from a terminal, output goes to a terminal or errors go to a terminal. However, a command like
myprog < somefile 1>someotherfile 2>errorfile
will fool code using isatty. But maybe that is what you want. If you want to take different actions based on whether there is a user typing input from a keyboard or input is coming from a file, isatty is what you need.

Writing a shell in C: How to get pid of running process

I'm trying to write a shell in C.
And now i want to make shell catch the signal of suspend, and suspend the running process, but i am not sure which function can return the pid of running process. Currently, i know proc/pid has status info of process, but I don't think scanning all processes to find running status is a good idea. So anyone can give me a hint?
Also I don't know if waitpid works for me, but from my understanding, waitpid is supposed to keep waiting until the child process stops, and this will never tell me if it's still running, am I right?

How to terminate a exe from a c program

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.

Killing a subprogram created by other program when its killed

I'm writting a C program that launches another program using the system() function. I'd want to know if there is a possible way to kill the program that is launched, if the main program is killed. I'm programming it for a Linux machine.
Example:
/* foo.c */
int main()
{
system("./blah");
return 0;
}
blah does whatever has to do. If I kill foo, blah is still running.
Is there any way to make foo to kill blah when it dies ?
You'll need to work with signal handling to know when someone/something is trying to kill your application, read the below documentation for further information.
linuxjournal.com - The Linux Signal Model
Besides that you'll need to know the process id of your spawned child process. For this I'd recommend to use something more sophisticated than system to fire up your launched process.
yolinux.com - Fork, Exec and Process control
You'll also have to know how to kill the spawned child (using it's pid).
pubs.opengroup.org - functions: kill

How can I send a signal to a popen'ed process in Perl?

I wrote a simple Perl script which will run in while loop and exit whenever any signal is send to this Perl script. I wrote a c program which creates a thread using the pthread_create() and in its start routine, it's using popen to execute that Perl script:
popen("/usr/bin/perl myprog.pl");
I am using the sigtrap in the Perl script to catch any signal it receives. Now I want to send signal (TERM) from my C program to this Perl process executed by the thread. How can I do that? Is there any way to send a signal to popen'ed processes. Please let me know if need more details.
Sending signals usually works using kill. In order to be able to kill you normally need the process id, PID, of the process you want to signal.
popen doesn't give you that. However, there's a couple of alternatives:
You could use a PID of 0, which would send your signal to every process in the process group. If you only have one parent process spawning one child process using popen, then using kill(0, your_signal) would be one way to go.
Another way to go would be to have the child process communicate its PID back to the parent process by, for example, just outputing that on a single line as the first thing after starting up.
In perl, that'd look like
print $$, "\n";
the process that did popen could then read that line from the filehandle it got, and extract a useful pid from that using strtol or atoi, and keep that around to use with kill later on, after having read the actual output of its child process.
If, for whatever reason, none of these approaches is viable for your problem, you probably want to stop using popen alltogether, and do most of what it does manually, most importantly the forking, as that's what'll give you the PID to use to later send signals.
popen() doesn't give you any way to access the PID of the child process, which you need in order to signal it.
You will need to do the gory work of popen() yourself (set up pipes, fork, exec, wait).

Resources