openstk
openstk

Reputation: 137

Catching system events in C Linux

I am writing an application on Linux which requires me to catch system events like:

  1. System reboot
  2. User 'xyz' logs in
  3. 'xyz' application crashes etc.

and need to execute some functionality based on that. For e.g.:

  1. Run backup script
  2. Run recovery program etc.

Can anyone please tell me how to catch system events in C/Linux ?

P.S: I am not talking about 'file system' events here :P

Upvotes: 3

Views: 2384

Answers (2)

kompas
kompas

Reputation: 425

I think you should consider reading systems logs - everything you ask about is logged to the syslog (for standard configuration). If your system uses syslog-ng, then you could even configure it to write directly to your program, see http://www.syslog.org/syslog-ng/v2/#id2536904 for details. But even with any other syslog daemon, you can always read file (or files) from /var/log just like tail -f does, end react on particular messages.

I'm not sure about catching application crashes - there's a kernel option to log every SIGSEGV in user processes, but AFAIK it is available only on ARM architecture - last resort would be to instrument your application (as Jan Hudec pointed out) to log something to syslog.

Upvotes: 4

Jan Hudec
Jan Hudec

Reputation: 76246

There is no concept of "system event". You need to specify which events you need to handle and implement appropriate mechanism for handling each:

  • System startup: The init process calls scripts from /etc/init.d during startup. The exact infrastructure differs slightly between distributions, but Linux Standards Base System Initialization should generally work on all.

  • User login/logout: The LSB also defines interface to the Pluggable Authentication Modules library. You can implement a shared library that will be called during login (and also other actions that require authentication and authorization). Depending on what you want to do there may already be a module that will work for you, so try looking for it first. In either case I don't think there is distribution-independent way of installing it and even on given distribution you have to consider that administrator might have made custom modification, so the installation will need manual intervention by the administrator.

  • Application crashes: You would have to instrument it.

Upvotes: 4

Related Questions