HARISH B
HARISH B

Reputation: 85

function or a systemcall similar to "mountpoint" command in linux

I wanted a function or a system call similar to mountpoint command Linux.
(this command will check if given directory is a mount point or not)

Upvotes: 0

Views: 2955

Answers (3)

The need for functions like this is usually (though not always) a red flag that you're not embracing the abstractions of the system...and you should reconsider whatever-it-is-you're-doing. If there is some choice you're making in your software based on whether something is a mountpoint...consider making that alternative behavior a parameter which can be controlled via scripting. People can parameterize your program to adopt that behavior via using the native mountpoint command, or whatever else.

With that disclaimer aside, here's an implementation of mountpoint.c:

https://fossies.org/linux/sysvinit/src/mountpoint.c

...and a reference on "testing the type of a file"

http://www.aquaphoenix.com/ref/gnu_c_library/libc_160.html

Upvotes: 1

Daniel Kamil Kozar
Daniel Kamil Kozar

Reputation: 19316

I'm not aware of an actual syscall that would do that, but what you can do is compare the device numbers of the directory that you want to check and its parent. That can be done with stat. Example code (error checking omitted) :

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(void) {
  struct stat mp, mp_parent;
  stat("/boot", &mp);
  stat("/boot/..", &mp_parent);

  if (mp.st_dev != mp_parent.st_dev)
    printf("/boot is a mount point.\n");
  else
    printf("/boot is not a mount point.\n");

  return 0;
}

Upvotes: 7

Nazar554
Nazar554

Reputation: 4195

Write your own app to parse /proc/mounts. Then compare your path with paths from /proc/mounts. If they are equal then path is a mount point.

Upvotes: 0

Related Questions