From: Karel Zak Date: Tue, 12 Oct 2010 14:17:16 +0000 (+0200) Subject: libmount: improve assert() and DBG() usage X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b56aae848a7b579b1841b6ec1e47f61f9fa2af7;p=util-linux libmount: improve assert() and DBG() usage Signed-off-by: Karel Zak --- diff --git a/shlibs/mount/src/context.c b/shlibs/mount/src/context.c index 08d4e1e8..86300392 100644 --- a/shlibs/mount/src/context.c +++ b/shlibs/mount/src/context.c @@ -818,16 +818,22 @@ int mnt_context_prepare_srcpath(mnt_context *cxt) const char *t, *v, *src; int rc = 0; + assert(cxt); + assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + if (!cxt || !cxt->fs) return -EINVAL; + DBG(CXT, mnt_debug_h(cxt, "preparing source path")); + src = mnt_fs_get_source(cxt->fs); /* ignore filesystems without a real source */ if (!src || (cxt->fs->flags & (MNT_FS_PSEUDO | MNT_FS_NET))) return 0; - DBG(CXT, mnt_debug_h(cxt, "preparing srcpath '%s'", src)); + DBG(CXT, mnt_debug_h(cxt, "srcpath '%s'", src)); cache = mnt_context_get_cache(cxt); type = mnt_fs_get_fstype(cxt->fs); @@ -882,6 +888,10 @@ int mnt_context_guess_fstype(mnt_context *cxt) const char *dev; int rc = -EINVAL; + assert(cxt); + assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + if (!cxt || !cxt->fs) return -EINVAL; @@ -945,6 +955,7 @@ int mnt_context_prepare_helper(mnt_context *cxt, const char *name, assert(cxt); assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); if (!type) type = mnt_fs_get_fstype(cxt->fs); @@ -989,6 +1000,30 @@ int mnt_context_prepare_helper(mnt_context *cxt, const char *name, return 0; } +int mnt_context_merge_mountflags(mnt_context *cxt) +{ + unsigned long fl = 0; + int rc; + + assert(cxt); + + DBG(CXT, mnt_debug_h(cxt, "merging mount flags")); + + rc = mnt_context_get_mountflags(cxt, &fl); + if (rc) + return rc; + cxt->mountflags = fl; + + fl = 0; + rc = mnt_context_get_userspace_mountflags(cxt, &fl); + if (rc) + return rc; + cxt->user_mountflags = fl; + + cxt->flags |= MNT_FL_MOUNTFLAGS_MERGED; + return 0; +} + /* * Prepare /etc/mtab or /var/run/mount/mountinfo update */ @@ -997,6 +1032,10 @@ int mnt_context_prepare_update(mnt_context *cxt, int act) int rc; const char *tgt = cxt->fs ? mnt_fs_get_target(cxt->fs) : NULL; + assert(cxt); + assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + if (act == MNT_ACT_UMOUNT && tgt && !strcmp(tgt, "/")) /* Don't try to touch mtab if umounting root FS */ cxt->flags |= MNT_FL_NOMTAB; @@ -1047,6 +1086,9 @@ static int apply_tab(mnt_context *cxt, mnt_tab *tb, int direction) const char *src = NULL, *tgt = NULL; int rc; + assert(cxt); + assert(cxt->fs); + if (!cxt->fs) return -EINVAL; @@ -1107,12 +1149,17 @@ int mnt_context_apply_fstab(mnt_context *cxt) mnt_tab *fstab, *mtab; const char *src = NULL, *tgt = NULL; + assert(cxt); + assert(cxt->fs); + if (!cxt || !cxt->fs) return -EINVAL; if (cxt->flags & MNT_FL_TAB_APPLIED) return 0; + DBG(CXT, mnt_debug_h(cxt, "appling fstab")); + if (cxt->fs) { src = mnt_fs_get_source(cxt->fs); tgt = mnt_fs_get_target(cxt->fs); @@ -1209,7 +1256,7 @@ int test_mount(struct mtest *ts, int argc, char *argv[]) if (rc) printf("failed to mount\n"); else { - printf("successfully mounted"); + printf("successfully mounted\n"); rc = mnt_context_post_mount(cxt); if (rc) printf("mtab update failed\n"); @@ -1272,7 +1319,7 @@ int test_umount(struct mtest *ts, int argc, char *argv[]) if (rc) printf("failed to umount\n"); else { - printf("successfully umounted"); + printf("successfully umounted\n"); rc = mnt_context_post_umount(cxt); if (rc) printf("mtab update failed\n"); diff --git a/shlibs/mount/src/context_mount.c b/shlibs/mount/src/context_mount.c index 4bfdb267..e12e0a93 100644 --- a/shlibs/mount/src/context_mount.c +++ b/shlibs/mount/src/context_mount.c @@ -34,12 +34,16 @@ static int fix_optstr(mnt_context *cxt) char *name, *val; size_t namesz, valsz; + assert(cxt); + assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + if (!cxt) return -EINVAL; if (!cxt->fs) return 0; - assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + DBG(CXT, mnt_debug_h(cxt, "mount: fixing optstr")); /* * we directly work with optstr pointer here @@ -52,7 +56,7 @@ static int fix_optstr(mnt_context *cxt) if (cxt->mountflags & MS_PROPAGATION) cxt->mountflags &= MS_PROPAGATION; - if (!mnt_optstr_get_option(*optstr, "user", &val, &valsz)) { + if (*optstr && !mnt_optstr_get_option(*optstr, "user", &val, &valsz)) { if (val) { cxt->orig_user = strndup(val, valsz); if (!cxt->orig_user) { @@ -159,11 +163,17 @@ static int evaluate_permissions(mnt_context *cxt) unsigned long u_flags; const char *srcpath; + assert(cxt); + assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + if (!cxt) return -EINVAL; if (!cxt->fs) return 0; + DBG(CXT, mnt_debug_h(cxt, "mount: evaluating permissions")); + mnt_context_get_userspace_mountflags(cxt, &u_flags); if (!mnt_context_is_restricted(cxt)) { @@ -215,26 +225,6 @@ static int evaluate_permissions(mnt_context *cxt) return 0; } -static int merge_mountflags(mnt_context *cxt) -{ - unsigned long fl = 0; - int rc; - - rc = mnt_context_get_mountflags(cxt, &fl); - if (rc) - return rc; - cxt->mountflags = fl; - - fl = 0; - rc = mnt_context_get_userspace_mountflags(cxt, &fl); - if (rc) - return rc; - cxt->user_mountflags = fl; - - cxt->flags |= MNT_FL_MOUNTFLAGS_MERGED; - return 0; -} - static int exec_helper(mnt_context *cxt) { char *o = NULL; @@ -243,6 +233,9 @@ static int exec_helper(mnt_context *cxt) assert(cxt); assert(cxt->fs); assert(cxt->helper); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + + DBG(CXT, mnt_debug_h(cxt, "mount: executing helper %s", cxt->helper)); rc = generate_helper_optstr(cxt, &o); if (rc) @@ -328,6 +321,7 @@ static int do_mount(mnt_context *cxt, const char *try_type) 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); @@ -396,9 +390,11 @@ int mnt_context_prepare_mount(mnt_context *cxt) !mnt_fs_get_target(cxt->fs))) return -EINVAL; + DBG(CXT, mnt_debug_h(cxt, "mount: preparing")); + rc = mnt_context_apply_fstab(cxt); if (!rc) - rc = merge_mountflags(cxt); + rc = mnt_context_merge_mountflags(cxt); if (!rc) rc = evaluate_permissions(cxt); if (!rc) @@ -436,9 +432,15 @@ int mnt_context_do_mount(mnt_context *cxt) int rc = -EINVAL; const char *type; + assert(cxt); + assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + if (!cxt || !cxt->fs || (cxt->fs->flags & MNT_FS_SWAP)) return -EINVAL; + DBG(CXT, mnt_debug_h(cxt, "mount: do mount")); + if (!(cxt->flags & MNT_FL_MOUNTDATA)) cxt->mountdata = (char *) mnt_fs_get_fs_optstr(cxt->fs); @@ -470,6 +472,10 @@ int mnt_context_post_mount(mnt_context *cxt) { int rc = 0; + assert(cxt); + assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + if (!cxt) return -EINVAL; /* diff --git a/shlibs/mount/src/context_umount.c b/shlibs/mount/src/context_umount.c index 1222fe6f..b2590be9 100644 --- a/shlibs/mount/src/context_umount.c +++ b/shlibs/mount/src/context_umount.c @@ -28,6 +28,9 @@ static int lookup_umount_fs(mnt_context *cxt) mnt_tab *mtab; mnt_fs *fs; + assert(cxt); + assert(cxt->fs); + tgt = mnt_fs_get_target(cxt->fs); if (!tgt) { DBG(CXT, mnt_debug_h(cxt, "umount: undefined target")); @@ -135,6 +138,10 @@ static int evaluate_permissions(mnt_context *cxt) int rc, ok = 0; mnt_fs *fs; + assert(cxt); + assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); + if (!cxt || !cxt->fs) return -EINVAL; @@ -148,9 +155,9 @@ static int evaluate_permissions(mnt_context *cxt) goto eperm; } - mnt_context_get_userspace_mountflags(cxt, &u_flags); + if (!(cxt->flags & MNT_FL_NOHELPERS) && + (cxt->user_mountflags & MNT_MS_UHELPER)) { - if (!(cxt->flags & MNT_FL_NOHELPERS) && (u_flags & MNT_MS_UHELPER)) { char *suffix = NULL; char *o = (char *) mnt_fs_get_optstr(cxt->fs); size_t valsz; @@ -216,7 +223,7 @@ static int evaluate_permissions(mnt_context *cxt) * The options `user', `owner' and `group' only allow unmounting by the * user that mounted (visible in mtab). */ - optstr = mnt_fs_get_optstr(fs); + optstr = mnt_fs_get_optstr(fs); /* FSTAB mount options! */ if (!optstr) goto eperm; @@ -267,6 +274,7 @@ static int exec_helper(mnt_context *cxt) assert(cxt); assert(cxt->fs); assert(cxt->helper); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); DBG_FLUSH; @@ -341,6 +349,7 @@ static int do_umount(mnt_context *cxt) assert(cxt); assert(cxt->fs); + assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED)); if (cxt->helper) return exec_helper(cxt); @@ -431,6 +440,8 @@ int mnt_context_prepare_umount(mnt_context *cxt) cxt->helper = NULL; rc = lookup_umount_fs(cxt); + if (!rc) + rc = mnt_context_merge_mountflags(cxt); if (!rc) rc = evaluate_permissions(cxt); if (!rc && !cxt->helper) diff --git a/shlibs/mount/src/mountP.h b/shlibs/mount/src/mountP.h index 9f05cf55..7d66f0ba 100644 --- a/shlibs/mount/src/mountP.h +++ b/shlibs/mount/src/mountP.h @@ -52,7 +52,7 @@ # define DBG(m,x) do { \ if ((MNT_DEBUG_ ## m) & libmount_debug_mask) {\ - fprintf(stderr, "libmount: %s: ", # m); \ + fprintf(stderr, "libmount: %8s: ", # m); \ x; \ } \ } while(0) @@ -288,5 +288,6 @@ extern int mnt_context_guess_fstype(mnt_context *cxt); extern int mnt_context_prepare_helper(mnt_context *cxt, const char *name, const char *type); extern int mnt_context_prepare_update(mnt_context *cxt, int act); extern mnt_fs *mnt_context_get_fs(mnt_context *cxt); +extern int mnt_context_merge_mountflags(mnt_context *cxt); #endif /* _LIBMOUNT_PRIVATE_H */ diff --git a/shlibs/mount/src/tab_update.c b/shlibs/mount/src/tab_update.c index 8b322dde..b027a552 100644 --- a/shlibs/mount/src/tab_update.c +++ b/shlibs/mount/src/tab_update.c @@ -596,7 +596,7 @@ int mnt_prepare_update(mnt_update *upd) return -EINVAL; DBG(UPDATE, mnt_debug_h(upd, - "prepare update (target %s, source %s, optstr %s)", + "prepare update (target=%s, source=%s, optstr=%s)", mnt_fs_get_target(upd->fs), mnt_fs_get_source(upd->fs), mnt_fs_get_optstr(upd->fs))); @@ -616,6 +616,9 @@ int mnt_prepare_update(mnt_update *upd) goto err; } } + + DBG(UPDATE, mnt_debug_h(upd, "filename: %s", upd->filename)); + if (!upd->format) { if (endswith(upd->filename, "mountinfo")) upd->format = MNT_FMT_MOUNTINFO;