static int update_table(struct libmnt_update *upd, struct libmnt_table *tb) { FILE *f; int rc, fd; char *uq = NULL; if (!tb || !upd->filename) return -EINVAL; DBG(UPDATE, mnt_debug_h(upd, "%s: updating", upd->filename)); fd = mnt_open_uniq_filename(upd->filename, &uq); if (fd < 0) return fd; /* error */ f = fdopen(fd, "w"); if (f) { struct stat st; struct libmnt_iter itr; struct libmnt_fs *fs; int fd; mnt_reset_iter(&itr, MNT_ITER_FORWARD); while(mnt_table_next_fs(tb, &itr, &fs) == 0) { if (upd->userspace_only) rc = fprintf_utab_fs(f, fs); else rc = fprintf_mtab_fs(f, fs); if (rc) { DBG(UPDATE, mnt_debug_h(upd, "%s: write entry failed: %m", uq)); goto leave; } } if (fflush(f) != 0) { rc = -errno; DBG(UPDATE, mnt_debug_h(upd, "%s: fflush failed: %m", uq)); goto leave; } fd = fileno(f); rc = fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) ? -errno : 0; if (!rc && stat(upd->filename, &st) == 0) /* Copy uid/gid from the present file before renaming. */ rc = fchown(fd, st.st_uid, st.st_gid) ? -errno : 0; fclose(f); rc = rename(uq, upd->filename) ? -errno : 0; } else { rc = -errno; close(fd); } leave: unlink(uq); /* be paranoid */ free(uq); return rc; }
/* * The default is to use fstype from cxt->fs, this could be overwritten by * @try_type argument. * * Returns: 0 on success, * >0 in case of mount(2) error (returns syscall errno), * <0 in case of other errors. */ static int do_mount(struct libmnt_context *cxt, const char *try_type) { int rc = 0; const char *src, *target, *type; unsigned long flags; assert(cxt); assert(cxt->fs); assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); if (try_type && !cxt->helper) { rc = mnt_context_prepare_helper(cxt, "mount", try_type); if (!rc) return rc; } if (cxt->helper) return exec_helper(cxt); flags = cxt->mountflags; src = mnt_fs_get_srcpath(cxt->fs); target = mnt_fs_get_target(cxt->fs); if (!src || !target) return -EINVAL; type = try_type ? : mnt_fs_get_fstype(cxt->fs); if (!(flags & MS_MGC_MSK)) flags |= MS_MGC_VAL; DBG(CXT, mnt_debug_h(cxt, "%smount(2) " "[source=%s, target=%s, type=%s, " " mountflags=0x%08lx, mountdata=%s]", (cxt->flags & MNT_FL_FAKE) ? "(FAKE) " : "", src, target, type, flags, cxt->mountdata ? "yes" : "<none>")); if (cxt->flags & MNT_FL_FAKE) cxt->syscall_status = 0; else { if (mount(src, target, type, flags, cxt->mountdata)) { cxt->syscall_status = -errno; DBG(CXT, mnt_debug_h(cxt, "mount(2) failed [errno=%d %m]", -cxt->syscall_status)); return -cxt->syscall_status; } DBG(CXT, mnt_debug_h(cxt, "mount(2) success")); cxt->syscall_status = 0; } if (try_type && cxt->update) { struct libmnt_fs *fs = mnt_update_get_fs(cxt->update); if (fs) rc = mnt_fs_set_fstype(fs, try_type); } return rc; }
/** * mnt_table_parse_stream: * @tb: tab pointer * @f: file stream * @filename: filename used for debug and error messages * * Returns: 0 on success, negative number in case of error. */ int mnt_table_parse_stream(struct libmnt_table *tb, FILE *f, const char *filename) { int nlines = 0; int rc = -1; int flags = 0; pid_t tid = -1; assert(tb); assert(f); assert(filename); DBG(TAB, mnt_debug_h(tb, "%s: start parsing [entries=%d, filter=%s]", filename, mnt_table_get_nents(tb), tb->fltrcb ? "yes" : "not")); /* necessary for /proc/mounts only, the /proc/self/mountinfo * parser sets the flag properly */ if (filename && strcmp(filename, _PATH_PROC_MOUNTS) == 0) flags = MNT_FS_KERNEL; while (!feof(f)) { struct libmnt_fs *fs = mnt_new_fs(); if (!fs) goto err; rc = mnt_table_parse_next(tb, f, fs, filename, &nlines); if (!rc && tb->fltrcb && tb->fltrcb(fs, tb->fltrcb_data)) rc = 1; /* filtered out by callback... */ if (!rc) { rc = mnt_table_add_fs(tb, fs); fs->flags |= flags; if (rc == 0 && tb->fmt == MNT_FMT_MOUNTINFO) rc = kernel_fs_postparse(tb, fs, &tid, filename); } mnt_unref_fs(fs); if (rc) { if (rc == 1) continue; /* recoverable error */ if (feof(f)) break; goto err; /* fatal error */ } } DBG(TAB, mnt_debug_h(tb, "%s: stop parsing (%d entries)", filename, mnt_table_get_nents(tb))); return 0; err: DBG(TAB, mnt_debug_h(tb, "%s: parse error (rc=%d)", filename, rc)); return rc; }
/* Check, if there already exists a mounted loop device on the mountpoint node * with the same parameters. */ static int is_mounted_same_loopfile(struct libmnt_context *cxt, const char *target, const char *backing_file, uint64_t offset) { struct libmnt_table *tb; struct libmnt_iter itr; struct libmnt_fs *fs; struct libmnt_cache *cache; assert(cxt); assert(cxt->fs); assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); if (!target || !backing_file || mnt_context_get_mtab(cxt, &tb)) return 0; DBG(CXT, mnt_debug_h(cxt, "checking if %s mounted on %s", backing_file, target)); cache = mnt_context_get_cache(cxt); mnt_reset_iter(&itr, MNT_ITER_BACKWARD); /* Search for mountpoint node in mtab, procceed if any of these has the * loop option set or the device is a loop device */ while (mnt_table_next_fs(tb, &itr, &fs) == 0) { const char *src = mnt_fs_get_source(fs); const char *opts = mnt_fs_get_user_options(fs); char *val; size_t len; int res = 0; if (!src || !mnt_fs_match_target(fs, target, cache)) continue; if (strncmp(src, "/dev/loop", 9) == 0) { res = loopdev_is_used((char *) src, backing_file, offset, LOOPDEV_FL_OFFSET); } else if (opts && (cxt->user_mountflags & MNT_MS_LOOP) && mnt_optstr_get_option(opts, "loop", &val, &len) == 0 && val) { val = strndup(val, len); res = loopdev_is_used((char *) val, backing_file, offset, LOOPDEV_FL_OFFSET); free(val); } if (res) { DBG(CXT, mnt_debug_h(cxt, "%s already mounted", backing_file)); return 1; } } return 0; }
int mnt_context_is_loopdev(struct libmnt_context *cxt) { const char *type, *src; assert(cxt); /* The mount flags have to be merged, otherwise we have to use * expensive mnt_context_get_user_mflags() instead of cxt->user_mountflags. */ assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); if (!cxt->fs) return 0; src = mnt_fs_get_srcpath(cxt->fs); if (!src) return 0; /* backing file not set */ if (cxt->user_mountflags & (MNT_MS_LOOP | MNT_MS_OFFSET | MNT_MS_SIZELIMIT)) { DBG(CXT, mnt_debug_h(cxt, "loopdev specific options detected")); return 1; } if (cxt->mountflags & (MS_BIND | MS_MOVE | MS_PROPAGATION)) return 0; /* Automatically create a loop device from a regular file if a * filesystem is not specified or the filesystem is known for libblkid * (these filesystems work with block devices only). The file size * should be at least 1KiB otherwise we will create empty loopdev where * is no mountable filesystem... * * Note that there is not a restriction (on kernel side) that prevents regular * file as a mount(2) source argument. A filesystem that is able to mount * regular files could be implemented. */ type = mnt_fs_get_fstype(cxt->fs); if (mnt_fs_is_regular(cxt->fs) && (!type || strcmp(type, "auto") == 0 || blkid_known_fstype(type))) { struct stat st; if (stat(src, &st) == 0 && S_ISREG(st.st_mode) && st.st_size > 1024) { DBG(CXT, mnt_debug_h(cxt, "automatically enabling loop= option")); cxt->user_mountflags |= MNT_MS_LOOP; mnt_optstr_append_option(&cxt->fs->user_optstr, "loop", NULL); return 1; } } return 0; }
static int do_mount_by_pattern(struct libmnt_context *cxt, const char *pattern) { int neg = pattern && strncmp(pattern, "no", 2) == 0; int rc = -EINVAL; char **filesystems, **fp; assert(cxt); assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); if (!neg && pattern) { /* * try all types from the list */ char *p, *p0; DBG(CXT, mnt_debug_h(cxt, "trying to mount by FS pattern list")); p0 = p = strdup(pattern); if (!p) return -ENOMEM; do { char *end = strchr(p, ','); if (end) *end = '\0'; rc = do_mount(cxt, p); p = end ? end + 1 : NULL; } while (!mnt_context_get_status(cxt) && p); free(p0); if (mnt_context_get_status(cxt)) return rc; } /* * try /etc/filesystems and /proc/filesystems */ DBG(CXT, mnt_debug_h(cxt, "trying to mount by filesystems lists")); rc = mnt_get_filesystems(&filesystems, neg ? pattern : NULL); if (rc) return rc; for (fp = filesystems; *fp; fp++) { rc = do_mount(cxt, *fp); if (mnt_context_get_status(cxt)) break; if (mnt_context_get_syscall_errno(cxt) != EINVAL) break; } mnt_free_filesystems(filesystems); return rc; }
/** * mnt_table_parse_stream: * @tb: tab pointer * @f: file stream * @filename: filename used for debug and error messages * * Returns: 0 on success, negative number in case of error. */ int mnt_table_parse_stream(struct libmnt_table *tb, FILE *f, const char *filename) { int nlines = 0; int rc = -1; int flags = 0; pid_t tid = -1; assert(tb); assert(f); assert(filename); DBG(TAB, mnt_debug_h(tb, "%s: start parsing (%d entries)", filename, mnt_table_get_nents(tb))); /* necessary for /proc/mounts only, the /proc/self/mountinfo * parser sets the flag properly */ if (filename && strcmp(filename, _PATH_PROC_MOUNTS) == 0) flags = MNT_FS_KERNEL; while (!feof(f)) { struct libmnt_fs *fs = mnt_new_fs(); if (!fs) goto err; rc = mnt_table_parse_next(tb, f, fs, filename, &nlines); if (!rc) { rc = mnt_table_add_fs(tb, fs); fs->flags |= flags; if (tb->fmt == MNT_FMT_MOUNTINFO && filename) { if (tid == -1) tid = path_to_tid(filename); fs->tid = tid; } } if (rc) { mnt_free_fs(fs); if (rc == 1) continue; /* recoverable error */ if (feof(f)) break; goto err; /* fatal error */ } } DBG(TAB, mnt_debug_h(tb, "%s: stop parsing (%d entries)", filename, mnt_table_get_nents(tb))); return 0; err: DBG(TAB, mnt_debug_h(tb, "%s: parse error (rc=%d)", filename, rc)); return rc; }
/* * This function uses @uf to found corresponding record in @tb, then the record * from @tb is updated (user specific mount options are added). * * Note that @uf must contain only user specific mount options instead of * VFS options (note that FS options are ignored). * * Returns modified filesystem (from @tb) or NULL. */ static struct libmnt_fs *mnt_table_merge_user_fs(struct libmnt_table *tb, struct libmnt_fs *uf) { struct libmnt_fs *fs; struct libmnt_iter itr; const char *optstr, *src, *target, *root, *attrs; assert(tb); assert(uf); if (!tb || !uf) return NULL; DBG(TAB, mnt_debug_h(tb, "merging user fs")); src = mnt_fs_get_srcpath(uf); target = mnt_fs_get_target(uf); optstr = mnt_fs_get_user_options(uf); attrs = mnt_fs_get_attributes(uf); root = mnt_fs_get_root(uf); if (!src || !target || !root || (!attrs && !optstr)) return NULL; mnt_reset_iter(&itr, MNT_ITER_BACKWARD); while(mnt_table_next_fs(tb, &itr, &fs) == 0) { const char *s = mnt_fs_get_srcpath(fs), *t = mnt_fs_get_target(fs), *r = mnt_fs_get_root(fs); if (fs->flags & MNT_FS_MERGED) continue; if (s && t && r && !strcmp(t, target) && !strcmp(s, src) && !strcmp(r, root)) break; } if (fs) { DBG(TAB, mnt_debug_h(tb, "found fs -- appending user optstr")); mnt_fs_append_options(fs, optstr); mnt_fs_append_attributes(fs, attrs); mnt_fs_set_bindsrc(fs, mnt_fs_get_bindsrc(uf)); fs->flags |= MNT_FS_MERGED; DBG(TAB, mnt_debug_h(tb, "found fs:")); DBG(TAB, mnt_fs_print_debug(fs, stderr)); } return fs; }
/** * mnt_resolve_tag: * @token: tag name * @value: tag value * @cache: for results or NULL * * Returns: device name or NULL in case of error. The result has to be * deallocated by free() if @cache is NULL. */ char *mnt_resolve_tag(const char *token, const char *value, struct libmnt_cache *cache) { char *p = NULL; assert(token); assert(value); DBG(CACHE, mnt_debug_h(cache, "resolving tag token=%s value=%s", token, value)); if (!token || !value) return NULL; if (cache) p = (char *) mnt_cache_find_tag(cache, token, value); if (!p) { /* returns newly allocated string */ p = blkid_evaluate_tag(token, value, cache ? &cache->bc : NULL); if (p && cache && mnt_cache_add_tag(cache, token, value, p, 0)) goto error; } return p; error: free(p); return NULL; }
/* note that the @native could be tha same pointer as @real */ static int mnt_cache_add_entry(struct libmnt_cache *cache, char *native, char *real, int flag) { struct mnt_cache_entry *e; assert(cache); assert(real); assert(native); if (cache->nents == cache->nallocs) { size_t sz = cache->nallocs + MNT_CACHE_CHUNKSZ; e = realloc(cache->ents, sz * sizeof(struct mnt_cache_entry)); if (!e) return -ENOMEM; cache->ents = e; cache->nallocs = sz; } e = &cache->ents[cache->nents]; e->native = native; e->real = real; e->flag = flag; cache->nents++; DBG(CACHE, mnt_debug_h(cache, "add entry [%2zd] (%s): %s: %s", cache->nents, (flag & MNT_CACHE_ISPATH) ? "path" : "tag", real, native)); return 0; }
static int update_modify_target(struct libmnt_update *upd, struct libmnt_lock *lc) { struct libmnt_table *tb = NULL; int rc = 0; DBG(UPDATE, mnt_debug_h(upd, "%s: modify target", upd->filename)); if (lc) rc = mnt_lock_file(lc); if (rc) return rc; tb = __mnt_new_table_from_file(upd->filename, upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB); if (tb) { struct libmnt_fs *cur = mnt_table_find_target(tb, mnt_fs_get_srcpath(upd->fs), MNT_ITER_BACKWARD); if (cur) { rc = mnt_fs_set_target(cur, mnt_fs_get_target(upd->fs)); if (!rc) rc = update_table(upd, tb); } } if (lc) mnt_unlock_file(lc); mnt_free_table(tb); return rc; }
static int update_remove_entry(struct libmnt_update *upd, struct libmnt_lock *lc) { struct libmnt_table *tb; int rc = 0; assert(upd); assert(upd->target); DBG(UPDATE, mnt_debug_h(upd, "%s: remove entry", upd->filename)); if (lc) rc = mnt_lock_file(lc); if (rc) return rc; tb = __mnt_new_table_from_file(upd->filename, upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB); if (tb) { struct libmnt_fs *rem = mnt_table_find_target(tb, upd->target, MNT_ITER_BACKWARD); if (rem) { mnt_table_remove_fs(tb, rem); rc = update_table(upd, tb); mnt_free_fs(rem); } } if (lc) mnt_unlock_file(lc); mnt_free_table(tb); return rc; }
static int update_add_entry(struct libmnt_update *upd, struct libmnt_lock *lc) { struct libmnt_table *tb; int rc = 0; assert(upd); assert(upd->fs); DBG(UPDATE, mnt_debug_h(upd, "%s: add entry", upd->filename)); if (lc) rc = mnt_lock_file(lc); if (rc) return rc; tb = __mnt_new_table_from_file(upd->filename, upd->userspace_only ? MNT_FMT_UTAB : MNT_FMT_MTAB); if (tb) { struct libmnt_fs *fs = mnt_copy_fs(NULL, upd->fs); if (!fs) rc = -ENOMEM; else { mnt_table_add_fs(tb, fs); rc = update_table(upd, tb); } } if (lc) mnt_unlock_file(lc); mnt_free_table(tb); return rc; }
/* note that the @key could be tha same pointer as @value */ static int cache_add_entry(struct libmnt_cache *cache, char *key, char *value, int flag) { struct mnt_cache_entry *e; assert(cache); assert(value); assert(key); if (cache->nents == cache->nallocs) { size_t sz = cache->nallocs + MNT_CACHE_CHUNKSZ; e = realloc(cache->ents, sz * sizeof(struct mnt_cache_entry)); if (!e) return -ENOMEM; cache->ents = e; cache->nallocs = sz; } e = &cache->ents[cache->nents]; e->key = key; e->value = value; e->flag = flag; cache->nents++; DBG(CACHE, mnt_debug_h(cache, "add entry [%2zd] (%s): %s: %s", cache->nents, (flag & MNT_CACHE_ISPATH) ? "path" : "tag", value, key)); return 0; }
/** * mnt_get_fstype: * @devname: device name * @ambi: returns TRUE if probing result is ambivalent (optional argument) * @cache: cache for results or NULL * * Returns: filesystem type or NULL in case of error. The result has to be * deallocated by free() if @cache is NULL. */ char *mnt_get_fstype(const char *devname, int *ambi, struct libmnt_cache *cache) { blkid_probe pr; const char *data; char *type = NULL; int rc; DBG(CACHE, mnt_debug_h(cache, "get %s FS type", devname)); if (cache) return mnt_cache_find_tag_value(cache, devname, "TYPE"); if (cache_get_probe(NULL, devname, &pr)) return NULL; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE); rc = blkid_do_safeprobe(pr); if (!rc && !blkid_probe_lookup_value(pr, "TYPE", &data, NULL)) type = strdup(data); if (ambi) *ambi = rc == -2 ? TRUE : FALSE; blkid_free_probe(pr); return type; }
/** * mnt_table_get_root_fs: * @tb: mountinfo file (/proc/self/mountinfo) * @root: returns pointer to the root filesystem (/) * * The function uses parent ID from mountinfo file to determine root filesystem * (the filesystem with the smallest ID). The function is designed mostly for * applications where is necessary to sort mountpoints by IDs to get the tree * of the mountpoints (e.g. findmnt default output). * * If you're not sure than use * * mnt_table_find_target(tb, "/", MNT_ITER_BACKWARD); * * this is more robust and usable for arbitrary tab file (including fstab). * * Returns: 0 on success or less then zero case of error. */ int mnt_table_get_root_fs(struct libmnt_table *tb, struct libmnt_fs **root) { struct libmnt_iter itr; struct libmnt_fs *fs; int root_id = 0; assert(tb); assert(root); if (!tb || !root) return -EINVAL; DBG(TAB, mnt_debug_h(tb, "lookup root fs")); mnt_reset_iter(&itr, MNT_ITER_FORWARD); while(mnt_table_next_fs(tb, &itr, &fs) == 0) { int id = mnt_fs_get_parent_id(fs); if (!id) break; /* @tab is not mountinfo file? */ if (!*root || id < root_id) { *root = fs; root_id = id; } } return root_id ? 0 : -EINVAL; }
static int append_comment(struct libmnt_table *tb, struct libmnt_fs *fs, const char *comm, int eof) { int rc, intro = mnt_table_get_nents(tb) == 0; if (intro && is_terminated_by_blank(mnt_table_get_intro_comment(tb))) intro = 0; DBG(TAB, mnt_debug_h(tb, "appending %s comment", intro ? "intro" : eof ? "trailing" : "fs")); if (intro) rc = mnt_table_append_intro_comment(tb, comm); else if (eof) { rc = mnt_table_set_trailing_comment(tb, mnt_fs_get_comment(fs)); if (!rc) rc = mnt_table_append_trailing_comment(tb, comm); if (!rc) rc = mnt_fs_set_comment(fs, NULL); } else rc = mnt_fs_append_comment(fs, comm); return rc; }
static int prepare_helper_from_options(struct libmnt_context *cxt, const char *name) { char *suffix = NULL; const char *opts; size_t valsz; if (mnt_context_is_nohelpers(cxt)) return 0; opts = mnt_fs_get_user_options(cxt->fs); if (!opts) return 0; if (mnt_optstr_get_option(opts, name, &suffix, &valsz)) return 0; suffix = strndup(suffix, valsz); if (!suffix) return -ENOMEM; DBG(CXT, mnt_debug_h(cxt, "umount: umount.%s %s requested", suffix, name)); return mnt_context_prepare_helper(cxt, "umount", suffix); }
/** * mnt_table_parse_mtab: * @tb: table * @filename: overwrites default (/etc/mtab or $LIBMOUNT_MTAB) or NULL * * This function parses /etc/mtab or /proc/self/mountinfo + * /run/mount/utabs or /proc/mounts. * * See also mnt_table_set_parser_errcb(). * * Returns: 0 on success or negative number in case of error. */ int mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename) { int rc; const char *utab = NULL; struct libmnt_table *u_tb; assert(tb); if (mnt_has_regular_mtab(&filename, NULL)) { DBG(TAB, mnt_debug_h(tb, "force %s usage", filename)); rc = mnt_table_parse_file(tb, filename); if (!rc) return 0; filename = NULL; /* failed */ } /* * useless /etc/mtab * -- read kernel information from /proc/self/mountinfo */ tb->fmt = MNT_FMT_MOUNTINFO; rc = mnt_table_parse_file(tb, _PATH_PROC_MOUNTINFO); if (rc) { /* hmm, old kernel? ...try /proc/mounts */ tb->fmt = MNT_FMT_MTAB; return mnt_table_parse_file(tb, _PATH_PROC_MOUNTS); } if (mnt_table_get_nents(tb) == 0) return 0; /* empty, ignore utab */ /* * try to read the user specific information from /run/mount/utabs */ utab = mnt_get_utab_path(); if (!utab || is_file_empty(utab)) return 0; u_tb = mnt_new_table(); if (!u_tb) return -ENOMEM; u_tb->fmt = MNT_FMT_UTAB; mnt_table_set_parser_fltrcb(u_tb, tb->fltrcb, tb->fltrcb_data); if (mnt_table_parse_file(u_tb, utab) == 0) { struct libmnt_fs *u_fs; struct libmnt_iter itr; mnt_reset_iter(&itr, MNT_ITER_BACKWARD); /* merge user options into mountinfo from the kernel */ while(mnt_table_next_fs(u_tb, &itr, &u_fs) == 0) mnt_table_merge_user_fs(tb, u_fs); } mnt_unref_table(u_tb); return 0; }
/** * mnt_new_cache: * * Returns: new struct libmnt_cache instance or NULL in case of ENOMEM error. */ struct libmnt_cache *mnt_new_cache(void) { struct libmnt_cache *cache = calloc(1, sizeof(*cache)); if (!cache) return NULL; DBG(CACHE, mnt_debug_h(cache, "alloc")); return cache; }
/** * mnt_free_table: * @tb: tab pointer * * Deallocates tab struct and all entries. */ void mnt_free_table(struct libmnt_table *tb) { if (!tb) return; mnt_reset_table(tb); DBG(TAB, mnt_debug_h(tb, "free")); free(tb); }
/** * mnt_context_prepare_mount: * @cxt: context * * Prepare context for mounting, unnecessary for mnt_context_mount(). * * Returns: negative number on error, zero on success */ int mnt_context_prepare_mount(struct libmnt_context *cxt) { int rc = -EINVAL; assert(cxt); assert(cxt->fs); assert(cxt->helper_exec_status == 1); assert(cxt->syscall_status == 1); if (!cxt || !cxt->fs || mnt_fs_is_swaparea(cxt->fs)) return -EINVAL; if (!mnt_fs_get_source(cxt->fs) && !mnt_fs_get_target(cxt->fs)) return -EINVAL; if (cxt->flags & MNT_FL_PREPARED) return 0; cxt->action = MNT_ACT_MOUNT; DBG(CXT, mnt_debug_h(cxt, "mount: preparing")); /* TODO: fstab is unnecessary for MS_{MOVE,BIND,STARED,...} */ rc = mnt_context_apply_fstab(cxt); if (!rc) rc = mnt_context_merge_mflags(cxt); if (!rc) rc = evaluate_permissions(cxt); if (!rc) rc = fix_optstr(cxt); if (!rc) rc = mnt_context_prepare_srcpath(cxt); if (!rc) rc = mnt_context_prepare_target(cxt); if (!rc) rc = mnt_context_guess_fstype(cxt); if (!rc) rc = mnt_context_prepare_helper(cxt, "mount", NULL); if (rc) { DBG(CXT, mnt_debug_h(cxt, "mount: preparing failed")); return rc; } cxt->flags |= MNT_FL_PREPARED; return rc; }
/** * mnt_context_prepare_umount: * @cxt: mount context * * Prepare context for umounting, unnecessary for mnt_context_umount(). * * Returns: 0 on success, and negative number in case of error. */ int mnt_context_prepare_umount(struct libmnt_context *cxt) { int rc; assert(cxt); assert(cxt->fs); assert(cxt->helper_exec_status == 1); assert(cxt->syscall_status == 1); if (!cxt || !cxt->fs || mnt_fs_is_swaparea(cxt->fs)) return -EINVAL; if (!mnt_context_get_source(cxt) && !mnt_context_get_target(cxt)) return -EINVAL; if (cxt->flags & MNT_FL_PREPARED) return 0; free(cxt->helper); /* be paranoid */ cxt->helper = NULL; cxt->action = MNT_ACT_UMOUNT; rc = lookup_umount_fs(cxt); if (!rc) rc = mnt_context_merge_mflags(cxt); if (!rc) rc = evaluate_permissions(cxt); if (!rc && !cxt->helper) { if (cxt->user_mountflags & MNT_MS_HELPER) /* on helper= mount option based helper */ rc = prepare_helper_from_options(cxt, "helper"); if (!rc && !cxt->helper) /* on fstype based helper */ rc = mnt_context_prepare_helper(cxt, "umount", NULL); } if (!rc && (cxt->user_mountflags & MNT_MS_LOOP)) /* loop option explicitly specified in mtab, detach this loop */ mnt_context_enable_loopdel(cxt, TRUE); if (!rc && mnt_context_is_loopdel(cxt) && cxt->fs) { const char *src = mnt_fs_get_srcpath(cxt->fs); if (src && (!is_loopdev(src) || loopdev_is_autoclear(src))) mnt_context_enable_loopdel(cxt, FALSE); } if (rc) { DBG(CXT, mnt_debug_h(cxt, "umount: preparing failed")); return rc; } cxt->flags |= MNT_FL_PREPARED; return rc; }
/** * mnt_context_do_mount * @cxt: context * * Call mount(2) or mount.type helper. Unnecessary for mnt_context_mount(). * * Note that this function could be called only once. If you want to mount * another source or target than you have to call mnt_reset_context(). * * If you want to call mount(2) for the same source and target with a diffrent * mount flags or fstype then call mnt_context_reset_status() and then try * again mnt_context_do_mount(). * * WARNING: non-zero return code does not mean that mount(2) syscall or * mount.type helper wasn't successfully called. * * Check mnt_context_get_status() after error! * * Returns: 0 on success; * >0 in case of mount(2) error (returns syscall errno), * <0 in case of other errors. */ int mnt_context_do_mount(struct libmnt_context *cxt) { const char *type; int res; assert(cxt); assert(cxt->fs); assert(cxt->helper_exec_status == 1); assert(cxt->syscall_status == 1); assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); assert((cxt->flags & MNT_FL_PREPARED)); assert((cxt->action == MNT_ACT_MOUNT)); DBG(CXT, mnt_debug_h(cxt, "mount: do mount")); if (!(cxt->flags & MNT_FL_MOUNTDATA)) cxt->mountdata = (char *) mnt_fs_get_fs_options(cxt->fs); type = mnt_fs_get_fstype(cxt->fs); if (type) res = do_mount(cxt, NULL); else res = do_mount_by_pattern(cxt, cxt->fstype_pattern); if (mnt_context_get_status(cxt) && !mnt_context_is_fake(cxt) && !cxt->helper) { /* * Mounted by mount(2), do some post-mount checks * * Kernel allows to use MS_RDONLY for bind mounts, but the * read-only request could be silently ignored. Check it to * avoid 'ro' in mtab and 'rw' in /proc/mounts. */ if ((cxt->mountflags & MS_BIND) && (cxt->mountflags & MS_RDONLY) && !mnt_is_readonly(mnt_context_get_target(cxt))) mnt_context_set_mflags(cxt, cxt->mountflags & ~MS_RDONLY); /* Kernel can silently add MS_RDONLY flag when mounting file * system that does not have write support. Check this to avoid * 'ro' in /proc/mounts and 'rw' in mtab. */ if (!(cxt->mountflags & (MS_RDONLY | MS_PROPAGATION | MS_MOVE)) && mnt_is_readonly(mnt_context_get_target(cxt))) mnt_context_set_mflags(cxt, cxt->mountflags | MS_RDONLY); } return res; }
/* * Filter out entries during tab file parsing. If @cb returns 1 then the entry * is ignored. */ int mnt_table_set_parser_fltrcb(struct libmnt_table *tb, int (*cb)(struct libmnt_fs *, void *), void *data) { assert(tb); DBG(TAB, mnt_debug_h(tb, "%s table parser filter", cb ? "set" : "unset")); tb->fltrcb = cb; tb->fltrcb_data = data; return 0; }
/** * mnt_new_update: * * Returns: newly allocated update handler */ struct libmnt_update *mnt_new_update(void) { struct libmnt_update *upd; upd = calloc(1, sizeof(*upd)); if (!upd) return NULL; DBG(UPDATE, mnt_debug_h(upd, "allocate")); return upd; }
/** * mnt_table_next_child_fs: * @tb: mountinfo file (/proc/self/mountinfo) * @itr: iterator * @parent: parental FS * @chld: returns the next child filesystem * * Note that filesystems are returned in the order how was mounted (according to * IDs in /proc/self/mountinfo). * * Returns: 0 on success, negative number in case of error or 1 at end of list. */ int mnt_table_next_child_fs(struct libmnt_table *tb, struct libmnt_iter *itr, struct libmnt_fs *parent, struct libmnt_fs **chld) { struct libmnt_fs *fs; int parent_id, lastchld_id = 0, chld_id = 0; if (!tb || !itr || !parent) return -EINVAL; DBG(TAB, mnt_debug_h(tb, "lookup next child of '%s'", mnt_fs_get_target(parent))); parent_id = mnt_fs_get_id(parent); if (!parent_id) return -EINVAL; /* get ID of the previously returned child */ if (itr->head && itr->p != itr->head) { MNT_ITER_ITERATE(itr, fs, struct libmnt_fs, ents); lastchld_id = mnt_fs_get_id(fs); } *chld = NULL; mnt_reset_iter(itr, MNT_ITER_FORWARD); while(mnt_table_next_fs(tb, itr, &fs) == 0) { int id; if (mnt_fs_get_parent_id(fs) != parent_id) continue; id = mnt_fs_get_id(fs); /* avoid infinite loop. This only happens in rare cases * such as in early userspace when the rootfs is its own parent */ if (id == parent_id) continue; if ((!lastchld_id || id > lastchld_id) && (!*chld || id < chld_id)) { *chld = fs; chld_id = id; } } if (!chld_id) return 1; /* end of iterator */ /* set the iterator to the @chld for the next call */ mnt_table_set_iter(tb, itr, *chld); return 0; }
/** * mnt_update_table: * @upd: update * @lc: lock or NULL * * High-level API to update /etc/mtab (or private /run/mount/utab file). * * The @lc lock is optional and will be created if necessary. Note that * the automatically created lock blocks all signals. * * See also mnt_lock_block_signals() and mnt_context_get_lock(). * * Returns: 0 on success, negative number on error. */ int mnt_update_table(struct libmnt_update *upd, struct libmnt_lock *lc) { struct libmnt_lock *lc0 = lc; int rc = -EINVAL; assert(upd); if (!upd->filename || !upd) return -EINVAL; if (!upd->ready) return 0; DBG(UPDATE, mnt_debug_h(upd, "%s: update tab", upd->filename)); if (upd->fs) { DBG(UPDATE, mnt_fs_print_debug(upd->fs, stderr)); } if (!lc) { lc = mnt_new_lock(upd->filename, 0); if (lc) mnt_lock_block_signals(lc, TRUE); } if (lc && upd->userspace_only) mnt_lock_use_simplelock(lc, TRUE); /* use flock */ if (!upd->fs && upd->target) rc = update_remove_entry(upd, lc); /* umount */ else if (upd->mountflags & MS_MOVE) rc = update_modify_target(upd, lc); /* move */ else if (upd->mountflags & MS_REMOUNT) rc = update_modify_options(upd, lc); /* remount */ else if (upd->fs) rc = update_add_entry(upd, lc); /* mount */ upd->ready = FALSE; DBG(UPDATE, mnt_debug_h(upd, "%s: update tab: done [rc=%d]", upd->filename, rc)); if (lc != lc0) mnt_free_lock(lc); return rc; }
/** * mnt_get_fstype: * @devname: device name * @ambi: returns TRUE if probing result is ambivalent (optional argument) * @cache: cache for results or NULL * * Returns: filesystem type or NULL in case of error. The result has to be * deallocated by free() if @cache is NULL. */ char *mnt_get_fstype(const char *devname, int *ambi, struct libmnt_cache *cache) { blkid_probe pr; const char *data; char *type = NULL; int rc; DBG(CACHE, mnt_debug_h(cache, "get %s FS type", devname)); if (cache) { char *val = NULL; rc = __mnt_cache_find_tag_value(cache, devname, "TYPE", &val); if (ambi) *ambi = rc == -2 ? TRUE : FALSE; return rc ? NULL : val; } /* * no cache, probe directly */ pr = blkid_new_probe_from_filename(devname); if (!pr) return NULL; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE); rc = blkid_do_safeprobe(pr); DBG(CACHE, mnt_debug_h(cache, "liblkid rc=%d", rc)); if (!rc && !blkid_probe_lookup_value(pr, "TYPE", &data, NULL)) type = strdup(data); if (ambi) *ambi = rc == -2 ? TRUE : FALSE; blkid_free_probe(pr); return type; }
/** * mnt_new_table: * * The tab is a container for struct libmnt_fs entries that usually represents a fstab, * mtab or mountinfo file from your system. * * See also mnt_table_parse_file(). * * Returns: newly allocated tab struct. */ struct libmnt_table *mnt_new_table(void) { struct libmnt_table *tb = NULL; tb = calloc(1, sizeof(*tb)); if (!tb) return NULL; DBG(TAB, mnt_debug_h(tb, "alloc")); INIT_LIST_HEAD(&tb->ents); return tb; }