From d7772bbe4e9ac62321a75e66bf8b7b52b781a6b7 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Wed, 22 Nov 2023 16:51:41 +0100 Subject: [PATCH] iiod: Switch to use the new Libiio debug macros Some error messages were converted to use IIO_PERROR() so that they also print the error message and code. This opens the door to have --quiet and --verbose (or --debug) options to IIOD, since Libiio allows to control the verbosity at runtime. Signed-off-by: Paul Cercueil --- iiod/debug.h | 42 ++++-------------- iiod/iiod.c | 108 ++++++++++++++++++----------------------------- iiod/responder.c | 4 +- iiod/serial.c | 10 +++-- iiod/usbd.c | 2 +- 5 files changed, 58 insertions(+), 108 deletions(-) diff --git a/iiod/debug.h b/iiod/debug.h index cfe2339c3..c5d877406 100644 --- a/iiod/debug.h +++ b/iiod/debug.h @@ -9,47 +9,23 @@ #ifndef __IIOD_DEBUG_H__ #define __IIOD_DEBUG_H__ -#include "../iio-config.h" +#include -#include +extern struct iio_context_params iiod_params; -#define NoLog_L 0 -#define Error_L 1 -#define Warning_L 2 -#define Info_L 3 -#define Debug_L 4 - -/* -------------------- */ - -/* Many of these debug printf include a Flawfinder: ignore, this is because, - * according to https://cwe.mitre.org/data/definitions/134.html which describes - * functions that accepts a format string as an argument, but the format - * string originates from an external source. All the IIO_DEBUG, IIO_INFO, - * IIO_WARNING, and IIO_ERRRO functions are called internally from the - * library, have fixed format strings and can not be modified externally. - */ #define IIO_DEBUG(...) \ - do { \ - if (LOG_LEVEL >= Debug_L) \ - fprintf(stdout, "DEBUG: " __VA_ARGS__); /* Flawfinder: ignore */ \ - } while (0) + prm_dbg(&iiod_params, __VA_ARGS__) #define IIO_INFO(...) \ - do { \ - if (LOG_LEVEL >= Info_L) \ - fprintf(stdout, __VA_ARGS__); /* Flawfinder: ignore */ \ - } while (0) + prm_info(&iiod_params, __VA_ARGS__) #define IIO_WARNING(...) \ - do { \ - if (LOG_LEVEL >= Warning_L) \ - fprintf(stderr, "WARNING: " __VA_ARGS__); /* Flawfinder: ignore */ \ - } while (0) + prm_warn(&iiod_params, __VA_ARGS__) #define IIO_ERROR(...) \ - do { \ - if (LOG_LEVEL >= Error_L) \ - fprintf(stderr, "ERROR: " __VA_ARGS__); /* Flawfinder: ignore */ \ - } while (0) + prm_err(&iiod_params, __VA_ARGS__) + +#define IIO_PERROR(err, ...) \ + prm_perror(&iiod_params, err, __VA_ARGS__) #endif /* __IIOD_DEBUG_H__ */ diff --git a/iiod/iiod.c b/iiod/iiod.c index 618a189ce..5b905d5c5 100644 --- a/iiod/iiod.c +++ b/iiod/iiod.c @@ -61,6 +61,9 @@ bool server_demux; struct thread_pool *main_thread_pool; +struct iio_context_params iiod_params = { + .log_level = LEVEL_INFO, +}; static struct sockaddr_in sockaddr = { .sin_family = AF_INET, @@ -160,22 +163,14 @@ static int main_interactive(struct iio_context *ctx, bool verbose, bool use_aio, flags = fcntl(STDIN_FILENO, F_GETFL); if (flags >= 0) flags = fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); - if (flags < 0) { - char err_str[1024]; - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_ERROR("Could not get/set O_NONBLOCK on STDIN_FILENO" - " %s\n", err_str); - } + if (flags < 0) + IIO_PERROR(errno, "Could not get/set O_NONBLOCK on stdin"); flags = fcntl(STDOUT_FILENO, F_GETFL); if (flags >= 0) flags = fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); - if (flags < 0) { - char err_str[1024]; - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_ERROR("Could not get/set O_NONBLOCK on STDOUT_FILENO" - " %s\n", err_str); - } + if (flags < 0) + IIO_PERROR(errno, "Could not get/set O_NONBLOCK on stdout"); } interpreter(ctx, STDIN_FILENO, STDOUT_FILENO, verbose, @@ -193,12 +188,11 @@ static int main_server(struct iio_context *ctx, bool debug, keepalive_intvl = 10, keepalive_probes = 6; struct pollfd pfd[2]; - char err_str[1024]; bool ipv6; IIO_INFO("Starting IIO Daemon version %u.%u.%s\n", - LIBIIO_VERSION_MAJOR, LIBIIO_VERSION_MINOR, - LIBIIO_VERSION_GIT); + LIBIIO_VERSION_MAJOR, LIBIIO_VERSION_MINOR, + LIBIIO_VERSION_GIT); sockaddr.sin_port = htons(port); sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); @@ -212,16 +206,13 @@ static int main_server(struct iio_context *ctx, bool debug, if (!ipv6) fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); if (fd < 0) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_ERROR("Unable to create socket: %s\n", err_str); + IIO_PERROR(errno, "Unable to create socket"); return EXIT_FAILURE; } ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); - if (ret < 0) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_WARNING("setsockopt SO_REUSEADDR : %s\n", err_str); - } + if (ret < 0) + IIO_PERROR(errno, "Failed to set SO_REUSEADDR"); #ifdef HAVE_IPV6 if (ipv6) @@ -231,8 +222,7 @@ static int main_server(struct iio_context *ctx, bool debug, if (!ipv6) ret = bind(fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)); if (ret < 0) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_ERROR("Bind failed: %s\n", err_str); + IIO_PERROR(errno, "Bind failed"); goto err_close_socket; } @@ -243,8 +233,7 @@ static int main_server(struct iio_context *ctx, bool debug, struct sockaddr_in sin; socklen_t len = sizeof(sin); if (getsockname(fd, (struct sockaddr *)&sin, &len) == -1) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_ERROR("getsockname failed : %s\n", err_str); + IIO_PERROR(errno, "getsockname failed"); goto err_close_socket; } port = ntohs(sin.sin_port); @@ -255,8 +244,7 @@ static int main_server(struct iio_context *ctx, bool debug, IIO_INFO("IPv6 support enabled\n"); if (listen(fd, 16) < 0) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_ERROR("Unable to mark as passive socket: %s\n", err_str); + IIO_PERROR(errno, "Unable to mark as passive socket"); goto err_close_socket; } @@ -291,9 +279,8 @@ static int main_server(struct iio_context *ctx, bool debug, if (new == -1) { if (errno == EAGAIN || errno == EINTR) continue; - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_ERROR("Failed to create connection socket: %s\n", - err_str); + + IIO_PERROR(errno, "Failed to create connection socket"); continue; } @@ -308,33 +295,27 @@ static int main_server(struct iio_context *ctx, bool debug, * and disconnect the client if no reply was received for one * minute. */ ret = setsockopt(new, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)); - if (ret < 0) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_WARNING("setsockopt SO_KEEPALIVE : %s", err_str); - } + if (ret < 0) + IIO_PERROR(errno, "setsockopt SO_KEEPALIVE"); + ret = setsockopt(new, IPPROTO_TCP, TCP_KEEPCNT, &keepalive_probes, sizeof(keepalive_probes)); - if (ret < 0) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_WARNING("setsockopt TCP_KEEPCNT : %s", err_str); - } + if (ret < 0) + IIO_PERROR(errno, "setsockopt TCP_KEEPCNT"); + ret = setsockopt(new, IPPROTO_TCP, TCP_KEEPIDLE, &keepalive_time, sizeof(keepalive_time)); - if (ret < 0) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_WARNING("setsockopt TCP_KEEPIDLE : %s", err_str); - } + if (ret < 0) + IIO_PERROR(errno, "setsockopt TCP_KEEPIDLE"); + ret = setsockopt(new, IPPROTO_TCP, TCP_KEEPINTVL, &keepalive_intvl, sizeof(keepalive_intvl)); - if (ret < 0) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_WARNING("setsockopt TCP_KEEPINTVL : %s", err_str); - } + if (ret < 0) + IIO_PERROR(errno, "setsockopt TCP_KEEPINTVL"); + ret = setsockopt(new, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); - if (ret < 0) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_WARNING("setsockopt TCP_NODELAY : %s", err_str); - } + if (ret < 0) + IIO_PERROR(errno, "setsockopt TCP_NODELAY"); cdata->fd = new; cdata->ctx = ctx; @@ -342,7 +323,7 @@ static int main_server(struct iio_context *ctx, bool debug, cdata->xml_zstd = xml_zstd; cdata->xml_zstd_len = xml_zstd_len; - if (LOG_LEVEL >= Info_L) { + if (iiod_params.log_level >= LEVEL_INFO) { struct sockaddr_in *caddr4 = (struct sockaddr_in *)&caddr; char ipaddr[IP_ADDR_LEN]; int zone = 0; @@ -359,8 +340,7 @@ static int main_server(struct iio_context *ctx, bool debug, } if (!inet_ntop(caddr4->sin_family, addr, ipaddr, sizeof(ipaddr) - 1)) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_ERROR("Error during inet_ntop: %s\n", err_str); + IIO_PERROR(errno, "Error during inet_ntop"); } else { ipaddr[IP_ADDR_LEN - 1] = '\0'; @@ -382,9 +362,7 @@ static int main_server(struct iio_context *ctx, bool debug, ret = thread_pool_add_thread(main_thread_pool, client_thd, cdata, "net_client_thd"); if (ret) { - iio_strerror(ret, err_str, sizeof(err_str)); - IIO_ERROR("Failed to create new client thread: %s\n", - err_str); + IIO_PERROR(ret, "Failed to create new client thread"); close(new); free(cdata); } @@ -556,16 +534,14 @@ int main(int argc, char **argv) main_thread_pool = thread_pool_new(); if (!main_thread_pool) { - iio_strerror(errno, err_str, sizeof(err_str)); - IIO_ERROR("Unable to create thread pool: %s\n", err_str); + IIO_PERROR(errno, "Unable to create thread pool"); return EXIT_FAILURE; } if (WITH_IIOD_USBD && ffs_mountpoint) { ret = init_usb_daemon(ffs_mountpoint, nb_pipes); if (ret < 0) { - iio_strerror(-ret, err_str, sizeof(err_str)); - IIO_ERROR("Unable to init USB: %s\n", err_str); + IIO_PERROR(ret, "Unable to init USB"); thread_pool_destroy(main_thread_pool); return EXIT_FAILURE; @@ -602,15 +578,13 @@ static int start_iiod(const char *uri, const char *ffs_mountpoint, int ep0_fd) { struct iio_context *ctx; - char err_str[1024]; void *xml_zstd; size_t xml_zstd_len = 0; int ret; - ctx = iio_create_context(NULL, uri); + ctx = iio_create_context(&iiod_params, uri); if (iio_err(ctx)) { - iio_strerror(-iio_err(ctx), err_str, sizeof(err_str)); - IIO_ERROR("Unable to create local context: %s\n", err_str); + IIO_PERROR(iio_err(ctx), "Unable to create local context"); return EXIT_FAILURE; } @@ -629,8 +603,7 @@ static int start_iiod(const char *uri, const char *ffs_mountpoint, debug, true, (unsigned int) nb_pipes, ep0_fd, main_thread_pool, xml_zstd, xml_zstd_len); if (ret) { - iio_strerror(-ret, err_str, sizeof(err_str)); - IIO_ERROR("Unable to start USB daemon: %s\n", err_str); + IIO_PERROR(ret, "Unable to start USB daemon"); ret = EXIT_FAILURE; goto out_free_xml_data; } @@ -641,8 +614,7 @@ static int start_iiod(const char *uri, const char *ffs_mountpoint, debug, main_thread_pool, xml_zstd, xml_zstd_len); if (ret) { - iio_strerror(-ret, err_str, sizeof(err_str)); - IIO_ERROR("Unable to start serial daemon: %s\n", err_str); + IIO_PERROR(ret, "Unable to start serial daemon"); ret = EXIT_FAILURE; goto out_thread_pool_stop; } diff --git a/iiod/responder.c b/iiod/responder.c index 276c23998..d175237d9 100644 --- a/iiod/responder.c +++ b/iiod/responder.c @@ -725,14 +725,14 @@ static void handle_transfer_block(struct parser_pdata *pdata, buf = get_iio_buffer(pdata, cmd, &entry); ret = iio_err(buf); if (ret) { - IIO_ERROR("handle_transfer_block: Could not find IIO buffer\n"); + IIO_PERROR(ret, "handle_transfer_block: Could not find IIO buffer"); return; } block = get_iio_block(pdata, entry, cmd, &block_entry); ret = iio_err(block); if (ret) { - IIO_ERROR("handle_transfer_block: Could not find IIO block\n"); + IIO_PERROR(ret, "handle_transfer_block: Could not find IIO block"); return; } diff --git a/iiod/serial.c b/iiod/serial.c index 23770b403..d1b4c109c 100644 --- a/iiod/serial.c +++ b/iiod/serial.c @@ -77,8 +77,9 @@ static int serial_configure(int fd, unsigned int uart_bps, err = tcgetattr(fd, &tty_attrs); if (err == -1) { - IIO_ERROR("tcgetattr failed\n"); - return -errno; + err = -errno; + IIO_PERROR(err, "tcgetattr failed"); + return err; } tty_attrs.c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN); @@ -221,8 +222,9 @@ static int serial_configure(int fd, unsigned int uart_bps, err = tcsetattr(fd, TCSANOW, &tty_attrs); if (err == -1) { - IIO_ERROR("Unable to apply serial settings\n"); - return -errno; + err = -errno; + IIO_PERROR(err, "Unable to apply serial settings"); + return err; } return 0; diff --git a/iiod/usbd.c b/iiod/usbd.c index 0479ed386..75362e692 100644 --- a/iiod/usbd.c +++ b/iiod/usbd.c @@ -204,7 +204,7 @@ static void usbd_main(struct thread_pool *pool, void *d) ret = handle_event(pdata, &event); if (ret) { - IIO_ERROR("Unable to handle event: %i\n", ret); + IIO_PERROR(ret, "Unable to handle event"); break; }