Blame src/utils.h

Packit Service db8df9
/*
Packit Service db8df9
 * Intel(R) Enclosure LED Utilities
Packit Service db8df9
 * Copyright (C) 2009-2019 Intel Corporation.
Packit Service db8df9
 *
Packit Service db8df9
 * This program is free software; you can redistribute it and/or modify it
Packit Service db8df9
 * under the terms and conditions of the GNU General Public License,
Packit Service db8df9
 * version 2, as published by the Free Software Foundation.
Packit Service db8df9
 *
Packit Service db8df9
 * This program is distributed in the hope it will be useful, but WITHOUT
Packit Service db8df9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit Service db8df9
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
Packit Service db8df9
 * more details.
Packit Service db8df9
 *
Packit Service db8df9
 * You should have received a copy of the GNU General Public License along with
Packit Service db8df9
 * this program; if not, write to the Free Software Foundation, Inc.,
Packit Service db8df9
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Packit Service db8df9
 *
Packit Service db8df9
 */
Packit Service db8df9
Packit Service db8df9
#ifndef _UTILS_H_INCLUDED_
Packit Service db8df9
#define _UTILS_H_INCLUDED_
Packit Service db8df9
Packit Service db8df9
#include <getopt.h>
Packit Service db8df9
#include "config_file.h"
Packit Service db8df9
#include "stdlib.h"
Packit Service db8df9
#include "stdint.h"
Packit Service db8df9
#include "list.h"
Packit Service db8df9
#include "status.h"
Packit Service db8df9
#include "syslog.h"
Packit Service db8df9
#include "ibpi.h"
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * Value is intentionally unused.
Packit Service db8df9
 * Macro to prevent compiler from raising unused result warning.
Packit Service db8df9
 */
Packit Service db8df9
#define UNUSED(x) do { if (x); } while (0)
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * Maximum number of bytes in temporary buffer. It is used for local variables.
Packit Service db8df9
 */
Packit Service db8df9
#define BUFFER_MAX          128
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * Maximum number of bytes in write buffer. It is used for local variables when
Packit Service db8df9
 * function needs to write a sysfs attribute.
Packit Service db8df9
 */
Packit Service db8df9
#define WRITE_BUFFER_SIZE   1024
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * This structure describes a device identifier. It consists of major and minor
Packit Service db8df9
 * attributes of device.
Packit Service db8df9
 */
Packit Service db8df9
struct device_id {
Packit Service db8df9
	int major, minor;
Packit Service db8df9
};
Packit Service db8df9
Packit Service db8df9
struct log_level_info {
Packit Service db8df9
	char prefix[10];
Packit Service db8df9
	int priority;
Packit Service db8df9
};
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 */
Packit Service db8df9
#define PREFIX_DEBUG          "  DEBUG: "
Packit Service db8df9
#define PREFIX_WARNING        "WARNING: "
Packit Service db8df9
#define PREFIX_INFO           "   INFO: "
Packit Service db8df9
#define PREFIX_ERROR          "  ERROR: "
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * This array describes a log levels priorities with message prefix .
Packit Service db8df9
 */
Packit Service db8df9
extern struct log_level_info log_level_infos[];
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * This global variable holds the name of binary file an application has been
Packit Service db8df9
 * executed from.
Packit Service db8df9
 */
Packit Service db8df9
extern char *progname;
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Reads integer value from a text file.
Packit Service db8df9
 *
Packit Service db8df9
 * This function assumes that the only text in a file is requested number to
Packit Service db8df9
 * read. In case of an error while reading from file the function will return
Packit Service db8df9
 * a value stored in defval argument.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           location of a file to be read.
Packit Service db8df9
 * @param[in]      defval         default value to return in case the file
Packit Service db8df9
 *                                does not exist.
Packit Service db8df9
 * @param[in]      name           name of a file to be read.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Value read from a file if successful, otherwise a value stored in
Packit Service db8df9
 *         defval argument.
Packit Service db8df9
 */
Packit Service db8df9
int get_int(const char *path, int defval, const char *name);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Reads 64-bit unsigned integer from a text file.
Packit Service db8df9
 *
Packit Service db8df9
 * This function assumes that the only text in a file is requested number to
Packit Service db8df9
 * read. In case of an error while reading from file the function will return
Packit Service db8df9
 * a value stored in defval argument.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           Path where a file is located.
Packit Service db8df9
 * @param[in]      defval         Default value to be returned in case of error.
Packit Service db8df9
 * @param[in]      name           Name of a file to be read.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Value read from a file if successful, otherwise a value stored in
Packit Service db8df9
 *         defval argument.
Packit Service db8df9
 */
Packit Service db8df9
uint64_t get_uint64(const char *path, uint64_t defval, const char *name);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Reads a content of a text file.
Packit Service db8df9
 *
Packit Service db8df9
 * This function reads a text file and return pointer to memory where the text
Packit Service db8df9
 * has been stored. The memory allocated by the function must be release as soon
Packit Service db8df9
 * as application does not require the content. Use free() function to give
Packit Service db8df9
 * allocated memory back to the system.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           Path where a file is located.
Packit Service db8df9
 * @param[in]      name           Name of a file to be read.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Pointer to memory buffer if successful, otherwise NULL pointer.
Packit Service db8df9
 */
Packit Service db8df9
char *get_text(const char *path, const char *name);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Reads boolean value from a text file.
Packit Service db8df9
 *
Packit Service db8df9
 * This function assumes that the only text in a file is the requested value to
Packit Service db8df9
 * read. The recognized boolean values are in the format 'Y' for True and 'N'
Packit Service db8df9
 * for False. In case of an error while reading from file the function will
Packit Service db8df9
 * return a value stored in defval argument.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           Path where a file is located.
Packit Service db8df9
 * @param[in]      defval         Default value to be returned in case of error.
Packit Service db8df9
 * @param[in]      name           Name of a file to be read.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Value read from a file if successful, otherwise a value stored in
Packit Service db8df9
 *         defval argument. 1 is returned for True, 0 for False.
Packit Service db8df9
 */
Packit Service db8df9
int get_bool(const char *path, int defval, const char *name);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Writes a text to file.
Packit Service db8df9
 *
Packit Service db8df9
 * This function writes a text to a file and return the number of bytes written.
Packit Service db8df9
 * If the file does not exist or the value is incorrect the function returns -1
Packit Service db8df9
 * and errno has additional error information.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           Location of file to write.
Packit Service db8df9
 * @param[in]      name           Name of file to write.
Packit Service db8df9
 * @param[in]      value          Text to write to a file.
Packit Service db8df9
 *
Packit Service db8df9
 * @return The number of bytes written or -1 if an error occurred.
Packit Service db8df9
 */
Packit Service db8df9
int put_text(const char *path, const char *name, const char *value);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Writes an integer value to a text file.
Packit Service db8df9
 *
Packit Service db8df9
 * This function writes an integer value to a text file. If the file does not
Packit Service db8df9
 * exist or the value is out of range the function returns -1 and errno variable
Packit Service db8df9
 * has additional error information.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           Location of file to write.
Packit Service db8df9
 * @param[in]      name           Name of file to write.
Packit Service db8df9
 * @param[in]      value          Integer value to write to a file.
Packit Service db8df9
 *
Packit Service db8df9
 * @return The number of bytes written or -1 if an error occurred.
Packit Service db8df9
 */
Packit Service db8df9
int put_int(const char *path, const char *name, int value);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Scans directory for files.
Packit Service db8df9
 *
Packit Service db8df9
 * This function reads a directory specified in path argument and puts found
Packit Service db8df9
 * file on a list. The function puts a canonical paths on the list, however it
Packit Service db8df9
 * does not resolve any symbolic link.
Packit Service db8df9
 *
Packit Service db8df9
 * This function allocates memory for the list elements. The caller should free
Packit Service db8df9
 * it using list_erase().
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           Path to directory to read.
Packit Service db8df9
 * @param[in]      result         Pointer to list where the directory contents
Packit Service db8df9
 *                                will be put.
Packit Service db8df9
 *
Packit Service db8df9
 * @return 0 on success, -1 on error.
Packit Service db8df9
 */
Packit Service db8df9
int scan_dir(const char *path, struct list *result);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Writes a text to file.
Packit Service db8df9
 *
Packit Service db8df9
 * This function writes content of text buffer to file. If the file does not
Packit Service db8df9
 * exist or a content is invalid the function returns -1 and errno variable has
Packit Service db8df9
 * additional error information.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           Location and name of file to write to.
Packit Service db8df9
 * @param[in]      buf            Pointer to text buffer to write to a file.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Number of bytes written if successful, otherwise -1 for an error.
Packit Service db8df9
 */
Packit Service db8df9
ssize_t buf_write(const char *path, const char *buf);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Reads the content of a text file.
Packit Service db8df9
 *
Packit Service db8df9
 * The function reads the content of a text file and stores it to memory buffer
Packit Service db8df9
 * allocated. The function determines a size of the file and allocates required
Packit Service db8df9
 * amount of memory. User is required to free allocated memory as soon as
Packit Service db8df9
 * application does not require the content. Use free() function to give memory
Packit Service db8df9
 * back to the system pool. The function replaces last end-of-line character
Packit Service db8df9
 * with '\0' character.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           Path and name of file to read.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Pointer to memory block if successful, otherwise NULL pointer.
Packit Service db8df9
 */
Packit Service db8df9
char *buf_read(const char *path);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Gets major and minor of device.
Packit Service db8df9
 *
Packit Service db8df9
 * The function reads from text buffer the major and minor of block device.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      buf            Pointer to text buffer to interpret.
Packit Service db8df9
 * @param[out]     did            Placeholder where major and minor will be
Packit Service db8df9
 *                                stored.
Packit Service db8df9
 *
Packit Service db8df9
 * @return The function does not return a value.
Packit Service db8df9
 */
Packit Service db8df9
void get_id(const char *buf, struct device_id *did);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Open a local log file.
Packit Service db8df9
 *
Packit Service db8df9
 * The function opens a file to write log messages. If the given file does not
Packit Service db8df9
 * exist the new one will be created. If the file already exist the file will be
Packit Service db8df9
 * opened in append mode and the pointer will be set at the end of a file.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      path           Location and name of a log file.
Packit Service db8df9
 *
Packit Service db8df9
 * @return The function returns 0 if successful, otherwise -1 and errno variable
Packit Service db8df9
 *         has additional error information.
Packit Service db8df9
 */
Packit Service db8df9
int log_open(const char *path);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Close a local log file.
Packit Service db8df9
 *
Packit Service db8df9
 * The function closes a local log file. If the file has not been opened the
Packit Service db8df9
 * function does nothing.
Packit Service db8df9
 *
Packit Service db8df9
 * @return The function does not return a value.
Packit Service db8df9
 */
Packit Service db8df9
void log_close(void);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Logs an message with given loglevel.
Packit Service db8df9
 *
Packit Service db8df9
 * The function logs a message at given level of verbosity.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      loglevel       Level of verbosity for a message.
Packit Service db8df9
 * @param[in]      buf            Buffer containing format of a message.
Packit Service db8df9
 * @param[in]      ...            Additional arguments according to format of
Packit Service db8df9
 *                                a message.
Packit Service db8df9
 *
Packit Service db8df9
 * @return The function does not return a value.
Packit Service db8df9
 */
Packit Service db8df9
void _log(enum log_level_enum loglevel, const char *buf, ...);
Packit Service db8df9
Packit Service db8df9
#define log_error(buf, ...)	_log(LOG_LEVEL_ERROR, buf, ##__VA_ARGS__)
Packit Service db8df9
#define log_debug(buf, ...)	_log(LOG_LEVEL_DEBUG, buf, ##__VA_ARGS__)
Packit Service db8df9
#define log_info(buf, ...)	_log(LOG_LEVEL_INFO, buf, ##__VA_ARGS__)
Packit Service db8df9
#define log_warning(buf, ...)	_log(LOG_LEVEL_WARNING, buf, ##__VA_ARGS__)
Packit Service db8df9
/**
Packit Service db8df9
 */
Packit Service db8df9
void set_invocation_name(char *invocation_name);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Copies a text buffer.
Packit Service db8df9
 *
Packit Service db8df9
 * This function copies source text buffer to destination buffer. The function
Packit Service db8df9
 * always return a null-terminated buffer even if src does not fit in dest.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[out]     dest           Pointer to destination buffer.
Packit Service db8df9
 * @param[in]      src            Pointer to source buffer.
Packit Service db8df9
 * @param[in]      size           Capacity of destination buffer in bytes.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Pointer to destination buffer even if function failed.
Packit Service db8df9
 */
Packit Service db8df9
char *str_cpy(char *dest, const char *src, size_t size);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Duplicates a text buffer.
Packit Service db8df9
 *
Packit Service db8df9
 * This function duplicates a text buffer. It allocates a new memory block and
Packit Service db8df9
 * copies the content of source buffer to new location. If pointer to source
Packit Service db8df9
 * buffer is NULL the function will return NULL, too. The caller is required to
Packit Service db8df9
 * free allocated memory as soon as content is not needed.
Packit Service db8df9
 *
Packit Service db8df9
 * @param[in]      src            Source buffer to duplicate the content.
Packit Service db8df9
 *
Packit Service db8df9
 * @return Pointer to allocated memory block if successful, otherwise NULL.
Packit Service db8df9
 */
Packit Service db8df9
char *str_dup(const char *src);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 */
Packit Service db8df9
char *truncate_path_component_rev(const char *path, int index);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 */
Packit Service db8df9
char *get_path_component_rev(const char *path, int index);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * @brief Extracts the 'hostX' part from path.
Packit Service db8df9
 */
Packit Service db8df9
char *get_path_hostN(const char *path);
Packit Service db8df9
Packit Service db8df9
int match_string(const char *string, const char *pattern);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 */
Packit Service db8df9
int get_log_fd(void);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 */
Packit Service db8df9
void print_opt(const char *long_opt, const char *short_opt, const char *desc);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 */
Packit Service db8df9
status_t set_log_path(const char *path);
Packit Service db8df9
Packit Service db8df9
/**
Packit Service db8df9
 * Internal enumeration type. It is used to help parse command line arguments.
Packit Service db8df9
 */
Packit Service db8df9
enum opt {
Packit Service db8df9
	OPT_ALL,
Packit Service db8df9
	OPT_CONFIG,
Packit Service db8df9
	OPT_DEBUG,
Packit Service db8df9
	OPT_ERROR,
Packit Service db8df9
	OPT_HELP,
Packit Service db8df9
	OPT_INFO,
Packit Service db8df9
	OPT_INTERVAL,
Packit Service db8df9
	OPT_LOG,
Packit Service db8df9
	OPT_QUIET,
Packit Service db8df9
	OPT_VERSION,
Packit Service db8df9
	OPT_WARNING,
Packit Service db8df9
	OPT_LOG_LEVEL,
Packit Service db8df9
	OPT_LIST_CTRL,
Packit Service db8df9
	OPT_LISTED_ONLY,
Packit Service db8df9
	OPT_FOREGROUND,
Packit Service db8df9
	OPT_NULL_ELEMENT
Packit Service db8df9
};
Packit Service db8df9
Packit Service db8df9
extern struct option longopt_all[];
Packit Service db8df9
void setup_options(struct option **longopt, char **shortopt, int *options,
Packit Service db8df9
			int options_nr);
Packit Service db8df9
int get_option_id(const char *optarg);
Packit Service db8df9
status_t set_verbose_level(int log_level);
Packit Service db8df9
Packit Service db8df9
const char *ibpi2str(enum ibpi_pattern ibpi);
Packit Service db8df9
Packit Service db8df9
#endif				/* _UTILS_H_INCLUDED_ */