Skip to content

Commit

Permalink
iiod: Switch to use the new Libiio debug macros
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pcercuei committed Dec 4, 2023
1 parent 33fadc9 commit d7772bb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 108 deletions.
42 changes: 9 additions & 33 deletions iiod/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,23 @@
#ifndef __IIOD_DEBUG_H__
#define __IIOD_DEBUG_H__

#include "../iio-config.h"
#include <iio/iio-debug.h>

#include <stdio.h>
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__ */
108 changes: 40 additions & 68 deletions iiod/iiod.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -308,41 +295,35 @@ 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;
cdata->debug = 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;
Expand All @@ -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';

Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions iiod/responder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions iiod/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion iiod/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit d7772bb

Please sign in to comment.