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_mountinfo(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_fanotify ()

int
mnt_monitor_enable_fanotify (struct libmnt_monitor *mn,
                             int enable,
                             int ns);

Enables or disables kernel VFS monitoring based on fanotify (since Linux 6.15). This monitor can return mount IDs of changed mount points. It's also possible to enable multiple monitors for different namespaces. If you do not need this advanced functionality, use the classic mnt_monitor_enable_mountinfo().

If the monitor does not exist and enable=1, it allocates new resources necessary for the monitor.

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

The function mnt_monitor_next_change() returns the namespace filename for this monitor (default is "/proc/self/ns/mnt").

The function mnt_monitor_event_next_fs() can return filesystems associated with the last event.

Return: 0 on success and <0 on error

Parameters

mn

monitor

 

enable

0 or 1

 

ns

namespace file descriptor (use -1 for default /proc/self/ns/mnt)

 

mnt_monitor_enable_kernel ()

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

Deprecated alias to mnt_monitor_enable_mountinfo().

Return: 0 on success and <0 on error

Parameters

mn

monitor

 

enable

0 or 1

 

mnt_monitor_enable_mountinfo ()

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

Enables or disables kernel VFS monitoring based on /proc/self/mountinfo. If the monitor does not exist and enable=1 then allocates new resources. This monitor only informs that any change has been made to the kernel mount table, but does not return details on what was changed and how. See also mnt_monitor_enable_fanotify().

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

 

Since: v2.42


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_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 is designed to provide details about changes. It is always recommended to use this function to avoid false positives.

This function iterates over a list of unprocessed events. When an event is returned by this function, it is marked as processed. If you need details about the last processed event, use the mnt_monitor_event_* functions.

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_event_next_fs ()

int
mnt_monitor_event_next_fs (struct libmnt_monitor *mn,
                           struct libmnt_fs *fs);

Fill in details about the next filesystem associated with the last event (as returned by mnt_monitor_next_change()). If the event does not provide details, it returns -ENOTSUP.

1
2
3
4
5
6
7
while (mnt_monitor_next_change(mn, NULL, &type) == 0) {
  if (type == MNT_MONITOR_TYPE_FANOTIFY) {
    while (mnt_monitor_event_next_fs(mn, fs) == 0) {
      printf("ID=%ju\n", mnt_fs_get_uniq_id(fs));
    }
  }
}

Parameters

mn

monitor

 

fs

filesystem

 

Returns

0 on success, 1 no more data, <0 on error

Since: 2.42


mnt_monitor_veil_kernel ()

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

Force monitor to ignore kernel events if the same mount/umount operation will generate an userspace event later. The kernel-only mount operation will be not affected.

Return: 0 on success and <0 on error.

Parameters

mn

monitor instance

 

enable

1 or 0

 

Since: 2.40


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 {
	int			refcount;
	int			fd;		/* public monitor file descriptor */

	struct list_head ents;
	struct monitor_entry *last;		/* last active returned by mnt_monitor_next_change() */

	bool			kernel_veiled;
};

Mount tables monitor