3

I want to write a c++ program for linux which monitors all processes running and write to a log file when any of those processes crashes due to sigsegv.

Is it possible to do this and if so what should I learn in order to implement it in c++?

chamal
  • 1,103
  • 1
  • 8
  • 6
  • Do you really want to write a script for this with so many free tools available? Try [SeaLion](http://sealion.com). It's pretty lightweight. – Kevin Jun 19 '14 at 10:49

3 Answers3

4

Trying to monitor all processes on the system would be onerous. If you are interested in SIGSEGV specifically, you might want to consider installing yourself as core dump handler instead. It will not catch processes that have asked to have core dumps disabled (ulimit -c 0), but you will get all others.

echo "|usr/local/sbin/crashcollector" >/proc/sys/kernel/core_pattern

Now /usr/local/sbin/crashcollector will be called with the core dump on its standard input every time a process crashes. This program can do whatever it wants, such as save the core dump and/or notify something else.

Celada
  • 21,627
  • 4
  • 64
  • 78
4

I expect you are going to catch all processes crash event. Using ptrace is an approach, but it is very complex, you need to trace all processes and attach to new processes created later, also you'll hit performance penalty.

You can catch all processes crash event by hook coredump:

echo "|yourcoredumphook" > /proc/sys/kernel/core_pattern

this will enable coredump hook, when a process terminated, yourcoredumphook will be started as root with coredump sent through stdin, so you can figure out which process terminated by analysis the coredump

Zang MingJie
  • 5,164
  • 1
  • 14
  • 27
0

You probably want to use ptrace for this. Take a look at this question: how to intercept linux signals ? (in C)

I imagine doing this for all processes would require a re-implementation of init, or perhaps a system that monitors the sys directory to call ptrace for each process.

Community
  • 1
  • 1
Woodrow Douglass
  • 2,605
  • 3
  • 25
  • 41