ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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?
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.
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).
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.