int CheckFileSystemMountingOption()

in src/common/commonutils/MountUtils.c [6:114]


int CheckFileSystemMountingOption(const char* mountFileName, const char* mountDirectory, const char* mountType, const char* desiredOption, char** reason, OsConfigLogHandle log)
{
    FILE* mountFileHandle = NULL;
    struct mntent* mountStruct = NULL;
    bool matchFound = false;
    int lineNumber = 1;
    int status = 0;

    if ((NULL == mountFileName) || ((NULL == mountDirectory) && (NULL == mountType)) || (NULL == desiredOption))
    {
        OsConfigLogError(log, "CheckFileSystemMountingOption called with invalid argument(s)");
        return EINVAL;
    }

    if (!FileExists(mountFileName))
    {
        OsConfigLogInfo(log, "CheckFileSystemMountingOption: file '%s' not found, nothing to check", mountFileName);
        if (OsConfigIsSuccessReason(reason))
        {
            OsConfigCaptureSuccessReason(reason, "'%s' is not found, nothing to check", mountFileName);
        }
        else
        {
            OsConfigCaptureReason(reason, "'%s' is not found", mountFileName);
        }
        return 0;
    }

    if (NULL != (mountFileHandle = setmntent(mountFileName, "r")))
    {
        while (NULL != (mountStruct = getmntent(mountFileHandle)))
        {
            if (((NULL != mountDirectory) && (NULL != mountStruct->mnt_dir) && (NULL != strstr(mountStruct->mnt_dir, mountDirectory))) ||
                ((NULL != mountType) && (NULL != mountStruct->mnt_type) && (NULL != strstr(mountStruct->mnt_type, mountType))))
            {
                matchFound = true;

                if (NULL != hasmntopt(mountStruct, desiredOption))
                {
                    OsConfigLogInfo(log, "CheckFileSystemMountingOption: option '%s' for mount directory '%s' or mount type '%s' found in '%s' at line %d ('%s')",
                        desiredOption, mountDirectory ? mountDirectory : "-", mountType ? mountType : "-", mountFileName, lineNumber, mountStruct->mnt_opts);

                    if (NULL != mountDirectory)
                    {
                        OsConfigCaptureSuccessReason(reason, "Option '%s' for mount directory '%s' found in '%s' at line %d ('%s')",
                            desiredOption, mountDirectory, mountFileName, lineNumber, mountStruct->mnt_opts);
                    }

                    if (NULL != mountType)
                    {
                        OsConfigCaptureSuccessReason(reason, "Option '%s' for mount type '%s' found in '%s' at line %d ('%s')",
                            desiredOption, mountType, mountFileName, lineNumber, mountStruct->mnt_opts);
                    }
                }
                else
                {
                    status = ENOENT;
                    OsConfigLogInfo(log, "CheckFileSystemMountingOption: option '%s' for mount directory '%s' or mount type '%s' missing from file '%s' at line %d ('%s')",
                        desiredOption, mountDirectory ? mountDirectory : "-", mountType ? mountType : "-", mountFileName, lineNumber, mountStruct->mnt_opts);

                    if (NULL != mountDirectory)
                    {
                        OsConfigCaptureReason(reason, "Option '%s' for mount directory '%s' is missing from file '%s' at line %d ('%s')",
                            desiredOption, mountDirectory, mountFileName, lineNumber, mountStruct->mnt_opts);
                    }

                    if (NULL != mountType)
                    {
                        OsConfigCaptureReason(reason, "Option '%s' for mount type '%s' missing from file '%s' at line %d ('%s')",
                            desiredOption, mountType, mountFileName, lineNumber, mountStruct->mnt_opts);
                    }
                }

                OsConfigLogDebug(log, "CheckFileSystemMountingOption, line %d in '%s': mnt_fsname '%s', mnt_dir '%s', mnt_type '%s', mnt_opts '%s', mnt_freq %d, mnt_passno %d",
                    lineNumber, mountFileName, mountStruct->mnt_fsname, mountStruct->mnt_dir, mountStruct->mnt_type, mountStruct->mnt_opts,
                    mountStruct->mnt_freq, mountStruct->mnt_passno);
            }

            lineNumber += 1;
        }

        if (false == matchFound)
        {
            status = 0;
            OsConfigLogInfo(log, "CheckFileSystemMountingOption: mount directory '%s' and/or mount type '%s' not found in '%s'",
                mountDirectory ? mountDirectory : "-", mountType ? mountType : "-", mountFileName);

            if (NULL != mountDirectory)
            {
                OsConfigCaptureSuccessReason(reason, "Found no entries about mount directory '%s' in '%s' to look for option '%s'", mountDirectory, mountFileName, desiredOption);
            }

            if (NULL != mountType)
            {
                OsConfigCaptureSuccessReason(reason, "Found no entries about mount type '%s' in '%s' to look for option '%s'", mountType, mountFileName, desiredOption);
            }
        }

        endmntent(mountFileHandle);
    }
    else
    {
        status = (0 == errno) ? ENOENT : errno;
        OsConfigLogInfo(log, "CheckFileSystemMountingOption: cannot open file '%s', setmntent() failed (%d, errno: %d)", mountFileName, status, errno);
        OsConfigCaptureReason(reason, "Cannot access '%s', setmntent() failed (%d)", mountFileName, status);
    }

    return status;
}