static int is_mounted(struct libmnt_fs *fs) { int rc; const char *src; src = mnt_fs_get_source(fs); if (!src) return 0; if (!mntcache) mntcache = mnt_new_cache(); if (!mtab) { mtab = mnt_new_table(); if (!mtab) err(FSCK_EX_ERROR, ("failed to initialize libmount table")); mnt_table_set_cache(mtab, mntcache); mnt_table_parse_mtab(mtab, NULL); } rc = mnt_table_find_source(mtab, src, MNT_ITER_BACKWARD) ? 1 : 0; if (verbose) { if (rc) printf(_("%s is mounted\n"), src); else printf(_("%s is not mounted\n"), src); } return rc; }
static int dir_to_device(const char *spec, dev_t *dev) { struct libmnt_table *tb = mnt_new_table_from_file("/proc/self/mountinfo"); struct libmnt_fs *fs; struct libmnt_cache *cache; int rc = -1; if (!tb) { /* * Fallback. Traditional way to detect mountpoints. This way * is independent on /proc, but not able to detect bind mounts. */ struct stat pst, st; char buf[PATH_MAX], *cn; int len; if (stat(spec, &st) != 0) return -1; cn = mnt_resolve_path(spec, NULL); /* canonicalize */ len = snprintf(buf, sizeof(buf), "%s/..", cn ? cn : spec); free(cn); if (len < 0 || (size_t) len + 1 > sizeof(buf)) return -1; if (stat(buf, &pst) !=0) return -1; if ((st.st_dev != pst.st_dev) || (st.st_dev == pst.st_dev && st.st_ino == pst.st_ino)) { *dev = st.st_dev; return 0; } return -1; } /* to canonicalize all necessary paths */ cache = mnt_new_cache(); mnt_table_set_cache(tb, cache); fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD); if (fs && mnt_fs_get_target(fs)) { *dev = mnt_fs_get_devno(fs); rc = 0; } mnt_free_table(tb); mnt_free_cache(cache); return rc; }
struct libmnt_table *get_swaps(void) { if (!swaps) { swaps = mnt_new_table(); if (!swaps) return NULL; mnt_table_set_cache(swaps, mntcache); if (mnt_table_parse_swaps(swaps, NULL) != 0) return NULL; } return swaps; }
struct libmnt_table *get_fstab(void) { if (!fstab) { fstab = mnt_new_table(); if (!fstab) return NULL; mnt_table_set_cache(fstab, mntcache); if (mnt_table_parse_fstab(fstab, NULL) != 0) return NULL; } return fstab; }
static int dir_to_device(struct mountpoint_control *ctl) { struct libmnt_table *tb = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO); struct libmnt_fs *fs; struct libmnt_cache *cache; int rc = -1; if (!tb) { /* * Fallback. Traditional way to detect mountpoints. This way * is independent on /proc, but not able to detect bind mounts. */ struct stat pst; char buf[PATH_MAX], *cn; int len; cn = mnt_resolve_path(ctl->path, NULL); /* canonicalize */ len = snprintf(buf, sizeof(buf), "%s/..", cn ? cn : ctl->path); free(cn); if (len < 0 || (size_t) len >= sizeof(buf)) return -1; if (stat(buf, &pst) !=0) return -1; if ((ctl->st.st_dev != pst.st_dev) || (ctl->st.st_dev == pst.st_dev && ctl->st.st_ino == pst.st_ino)) { ctl->dev = ctl->st.st_dev; return 0; } return -1; } /* to canonicalize all necessary paths */ cache = mnt_new_cache(); mnt_table_set_cache(tb, cache); mnt_unref_cache(cache); fs = mnt_table_find_target(tb, ctl->path, MNT_ITER_BACKWARD); if (fs && mnt_fs_get_target(fs)) { ctl->dev = mnt_fs_get_devno(fs); rc = 0; } mnt_unref_table(tb); return rc; }
/* * See if device has been mounted by looking in mount table. If so, set * device name and mount point name, and return 1, otherwise return 0. */ static int device_get_mountpoint(char **devname, char **mnt) { struct libmnt_fs *fs; int rc; *mnt = NULL; if (!mtab) { mtab = mnt_new_table(); if (!mtab) err(EXIT_FAILURE, _("failed to initialize libmount table")); cache = mnt_new_cache(); mnt_table_set_cache(mtab, cache); if (p_option) rc = mnt_table_parse_file(mtab, _PATH_PROC_MOUNTINFO); else rc = mnt_table_parse_mtab(mtab, NULL); if (rc) err(EXIT_FAILURE, _("failed to parse mount table")); } fs = mnt_table_find_source(mtab, *devname, MNT_ITER_BACKWARD); if (!fs) { /* maybe 'devname' is mountpoint rather than a real device */ fs = mnt_table_find_target(mtab, *devname, MNT_ITER_BACKWARD); if (fs) { free(*devname); *devname = xstrdup(mnt_fs_get_source(fs)); } } if (fs) { *mnt = xstrdup(mnt_fs_get_target(fs)); /* We'll call umount(), so remove the filesystem from the table * to avoid duplicate results in the next device_get_mountpoint() * call */ mnt_free_fs(fs); } return *mnt ? 0 : -1; }
/* * See if device has been mounted by looking in mount table. If so, set * device name and mount point name, and return 1, otherwise return 0. */ static int device_get_mountpoint(char **devname, char **mnt) { struct libmnt_fs *fs; int rc; *mnt = NULL; if (!mtab) { struct libmnt_cache *cache; mtab = mnt_new_table(); if (!mtab) err(EXIT_FAILURE, _("failed to initialize libmount table")); cache = mnt_new_cache(); mnt_table_set_cache(mtab, cache); mnt_unref_cache(cache); if (p_option) rc = mnt_table_parse_file(mtab, _PATH_PROC_MOUNTINFO); else rc = mnt_table_parse_mtab(mtab, NULL); if (rc) err(EXIT_FAILURE, _("failed to parse mount table")); } fs = mnt_table_find_source(mtab, *devname, MNT_ITER_BACKWARD); if (!fs) { /* maybe 'devname' is mountpoint rather than a real device */ fs = mnt_table_find_target(mtab, *devname, MNT_ITER_BACKWARD); if (fs) { free(*devname); *devname = xstrdup(mnt_fs_get_source(fs)); } } if (fs) *mnt = xstrdup(mnt_fs_get_target(fs)); return *mnt ? 0 : -1; }