diff options
Diffstat (limited to 'pullreqd.c')
-rw-r--r-- | pullreqd.c | 44 |
1 files changed, 14 insertions, 30 deletions
@@ -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(); |