]> git.decadent.org.uk Git - nfs-utils.git/blob - support/misc/mountpoint.c
Fix various issues discovered by Coverity
[nfs-utils.git] / support / misc / mountpoint.c
1
2 /*
3  * check if a given path is a mountpoint 
4  */
5
6 #include <string.h>
7 #include <malloc.h>
8 #include <sys/stat.h>
9
10 int
11 is_mountpoint(char *path)
12 {
13         /* Check if 'path' is a current mountpoint.
14          * Possibly we should also check it is the mountpoint of the 
15          * filesystem holding the target directory, but there doesn't
16          * seem a lot of point.
17          *
18          * We deem it to be a mountpoint if appending a ".." gives a different
19          * device or the same inode number.
20          */
21         char *dotdot;
22         struct stat stb, pstb;
23         int rv;
24
25         dotdot = malloc(strlen(path)+4);
26         if (!dotdot)
27                 return 0;
28         strcat(strcpy(dotdot, path), "/..");
29         if (lstat(path, &stb) != 0 ||
30             lstat(dotdot, &pstb) != 0)
31                 rv = 0;
32         else
33                 if (stb.st_dev != pstb.st_dev ||
34                     stb.st_ino == pstb.st_ino)
35                         rv = 1;
36                 else
37                         rv = 0;
38         free(dotdot);
39         return rv;
40 }