LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2018, 10:48 AM   #1
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Rep: Reputation: 17
Callbacks for operating system events


Does linux have the notion of registering callbacks when certain operating system events occur ?

As an example in my C++ code I would like to be able to register a function callback when some file change is made in a particular directory. Is such a thing possible in Linux ? Similarly with other actions which the operating system, aka kernel, might process, does Linux have the notion of registering a callback function which will be notified of such an action ?

It seems as if a program must track events in the operating system by actively checking for some change in a background thread rather than by being notified, through a function callback, when some change happens. But perhaps I have misunderstood how tracking operating system events works in Linux.
 
Old 08-04-2018, 12:56 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I think in general there is no such feature, but there are tools which can recognize specific type of events.
Probably you are looking for this: https://github.com/rvoicilas/inotify-tools/wiki
 
Old 08-04-2018, 01:28 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
There are some kind of callbacks, such as signal-handlers or /proc/sys/kernel/hotplug.
But in this case you need dnotify/inotify.
 
Old 08-05-2018, 12:32 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by eldiener View Post
As an example in my C++ code I would like to be able to register a function callback when some file change is made in a particular directory.
For that? Yes. It's called inotify.
 
Old 08-05-2018, 07:07 AM   #5
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by dugan View Post
For that? Yes. It's called inotify.
AFAICS inotify does not use function callbacks.
 
Old 08-05-2018, 08:45 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you can say inotifywait itself is a callback (or probably better to say it has a callback function which can be "registered" - when you start this app). But again, in general you cannot register callback functions to any (arbitrary) event.
From the other hand there is a way to use hooks to catch system calls, see LD_PRELOAD.
But would be nice to know what do you really need, what is your goal?
 
Old 08-05-2018, 01:42 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by eldiener View Post
AFAICS inotify does not use function callbacks.
Can you give us a better example of why you need to do this with function callbacks"?
 
Old 08-08-2018, 07:14 PM   #8
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by dugan View Post
Can you give us a better example of why you need to do this with function callbacks"?
I understand how inotify works but it is an active monitoring system where the programmer himself must create a background thread to catch certain events, either by reading, selecting, polling etc. I also understand it is a robust system. But I am surprised that no Linux programnmers have created a 3rd party library over inotify that allows C/C++ code to register callbacks when certain events occur so that not every programmer who wants to catch these events has to create their own background thread and monitoring system. In general I am always a little amazed that callbacks are not the popular way of designing event driven libraries in Linux.

I am sure every C/C++ programmer knows what a callback is. In C it is simply a function whose address can be passed to some other functionality so that if something happens the function is called, often with some parameters passed. In C++ it can be any callable, which extends the C function callback interface to also include function objects, class member functions and lambda functions. With Boost C++, of which I am a library developer and active participant, callbacks in libraries are very much used and callback interfaces are very popular. But even in C there is nothing wrong with simple function callbacks.

I understand I can design my own callback library over inotify and push it to a Github repository and other C/C++ programmers may find it popular and use it. But as I said I am still very surprised that even my mention of an event driven callback library draws responses asking me to explain callback interfaces as if in the year 2018 this is just an amazing new concept in the Linux programming world that no one has encountered before. Wow, where has everyone been for the last 30 years or so ?

Thanks ! I will work with inotify and just get used to the fact that event driven callback libraries, for the various OS events that can happen when an end-user uses Linux, are just not something that Linux programmers have ever considered important.
 
Old 08-08-2018, 07:23 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
I think your specific example might have matched inotify better than you intended.

For more general asynchrous event-handling, look into libuv or libevent.

Here are libuv's docs for file watching:

https://nikhilm.github.io/uvbook/fil...-change-events

Last edited by dugan; 08-08-2018 at 09:30 PM.
 
1 members found this post helpful.
Old 08-09-2018, 06:56 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by eldiener View Post
for the various OS events that can happen when an end-user uses Linux, are just not something that Linux programmers have ever considered important.
User-space callbacks using a library running in the same process is one thing, but running arbitrary functions in kernel-space is quite problematic, so the Linux kernel does not let userspace programs register callback functions (of course various libraries that run on Linux-based OS do use callbacks all the time, as shown in dugan's libuv example).
 
Old 08-09-2018, 07:47 AM   #11
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by ntubski View Post
User-space callbacks using a library running in the same process is one thing, but running arbitrary functions in kernel-space is quite problematic, so the Linux kernel does not let userspace programs register callback functions (of course various libraries that run on Linux-based OS do use callbacks all the time, as shown in dugan's libuv example).
I understand this. I do not expect the Linux kernel itself to provide user-space callbacks. I was really just surprised that so few libraries based on monitoring kernel events provide a user-space callback interface rather than an just another low-level interface for monitoring. It is much easier from the programmer's persepective to pass some function address to be called when an event occurs into an API than to have to run a loop to monitor one or more events in a background thread.

I will look into libuv.
 
Old 08-09-2018, 07:48 AM   #12
eldiener
Member
 
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by dugan View Post
I think your specific example might have matched inotify better than you intended.

For more general asynchrous event-handling, look into libuv or libevent.

Here are libuv's docs for file watching:

https://nikhilm.github.io/uvbook/fil...-change-events
Thanks ! I will look into those libraries.
 
Old 08-09-2018, 07:54 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by eldiener View Post
It is much easier from the programmer's persepective to pass some function address to be called when an event occurs into an API than to have to run a loop to monitor one or more events in a background thread.
It is problematic, as it was explained. The callback will run in kernel-space and not in user-space. Binding arbitrary functions into the kernel is unsafe, unreliable and not secure...
It is much easier from the programmer's perspective - to harm/damage or just slow down the system.
 
Old 08-09-2018, 06:07 PM   #14
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by eldiener View Post
I do not expect the Linux kernel itself to provide user-space callbacks.
Then you should have asked about GNU, not Linux!

Okay, above is not entirely serious, but I do read your original question as rather about the kernel specifically, which now seems to be somewhat misleading. Remember, programmers tend to be a bit... literal minded.
 
Old 08-09-2018, 06:31 PM   #15
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Quote:
Originally Posted by ntubski View Post
... programmers tend to be a bit... literal minded.
No, they are pedantic - no "a bit" about it. The kernel devs most of all. I was indeed drawn in by the thread (mis-)title.
 
  


Reply

Tags
event



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem operating command with DUAL operating system with ubuntu & windows 7 gne Linux - Newbie 2 02-01-2015 08:12 AM
LXer: The non-operating system operating system LXer Syndicated Linux News 0 06-26-2010 05:42 PM
'Operating system not found' Any operating system installed wont work. TechniSlave Linux - Newbie 55 02-09-2009 11:02 AM
Catching events from system? kalleanka Programming 4 03-02-2004 04:08 PM
Java - Interfaces, inner classes and callbacks for events gwp Programming 3 12-09-2003 04:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration