If you want to really register all records and the completion of the process, you need to connect to the kernel. This means modifying the kernel, or at least writing a kernel module. Linux Security Modules will certainly allow you to connect to an input, but I'm not sure if you can connect to an output.
If you can live with a random pass back (if the binary is linked statically or somehow avoids setting up your environment), there is a simple option, preloading the library.
The Linux dynamic linker has a function that, if the environment variable LD_PRELOAD (see this question) names a shared library, it will force the library to be loaded into the initial process. In this way, you can create a library in which static initialization will tell the daemon that the process has started, and have the process detect when the process terminates.
Static initialization is easiest to do by creating a global object with a constructor in C ++. The dynamic linker ensures that the static constructor is launched when the library loads.
It will also try to start the corresponding destructor when the process ends, so you can simply register this process in the constructor and destructor. But this will not work if the process freezes from signal 9 (KILL), and I'm not sure what other signals will do.
So, instead, you should have a daemon and in the constructor tell the daemon about the start of the process and make sure that it will notice when the process exits on its own. One option that comes to mind is opening the unix domain daemon to the daemon and leaving it open. The core will close it when the process dies and the demon will notice. You must take some precautions to use the high descriptor number for the socket, as some processes may assume that the low descriptor numbers (3, 4, 5) are free and dup2 for them. And don't forget to allow more filedescriptors for the daemon and for the system as a whole.
Note that simply by polling the / proc file system you will probably miss a large number of processes that only live for a second. There are really a lot of them on unix.
source share