summaryrefslogtreecommitdiff
path: root/pullreqd.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2021-06-12 08:18:20 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2021-06-12 08:18:20 -0400
commit74ad46535b87acec38f0a76070d3caf0e9986b6d (patch)
treeef64eb3781cb8a0a568ed07e08be871e2868074d /pullreqd.c
parent169463df0e28d011988d3f9fe16af3b7a2db2db9 (diff)
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.
Diffstat (limited to 'pullreqd.c')
-rw-r--r--pullreqd.c44
1 files changed, 14 insertions, 30 deletions
diff --git a/pullreqd.c b/pullreqd.c
index 3645f2f..a0c8c69 100644
--- a/pullreqd.c
+++ b/pullreqd.c
@@ -14,6 +14,7 @@
#include <syslog.h>
#include <unistd.h>
#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();