From 74ad46535b87acec38f0a76070d3caf0e9986b6d Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Sat, 12 Jun 2021 08:18:20 -0400 Subject: Replaced all log messages with the macros from `macros.h`. This was done not only to make it smaller, and easier to read, but also to make it easier for debugging. --- config.c | 3 ++- pullreqd.c | 44 ++++++++++++++------------------------------ 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/config.c b/config.c index 0454b8b..6b1da23 100644 --- a/config.c +++ b/config.c @@ -4,6 +4,7 @@ #include #include #include "config.h" +#include "macros.h" char *read_file(const char *filename, long *size) { /* Open the file. */ @@ -153,7 +154,7 @@ config *parse_config(const char *filename) { if (opt->offset == offsetof(config, port)) { const int port = strtol(val.str, NULL, 0); if (port <= 0 || port > 65535) { - syslog(LOG_ERR, "Invalid port %d. (Valid port must be between 1, and 65535.)", port); + log(LOG_ERR, "Invalid port %d. (Valid port must be between 1, and 65535.)", port); cleanup_config(cfg); free(line); free(buf); diff --git a/pullreqd.c b/pullreqd.c index 3645f2f..a0c8c69 100644 --- a/pullreqd.c +++ b/pullreqd.c @@ -14,6 +14,7 @@ #include #include #include "config.h" +#include "macros.h" #define UNIX_PATH_MAX 108 @@ -73,15 +74,15 @@ void init_daemon(int change_dir, char *path) { } int init_config(char *config_file, config **cfg) { - syslog(LOG_INFO, "Reading config file %s.", config_file); + log(LOG_INFO, "Reading config file %s.", config_file); *cfg = parse_config(config_file); /* Did the config file parser succeed? */ if (*cfg != NULL) { - syslog(LOG_INFO, "Successfully read %s.", config_file); + log(LOG_INFO, "Successfully read %s.", config_file); } else { - syslog(LOG_ERR, "Error reading %s.", config_file); + log(LOG_ERR, "Error reading %s.", config_file); } return (*cfg != NULL); @@ -100,10 +101,8 @@ int init_socket(config *cfg) { if (sock_type == AF_UNIX) { size_t path_length = strlen(cfg->sock); - if (path_length >= UNIX_PATH_MAX) { - syslog(LOG_ERR, "The socket path is too long, and exceeded %i characters.", UNIX_PATH_MAX); - return -1; - } + /* Is the path too long? */ + did_fail(path_length >= UNIX_PATH_MAX, "The socket path is too long, and exceeded %i characters.", UNIX_PATH_MAX); sockaddr_size = sizeof(sock_addr.sa_un); sock_addr.sa_un.sun_family = sock_type; @@ -120,10 +119,7 @@ int init_socket(config *cfg) { const int gai_ret = getaddrinfo(cfg->sock, cfg->port, &hints, &ainfo_root); /* Did getaddrinfo fail? */ - if (gai_ret) { - syslog(LOG_ERR, "getaddrinfo() failed for hostname %s. Reason: %s", cfg->sock, gai_strerror(gai_ret)); - return -1; - } + did_fail_reason(gai_ret, "getaddrinfo() failed for hostname %s.", cfg->sock, gai_strerror(gai_ret)); /* TODO: Create sockets for all resolved addresses, instead of just the first. */ sockaddr_size = ainfo_root->ai_addrlen; @@ -136,33 +132,21 @@ int init_socket(config *cfg) { const int fd = socket(sock_addr.sa.sa_family, SOCK_SEQPACKET, 0); /* Did we fail to create the listen socket? */ - if (fd < 0) { - syslog(LOG_ERR, "Failed to create listen socket. Reason: %s", strerror(errno)); - return -1; - } + did_fail_reason(fd < 0, "Failed to create listen socket.", strerror(errno)); /* Did we fail to bind the listen socket? */ - if (bind(fd, &sock_addr.sa, sockaddr_size) < 0) { - syslog(LOG_ERR, "Failed to bind listen socket. Reason: %s", strerror(errno)); - return -1; - } + did_fail_reason(bind(fd, &sock_addr.sa, sockaddr_size) < 0, "Failed to bind listen socket.", strerror(errno)); int one = 1; /* Did we fail to enable SO_REUSEADDR? */ - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { - syslog(LOG_ERR, "Failed to enable SO_REUSEADDR. Reason: %s", strerror(errno)); - return -1; - } + did_fail_reason(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0, "Failed to enable SO_REUSEADDR.", strerror(errno)); /* Did we fail to listen to the socket? */ - if (listen(fd, 20) < 0) { - syslog(LOG_ERR, "Failed to listen to socket. Reason: %s", strerror(errno)); - return -1; - } + did_fail_reason(listen(fd, 20) < 0, "Failed to listen to socket.", strerror(errno)); - syslog(LOG_INFO, "Successfully created socket descriptor %i for address %s.", fd, cfg->sock); + log(LOG_INFO, "Successfully created socket descriptor %i for address %s.", fd, cfg->sock); return fd; } @@ -198,7 +182,7 @@ int main(int argc, char **argv) { int exit_status = EXIT_FAILURE; init_daemon(1, "/"); - syslog(LOG_NOTICE, "pullreqd started."); + log(LOG_NOTICE, "pullreqd started."); /* Read the config file. */ config_read = init_config(config_file, &cfg); @@ -216,7 +200,7 @@ int main(int argc, char **argv) { cleanup(cfg, listen_socket); - syslog(LOG_NOTICE, "pullreqd stopped."); + log(LOG_NOTICE, "pullreqd stopped."); closelog(); -- cgit v1.2.3-13-gbd6f