/* returns: error = -1, success = 0 , unknown = 1 */ static int is_vers4(struct libmnt_context *cxt) { struct libmnt_fs *fs = mnt_context_get_fs(cxt); struct libmnt_table *tb = NULL; const char *src = mnt_context_get_source(cxt), *tgt = mnt_context_get_target(cxt); int rc = 1; if (!src || !tgt) return -1; if (!mnt_fs_is_kernel(fs)) { struct libmnt_table *tb = mnt_new_table_from_file("/proc/mounts"); if (!tb) return -1; fs = mnt_table_find_pair(tb, src, tgt, MNT_ITER_BACKWARD); } if (fs) { const char *type = mnt_fs_get_fstype(fs); if (type && strcmp(type, "nfs4") == 0) rc = 0; } mnt_free_table(tb); 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; }
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; }
static char *dir_to_device(const char *spec) { struct libmnt_table *tb = mnt_new_table_from_file("/proc/self/mountinfo"); struct libmnt_fs *fs; char *res = NULL; if (!tb) return NULL; fs = mnt_table_find_target(tb, spec, MNT_ITER_BACKWARD); if (fs && mnt_fs_get_target(fs)) res = xstrdup(mnt_fs_get_source(fs)); mnt_free_table(tb); return res; }
int main(const int argc, const char* argv[]) { if (argc == 2) { if (strcmp(argv[1], "--verbose") == 0) { verbose_mode = 1; } } prepare(); clearSupplementaryGroups(); selfCheck(); struct libmnt_table* mt = mnt_new_table_from_file("/opt/xware_desktop/mounts"); if (mt == NULL) { fprintf(stderr, "mnt_new_table_from_file failed.\n"); exit(EXIT_FAILURE); } struct libmnt_iter* itr = mnt_new_iter(MNT_ITER_FORWARD); struct libmnt_fs* fs = NULL; while(1) { int tmp = mnt_table_find_next_fs(mt, itr, &matchAll, NULL, &fs); if (tmp < 0) { // error fprintf(stderr, "mnt_table_find_next_fs failed.\n"); break; } else { if (tmp == 1) { // reach EOF break; } const char* target = mnt_fs_get_target(fs); if (target == NULL) { fprintf(stderr, "mnt_fs_get_target failed.\n"); } else { printf("%s%s%s\n", BOLD, target, NOSTYLE); printf("================================\n"); if (checkDirXPermissions(target)) { checkTargetDirPermissions(target); } printf("\n"); } } } printf("%s", NOSTYLE); mnt_free_fs(fs); mnt_free_iter(itr); mnt_free_table(mt); return 0; }
int main (int argc, char *argv[]) { struct libmnt_fs *fs; struct libmnt_table *tab; mnt_init_debug(0xffff); tab = mnt_new_table_from_file("/proc/self/mounts"); fs = mnt_table_find_target(tab, argv[2], MNT_ITER_BACKWARD); char *source = mnt_fs_get_source(fs); if (strcmp(source, argv[1]) == 0) printf("is mounted\n"); else printf("could not find fs in tab\n"); mnt_free_table(tab); #if 0 int rc; struct libmnt_fs *fs; struct libmnt_context *cxt = mnt_new_context(); mnt_init_debug(0xffff); mnt_context_set_source(cxt, argv[1]); mnt_context_set_target(cxt, argv[2]); fs = mnt_context_get_fs(cxt); if (fs != NULL) { if (mnt_context_is_fs_mounted(cxt, fs, &rc)) rc = 1; else printf("can't find fs in mtab\n"); } else printf("Can't get fs from mnt context\n"); mnt_free_context(cxt); #endif return 0; }
/* * Sets fs-root and fs-type to @upd->fs according to the @fs template and * @mountfalgs. For MS_BIND mountflag it reads information about source * filesystem from /proc/self/mountinfo. */ static int set_fs_root(struct libmnt_update *upd, struct libmnt_fs *fs, unsigned long mountflags) { struct libmnt_fs *src_fs; char *fsroot = NULL; const char *src; int rc = 0; DBG(UPDATE, mnt_debug("setting FS root")); assert(upd); assert(upd->fs); assert(fs); if (mountflags & MS_BIND) { if (!upd->mountinfo) upd->mountinfo = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO); src = mnt_fs_get_srcpath(fs); if (src) { rc = mnt_fs_set_bindsrc(upd->fs, src); if (rc) goto err; } } src_fs = mnt_table_get_fs_root(upd->mountinfo, fs, mountflags, &fsroot); if (src_fs) { src = mnt_fs_get_srcpath(src_fs); rc = mnt_fs_set_source(upd->fs, src); if (rc) goto err; mnt_fs_set_fstype(upd->fs, mnt_fs_get_fstype(src_fs)); } upd->fs->root = fsroot; return 0; err: free(fsroot); return rc; }