Replace fprintf() with syslog()

This commit is contained in:
Alex D. 2021-01-16 22:40:39 +00:00
parent 6fb4c44039
commit b0350595ba
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
10 changed files with 84 additions and 88 deletions

View File

@ -20,17 +20,13 @@
#include "logging.h"
#include <errno.h> // errno
#include <stdio.h> // fprintf()
#include <stdlib.h>
#include <errno.h> // errno
#include <stdio.h> // fprintf()
#include <string.h> // strerror()
#include <sys/types.h> // size_t ssize_t
#include <syslog.h> // syslog()
#include <unistd.h> // write()
#define UIRC_IRCV3
#define UIRC_HELPERS
#include <uirc/uirc.h> // Tok_mesg()
ssize_t
get_buffer_line(char* buf)
{
@ -52,11 +48,11 @@ flush_buffer(char* buf, size_t buflen, int fd)
for (;;) {
if ((size_t)(res = write(fd, pos, buflen - (size_t)(pos - buf))) != buflen - (size_t)(pos - buf)) {
if (res == -1 && errno != EINTR) {
LOG(LOG_WARN, "Couldn't flush buffer to fd %i. " ERRNOFMT, fd, strerror(errno), errno);
LOG(LOG_WARNING, "Couldn't flush buffer to fd %i: " ERRNOFMT, fd, strerror(errno), errno);
return -1;
}
pos += res;
LOG(LOG_DEBUG, "Wrote %lu bytes to fd %i.", res, fd);
LOG(LOG_DEBUG, "Wrote %lu bytes to fd %i", res, fd);
} else
return res;
}
@ -68,10 +64,10 @@ read_buffer(Buffer_Info* buf)
ssize_t brd;
if ((brd = read(buf->fd, buf->buffer + buf->append_pos, buf->csize - buf->append_pos - 1)) > 0) {
*(buf->buffer + (buf->append_pos += (size_t) brd)) = '\0';
LOG(LOG_DEBUG, "Read %li bytes from socket buffer. Now appending at %li with \"%s\" so far.", brd, buf->append_pos, buf->buffer);
LOG(LOG_DEBUG, "Read %li bytes from socket buffer and currently at position %li", brd, buf->append_pos, buf->buffer);
} else if (brd == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return 0;
LOG(LOG_WARN, "Failed to read inbound traffic on fd %i. " ERRNOFMT, buf->fd, strerror(errno), errno);
LOG(LOG_WARNING, "Failed to read inbound traffic on fd %i: " ERRNOFMT, buf->fd, strerror(errno), errno);
}
return brd;
}

View File

@ -28,6 +28,7 @@
#include <string.h> // strerror()
#include <sys/socket.h> // sockaddr connect()
#include <sys/types.h> // size_t ssize_t socklen_t
#include <syslog.h> // syslog()
int
init_chanarray(Channel** chans)
@ -37,7 +38,7 @@ init_chanarray(Channel** chans)
if ((*chans = malloc(sizeof(Channel))) != NULL) {
memset(*chans, '\0', sizeof(Channel));
} else {
LOG(LOG_WARN, "Could not initialise channel array. " ERRNOFMT, strerror(errno), errno);
LOG(LOG_ERR, "Could not initialise channel array: " ERRNOFMT, strerror(errno), errno);
return 0;
}
}
@ -49,13 +50,13 @@ resize_chanarray(Channel** chans)
{
unsigned int i = get_channelindex(NULL, *chans);
Channel* tmp = NULL;
LOG(LOG_DEBUG, "Found %i existing channels.", i);
LOG(LOG_DEBUG, "Found %i existing channels", i);
if ((tmp = realloc(*chans, (i + 2) * sizeof(Channel))) != NULL) {
*chans = tmp;
tmp[i + 1].name = NULL;
tmp[i + 1].key = NULL;
} else {
LOG(LOG_WARN, "Could not allocate channel struct. " ERRNOFMT, strerror(errno), errno);
LOG(LOG_ERR, "Could not allocate channel struct: " ERRNOFMT, strerror(errno), errno);
return -1;
}
return (signed int) i;
@ -67,13 +68,13 @@ set_channel(Channel* chan, const char* name, const char* key, bool joined)
LOG(LOG_DEBUG, "Setting channel %s", (name == NULL) ? chan->name : name);
if (name != NULL) {
if (!allocate_copy(&chan->name, name)) {
LOG(LOG_WARN, "Couldn't allocate memory for the channel name %s. " ERRNOFMT, name, strerror(errno), errno);
LOG(LOG_ERR, "Couldn't allocate memory for the channel name %s: " ERRNOFMT, name, strerror(errno), errno);
return 0;
}
}
if (key != NULL) {
if (!allocate_copy(&chan->key, key)) {
LOG(LOG_WARN, "Couldn't allocate memory for the channel %s key. " ERRNOFMT, (name == NULL) ? chan->name : name, strerror(errno), errno);
LOG(LOG_ERR, "Couldn't allocate memory for the channel %s key: " ERRNOFMT, (name == NULL) ? chan->name : name, strerror(errno), errno);
return 0;
}
}

View File

@ -23,6 +23,8 @@
#include "logging.h"
#include "memory.h"
#include <syslog.h> // syslog()
#ifdef UIRCD_FEATURE_LIBCONFIG
#include <libconfig.h> // config_t config_setting_t config_init() ...
@ -73,11 +75,11 @@ parse_configfile(char* config_path, Connection* conn)
};
mapconf(user, usermaps, sizeof(usermaps) / sizeof(*usermaps));
}
LOG(LOG_DEBUG, "%s.", "Parsed configuration file successfully");
LOG(LOG_DEBUG, "Parsed configuration file successfully");
res = 1;
}
} else
LOG(LOG_WARN, "Encountered a error while reading the config file. %s in %s line %d", config_error_text(&conf), config_error_file(&conf), config_error_line(&conf));
LOG(LOG_WARNING, "Encountered a error while reading the config file: %s :\"%s\" line %d", config_error_text(&conf), config_error_file(&conf), config_error_line(&conf));
config_destroy(&conf);
return res;
}
@ -90,9 +92,9 @@ mapconf(config_setting_t* setting, Mapping* mapping, unsigned long len)
config_setting_lookup_string(setting, mapping[i].str, &var);
if (var != NULL) {
if (allocate_copy(mapping[i].save, var)) {
LOG(LOG_DEBUG, "Saved %s to %p", var, (const void*) mapping[i].save);
LOG(LOG_DEBUG, "Allocated %s to %p", var, (const void*) mapping[i].save);
} else {
LOG(LOG_WARN, "Failed to allocate memory to save config variable %s = %s", mapping[i].str, var);
LOG(LOG_ERR, "Failed to allocate memory to save config variable %s as %s", mapping[i].str, var);
}
}
}

View File

@ -32,6 +32,7 @@
#include <string.h> // strerror()
#include <sys/socket.h> // sockaddr connect()
#include <sys/types.h> // size_t ssize_t socklen_t
#include <syslog.h>
#include <time.h>
#include <unistd.h> // ??
@ -45,20 +46,20 @@ init_connection(Connection* conn)
struct addrinfo* addr_info;
if ((getaddrres = getaddrinfo(conn->data.address, conn->data.service, NULL, &addr_info)) != 0) {
freeaddrinfo(addr_info);
LOG(LOG_WARN, "Failed to get address info for " ADDRFMT ". " ERRNOFMT, conn->data.address, conn->data.service, gai_strerror(getaddrres), getaddrres);
LOG(LOG_WARNING, "Failed to get address info for " ADDRFMT ": " ERRNOFMT, conn->data.address, conn->data.service, gai_strerror(getaddrres), getaddrres);
if (getaddrres != EAI_AGAIN && getaddrres != EAI_NONAME) return INIT_HARDFAIL;
else
return INIT_SOFTFAIL;
}
if ((sockfd = socket(addr_info->ai_family, addr_info->ai_socktype, addr_info->ai_protocol)) < 0) {
LOG(LOG_ERROR, "Failed to open a socket for " ADDRFMT ". " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
LOG(LOG_WARNING, "Failed to open a socket for " ADDRFMT ": " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
freeaddrinfo(addr_info);
return INIT_HARDFAIL;
}
if ((connectres = connect(sockfd, addr_info->ai_addr, addr_info->ai_addrlen)) == -1) {
close(sockfd);
freeaddrinfo(addr_info);
LOG(LOG_ERROR, "Failed to connect to host " ADDRFMT ". " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
LOG(LOG_WARNING, "Failed to connect to host " ADDRFMT ": " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
if (errno != EADDRNOTAVAIL && errno != ETIMEDOUT && errno != ECONNRESET && errno != ECONNREFUSED) return INIT_HARDFAIL;
else
return INIT_SOFTFAIL;
@ -136,22 +137,22 @@ auto_msg_actions(IRC_Message* mesg, Connection* conn, Buffer_Info* buf)
time_t ctime = time(NULL);
switch (mesg->cmd) {
case (PING): {
LOG(LOG_DEBUG, "Auto-replying to ping \"%s\".", mesg->args[0]);
LOG(LOG_DEBUG, "Auto-replying to ping \"%s\"", mesg->args[0]);
if ((len = Assm_mesg(buf->buffer, Assm_cmd_PONG(mesg->args[0], NULL), buf->csize)) > 0)
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
LOG(LOG_WARN, "Couldn't pong " ADDRFMT ". " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
LOG(LOG_WARNING, "Couldn't pong " ADDRFMT ". " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
conn->info.state = CONN_RECONNECTING;
return 0;
}
break;
}
case (PONG): {
if (mesg->trailing && mesg->args[1] != NULL) { LOG(LOG_DEBUG, "Got PONG back with mesg \"%s\".", mesg->args[1]); }
if (mesg->trailing && mesg->args[1] != NULL) { LOG(LOG_DEBUG, "Got PONG with mesg \"%s\"", mesg->args[1]); }
break;
}
/* Autojoin channels from current conn on first response from the server */
case (RPL_WELCOME): {
LOG(LOG_INFO, "Connection established to " ADDRFMT ".", conn->data.address, conn->data.service);
LOG(LOG_NOTICE, "Connection established to " ADDRFMT, conn->data.address, conn->data.service);
conn->info.state = CONN_ACTIVE;
conn->info.reconinter = 0;
if (!commit_channelist(buf, conn)) return 0;
@ -161,14 +162,14 @@ auto_msg_actions(IRC_Message* mesg, Connection* conn, Buffer_Info* buf)
case (CAP): {
if (mesg->args[1] != NULL) {
if (strcmp(mesg->args[1], "LS") == 0) {
LOG(LOG_VERBOSE,
LOG(LOG_INFO,
"Requesting capabilities \"%s\" on " ADDRFMT ".",
mesg->args[2],
conn->data.address,
conn->data.service);
if ((len = Assm_mesg(buf->buffer, Assm_cmd_CAP_REQ(mesg->args[2]), buf->csize)) > 0) {
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
LOG(LOG_WARN,
LOG(LOG_WARNING,
"Couldn't request capabilities \"%s\" on " ADDRFMT ". " ERRNOFMT,
mesg->args[2],
conn->data.address,
@ -180,10 +181,10 @@ auto_msg_actions(IRC_Message* mesg, Connection* conn, Buffer_Info* buf)
}
}
} else if (strcmp(mesg->args[1], "ACK") == 0) {
LOG(LOG_VERBOSE, "Ending capability negotiation on " ADDRFMT ".", conn->data.address, conn->data.service);
LOG(LOG_INFO, "Ending capability negotiation on " ADDRFMT ".", conn->data.address, conn->data.service);
if ((len = Assm_mesg(buf->buffer, Assm_cmd_CAP_END(), buf->csize)) > 0) {
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
LOG(LOG_WARN,
LOG(LOG_WARNING,
"Couldn't end capability negotiation on " ADDRFMT ". " ERRNOFMT,
conn->data.address,
conn->data.service,
@ -199,7 +200,7 @@ auto_msg_actions(IRC_Message* mesg, Connection* conn, Buffer_Info* buf)
}
*/
case (ERROR): {
LOG(LOG_ERROR, "Received error: %s.", mesg->args[0]);
LOG(LOG_ERR, "Received error: %s", mesg->args[0]);
break;
}
case (JOIN):
@ -210,7 +211,7 @@ auto_msg_actions(IRC_Message* mesg, Connection* conn, Buffer_Info* buf)
}
case (ERR_NICKNAMEINUSE):
case (ERR_NICKCOLLISION): {
LOG(LOG_ERROR, "Nickname \"%s\" is already taken.", conn->user.nickname);
LOG(LOG_WARNING, "Nickname \"%s\" is already taken", conn->user.nickname);
conn->info.state = CONN_RECONNECTING;
break;
}
@ -226,7 +227,13 @@ commit_channelist(Buffer_Info* buf, Connection* conn)
IRC_Message msg = { .args = { conn->info.channels[i].name, conn->info.channels[i].key, NULL }, .trailing = true, .cmd = (conn->info.channels[i].joined) ? JOIN : PART };
if ((len = Assm_mesg(buf->buffer, &msg, buf->csize)) > 0) {
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
LOG(LOG_WARN, "Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT, conn->info.channels[i].name, conn->data.address, conn->data.service, strerror(errno), errno);
LOG(LOG_WARNING,
"Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT,
conn->info.channels[i].name,
conn->data.address,
conn->data.service,
strerror(errno),
errno);
conn->info.state = CONN_RECONNECTING;
return 0;
}
@ -242,9 +249,9 @@ register_user(Connection* conn, Buffer_Info* buf)
/* Registration process and CAP negotiation (TODO)
if ((temp = Assm_mesg(buf.send[0].buffer, Assm_cmd_CAP_LS("302"), sizeof(buf.send[0].buffer))) > 0) {
buf.send[0].append_pos = (size_t) temp;
LOG(LOG_VERBOSE, "Sending a CAP LS to " ADDRFMT ".", connection.data.address, connection.data.service);
LOG(LOG_INFO, "Sending a CAP LS to " ADDRFMT ".", connection.data.address, connection.data.service);
if (flush_buffer(buf.send[0].buffer, buf.send[0].append_pos, buf.send[0].fd) == -1) {
LOG(LOG_WARN,
LOG(LOG_WARNING,
"Couldn't send CAP LS to " ADDRFMT ". " ERRNOFMT,
connection.data.address,
connection.data.service,
@ -258,7 +265,7 @@ register_user(Connection* conn, Buffer_Info* buf)
if (conn->user.password != NULL && (temp = Assm_mesg(buf->buffer, Assm_cmd_PASS(conn->user.password), buf->csize)) > 0) {
buf->append_pos = (size_t) temp;
if (flush_buffer(buf->buffer, buf->append_pos, buf->fd) != -1) {
LOG(LOG_VERBOSE, "Sent PASS authentication to " ADDRFMT, conn->data.address, conn->data.service);
LOG(LOG_INFO, "Sent PASS authentication to " ADDRFMT, conn->data.address, conn->data.service);
} else {
conn->info.state = CONN_RECONNECTING;
return 0;
@ -267,7 +274,7 @@ register_user(Connection* conn, Buffer_Info* buf)
if ((temp = Assm_mesg(buf->buffer, Assm_cmd_NICK(conn->user.nickname), buf->csize)) > 0) {
buf->append_pos = (size_t) temp;
if (flush_buffer(buf->buffer, buf->append_pos, buf->fd) != -1) {
LOG(LOG_VERBOSE, "Sent NICK registration to " ADDRFMT " with nickname \"%s\".", conn->data.address, conn->data.service, conn->user.nickname);
LOG(LOG_INFO, "Sent NICK registration to " ADDRFMT " with nickname \"%s\".", conn->data.address, conn->data.service, conn->user.nickname);
} else {
conn->info.state = CONN_RECONNECTING;
return 0;
@ -276,7 +283,7 @@ register_user(Connection* conn, Buffer_Info* buf)
if ((temp = Assm_mesg(buf->buffer, Assm_cmd_USER(conn->user.username, conn->user.realname, 0), buf->csize)) > 0) {
buf->append_pos = (size_t) temp;
if (flush_buffer(buf->buffer, buf->append_pos, buf->fd) != -1) {
LOG(LOG_VERBOSE,
LOG(LOG_INFO,
"Sent USER registration to " ADDRFMT " with username \"%s\" and realname \"%s\".",
conn->data.address,
conn->data.service,

View File

@ -31,6 +31,7 @@
#include <string.h> // strerror()
#include <sys/stat.h> // mkfifo() mkdir()
#include <sys/types.h> // size_t ssize_t mode_t
#include <syslog.h> // syslog()
int
mkdir_bottomup(char* path)
@ -43,11 +44,11 @@ mkdir_bottomup(char* path)
if (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
if (errno != EEXIST) {
*(x + 1) = save;
LOG(LOG_ERROR, "Could not create directory \"%s\".", path);
LOG(LOG_ERR, "Could not create directory \"%s\": " ERRNOFMT, path, strerror(errno), errno);
return 0;
}
} else
LOG(LOG_DEBUG, "Created directory at: %s", path);
LOG(LOG_DEBUG, "Created directory at \"%s\"", path);
*(x + 1) = save;
x++;
}
@ -60,16 +61,16 @@ makeinput(const char* path)
{
if (path == NULL) return -1;
if (mkfifo(path, S_IRUSR | S_IWUSR | S_IWGRP) == 0) {
LOG(LOG_VERBOSE, "Created a FIFO pipe for input at %s.", path);
LOG(LOG_DEBUG, "Created a FIFO pipe for input at \"%s\"", path);
} else if (errno != EEXIST) {
LOG(LOG_WARN, "Couldn't create FIFO at \"%s\" for input. " ERRNOFMT, path, strerror(errno), errno);
LOG(LOG_WARNING, "Couldn't create FIFO at \"%s\" for input: " ERRNOFMT, path, strerror(errno), errno);
return -1;
}
int fd;
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) == -1) {
LOG(LOG_WARN, "Couldn't open FIFO pipe \"%s\" for reading. " ERRNOFMT, path, strerror(errno), errno);
LOG(LOG_WARNING, "Couldn't open FIFO pipe \"%s\" for reading: " ERRNOFMT, path, strerror(errno), errno);
} else
LOG(LOG_DEBUG, "Opened \"%s\" for reading.", path);
LOG(LOG_DEBUG, "Opened FIFO pipe \"%s\" for reading", path);
return fd;
}
@ -79,11 +80,11 @@ write_log(const char* path, const char* message)
FILE* logfile;
if ((logfile = fopen(path, "a")) != NULL) {
fprintf(logfile, "%s", message);
LOG(LOG_DEBUG, "Wrote to log %s.", path);
LOG(LOG_DEBUG, "Wrote to log %s", path);
fclose(logfile);
return 1;
} else
LOG(LOG_WARN, "Couldn't open file \"%s\" for appending.", path);
LOG(LOG_WARNING, "Couldn't open file \"%s\" for appending: " ERRNOFMT, path, strerror(errno), errno);
return 0;
}
@ -106,11 +107,11 @@ add_socket_flags(int fd, int flags)
int cflgs;
if ((cflgs = fcntl(fd, F_GETFL)) != -1) {
if (fcntl(fd, F_SETFL, cflgs | flags) == -1) {
LOG(LOG_WARN, "Couldn't add socket flags. " ERRNOFMT, strerror(errno), errno);
LOG(LOG_WARNING, "Couldn't add socket flags: " ERRNOFMT, strerror(errno), errno);
} else
return 1;
} else
LOG(LOG_WARN, "Failed to get socket flags. " ERRNOFMT, strerror(errno), errno);
LOG(LOG_WARNING, "Failed to get socket flags: " ERRNOFMT, strerror(errno), errno);
return 0;
}

View File

@ -17,7 +17,3 @@
*/
#include "logging.h"
int loglevel = LOG_INFO;
const char logchars[] = { [LOG_DEBUG] = 'D', [LOG_VERBOSE] = 'V', [LOG_INFO] = 'I', [LOG_WARN] = 'W', [LOG_ERROR] = 'E', [LOG_FATAL] = 'F' };

View File

@ -19,21 +19,10 @@
#ifndef UIRCD_GUARD_LOGGING
#define UIRCD_GUARD_LOGGING
#define LOG_FATAL 0
#define LOG_ERROR 1
#define LOG_WARN 2
#define LOG_INFO 3
#define LOG_VERBOSE 4
#define LOG_DEBUG 5
#define ERRNOFMT "%s (%i)"
#define ADDRFMT "%s:%s"
#define LOG(LEVEL, FORMAT, ...) \
if (LEVEL <= loglevel) fprintf(stderr, "[%c:L%i] " FORMAT "\n", logchars[LEVEL], __LINE__, __VA_ARGS__)
extern const char logchars[];
extern int loglevel;
#define LOG(LEVEL, ...) syslog(LEVEL, __VA_ARGS__)
#endif /* UIRCD_GUARD_LOGGING */

View File

@ -32,6 +32,7 @@
#include <stdlib.h> // EXIT_SUCCESS EXIT_FAILURE
#include <string.h> // strerror()
#include <sys/types.h> // time_t size_t
#include <syslog.h> // openlog() syslog()
#include <time.h> // time()
#include <unistd.h> // getopt() chdir() close()
@ -54,8 +55,9 @@ parse_args(int argc, char** argv, Connection* conn)
!= -1) {
switch (c) {
case 'V': {
loglevel = atoi(optarg);
LOG(LOG_DEBUG, "Log level was set to %i", loglevel);
int levelmask = atoi(optarg);
setlogmask(LOG_UPTO(levelmask));
LOG(LOG_DEBUG, "Log level was set to %i", levelmask);
break;
}
case 'a': allocate_copy(&conn->data.address, optarg); break;
@ -74,11 +76,11 @@ parse_args(int argc, char** argv, Connection* conn)
case 'c': {
resize_chanarray(&conn->info.channels);
chanindex = get_channelindex(optarg, conn->info.channels);
if (!set_channel(&conn->info.channels[chanindex], optarg, NULL, true)) LOG(LOG_WARN, "Couldn't set channel %s as chan #%lu.", optarg, chanindex);
if (!set_channel(&conn->info.channels[chanindex], optarg, NULL, true)) LOG(LOG_WARNING, "Couldn't set channel %s as chan #%lu", optarg, chanindex);
break;
}
case 'k': {
if (!set_channel(&conn->info.channels[chanindex], NULL, optarg, true)) LOG(LOG_WARN, "Couldn't set key for channel #%lu.", chanindex);
if (!set_channel(&conn->info.channels[chanindex], NULL, optarg, true)) LOG(LOG_WARNING, "Couldn't set key for channel #%lu", chanindex);
break;
}
case 'v': printf("uIRCd version " UIRCD_VERSION); return 0;
@ -108,7 +110,7 @@ print_help(void)
{ 'd', "str", "Directory for logs", "current dir" },
{ 'q', "str", "Quit message", UIRCD_DEFAULT_QUITMSG },
{ 't', "uint", "Timeout duration", NULL },
{ 'V', "int", "Log level", "3 [LOG_INFO]" },
{ 'V', "int", "Log level", "7 [LOG_INFO]" },
{ 'c', "str", "Channel", NULL },
{ 'k', "str", "Channel key", NULL },
#ifdef UIRCD_FEATURE_LIBCONFIG
@ -146,9 +148,11 @@ main(int argc, char* argv[])
/*
* Initialisation
*/
openlog("uIRCd", LOG_CONS | LOG_PID | LOG_PERROR, LOG_DAEMON);
setlogmask(LOG_UPTO(LOG_INFO));
init_chanarray(&connection.info.channels);
if (!set_config_defaults(&connection)) {
LOG(LOG_FATAL, "Couldn't allocate memory for configuration variables. " ERRNOFMT, strerror(errno), errno);
LOG(LOG_ERR, "Couldn't allocate memory for configuration variables: " ERRNOFMT, strerror(errno), errno);
return EXIT_FAILURE;
}
switch (parse_args(argc, argv, &connection)) {
@ -159,10 +163,10 @@ main(int argc, char* argv[])
int tmpres;
// TODO: Add chroot()/jail support by default where supported
if ((tmpres = chdir(connection.data.path)) != 0) {
LOG(LOG_ERROR, "Couldn't change log directory to %s. " ERRNOFMT, connection.data.path, strerror(errno), errno);
LOG(LOG_ERR, "Couldn't change log directory to %s: " ERRNOFMT, connection.data.path, strerror(errno), errno);
return EXIT_FAILURE;
}
LOG(LOG_VERBOSE, "Changed root directory to %s.", connection.data.path);
LOG(LOG_INFO, "Changed root directory to %s", connection.data.path);
}
setup_signals();
@ -172,19 +176,19 @@ main(int argc, char* argv[])
for (;;) {
ctime = time(NULL);
if (connection.info.state == CONN_CLOSED) {
LOG(LOG_VERBOSE, "%s", "Exiting gracefully.");
LOG(LOG_INFO, "Exiting gracefully");
return EXIT_SUCCESS;
} else if (!run || connection.info.state == CONN_CLOSING) {
signed long temp;
if ((temp = Assm_mesg(buf.send[0].buffer, Assm_cmd_QUIT(connection.data.quitmsg), buf.send[0].csize)) > 0) {
buf.send[0].append_pos = (unsigned long) temp;
if (flush_buffer(buf.send[0].buffer, buf.send[0].append_pos, buf.send[0].fd) != -1)
LOG(LOG_VERBOSE, "Sent a QUIT message to " ADDRFMT " containing \"%s\".", connection.data.address, connection.data.service, connection.data.quitmsg);
LOG(LOG_INFO, "Sent a QUIT message to " ADDRFMT " containing \"%s\"", connection.data.address, connection.data.service, connection.data.quitmsg);
}
reset_buffer(&buf.recv[0], 2); // net rw/FIFO read fd
reset_buffer(&buf.recv[1], 2); // FIFO read fd
connection.info.state = CONN_CLOSED;
LOG(LOG_VERBOSE, "Connection to " ADDRFMT " was closed.", connection.data.address, connection.data.service);
LOG(LOG_INFO, "Connection to " ADDRFMT " was closed", connection.data.address, connection.data.service);
continue;
} else if (connection.info.state == CONN_RECONNECTING) {
connection.info.state = CONN_PENDING;
@ -224,7 +228,7 @@ main(int argc, char* argv[])
} else if (connection.info.state == CONN_IDLE) {
if (connection.info.state == CONN_IDLE) nanosleep(&sleep, NULL);
if (connection.data.timeout > 0 && connection.info.l_message < ctime - connection.data.timeout) {
LOG(LOG_WARN, "Timed out because no message was received since %lu.", connection.info.l_message);
LOG(LOG_WARNING, "Timed out because no message was received since %lu", connection.info.l_message);
connection.info.state = CONN_RECONNECTING;
continue;
}
@ -247,7 +251,7 @@ main(int argc, char* argv[])
}
}
} else
LOG(LOG_WARN, "%s", "Received invalid IRC message on FIFO (see RFC2812).");
LOG(LOG_WARNING, "Received invalid IRC message on FIFO (see RFC2812)");
for (long unsigned int x = 0; x < buf.recv[1].csize && *(buf.recv[1].buffer + len + x); x++) *(buf.recv[1].buffer + x) = *(buf.recv[1].buffer + x + len);
buf.recv[1].append_pos -= (unsigned long) len;
@ -256,7 +260,7 @@ main(int argc, char* argv[])
#ifdef UIRCD_RELAXED_RFC
// TODO: Add resize here
#else /* UIRCD_RELAXED_RFC */
LOG(LOG_WARN, "%s.", "FIFO buffer is full and no message could be parsed. Cleared buffer.");
LOG(LOG_WARNING, "FIFO buffer is full and no message could be parsed");
reset_buffer(&buf.recv[1], false);
#endif /* UIRCD_RELAXED_RFC */
}
@ -290,7 +294,7 @@ main(int argc, char* argv[])
char logpath[UIRCD_LIMITS_PATH];
if (Assm_mesg(buf.log.buffer, &buffer, buf.log.csize) > 0) {
printf("%s\r\n", buf.log.buffer);
printf("%s", buf.log.buffer);
bool match[][3] = { { false, false, false }, { true, false, false }, { true, false, true } };
for (unsigned long i = 0; i < sizeof(match) / sizeof(*match); i++) {
if (get_path(logpath, sizeof(logpath), &connection, &buffer, match[i][0], match[i][1], match[i][2]) >= 0) {
@ -306,19 +310,17 @@ main(int argc, char* argv[])
*(buf.recv[0].buffer + buf.recv[0].append_pos) = '\0';
connection.info.state = CONN_ACTIVE;
} else
LOG(LOG_WARN, "%s", "Received invalid IRC message (see RFC2812).");
LOG(LOG_WARNING, "Received invalid IRC message (see RFC2812).");
} else if (len == -1) {
connection.info.state = CONN_RECONNECTING;
} else if (buf.recv[0].append_pos == buf.recv[0].csize - 1) {
#ifdef UIRCD_RELAXED_RFC
// TODO: Add resize here
#else /* UIRCD_RELAXED_RFC */
LOG(LOG_WARN, "%s.", "Receive buffer is full and no message could be parsed.");
LOG(LOG_WARNING, "Receive buffer is full and no message could be parsed");
connection.info.state = CONN_RECONNECTING;
#endif /* UIRCD_RELAXED_RFC */
}
}
LOG(LOG_FATAL, "%s", "We shouldn't be here.");
return EXIT_FAILURE;
}

View File

@ -24,6 +24,7 @@
#include <stdio.h> // fprintf()
#include <stdlib.h> // malloc() free()
#include <string.h> // strcpy()
#include <syslog.h> // syslog()
/* Description:
* This is a allocation manager that frees or allocates variables to pointers depending on the context.
@ -35,8 +36,8 @@
int
allocate_copy(char** ptr, const char* const var)
{
if (var == NULL) LOG(LOG_DEBUG, "%s.", "Freeing pointer because provided variable has the address of NULL");
if (*ptr != NULL) LOG(LOG_DEBUG, "%s.", "Deallocating already allocated pointer for replacement");
if (var == NULL) LOG(LOG_DEBUG, "Freeing pointer because provided variable has the address of NULL");
if (*ptr != NULL) LOG(LOG_DEBUG, "Deallocating already allocated pointer for replacement");
free(*ptr);
*ptr = NULL;
if (var == NULL) return 1;

View File

@ -25,6 +25,7 @@
#include <stdbool.h> // true, false
#include <stdio.h> // NULL, fprintf()
#include <string.h> // strerror()
#include <syslog.h>
sig_atomic_t volatile run = true;
@ -40,11 +41,11 @@ setup_signals(void)
struct sigaction sa = { .sa_flags = SA_SIGINFO, .sa_sigaction = stop_loop };
sigemptyset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, NULL) == -1) {
LOG(LOG_WARN, "sigaction() failed. Children won't respond to signal SIGINT. " ERRNOFMT, strerror(errno), errno);
LOG(LOG_WARNING, "sigaction() failed: Children won't respond to signal SIGINT: " ERRNOFMT, strerror(errno), errno);
res = 0;
}
if (sigaction(SIGTERM, &sa, NULL) == -1) {
LOG(LOG_WARN, "sigaction() failed. Children won't respond to signal SIGTERM. " ERRNOFMT, strerror(errno), errno);
LOG(LOG_WARNING, "sigaction() failed: Children won't respond to signal SIGTERM: " ERRNOFMT, strerror(errno), errno);
res = 0;
}
return res;