Blame libopeniscsiusr/misc.h

Packit eace71
/*
Packit eace71
 * Copyright (C) 2017 Red Hat, Inc.
Packit eace71
 *
Packit eace71
 * This program is free software: you can redistribute it and/or modify
Packit eace71
 * it under the terms of the GNU General Public License as published by
Packit eace71
 * the Free Software Foundation, either version 3 of the License, or
Packit eace71
 * (at your option) any later version.
Packit eace71
 *
Packit eace71
 * This program is distributed in the hope that it will be useful,
Packit eace71
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit eace71
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit eace71
 * GNU General Public License for more details.
Packit eace71
 *
Packit eace71
 * You should have received a copy of the GNU General Public License
Packit eace71
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit eace71
 *
Packit eace71
 * Author: Gris Ge <fge@redhat.com>
Packit eace71
 */
Packit eace71
#ifndef __ISCSI_USR_MISC_H__
Packit eace71
#define __ISCSI_USR_MISC_H__
Packit eace71
Packit eace71
#include <stdint.h>
Packit eace71
#include <stdbool.h>
Packit eace71
#include <assert.h>
Packit eace71
#include <stdarg.h>
Packit eace71
#include <dirent.h>
Packit eace71
#include <net/if.h>
Packit eace71
Packit eace71
#include "libopeniscsiusr/libopeniscsiusr.h"
Packit eace71
Packit eace71
#define _good(rc, rc_val, out) \
Packit eace71
	do { \
Packit eace71
		rc_val = rc; \
Packit eace71
		if (rc_val != LIBISCSI_OK) \
Packit eace71
			goto out; \
Packit eace71
	} while(0)
Packit eace71
Packit eace71
#define _asprintf(...) \
Packit eace71
	(asprintf(__VA_ARGS__) == -1 ? LIBISCSI_ERR_NOMEM : LIBISCSI_OK)
Packit eace71
Packit eace71
__DLL_LOCAL void _iscsi_log(struct iscsi_context *ctx, int priority,
Packit eace71
			    const char *file, int line, const char *func_name,
Packit eace71
			    const char *format, ...);
Packit eace71
__DLL_LOCAL void _iscsi_log_stderr(struct iscsi_context *ctx, int priority,
Packit eace71
				   const char *file, int line,
Packit eace71
				   const char *func_name, const char *format,
Packit eace71
				   va_list args);
Packit eace71
Packit eace71
#define _iscsi_log_cond(ctx, prio, arg...) \
Packit eace71
	do { \
Packit eace71
		if ((ctx != NULL) && \
Packit eace71
		    (iscsi_context_log_priority_get(ctx) >= prio)) \
Packit eace71
			_iscsi_log(ctx, prio, __FILE__, __LINE__, \
Packit eace71
				   __FUNCTION__, ## arg); \
Packit eace71
	} while (0)
Packit eace71
Packit eace71
#define _debug(ctx, arg...) \
Packit eace71
	_iscsi_log_cond(ctx, LIBISCSI_LOG_PRIORITY_DEBUG, ## arg)
Packit eace71
#define _info(ctx, arg...) \
Packit eace71
	_iscsi_log_cond(ctx, LIBISCSI_LOG_PRIORITY_INFO, ## arg)
Packit eace71
#define _warn(ctx, arg...) \
Packit eace71
	_iscsi_log_cond(ctx, LIBISCSI_LOG_PRIORITY_WARNING, ## arg)
Packit eace71
#define _error(ctx, arg...) \
Packit eace71
	_iscsi_log_cond(ctx, LIBISCSI_LOG_PRIORITY_ERROR, ## arg)
Packit eace71
Packit eace71
#define _iscsi_getter_func_gen(struct_name, prop_name, prop_type) \
Packit eace71
	prop_type struct_name##_##prop_name##_get(struct struct_name *d) \
Packit eace71
	{ \
Packit eace71
		assert(d != NULL); \
Packit eace71
		return d->prop_name; \
Packit eace71
	}
Packit eace71
Packit eace71
/*
Packit eace71
 * Check pointer returned by malloc() or strdup() or calloc(), if NULL, set
Packit eace71
 * rc as LIBISCSI_ERR_NO_MEMORY, report error and goto goto_out.
Packit eace71
 */
Packit eace71
#define _alloc_null_check(ctx, ptr, rc, goto_out) \
Packit eace71
	do { \
Packit eace71
		if (ptr == NULL) { \
Packit eace71
			rc = LIBISCSI_ERR_NOMEM; \
Packit eace71
			_error(ctx, iscsi_strerror(rc)); \
Packit eace71
			goto goto_out; \
Packit eace71
		} \
Packit eace71
	} while(0)
Packit eace71
Packit eace71
#define _STRERR_BUFF_LEN	1024
Packit eace71
#define _strerror(err_no, buff) \
Packit eace71
	strerror_r(err_no, buff, _STRERR_BUFF_LEN)
Packit eace71
Packit eace71
/* Workaround for suppress GCC 8 `stringop-truncation` warnings. */
Packit eace71
#define _strncpy(dst, src, size) \
Packit eace71
	do { \
Packit eace71
		memcpy(dst, src, \
Packit eace71
		       (size_t) size > strlen(src) ? \
Packit eace71
		       strlen(src) : (size_t) size); \
Packit eace71
		* (char *) (dst + \
Packit eace71
			    ((size_t) size - 1 > strlen(src) ? \
Packit eace71
			     strlen(src) : (size_t) (size - 1))) = '\0'; \
Packit eace71
	} while(0)
Packit eace71
Packit eace71
__DLL_LOCAL int _scan_filter_skip_dot(const struct dirent *dir);
Packit eace71
Packit eace71
__DLL_LOCAL bool _file_exists(const char *path);
Packit eace71
Packit eace71
Packit eace71
#define _ETH_DRIVER_NAME_MAX_LEN	32
Packit eace71
/* ^ Defined in linux/ethtool.h `struct ethtool_drvinfo`. */
Packit eace71
Packit eace71
struct _eth_if {
Packit eace71
	char driver_name[_ETH_DRIVER_NAME_MAX_LEN];
Packit eace71
	char if_name[IF_NAMESIZE];
Packit eace71
};
Packit eace71
Packit eace71
__DLL_LOCAL int _eth_ifs_get(struct iscsi_context *ctx,
Packit eace71
			     struct _eth_if ***eifs, uint32_t *eif_count);
Packit eace71
Packit eace71
__DLL_LOCAL void _eth_ifs_free(struct _eth_if **eifs, uint32_t eif_count);
Packit eace71
Packit eace71
__DLL_LOCAL int _scandir(struct iscsi_context *ctx, const char *dir_path,
Packit eace71
			 struct dirent ***namelist, int *count);
Packit eace71
__DLL_LOCAL void _scandir_free(struct dirent **namelist, int count);
Packit eace71
Packit eace71
#endif /* End of __ISCSI_USR_MISC_H__ */