According to the proc manual, you can track mount point changes on a linux system by opening "/ proc / mounts" and adding a file descriptor for reading fd_set to select() .
The following code snippet works on Ubuntu 9.04, not Ubuntu 10.04 (with the Linux kernel 2.6.32):
int mfd = open("/proc/mounts", O_RDONLY, 0); fd_set rfds; struct timeval tv; int rv; FD_ZERO(&rfds); FD_SET(mfd, &rfds); tv.tv_sec = 5; tv.tv_usec = 0; int changes = 0; while ((rv = select(mfd+1, &rfds, NULL, NULL, &tv)) >= 0) { if (FD_ISSET(mfd, &rfds)) { fprintf(stdout, "Mount points changed. %d.\n", changes++); } FD_ZERO(&rfds); FD_SET(mfd, &rfds); tv.tv_sec = 5; tv.tv_usec = 0; if (changes > 10) { exit(EXIT_FAILURE); } }
Compiled snippet.
The file descriptor is always read on one machine, and therefore it continues to pop up in the selected call. There are even no changes to the mounts.
Did I miss something?
Thanks in advance for your help!
man 5 proc:
/ proc / [pid] / mounts (since Linux 2.4.19)
This is a list of all file systems that are currently installed in the mount mount namespace. The format of this file is documented in fstab (5). Starting with version 2.6.15, this file is infected: after opening the file for reading, changing this file (i.e. mounting or disconnecting the file system) causes selection (2) to mark the file descriptor as readable, and poll (2) and epoll_wait ( 2) note that the file has an error condition.
c linux filesystems mount procfs
shoban
source share