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; }
int test_read_tags(struct libmnt_test *ts, int argc, char *argv[]) { char line[BUFSIZ]; struct libmnt_cache *cache; int i; cache = mnt_new_cache(); if (!cache) return -ENOMEM; while(fgets(line, sizeof(line), stdin)) { size_t sz = strlen(line); if (line[sz - 1] == '\n') line[sz - 1] = '\0'; if (!strcmp(line, "quit")) break; if (*line == '/') { if (mnt_cache_read_tags(cache, line) < 0) fprintf(stderr, "%s: read tags failed\n", line); } else if (strchr(line, '=')) { char *tag, *val; const char *cn = NULL; if (!blkid_parse_tag_string(line, &tag, &val)) { cn = cache_find_tag(cache, tag, val); free(tag); free(val); } if (cn) printf("%s: %s\n", line, cn); else printf("%s: not cached\n", line); } } for (i = 0; i < cache->nents; i++) { struct mnt_cache_entry *e = &cache->ents[i]; if (!(e->flag & MNT_CACHE_ISTAG)) continue; printf("%15s : %5s : %s\n", e->value, e->key, e->key + strlen(e->key) + 1); } mnt_free_cache(cache); return 0; }
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; }
/* * 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; }
int test_resolve_spec(struct libmnt_test *ts, int argc, char *argv[]) { char line[BUFSIZ]; struct libmnt_cache *cache; cache = mnt_new_cache(); if (!cache) return -ENOMEM; while(fgets(line, sizeof(line), stdin)) { size_t sz = strlen(line); char *p; if (sz > 0 && line[sz - 1] == '\n') line[sz - 1] = '\0'; p = mnt_resolve_spec(line, cache); printf("%s : %s\n", line, p); } mnt_unref_cache(cache); return 0; }
int main(int argc, char *argv[]) { int status = 0, c; size_t i; static const struct option long_opts[] = { { "all", 0, 0, 'a' }, { "help", 0, 0, 'h' }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "ahvVL:U:", long_opts, NULL)) != -1) { switch (c) { case 'a': /* all */ ++all; break; case 'h': /* help */ usage(stdout); break; case 'v': /* be chatty */ ++verbose; break; case 'V': /* version */ printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'L': add_label(optarg); break; case 'U': add_uuid(optarg); break; case '?': default: usage(stderr); } } argv += optind; if (!all && !numof_labels() && !numof_uuids() && *argv == NULL) usage(stderr); mnt_init_debug(0); mntcache = mnt_new_cache(); for (i = 0; i < numof_labels(); i++) status |= swapoff_by("LABEL", get_label(i), !QUIET); for (i = 0; i < numof_uuids(); i++) status |= swapoff_by("UUID", get_uuid(i), !QUIET); while (*argv != NULL) status |= do_swapoff(*argv++, !QUIET, !CANONIC); if (all) status |= swapoff_all(); free_tables(); mnt_unref_cache(mntcache); return status; }