Commit 4897a330 authored by Jim Harris's avatar Jim Harris Committed by Tomasz Zawadzki
Browse files

util: add spdk_read_sysfs_attribute



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


Reviewed-by: default avatarChangpeng Liu <changpeng_liu@hotmail.com>
Community-CI: Mellanox Build Bot
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
parent cec5d0ac
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -36,6 +36,23 @@ void *spdk_posix_file_load(FILE *file, size_t *size);
 */
void *spdk_posix_file_load_from_name(const char *file_name, size_t *size);

/**
 * Get the string value for a given sysfs attribute path
 *
 * When successful, the returned string will be null-terminated, without
 * a trailing newline.
 *
 * \param attribute output parameter for contents of the attribute; caller must
 *		    free() the buffer pointed to by attribute at some
 *		    point after a successful call
 * \param path_format format string for constructing patch to sysfs file
 *
 * \return 0 on success
 *         negative errno if unable to read the attribute
 */
int spdk_read_sysfs_attribute(char **attribute, const char *path_format, ...)
__attribute__((format(printf, 2, 3)));

#ifdef __cplusplus
}
#endif
+55 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
 */

#include "spdk/file.h"
#include "spdk/string.h"

void *
spdk_posix_file_load(FILE *file, size_t *size)
@@ -57,3 +58,57 @@ spdk_posix_file_load_from_name(const char *file_name, size_t *size)

	return data;
}

static int
read_sysfs_attribute(char **attribute_p, const char *format, va_list args)
{
	char *attribute;
	FILE *file;
	char *path;
	size_t len = 0;
	ssize_t read;

	path = spdk_vsprintf_alloc(format, args);
	if (path == NULL) {
		return -ENOMEM;
	}

	file = fopen(path, "r");
	free(path);
	if (file == NULL) {
		return -errno;
	}

	*attribute_p = NULL;
	read = getline(attribute_p, &len, file);
	fclose(file);
	attribute = *attribute_p;
	if (read == -1) {
		/* getline man page says line should be freed even on failure. */
		free(attribute);
		return -errno;
	}

	/* len is the length of the allocated buffer, which may be more than
	 * the string's length. Reuse len to hold the actual strlen.
	 */
	len = strlen(attribute);
	if (attribute[len - 1] == '\n') {
		attribute[len - 1] = '\0';
	}

	return 0;
}

int
spdk_read_sysfs_attribute(char **attribute_p, const char *path_format, ...)
{
	va_list args;
	int rc;

	va_start(args, path_format);
	rc = read_sysfs_attribute(attribute_p, path_format, args);
	va_end(args);

	return rc;
}
+1 −0
Original line number Diff line number Diff line
@@ -97,6 +97,7 @@
	# public functions in file.h
	spdk_posix_file_load;
	spdk_posix_file_load_from_name;
	spdk_read_sysfs_attribute;

	# public functions in hexlify.h
	spdk_hexlify;
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

DIRS-y = base64.c bit_array.c cpuset.c crc16.c crc32_ieee.c crc32c.c crc64.c dif.c \
	 iov.c math.c pipe.c string.c xor.c
	 file.c iov.c math.c pipe.c string.c xor.c

.PHONY: all clean $(DIRS-y)

+10 −0
Original line number Diff line number Diff line
#  SPDX-License-Identifier: BSD-3-Clause
#  Copyright (C) 2024 Samsung Electronics Co., Ltd.
#  All rights reserved.
#

SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..)

TEST_FILE = file_ut.c

include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk
Loading