openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/util-linux/2.33.2-r0/util-linux-2.33.2/libmount/src/tab.c
int mnt_table_is_fs_mounted(struct libmnt_table* tb, struct libmnt_fs* fstab_fs)
{
    struct libmnt_iter itr;
    struct libmnt_fs* fs;

    char* root = NULL;
    char* src2 = NULL;
    const char *src = NULL, *tgt = NULL;
    char* xtgt = NULL;
    int rc = 0;
    dev_t devno = 0;

    DBG(FS,
        ul_debugobj(fstab_fs, "mnt_table_is_fs_mounted: target=%s, source=%s",
                    mnt_fs_get_target(fstab_fs), mnt_fs_get_source(fstab_fs)));

    if (mnt_fs_is_swaparea(fstab_fs) || mnt_table_is_empty(tb))
    {
        DBG(FS, ul_debugobj(fstab_fs, "- ignore (swap or no data)"));
        return 0;
    }

    if (is_mountinfo(tb))
    {
        /* @tb is mountinfo, so we can try to use fs-roots */
        struct libmnt_fs* rootfs;
        int flags = 0;

        if (mnt_fs_get_option(fstab_fs, "bind", NULL, NULL) == 0 ||
            mnt_fs_get_option(fstab_fs, "rbind", NULL, NULL) == 0)
            flags = MS_BIND;

        rootfs = mnt_table_get_fs_root(tb, fstab_fs, flags, &root);
        if (rootfs)
        {
            const char* fstype = mnt_fs_get_fstype(rootfs);

            src = mnt_fs_get_srcpath(rootfs);
            if (fstype && strncmp(fstype, "nfs", 3) == 0 && root)
            {
                /* NFS stores the root at the end of the source */
                src = src2 = strappend(src, root);
                free(root);
                root = NULL;
            }
        }
    }

    if (!src)
        src = mnt_fs_get_source(fstab_fs);

    if (src && tb->cache && !mnt_fs_is_pseudofs(fstab_fs))
        src = mnt_resolve_spec(src, tb->cache);

    if (src && root)
    {
        struct stat st;

        devno = mnt_fs_get_devno(fstab_fs);
        if (!devno && stat(src, &st) == 0 && S_ISBLK(st.st_mode))
            devno = st.st_rdev;
    }

    tgt = mnt_fs_get_target(fstab_fs);

    if (!tgt || !src)
    {
        DBG(FS, ul_debugobj(fstab_fs, "- ignore (no source/target)"));
        goto done;
    }
    mnt_reset_iter(&itr, MNT_ITER_FORWARD);

    DBG(FS, ul_debugobj(fstab_fs,
                        "mnt_table_is_fs_mounted: src=%s, tgt=%s, root=%s", src,
                        tgt, root));

    while (mnt_table_next_fs(tb, &itr, &fs) == 0)
    {

        int eq = mnt_fs_streq_srcpath(fs, src);

        if (!eq && devno && mnt_fs_get_devno(fs) == devno)
            eq = 1;

        if (!eq)
        {
            /* The source does not match. Maybe the source is a loop
             * device backing file.
             */
            uint64_t offset = 0;
            char* val;
            size_t len;
            int flags = 0;

            if (!mnt_fs_get_srcpath(fs) ||
                !startswith(mnt_fs_get_srcpath(fs), "/dev/loop"))
                continue; /* does not look like loopdev */

            if (mnt_fs_get_option(fstab_fs, "offset", &val, &len) == 0)
            {
                if (mnt_parse_offset(val, len, &offset))
                {
                    DBG(FS, ul_debugobj(fstab_fs, "failed to parse offset="));
                    continue;
                }
                flags = LOOPDEV_FL_OFFSET;
            }

            DBG(FS, ul_debugobj(fs, "checking for loop: src=%s",
                                mnt_fs_get_srcpath(fs)));
#if __linux__
            if (!loopdev_is_used(mnt_fs_get_srcpath(fs), src, offset, 0, flags))
                continue;

            DBG(FS, ul_debugobj(fs, "used loop"));
#endif
        }

        if (root)
        {
            const char* fstype = mnt_fs_get_fstype(fs);

            if (fstype && strcmp(fstype, "cifs") == 0)
            {
                const char* unc_subdir = get_cifs_unc_subdir_path(src);
                const char* path_on_fs = mnt_fs_get_root(fs);
                if (!unc_subdir || !path_on_fs ||
                    !streq_paths(unc_subdir, path_on_fs))
                    continue;
            }
            else
            {
                const char* r = mnt_fs_get_root(fs);
                if (!r || strcmp(r, root) != 0)
                    continue;
            }
        }

        /*
         * Compare target, try to minimize the number of situations when we
         * need to canonicalize the path to avoid readlink() on
         * mountpoints.
         */
        if (!xtgt)
        {
            if (mnt_fs_streq_target(fs, tgt))
                break;
            if (tb->cache)
                xtgt = mnt_resolve_path(tgt, tb->cache);
        }
        if (xtgt && mnt_fs_streq_target(fs, xtgt))
            break;
    }

    if (fs)
        rc = 1; /* success */
done:
    free(root);

    DBG(TAB, ul_debugobj(tb, "mnt_table_is_fs_mounted: %s [rc=%d]", src, rc));
    free(src2);
    return rc;
}
/**
 * mnt_table_is_fs_mounted:
 * @tb: /proc/self/mountinfo file
 * @fstab_fs: /etc/fstab entry
 *
 * Checks if the @fstab_fs entry is already in the @tb table. The "swap" is
 * ignored. This function explicitly compares the source, target and root of the
 * filesystems.
 *
 * Note that source and target are canonicalized only if a cache for @tb is
 * defined (see mnt_table_set_cache()). The target canonicalization may
 * trigger automount on autofs mountpoints!
 *
 * Don't use it if you want to know if a device is mounted, just use
 * mnt_table_find_source() on the device.
 *
 * This function is designed mostly for "mount -a".
 *
 * Returns: 0 or 1
 */


Checks if the @fstab_fs entry is already in the @tb table. The "swap" is
ignored. This function explicitly compares the source, target and root of the
filesystems.

Note that source and target are canonicalized only if a cache for @tb is
defined ). The target canonicalization may
trigger automount on autofs mountpoints!

Don't use it if you want to know if a device is mounted, just use
mnt_table_find_source() on the device.

This function is designed mostly for "mount -a".

0 or 1
Checks if the @fstab_fs entry is already in the @tb table . The " swap " is ignored . This function explicitly compares the source target and root of the filesystems . Note that source and target are canonicalized only if a cache for @tb is defined ) . The target canonicalization may trigger automount on autofs mountpoints! Don ' t use it if you want to know if a device is mounted just use mnt_table_find_source () on the device . This function is designed mostly for " mount - a " . 0 or 1
int mnt_table_is_fs_mounted(struct libmnt_table* tb, struct libmnt_fs* fstab_fs)
{
    struct libmnt_iter itr;
    struct libmnt_fs* fs;
    char* root = NULL;
    char* src2 = NULL;
    const char *src = NULL, *tgt = NULL;
    char* xtgt = NULL;
    int rc = 0;
    dev_t devno = 0;
    DBG(FS,
        ul_debugobj(fstab_fs, "mnt_table_is_fs_mounted: target=%s, source=%s",
                    mnt_fs_get_target(fstab_fs), mnt_fs_get_source(fstab_fs)));
    if (mnt_fs_is_swaparea(fstab_fs) || mnt_table_is_empty(tb))
    {
        DBG(FS, ul_debugobj(fstab_fs, "- ignore (swap or no data)"));
        return 0;
    }
    if (is_mountinfo(tb))
    {
        struct libmnt_fs* rootfs;
        int flags = 0;
        if (mnt_fs_get_option(fstab_fs, "bind", NULL, NULL) == 0 ||
            mnt_fs_get_option(fstab_fs, "rbind", NULL, NULL) == 0)
            flags = MS_BIND;
        rootfs = mnt_table_get_fs_root(tb, fstab_fs, flags, &root);
        if (rootfs)
        {
            const char* fstype = mnt_fs_get_fstype(rootfs);
            src = mnt_fs_get_srcpath(rootfs);
            if (fstype && strncmp(fstype, "nfs", 3) == 0 && root)
            {
                src = src2 = strappend(src, root);
                free(root);
                root = NULL;
            }
        }
    }
    if (!src)
        src = mnt_fs_get_source(fstab_fs);
    if (src && tb->cache && !mnt_fs_is_pseudofs(fstab_fs))
        src = mnt_resolve_spec(src, tb->cache);
    if (src && root)
    {
        struct stat st;
        devno = mnt_fs_get_devno(fstab_fs);
        if (!devno && stat(src, &st) == 0 && S_ISBLK(st.st_mode))
            devno = st.st_rdev;
    }
    tgt = mnt_fs_get_target(fstab_fs);
    if (!tgt || !src)
    {
        DBG(FS, ul_debugobj(fstab_fs, "- ignore (no source/target)"));
        goto done;
    }
    mnt_reset_iter(&itr, MNT_ITER_FORWARD);
    DBG(FS, ul_debugobj(fstab_fs,
                        "mnt_table_is_fs_mounted: src=%s, tgt=%s, root=%s", src,
                        tgt, root));
    while (mnt_table_next_fs(tb, &itr, &fs) == 0)
    {
        int eq = mnt_fs_streq_srcpath(fs, src);
        if (!eq && devno && mnt_fs_get_devno(fs) == devno)
            eq = 1;
        if (!eq)
        {
            uint64_t offset = 0;
            char* val;
            size_t len;
            int flags = 0;
            if (!mnt_fs_get_srcpath(fs) ||
                !startswith(mnt_fs_get_srcpath(fs), "/dev/loop"))
                continue; 
            if (mnt_fs_get_option(fstab_fs, "offset", &val, &len) == 0)
            {
                if (mnt_parse_offset(val, len, &offset))
                {
                    DBG(FS, ul_debugobj(fstab_fs, "failed to parse offset="));
                    continue;
                }
                flags = LOOPDEV_FL_OFFSET;
            }
            DBG(FS, ul_debugobj(fs, "checking for loop: src=%s",
                                mnt_fs_get_srcpath(fs)));
#if __linux__
            if (!loopdev_is_used(mnt_fs_get_srcpath(fs), src, offset, 0, flags))
                continue;
            DBG(FS, ul_debugobj(fs, "used loop"));
#endif
        }
        if (root)
        {
            const char* fstype = mnt_fs_get_fstype(fs);
            if (fstype && strcmp(fstype, "cifs") == 0)
            {
                const char* unc_subdir = get_cifs_unc_subdir_path(src);
                const char* path_on_fs = mnt_fs_get_root(fs);
                if (!unc_subdir || !path_on_fs ||
                    !streq_paths(unc_subdir, path_on_fs))
                    continue;
            }
            else
            {
                const char* r = mnt_fs_get_root(fs);
                if (!r || strcmp(r, root) != 0)
                    continue;
            }
        }
        if (!xtgt)
        {
            if (mnt_fs_streq_target(fs, tgt))
                break;
            if (tb->cache)
                xtgt = mnt_resolve_path(tgt, tb->cache);
        }
        if (xtgt && mnt_fs_streq_target(fs, xtgt))
            break;
    }
    if (fs)
        rc = 1; 
done:
    free(root);
    DBG(TAB, ul_debugobj(tb, "mnt_table_is_fs_mounted: %s [rc=%d]", src, rc));
    free(src2);
    return rc;
}
int mnt_table_is_fs_mounted ( struct libmnt_table * tb , struct libmnt_fs * fstab_fs ) { struct libmnt_iter itr ; struct libmnt_fs * fs ; char * root = NULL ; char * src2 = NULL ; const char * src = NULL , * tgt = NULL ; char * xtgt = NULL ; int rc = 0 ; dev_t devno = 0 ; DBG ( FS , ul_debugobj ( fstab_fs , " " , mnt_fs_get_target ( fstab_fs ) , mnt_fs_get_source ( fstab_fs ) ) ) ; if ( mnt_fs_is_swaparea ( fstab_fs ) || mnt_table_is_empty ( tb ) ) { DBG ( FS , ul_debugobj ( fstab_fs , " " ) ) ; return 0 ; } if ( is_mountinfo ( tb ) ) { struct libmnt_fs * rootfs ; int flags = 0 ; if ( mnt_fs_get_option ( fstab_fs , " " , NULL , NULL ) == 0 || mnt_fs_get_option ( fstab_fs , " " , NULL , NULL ) == 0 ) flags = MS_BIND ; rootfs = mnt_table_get_fs_root ( tb , fstab_fs , flags , & root ) ; if ( rootfs ) { const char * fstype = mnt_fs_get_fstype ( rootfs ) ; src = mnt_fs_get_srcpath ( rootfs ) ; if ( fstype && strncmp ( fstype , " " , 3 ) == 0 && root ) { src = src2 = strappend ( src , root ) ; free ( root ) ; root = NULL ; } } } if ( ! src ) src = mnt_fs_get_source ( fstab_fs ) ; if ( src && tb -> cache && ! mnt_fs_is_pseudofs ( fstab_fs ) ) src = mnt_resolve_spec ( src , tb -> cache ) ; if ( src && root ) { struct stat st ; devno = mnt_fs_get_devno ( fstab_fs ) ; if ( ! devno && stat ( src , & st ) == 0 && S_ISBLK ( st . st_mode ) ) devno = st . st_rdev ; } tgt = mnt_fs_get_target ( fstab_fs ) ; if ( ! tgt || ! src ) { DBG ( FS , ul_debugobj ( fstab_fs , " " ) ) ; goto done ; } mnt_reset_iter ( & itr , MNT_ITER_FORWARD ) ; DBG ( FS , ul_debugobj ( fstab_fs , " " , src , tgt , root ) ) ; while ( mnt_table_next_fs ( tb , & itr , & fs ) == 0 ) { int eq = mnt_fs_streq_srcpath ( fs , src ) ; if ( ! eq && devno && mnt_fs_get_devno ( fs ) == devno ) eq = 1 ; if ( ! eq ) { uint64_t offset = 0 ; char * val ; size_t len ; int flags = 0 ; if ( ! mnt_fs_get_srcpath ( fs ) || ! startswith ( mnt_fs_get_srcpath ( fs ) , " " ) ) continue ; if ( mnt_fs_get_option ( fstab_fs , " " , & val , & len ) == 0 ) { if ( mnt_parse_offset ( val , len , & offset ) ) { DBG ( FS , ul_debugobj ( fstab_fs , " " ) ) ; continue ; } flags = LOOPDEV_FL_OFFSET ; } DBG ( FS , ul_debugobj ( fs , " " , mnt_fs_get_srcpath ( fs ) ) ) ; #if __linux__ if ( ! loopdev_is_used ( mnt_fs_get_srcpath ( fs ) , src , offset , 0 , flags ) ) continue ; DBG ( FS , ul_debugobj ( fs , " " ) ) ; #endif } if ( root ) { const char * fstype = mnt_fs_get_fstype ( fs ) ; if ( fstype && strcmp ( fstype , " " ) == 0 ) { const char * unc_subdir = get_cifs_unc_subdir_path ( src ) ; const char * path_on_fs = mnt_fs_get_root ( fs ) ; if ( ! unc_subdir || ! path_on_fs || ! streq_paths ( unc_subdir , path_on_fs ) ) continue ; } else { const char * r = mnt_fs_get_root ( fs ) ; if ( ! r || strcmp ( r , root ) != 0 ) continue ; } } if ( ! xtgt ) { if ( mnt_fs_streq_target ( fs , tgt ) ) break ; if ( tb -> cache ) xtgt = mnt_resolve_path ( tgt , tb -> cache ) ; } if ( xtgt && mnt_fs_streq_target ( fs , xtgt ) ) break ; } if ( fs ) rc = 1 ; done : free ( root ) ; DBG ( TAB , ul_debugobj ( tb , " " , src , rc ) ) ; free ( src2 ) ; return rc ; }
mnt_table_is_fs_mounted:
 @tb: /proc/self/mountinfo file
 @fstab_fs: /etc/fstab entry
mnt_table_is_fs_mounted : @tb : / proc / self / mountinfo file @fstab_fs : / etc / fstab entry
/* @tb is mountinfo, so we can try to use fs-roots */ /* NFS stores the root at the end of the source */ /* The source does not match. Maybe the source is a loop
             * device backing file.
             */ /* does not look like loopdev */ /*
         * Compare target, try to minimize the number of situations when we
         * need to canonicalize the path to avoid readlink() on
         * mountpoints.
         */ /* success */