Commit 2276e22d authored by Jim Harris's avatar Jim Harris Committed by Tomasz Zawadzki
Browse files

util: add spdk_read_sysfs_attribute_uint32



Signed-off-by: default avatarJim Harris <jim.harris@samsung.com>
Change-Id: I87b1794e2758618c65f860609eaa686f70fc92d0
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/24075


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Community-CI: Mellanox Build Bot
Reviewed-by: default avatarChangpeng Liu <changpeng_liu@hotmail.com>
parent 4897a330
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -53,6 +53,18 @@ void *spdk_posix_file_load_from_name(const char *file_name, size_t *size);
int spdk_read_sysfs_attribute(char **attribute, const char *path_format, ...)
__attribute__((format(printf, 2, 3)));

/**
 * Get the uint32 value for a given sysfs attribute path
 *
 * \param attribute output parameter for contents of the attribute
 * \param path_format format string for constructing patch to sysfs file
 *
 * \return 0 on success
 *         negative errno if unable to read the attribute or it is not a uint32
 */
int spdk_read_sysfs_attribute_uint32(uint32_t *attribute, const char *path_format, ...)
__attribute__((format(printf, 2, 3)));

#ifdef __cplusplus
}
#endif
+26 −0
Original line number Diff line number Diff line
@@ -112,3 +112,29 @@ spdk_read_sysfs_attribute(char **attribute_p, const char *path_format, ...)

	return rc;
}

int
spdk_read_sysfs_attribute_uint32(uint32_t *attribute, const char *path_format, ...)
{
	char *attribute_str = NULL;
	long long int val;
	va_list args;
	int rc;

	va_start(args, path_format);
	rc = read_sysfs_attribute(&attribute_str, path_format, args);
	va_end(args);

	if (rc != 0) {
		return rc;
	}

	val = spdk_strtoll(attribute_str, 0);
	free(attribute_str);
	if (val < 0 || val > UINT32_MAX) {
		return -EINVAL;
	}

	*attribute = (uint32_t)val;
	return 0;
}
+1 −0
Original line number Diff line number Diff line
@@ -98,6 +98,7 @@
	spdk_posix_file_load;
	spdk_posix_file_load_from_name;
	spdk_read_sysfs_attribute;
	spdk_read_sysfs_attribute_uint32;

	# public functions in hexlify.h
	spdk_hexlify;
+64 −0
Original line number Diff line number Diff line
@@ -38,6 +38,69 @@ _read_sysfs_attribute(void)
	CU_ASSERT(rc == -ENOENT);
}

static void
read_sysfs_attribute_uint32(void)
{
	/* Don't try to use real sysfs paths for the unit test. Instead
	 * simulate sysfs attributes with some temporary files.
	 */
	const char *path = "/tmp/spdk_file_ut_2024";
	const char *setup = "111\n";
	uint32_t attr;
	FILE *f;
	int rc;

	f = fopen(path, "w");
	SPDK_CU_ASSERT_FATAL(f != NULL);
	rc = fwrite(setup, strlen(setup) + 1, 1, f);
	CU_ASSERT(rc == 1);
	rc = fclose(f);
	CU_ASSERT(rc == 0);

	rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024);
	CU_ASSERT(rc == 0);
	CU_ASSERT(attr == 111);

	setup = "0xFFFFFFFF\n";
	f = fopen(path, "w");
	SPDK_CU_ASSERT_FATAL(f != NULL);
	rc = fwrite(setup, strlen(setup) + 1, 1, f);
	CU_ASSERT(rc == 1);
	rc = fclose(f);
	CU_ASSERT(rc == 0);

	rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024);
	CU_ASSERT(rc == 0);
	CU_ASSERT(attr == UINT32_MAX);

	/* Write a value larger than UINT32_MAX */
	setup = "0x100000000\n";
	f = fopen(path, "w");
	SPDK_CU_ASSERT_FATAL(f != NULL);
	rc = fwrite(setup, strlen(setup) + 1, 1, f);
	CU_ASSERT(rc == 1);
	rc = fclose(f);
	CU_ASSERT(rc == 0);

	rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024);
	CU_ASSERT(rc == -EINVAL);

	/* Write a negative number */
	setup = "-1\n";
	f = fopen(path, "w");
	SPDK_CU_ASSERT_FATAL(f != NULL);
	rc = fwrite(setup, strlen(setup) + 1, 1, f);
	CU_ASSERT(rc == 1);
	rc = fclose(f);
	CU_ASSERT(rc == 0);

	rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/spdk_file_ut_%d", 2024);
	CU_ASSERT(rc == -EINVAL);

	rc = spdk_read_sysfs_attribute_uint32(&attr, "/tmp/some_non_existent_file");
	CU_ASSERT(rc == -ENOENT);
}

int
main(int argc, char **argv)
{
@@ -49,6 +112,7 @@ main(int argc, char **argv)
	suite = CU_add_suite("file", NULL, NULL);

	CU_ADD_TEST(suite, _read_sysfs_attribute);
	CU_ADD_TEST(suite, read_sysfs_attribute_uint32);

	num_failures = spdk_ut_run_tests(argc, argv, NULL);