Monitor

Monitor — interface to monitor mount tables

Functions

Types and Values

Description

For example monitor VFS (/proc/self/mountinfo) for changes:

1
2
3
4
5
6
7
8
9
10
11
const char *filename;
struct libmount_monitor *mn = mnt_new_monitor();

mnt_monitor_enable_kernel(mn, TRUE));

printf("waiting for changes...\n");
while (mnt_monitor_wait(mn, -1) > 0) {
   while (mnt_monitor_next_change(mn, &filename, NULL) == 0)
      printf(" %s: change detected\n", filename);
}
mnt_unref_monitor(mn);

Functions

mnt_new_monitor ()

struct libmnt_monitor *
mnt_new_monitor (void);

The initial refcount is 1, and needs to be decremented to release the resources of the filesystem.

Returns

newly allocated struct libmnt_monitor.


mnt_ref_monitor ()

void
mnt_ref_monitor (struct libmnt_monitor *mn);

Increments reference counter.

Parameters

mn

monitor pointer

 

mnt_unref_monitor ()

void
mnt_unref_monitor (struct libmnt_monitor *mn);

Decrements the reference counter, on zero the mn is automatically deallocated.

Parameters

mn

monitor pointer

 

mnt_monitor_enable_userspace ()

int
mnt_monitor_enable_userspace (struct libmnt_monitor *mn,
                              int enable,
                              const char *filename);

Enables or disables userspace monitoring. If the userspace monitor does not exist and enable=1 then allocates new resources necessary for the monitor.

If the top-level monitor has been already created (by mnt_monitor_get_fd() or mnt_monitor_wait()) then it's updated according to enable .

The filename is used only the first time when you enable the monitor. It's impossible to have more than one userspace monitor. The recommended is to use NULL as filename.

The userspace monitor is unsupported for systems with classic regular /etc/mtab file.

Return: 0 on success and <0 on error

Parameters

mn

monitor

 

enable

0 or 1

 

filename

overwrites default

 

mnt_monitor_enable_kernel ()

int
mnt_monitor_enable_kernel (struct libmnt_monitor *mn,
                           int enable);

Enables or disables kernel VFS monitoring. If the monitor does not exist and enable=1 then allocates new resources necessary for the monitor.

If the top-level monitor has been already created (by mnt_monitor_get_fd() or mnt_monitor_wait()) then it's updated according to enable .

Return: 0 on success and <0 on error

Parameters

mn

monitor

 

enable

0 or 1

 

mnt_monitor_get_fd ()

int
mnt_monitor_get_fd (struct libmnt_monitor *mn);

The file descriptor is associated with all monitored files and it's usable for example for epoll. You have to call mnt_monitor_event_cleanup() or mnt_monitor_next_change() after each event.

Parameters

mn

monitor

 

Returns

>=0 (fd) on success, <0 on error


mnt_monitor_close_fd ()

int
mnt_monitor_close_fd (struct libmnt_monitor *mn);

Close monitor file descriptor. This is usually unnecessary, because mnt_unref_monitor() cleanups all.

The function is necessary only if you want to reset monitor setting. The next mnt_monitor_get_fd() or mnt_monitor_wait() will use newly initialized monitor. This restart is unnecessary for mnt_monitor_enable_*() functions.

Parameters

mn

monitor

 

Returns

0 on success, <0 on error.


mnt_monitor_next_change ()

int
mnt_monitor_next_change (struct libmnt_monitor *mn,
                         const char **filename,
                         int *type);

The function does not wait and it's designed to provide details about changes. It's always recommended to use this function to avoid false positives.

Parameters

mn

monitor

 

filename

returns changed file (optional argument)

 

type

returns MNT_MONITOR_TYPE_* (optional argument)

 

Returns

0 on success, 1 no change, <0 on error


mnt_monitor_event_cleanup ()

int
mnt_monitor_event_cleanup (struct libmnt_monitor *mn);

This function cleanups (drain) internal buffers. It's necessary to call this function after event if you do not call mnt_monitor_next_change().

Parameters

mn

monitor

 

Returns

0 on success, <0 on error


mnt_monitor_wait ()

int
mnt_monitor_wait (struct libmnt_monitor *mn,
                  int timeout);

Waits for the next change, after the event it's recommended to use mnt_monitor_next_change() to get more details about the change and to avoid false positive events.

Parameters

mn

monitor

 

timeout

number of milliseconds, -1 block indefinitely, 0 return immediately

 

Returns

1 success (something changed), 0 timeout, <0 error.

Types and Values

struct libmnt_monitor

struct libmnt_monitor;

Mount tables monitor