-int find_mount_root(const char *path, char **mount_root)
-{
- FILE *mnttab;
- int fd;
- struct mntent *ent;
- int len;
- int longest_matchlen = 0;
- char *longest_match = NULL;
-
- fd = open(path, O_RDONLY | O_NOATIME);
- if (fd < 0)
- return -errno;
- close(fd);
-
- mnttab = fopen("/proc/mounts", "r");
- while ((ent = getmntent(mnttab))) {
- len = strlen(ent->mnt_dir);
- if (strncmp(ent->mnt_dir, path, len) == 0) {
- /* match found */
- if (longest_matchlen < len) {
- free(longest_match);
- longest_matchlen = len;
- longest_match = strdup(ent->mnt_dir);
- }
- }
- }
- fclose(mnttab);
-
- if (!longest_match) {
- fprintf(stderr,
- "ERROR: Failed to find mount root for path %s.\n",
- path);
- return -ENOENT;
- }
-
- *mount_root = realpath(longest_match, NULL);
- free(longest_match);
-
- return 0;
-}
-