Namitha
Namitha

Reputation: 365

C++ Check if mount path is still mounted

I have the details of the mount path (specifically mount prefix) as obtained using getmntent in the structure as defined below:

struct mntent {
    char *mnt_fsname;   /* name of mounted file system */
    char *mnt_dir;      /* file system path prefix */
    char *mnt_type;     /* mount type (see mntent.h) */
    char *mnt_opts;     /* mount options (see mntent.h) */
    int   mnt_freq;     /* dump frequency in days */
    int   mnt_passno;   /* pass number on parallel fsck */
};

Using mnt_dir I want to check if the mount path is still mounted after a while as it is possible that before some processing is done on it, it might have been unmounted. What is the most efficient way to check if the path is still mounted?

Also Is there a way to get callback in case the path gets unmounted?

Upvotes: 0

Views: 3060

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118292

I'd say that the most efficient way is to cache st_dev and st_ino returned by stat() (although probably caching just st_dev should be enough).

If the volume gets unmounted, the mount point reverts to the empty subdirectory in the parent filesystem where the volume was originally mounted, and stat() will return a different device+inode, for the same file path.

As far as being notified, poke around the inotify(7) interface, paying attention to the IN_UNMOUNT event.

Upvotes: 2

Related Questions