From eb40a7a4be8c04b527fb712106c345b770babe36 Mon Sep 17 00:00:00 2001 From: Jan Synacek Date: Tue, 2 Jul 2013 13:47:00 +0200 Subject: LogicalFile: use udev to fill info about the filesystem --- openlmi-providers.spec | 3 ++ src/logicalfile/CMakeLists.txt | 6 ++-- .../LMI_DirectoryContainsFileProvider.c | 33 ++++++++++++++--- src/logicalfile/LMI_FileIdentityProvider.c | 29 +++++++++++---- src/logicalfile/LMI_RootDirectoryProvider.c | 15 ++++++-- src/logicalfile/file.c | 42 ++++++++++++++++++++++ src/logicalfile/file.h | 10 +++--- 7 files changed, 116 insertions(+), 22 deletions(-) diff --git a/openlmi-providers.spec b/openlmi-providers.spec index 1770ca8..f323427 100644 --- a/openlmi-providers.spec +++ b/openlmi-providers.spec @@ -24,6 +24,8 @@ BuildRequires: libuser-devel BuildRequires: python2-devel # For openlmi-hardware BuildRequires: pciutils-devel +# For openlmi-logicalfile +BuildRequires: libudev-devel # For openlmi-register-pegasus script Requires: python2 # sblim-sfcb or tog-pegasus @@ -145,6 +147,7 @@ SFCB were tested. Summary: CIM provider for reading files and directories Requires: %{name}%{?_isa} = %{version}-%{release} Requires: sblim-cmpi-base +Requires: systemd-libs %description -n openlmi-logicalfile %{summary}. diff --git a/src/logicalfile/CMakeLists.txt b/src/logicalfile/CMakeLists.txt index 7f3c05d..c0d6b25 100644 --- a/src/logicalfile/CMakeLists.txt +++ b/src/logicalfile/CMakeLists.txt @@ -19,10 +19,10 @@ add_library(${LIBRARY_NAME} SHARED ${CIM_HEADERS} ) -#pkg_check_modules(LIBPCI REQUIRED libpci) +pkg_check_modules(LIBUDEV REQUIRED libudev) -include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMPI_INCLUDE_DIR} ${LIBPCI_INCLUDE_DIR}) -target_link_libraries(${LIBRARY_NAME} openlmicommon ${KONKRETCMPI_LIBRARIES} ${LIBPCI_LIBRARIES}) +include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMPI_INCLUDE_DIR}) +target_link_libraries(${LIBRARY_NAME} openlmicommon ${KONKRETCMPI_LIBRARIES} ${LIBUDEV_LIBRARIES}) # Create registration file cim_registration(${PROVIDER_NAME} ${LIBRARY_NAME} ${MOF} share/openlmi-providers) diff --git a/src/logicalfile/LMI_DirectoryContainsFileProvider.c b/src/logicalfile/LMI_DirectoryContainsFileProvider.c index d7dda12..339c486 100644 --- a/src/logicalfile/LMI_DirectoryContainsFileProvider.c +++ b/src/logicalfile/LMI_DirectoryContainsFileProvider.c @@ -56,6 +56,7 @@ static CMPIStatus dir_file_objectpaths( char rpath[BUFLEN]; char fileclass[BUFLEN]; char *aux = strdup(path); + char *fsname; if (!strcmp(de->d_name, "..")) { strncpy(rpath, dirname(aux), BUFLEN); @@ -72,15 +73,19 @@ static CMPIStatus dir_file_objectpaths( CMReturnWithChars(_cb, CMPI_RC_ERR_NOT_FOUND, err); } else { get_class_from_stat(&sb, fileclass); + if (get_fsname_from_stat(&sb, &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } } CIM_LogicalFileRef cim_lfr; CIM_LogicalFileRef_Init(&cim_lfr, _cb, namespace); - fill_logicalfile(CIM_LogicalFileRef, &cim_lfr, rpath, fileclass); + fill_logicalfile(CIM_LogicalFileRef, &cim_lfr, rpath, fsname, fileclass); o = CIM_LogicalFileRef_ToObjectPath(&cim_lfr, &st); CMSetClassName(o, fileclass); ops[i++] = o; + free(fsname); } *count = i; CMReturn(CMPI_RC_OK); @@ -163,7 +168,12 @@ static CMPIStatus associators( CIM_DirectoryRef lmi_dr; CIM_DirectoryRef_Init(&lmi_dr, _cb, ns); char *aux = strdup(path); - fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dirname(aux), LMI_UnixDirectory_ClassName); + char *dir = dirname(aux); + char *fsname; + if (get_fsname_from_path(dir, &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } + fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dir, fsname, LMI_UnixDirectory_ClassName); o = CIM_DirectoryRef_ToObjectPath(&lmi_dr, &st); CMSetClassName(o, LMI_UnixDirectory_ClassName); @@ -174,6 +184,7 @@ static CMPIStatus associators( CMReturnInstance(cr, ci); } free(aux); + free(fsname); } CMReturn(CMPI_RC_OK); } @@ -219,10 +230,14 @@ static CMPIStatus references( const char *ccname = KChars(cd.value.string); pathd = CMGetKey(cop, "Name", &st); const char *path = KChars(pathd.value.string); + char *fsname; + if (get_fsname_from_path(path, &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } if (!strcmp(ccname, LMI_UnixDirectory_ClassName)) { /* got GroupComponent - DirectoryRef */ - fill_logicalfile(CIM_DirectoryRef, &lmi_dr, path, LMI_UnixDirectory_ClassName); + fill_logicalfile(CIM_DirectoryRef, &lmi_dr, path, fsname, LMI_UnixDirectory_ClassName); o = CIM_DirectoryRef_ToObjectPath(&lmi_dr, &st); CMSetClassName(o, LMI_UnixDirectory_ClassName); LMI_DirectoryContainsFile_SetObjectPath_GroupComponent(&lmi_dcf, o); @@ -249,14 +264,20 @@ static CMPIStatus references( } } else { /* got PartComponent - LogicalFileRef */ - fill_logicalfile(CIM_LogicalFileRef, &lmi_lfr, path, ccname); + fill_logicalfile(CIM_LogicalFileRef, &lmi_lfr, path, fsname, ccname); o = CIM_LogicalFileRef_ToObjectPath(&lmi_lfr, &st); CMSetClassName(o, ccname); LMI_DirectoryContainsFile_SetObjectPath_PartComponent(&lmi_dcf, o); /* GroupComponent */ char *aux = strdup(path); - fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dirname(aux), LMI_UnixDirectory_ClassName); + char *dir = dirname(aux); + char *fsname; + if (get_fsname_from_path(dir, &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } + + fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dir, fsname, LMI_UnixDirectory_ClassName); o = CIM_DirectoryRef_ToObjectPath(&lmi_dr, &st); CMSetClassName(o, LMI_UnixDirectory_ClassName); LMI_DirectoryContainsFile_SetObjectPath_GroupComponent(&lmi_dcf, o); @@ -268,7 +289,9 @@ static CMPIStatus references( CMReturnInstance(cr, ci); } free(aux); + free(fsname); } + free(fsname); CMReturn(CMPI_RC_OK); } diff --git a/src/logicalfile/LMI_FileIdentityProvider.c b/src/logicalfile/LMI_FileIdentityProvider.c index 86f3b7f..cf81504 100644 --- a/src/logicalfile/LMI_FileIdentityProvider.c +++ b/src/logicalfile/LMI_FileIdentityProvider.c @@ -67,6 +67,8 @@ static CMPIStatus associators( const char *ns = KNameSpace(cop); CMPIData pathd; CMPIData cd; + char fileclass[BUFLEN]; + char *fsname; /* allow only LMI_UnixFile and classes derived from CIM_LogicalFile */ if (!check_valid_classes(cop)) { @@ -85,8 +87,10 @@ static CMPIStatus associators( cd = CMGetKey(cop, "LFCreationClassName", &st); const char *path = KChars(pathd.value.string); const char *ccname = KChars(cd.value.string); - char fileclass[BUFLEN]; get_class_from_path(path, fileclass); + if (get_fsname_from_path(path, &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } /* TODO is this error necessary? */ if (strcmp(fileclass, ccname)) { @@ -96,7 +100,7 @@ static CMPIStatus associators( CIM_LogicalFileRef cim_lfr; CIM_LogicalFileRef_Init(&cim_lfr, _cb, ns); - fill_logicalfile(CIM_LogicalFileRef, &cim_lfr, path, ccname); + fill_logicalfile(CIM_LogicalFileRef, &cim_lfr, path, fsname, ccname); o = CIM_LogicalFileRef_ToObjectPath(&cim_lfr, &st); CMSetClassName(o, fileclass); } else { @@ -110,8 +114,10 @@ static CMPIStatus associators( pathd = CMGetKey(cop, "Name", &st); cd = CMGetKey(cop, "CreationClassName", &st); const char *path = KChars(pathd.value.string); - char fileclass[BUFLEN]; get_class_from_path(path, fileclass); + if (get_fsname_from_path(path, &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } LMI_UnixFile lmi_uf; LMI_UnixFile_Init(&lmi_uf, _cb, ns); @@ -119,7 +125,7 @@ static CMPIStatus associators( LMI_UnixFile_Set_CSCreationClassName(&lmi_uf, get_system_creation_class_name()); LMI_UnixFile_Set_CSName(&lmi_uf, get_system_name()); LMI_UnixFile_Set_FSCreationClassName(&lmi_uf, FSCREATIONCLASSNAME); - LMI_UnixFile_Set_FSName(&lmi_uf, FSNAME); + LMI_UnixFile_Set_FSName(&lmi_uf, fsname); LMI_UnixFile_Set_LFCreationClassName(&lmi_uf, fileclass); o = LMI_UnixFile_ToObjectPath(&lmi_uf, &st); } @@ -130,6 +136,7 @@ static CMPIStatus associators( ci = _cb->bft->getInstance(_cb, cc, o, properties, &st); res = CMReturnInstance(cr, ci); } + free(fsname); return res; } @@ -150,6 +157,8 @@ static CMPIStatus references( const char *ns = KNameSpace(cop); CMPIInstance *ci; CMPIObjectPath *o; + char fileclass[BUFLEN]; + char *fsname; /* * allow only LMI_UnixFile and classes derived from CIM_LogicalFile @@ -176,8 +185,10 @@ static CMPIStatus references( cd = CMGetKey(cop, "LFCreationClassName", &st); const char *path = KChars(pathd.value.string); const char *ccname = KChars(cd.value.string); - char fileclass[BUFLEN]; get_class_from_path(path, fileclass); + if (get_fsname_from_path(path, &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } /* TODO is this error necessary? */ if (strcmp(fileclass, ccname)) { @@ -188,7 +199,7 @@ static CMPIStatus references( /* SystemElement */ CIM_LogicalFileRef lmi_lfr; CIM_LogicalFileRef_Init(&lmi_lfr, _cb, ns); - fill_logicalfile(CIM_LogicalFileRef, &lmi_lfr, path, ccname); + fill_logicalfile(CIM_LogicalFileRef, &lmi_lfr, path, fsname, ccname); o = CIM_LogicalFileRef_ToObjectPath(&lmi_lfr, &st); CMSetClassName(o, fileclass); LMI_FileIdentity_SetObjectPath_SystemElement(&lmi_fi, o); @@ -205,6 +216,9 @@ static CMPIStatus references( cd = CMGetKey(cop, "CreationClassName", &st); const char *path = KChars(pathd.value.string); const char *ccname = KChars(cd.value.string); + if (get_fsname_from_path(path, &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } /* SameElement */ LMI_UnixFile lmi_uf; @@ -213,7 +227,7 @@ static CMPIStatus references( LMI_UnixFile_Set_CSCreationClassName(&lmi_uf, get_system_creation_class_name()); LMI_UnixFile_Set_CSName(&lmi_uf, get_system_name()); LMI_UnixFile_Set_FSCreationClassName(&lmi_uf, FSCREATIONCLASSNAME); - LMI_UnixFile_Set_FSName(&lmi_uf, FSNAME); + LMI_UnixFile_Set_FSName(&lmi_uf, fsname); LMI_UnixFile_Set_LFCreationClassName(&lmi_uf, ccname); o = LMI_UnixFile_ToObjectPath(&lmi_uf, &st); LMI_FileIdentity_SetObjectPath_SameElement(&lmi_fi, o); @@ -226,6 +240,7 @@ static CMPIStatus references( ci = LMI_FileIdentity_ToInstance(&lmi_fi, &st); res = CMReturnInstance(cr, ci); } + free(fsname); return res; } diff --git a/src/logicalfile/LMI_RootDirectoryProvider.c b/src/logicalfile/LMI_RootDirectoryProvider.c index 6ced12b..dc6cd6b 100644 --- a/src/logicalfile/LMI_RootDirectoryProvider.c +++ b/src/logicalfile/LMI_RootDirectoryProvider.c @@ -62,6 +62,10 @@ static CMPIStatus associators( CMPIData pathd; pathd = CMGetKey(cop, "Name", &st); const char *path = KChars(pathd.value.string); + char *fsname; + if (get_fsname_from_path("/", &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } /* * allow only LMI_UnixDirectory and Linux_ComputerSystem @@ -94,7 +98,7 @@ static CMPIStatus associators( /* got Linux_ComputerSystem */ LMI_UnixDirectory lmi_ud; LMI_UnixDirectory_Init(&lmi_ud, _cb, ns); - fill_logicalfile(LMI_UnixDirectory, &lmi_ud, "/", LMI_UnixDirectory_ClassName); + fill_logicalfile(LMI_UnixDirectory, &lmi_ud, "/", fsname, LMI_UnixDirectory_ClassName); o = LMI_UnixDirectory_ToObjectPath(&lmi_ud, &st); if (names) { CMReturnObjectPath(cr, o); @@ -103,7 +107,7 @@ static CMPIStatus associators( CMReturnInstance(cr, ci); } } - + free(fsname); CMReturn(CMPI_RC_OK); } @@ -130,6 +134,10 @@ static CMPIStatus references( cd = CMGetKey(cop, "CreationClassName", &st); const char *path = KChars(pathd.value.string); const char *ccname = KChars(cd.value.string); + char *fsname; + if (get_fsname_from_path("/", &fsname) < 0) { + CMReturnWithChars(_cb, CMPI_RC_ERR_FAILED, "Can't get filesystem name"); + } /* * allow only LMI_UnixDirectory and Linux_ComputerSystem @@ -166,7 +174,7 @@ static CMPIStatus references( LMI_UnixDirectory lmi_ud; LMI_UnixDirectory_Init(&lmi_ud, _cb, ns); - fill_logicalfile(LMI_UnixDirectory, &lmi_ud, "/", LMI_UnixDirectory_ClassName); + fill_logicalfile(LMI_UnixDirectory, &lmi_ud, "/", fsname, LMI_UnixDirectory_ClassName); o = LMI_UnixDirectory_ToObjectPath(&lmi_ud, &st); LMI_RootDirectory_SetObjectPath_PartComponent(&lmi_rd, o); } @@ -178,6 +186,7 @@ static CMPIStatus references( ci = LMI_RootDirectory_ToInstance(&lmi_rd, &st); CMReturnInstance(cr, ci); } + free(fsname); CMReturn(CMPI_RC_OK); } diff --git a/src/logicalfile/file.c b/src/logicalfile/file.c index c17f0fc..64b7d52 100644 --- a/src/logicalfile/file.c +++ b/src/logicalfile/file.c @@ -95,6 +95,48 @@ int get_class_from_path(const char *path, char *fileclass) return rc; } +int get_fsname_from_stat(const struct stat *sb, char **fname) +{ + static struct udev *udev_ctx = NULL; + struct udev_device *udev_dev; + const char *dev_name; + int rc = 0; + + if (!udev_ctx) { + udev_ctx = udev_new(); + if (!udev_ctx) { + rc = -1; + goto err; + } + } + + char dev_id[16]; + snprintf(dev_id, 16, "b%u:%u", major(sb->st_dev), minor(sb->st_dev)); + + udev_dev = udev_device_new_from_device_id(udev_ctx, dev_id); + if ((dev_name = udev_device_get_property_value(udev_dev, "ID_FS_UUID_ENC"))) { + rc = asprintf(fname, "UUID=%s", dev_name); + } else if ((dev_name = udev_device_get_property_value(udev_dev, "DEVNAME"))) { + rc = asprintf(fname, "DEVICE=%s", dev_name); + } else { + rc = asprintf(fname, "Unknown"); + } +err: + return rc; +} + +int get_fsname_from_path(const char *path, char **fsname) +{ + struct stat sb; + int rc; + + rc = lstat(path, &sb); + if (rc == 0) { + rc = get_fsname_from_stat(&sb, fsname); + } + return rc; +} + void _dump_objectpath(const CMPIObjectPath *o) { printf("OP: %s\n", CMGetCharsPtr(o->ft->toString(o, NULL), NULL)); diff --git a/src/logicalfile/file.h b/src/logicalfile/file.h index b1b3358..df906c2 100644 --- a/src/logicalfile/file.h +++ b/src/logicalfile/file.h @@ -29,6 +29,7 @@ #include #include #include +#include #include "globals.h" #ifndef BUFLEN @@ -38,8 +39,7 @@ #define PATH_MAX 4096 #endif -#define FSCREATIONCLASSNAME "FSCCNToBeAgreedOn" -#define FSNAME "FSNToBeAgreedOn" +#define FSCREATIONCLASSNAME "LMI_LocalFileSystem" #define sb_permmask(sb) ((sb).st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) #define sb_isreadable(sb) ( \ @@ -59,12 +59,12 @@ ) #define stoms(t) ((t)*1000000) -#define fill_logicalfile(type, obj, name, creation_class) \ +#define fill_logicalfile(type, obj, name, fsname, creation_class) \ type##_Set_Name((obj), (name)); \ type##_Set_CSCreationClassName((obj), get_system_creation_class_name()); \ type##_Set_CSName((obj), get_system_name()); \ type##_Set_FSCreationClassName((obj), FSCREATIONCLASSNAME); \ - type##_Set_FSName((obj), FSNAME); \ + type##_Set_FSName((obj), (fsname)); \ type##_Set_CreationClassName((obj), (creation_class)); #define fill_basic(cmpitype, path, stattype, error) \ @@ -103,6 +103,8 @@ enum RequiredNames { CMPIStatus lmi_check_required(const CMPIBroker *, const CMPIObjectPath *, const enum RequiredNames); void get_class_from_stat(const struct stat *, char *); int get_class_from_path(const char *, char *); +int get_fsname_from_stat(const struct stat *, char **); +int get_fsname_from_path(const char *, char **); void _dump_objectpath(const CMPIObjectPath *); -- cgit