Skip to content

Commit

Permalink
smart: Refactor and split into libbd_smartmontools
Browse files Browse the repository at this point in the history
Refactoring in preparation for a second implementation based
on libatasmart.
  • Loading branch information
tbzatek committed Jan 17, 2024
1 parent c2be582 commit fe5828c
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 233 deletions.
6 changes: 3 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ if !WITH_NVME
DISTCHECK_CONFIGURE_FLAGS += --without-nvme
endif

if !WITH_SMART
DISTCHECK_CONFIGURE_FLAGS += --without-smart
if !WITH_SMARTMONTOOLS
DISTCHECK_CONFIGURE_FLAGS += --without-smartmontools
endif

if !WITH_SWAP
Expand All @@ -72,7 +72,7 @@ MAINTAINERCLEANFILES = Makefile.in aclocal.m4 config.guess config.sub \
configure depcomp install-sh ltmain.sh missing py-compile compile ar-lib \
m4/*.m4

LIBDIRS = src/utils/.libs:src/plugins/.libs:src/plugins/fs/.libs:src/plugins/nvme/.libs:src/lib/.libs
LIBDIRS = src/utils/.libs:src/plugins/.libs:src/plugins/fs/.libs:src/plugins/nvme/.libs:src/plugins/smart/.libs:src/lib/.libs
GIDIR = src/lib

if WITH_PYTHON3
Expand Down
24 changes: 15 additions & 9 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ AC_CONFIG_FILES([Makefile src/Makefile \
src/plugins/Makefile \
src/plugins/fs/Makefile \
src/plugins/nvme/Makefile \
src/plugins/smart/Makefile \
src/utils/Makefile \
src/utils/blockdev-utils.pc \
src/lib/Makefile \
Expand Down Expand Up @@ -153,16 +154,21 @@ AS_IF([test "x$with_nvme" != "xno"],
[AC_DEFINE([WITH_BD_NVME], [], [Define if nvme is supported]) AC_SUBST([WITH_NVME], [1])],
[])

AC_ARG_WITH([smart],
AS_HELP_STRING([--with-smart], [support smart @<:@default=yes@:>@]),
AC_ARG_WITH([smartmontools],
AS_HELP_STRING([--with-smartmontools], [support ATA/SCSI S.M.A.R.T. via smartmontools @<:@default=yes@:>@]),
[],
[with_smart=yes])
[with_smartmontools=yes])

AC_SUBST([WITH_SMARTMONTOOLS], [0])
AM_CONDITIONAL(WITH_SMARTMONTOOLS, test "x$with_smartmontools" != "xno")
AS_IF([test "x$with_smartmontools" != "xno"],
[AC_DEFINE([WITH_BD_SMART], [], [Define if smart is supported])
AC_SUBST([WITH_SMART], [1])
AC_SUBST([WITH_SMARTMONTOOLS], [1])],
[])

AC_SUBST([WITH_SMART], [0])
AM_CONDITIONAL(WITH_SMART, test "x$with_smart" != "xno")
AS_IF([test "x$with_smart" != "xno"],
[AC_DEFINE([WITH_BD_SMART], [], [Define if smart is supported]) AC_SUBST([WITH_SMART], [1])],
[])
AM_CONDITIONAL(WITH_SMART, test "x$with_smartmontools" != "xno")

LIBBLOCKDEV_PLUGIN([BTRFS], [btrfs])
LIBBLOCKDEV_PLUGIN([CRYPTO], [crypto])
Expand All @@ -177,7 +183,7 @@ LIBBLOCKDEV_PLUGIN([PART], [part])
LIBBLOCKDEV_PLUGIN([FS], [fs])
LIBBLOCKDEV_PLUGIN([NVDIMM], [nvdimm])
LIBBLOCKDEV_PLUGIN([NVME], [nvme])
LIBBLOCKDEV_PLUGIN([SMART], [smart])
LIBBLOCKDEV_PLUGIN([SMART], [smartmontools])

dnl these packages/modules are always needed
LIBBLOCKDEV_PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.42.2])
Expand Down Expand Up @@ -318,7 +324,7 @@ echo "
NVMe plugin: ${with_nvme}
Part plugin: ${with_part}
S390 plugin: ${s390_info}
S.M.A.R.T. plugin: ${with_smart}
Smartmontools plugin: ${with_smartmontools}
Swap plugin: ${with_swap}

GObject introspection: ${found_introspection}
Expand Down
2 changes: 1 addition & 1 deletion data/conf.d/00-default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ sonames=libbd_nvme.so.3
sonames=libbd_part.so.3

[smart]
sonames=libbd_smart.so.3
sonames=libbd_smartmontools.so.3

[swap]
sonames=libbd_swap.so.3
Expand Down
1 change: 1 addition & 0 deletions include/blockdev/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
all-local:
for header in ${srcdir}/../../src/plugins/*.h; do ln -sf $${header} ./; done
for header in ${srcdir}/../../src/plugins/nvme/nvme.h; do ln -sf $${header} ./; done
for header in ${srcdir}/../../src/plugins/smart/smart.h; do ln -sf $${header} ./; done
for header in ${srcdir}/../../src/utils/*.h; do ln -sf $${header} ./; done
for header in ${srcdir}/../../src/lib/*.h; do ln -sf $${header} ./; done
mkdir -p fs;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/blockdev.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static gchar * default_plugin_so[BD_PLUGIN_UNDEF] = {
"libbd_dm.so.@MAJOR_VER@", "libbd_mdraid.so.@MAJOR_VER@",
"libbd_s390.so.@MAJOR_VER@", "libbd_part.so.@MAJOR_VER@",
"libbd_fs.so.@MAJOR_VER@", "libbd_nvdimm.so.@MAJOR_VER@",
"libbd_nvme.so.@MAJOR_VER@", "libbd_smart.so.@MAJOR_VER@",
"libbd_nvme.so.@MAJOR_VER@", "libbd_smartmontools.so.@MAJOR_VER@",
};
static BDPluginStatus plugins[BD_PLUGIN_UNDEF] = {
{{BD_PLUGIN_LVM, NULL}, NULL},
Expand Down
20 changes: 4 additions & 16 deletions src/plugins/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ if WITH_NVME
SUBDIRS += nvme
endif

if WITH_SMART
SUBDIRS += smart
endif

lib_LTLIBRARIES =

if WITH_BTRFS
Expand Down Expand Up @@ -46,10 +50,6 @@ if WITH_NVDIMM
lib_LTLIBRARIES += libbd_nvdimm.la
endif

if WITH_SMART
lib_LTLIBRARIES += libbd_smart.la
endif

if WITH_SWAP
lib_LTLIBRARIES += libbd_swap.la
endif
Expand Down Expand Up @@ -140,14 +140,6 @@ libbd_nvdimm_la_CPPFLAGS = -I${builddir}/../../include/
libbd_nvdimm_la_SOURCES = nvdimm.c nvdimm.h check_deps.c check_deps.h
endif

if WITH_SMART
libbd_smart_la_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS) $(JSON_GLIB_CFLAGS) -Wall -Wextra -Werror
libbd_smart_la_LIBADD = ${builddir}/../utils/libbd_utils.la $(GLIB_LIBS) $(GIO_LIBS) $(JSON_GLIB_LIBS)
libbd_smart_la_LDFLAGS = -L${srcdir}/../utils/ -version-info 3:0:0 -Wl,--no-undefined
libbd_smart_la_CPPFLAGS = -I${builddir}/../../include/
libbd_smart_la_SOURCES = smart.c smart.h check_deps.c check_deps.h
endif

if WITH_SWAP
libbd_swap_la_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS) $(BLKID_CFLAGS) $(UUID_CFLAGS) -Wall -Wextra -Werror
libbd_swap_la_LIBADD = ${builddir}/../utils/libbd_utils.la $(GLIB_LIBS) $(GIO_LIBS) $(BLKID_LIBS) $(UUID_LIBS)
Expand Down Expand Up @@ -208,10 +200,6 @@ if WITH_NVDIMM
libinclude_HEADERS += nvdimm.h
endif

if WITH_SMART
libinclude_HEADERS += smart.h
endif

if WITH_SWAP
libinclude_HEADERS += swap.h
endif
Expand Down
29 changes: 29 additions & 0 deletions src/plugins/smart/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
AUTOMAKE_OPTIONS = subdir-objects

lib_LTLIBRARIES =


if WITH_SMART
libincludedir = $(includedir)/blockdev
libinclude_HEADERS = smart.h
endif


if WITH_SMARTMONTOOLS

lib_LTLIBRARIES += libbd_smartmontools.la

libbd_smartmontools_la_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS) $(JSON_GLIB_CFLAGS) -Wall -Wextra -Werror
libbd_smartmontools_la_LIBADD = ${builddir}/../../utils/libbd_utils.la $(GLIB_LIBS) $(GIO_LIBS) $(JSON_GLIB_LIBS)
libbd_smartmontools_la_LDFLAGS = -L${srcdir}/../../utils/ -version-info 3:0:0 -Wl,--no-undefined -export-symbols-regex '^bd_.*'
libbd_smartmontools_la_CPPFLAGS = -I${builddir}/../../../include/ -I${srcdir}/../ -I. -DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\"

libbd_smartmontools_la_SOURCES = \
smart.h \
smart-private.h \
smart-common.c \
smartmontools.c \
../check_deps.c \
../check_deps.h

endif
152 changes: 152 additions & 0 deletions src/plugins/smart/smart-common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright (C) 2014-2024 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Tomas Bzatek <[email protected]>
*/

#include <glib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>

#include <blockdev/utils.h>
#include <check_deps.h>

#include "smart.h"
#include "smart-private.h"


/**
* bd_smart_error_quark: (skip)
*/
GQuark bd_smart_error_quark (void)
{
return g_quark_from_static_string ("g-bd-smart-error-quark");
}

/**
* bd_smart_ata_attribute_free: (skip)
* @attr: (nullable): %BDSmartATAAttribute to free
*
* Frees @attr.
*/
void bd_smart_ata_attribute_free (BDSmartATAAttribute *attr) {
if (attr == NULL)
return;
g_free (attr->name);
g_free (attr->well_known_name);
g_free (attr->pretty_value_string);
g_free (attr);
}

/**
* bd_smart_ata_attribute_copy: (skip)
* @attr: (nullable): %BDSmartATAAttribute to copy
*
* Creates a new copy of @attr.
*/
BDSmartATAAttribute * bd_smart_ata_attribute_copy (BDSmartATAAttribute *attr) {
BDSmartATAAttribute *new_attr;

if (attr == NULL)
return NULL;

new_attr = g_new0 (BDSmartATAAttribute, 1);
memcpy (new_attr, attr, sizeof (BDSmartATAAttribute));
new_attr->name = g_strdup (attr->name);
new_attr->well_known_name = g_strdup (attr->well_known_name);
new_attr->pretty_value_string = g_strdup (attr->pretty_value_string);

return new_attr;
}

/**
* bd_smart_ata_free: (skip)
* @data: (nullable): %BDSmartATA to free
*
* Frees @data.
*/
void bd_smart_ata_free (BDSmartATA *data) {
BDSmartATAAttribute **attr;

if (data == NULL)
return;

for (attr = data->attributes; attr && *attr; attr++)
bd_smart_ata_attribute_free (*attr);
g_free (data->attributes);
g_free (data);
}

/**
* bd_smart_ata_copy: (skip)
* @data: (nullable): %BDSmartATA to copy
*
* Creates a new copy of @data.
*/
BDSmartATA * bd_smart_ata_copy (BDSmartATA *data) {
BDSmartATA *new_data;
BDSmartATAAttribute **attr;
GPtrArray *ptr_array;

if (data == NULL)
return NULL;

new_data = g_new0 (BDSmartATA, 1);
memcpy (new_data, data, sizeof (BDSmartATA));

ptr_array = g_ptr_array_new ();
for (attr = data->attributes; attr && *attr; attr++)
g_ptr_array_add (ptr_array, bd_smart_ata_attribute_copy (*attr));
g_ptr_array_add (ptr_array, NULL);
new_data->attributes = (BDSmartATAAttribute **) g_ptr_array_free (ptr_array, FALSE);

return new_data;
}

/**
* bd_smart_scsi_free: (skip)
* @data: (nullable): %BDSmartSCSI to free
*
* Frees @data.
*/
void bd_smart_scsi_free (BDSmartSCSI *data) {
if (data == NULL)
return;

g_free (data->scsi_ie_string);
g_free (data);
}

/**
* bd_smart_scsi_copy: (skip)
* @data: (nullable): %BDSmartSCSI to copy
*
* Creates a new copy of @data.
*/
BDSmartSCSI * bd_smart_scsi_copy (BDSmartSCSI *data) {
BDSmartSCSI *new_data;

if (data == NULL)
return NULL;

new_data = g_new0 (BDSmartSCSI, 1);
memcpy (new_data, data, sizeof (BDSmartSCSI));
new_data->scsi_ie_string = g_strdup (data->scsi_ie_string);

return new_data;
}
Loading

0 comments on commit fe5828c

Please sign in to comment.