Renamed log_trace() to log()

This commit is contained in:
2016-08-10 20:21:54 +03:00
parent 65b923cba8
commit dcd7a46d0a
10 changed files with 100 additions and 97 deletions

View File

@@ -14,7 +14,7 @@ SOURCES = main.c \
confparser.c \
rmps.c \
enum_functions.c \
log_trace.c \
log.c \
thread_pool.c
OBJECTS = $(SOURCES:.c=.o)

View File

@@ -4,7 +4,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "log_trace.h"
#include "log.h"
#include "confparser.h"
#include "enum_functions.h"
@@ -36,7 +36,7 @@ struct conf_table conf = {
}
};
void confexport()
void confexport(void)
{
printf( "db.type=%s\n"
"db.hostname=%s\n"
@@ -83,7 +83,7 @@ static int fopen_and_mkdir(const char *dir) {
if(*p == '/') {
*p = 0;
if (mkdir(tmp, S_IRWXU) == -1 && errno != EEXIST) {
log_trace( ERROR,
log( ERROR,
"Permission denied to create directory: %s",
tmp );
return 1;
@@ -92,7 +92,7 @@ static int fopen_and_mkdir(const char *dir) {
}
fp = fopen(dir, "a");
if (!fp) {
log_trace(ERROR, "Permission denied to write into: %s", dir);
log(ERROR, "Permission denied to write into: %s", dir);
return 1;
}
fclose(fp);
@@ -100,7 +100,7 @@ static int fopen_and_mkdir(const char *dir) {
}
static int test_conf_perms()
static int test_conf_perms(void)
{
struct stat s;
char confresult[128];
@@ -109,33 +109,33 @@ static int test_conf_perms()
if (err == -1) {
if (errno == ENOENT) {
enumtostr(confresult, CONF_DIR_MISSING);
log_trace(ERROR, confresult);
log(ERROR, confresult);
return 1;
}
} else {
if (!S_ISDIR(s.st_mode)) {
enumtostr(confresult, CONF_DIR_NOTDIR);
log_trace(ERROR, confresult);
log(ERROR, confresult);
return 1;
}
if ( !(S_IRUSR & s.st_mode) ||
!(S_IXUSR & s.st_mode) ) {
enumtostr(confresult, CONF_DIR_PERM);
log_trace(ERROR, confresult);
log(ERROR, confresult);
return 1;
}
if (s.st_uid != 0) {
enumtostr(confresult, CONF_DIR_UID_INSECURE);
log_trace(WARNING, confresult);
log(WARNING, confresult);
}
else if (s.st_gid != 0) {
enumtostr(confresult, CONF_DIR_GID_INSECURE);
log_trace(WARNING, confresult);
log(WARNING, confresult);
}
else if ( (S_IROTH & s.st_mode) ||
(S_IWOTH & s.st_mode) ) {
enumtostr(confresult, CONF_DIR_PERM_INSECURE);
log_trace(WARNING, confresult);
log(WARNING, confresult);
}
}
@@ -144,45 +144,45 @@ static int test_conf_perms()
if (err == -1) {
if (errno == ENOENT) {
enumtostr(confresult, CONF_MISSING);
log_trace(ERROR, confresult);
log(ERROR, confresult);
return 1;
}
} else {
if (!S_ISREG(s.st_mode)) {
enumtostr(confresult, CONF_NOTFILE);
log_trace(ERROR, confresult);
log(ERROR, confresult);
return 1;
}
if (!(S_IRUSR & s.st_mode)) {
enumtostr(confresult, CONF_PERM);
log_trace(ERROR, confresult);
log(ERROR, confresult);
return 1;
}
if (s.st_uid != 0) {
enumtostr(confresult, CONF_FILE_UID_INSECURE);
log_trace(WARNING, confresult);
log(WARNING, confresult);
}
else if (s.st_gid != 0) {
enumtostr(confresult, CONF_FILE_GID_INSECURE);
log_trace(WARNING, confresult);
log(WARNING, confresult);
}
else if ( (S_IROTH & s.st_mode) ||
(S_IWOTH & s.st_mode) ) {
enumtostr(confresult, CONF_FILE_PERM_INSECURE);
log_trace(WARNING, confresult);
log(WARNING, confresult);
}
}
return 0; /* conf is readable */
}
static int test_conf_syntax()
static int test_conf_syntax(void)
{
int i, j = 0, ok = 1, failed = 0;
char buf[CFGLINESIZE], *tmp;
FILE *fp = fopen("/etc/rmps/rmps.conf", "r");
if (fp == NULL) {
log_trace(ERROR, "Failed to read /etc/rmps/rmps.conf");
log(ERROR, "Failed to read /etc/rmps/rmps.conf");
return 1;
}
@@ -198,7 +198,7 @@ static int test_conf_syntax()
if ((tmp = strstr(buf, "=")))
*tmp = '\0';
else {
log_trace( ERROR,
log( ERROR,
"Bad entry in /etc/rmps/rmps.conf, line %d: %s",
j, buf );
ok = 0;
@@ -210,7 +210,7 @@ static int test_conf_syntax()
if (tmp[i] == '\n')
tmp[i] = '\0';
if (tmp[strspn(tmp + 1, " \t\v\r\n") + 1] == '\0') {
log_trace( ERROR,
log( ERROR,
"Specified entry without value, line %d: %s",
j, buf );
failed = 1;
@@ -278,22 +278,22 @@ static int test_conf_syntax()
failed = 1;
} else if (!strcmp(buf, "rmps.certfile")) {
if (access(tmp + 1, F_OK) == -1) {
log_trace(ERROR, "%s is missing", tmp + 1);
log(ERROR, "%s is missing", tmp + 1);
failed = 1;
}
else if (access(tmp + 1, R_OK) == -1) {
log_trace(ERROR, "%s is not readable", tmp + 1);
log(ERROR, "%s is not readable", tmp + 1);
failed = 1;
} else
strncpy(conf.rmps.certfile, tmp + 1, sizeof(conf.rmps.certfile));
}
else if (!strcmp(buf, "rmps.keyfile")) {
if (access(tmp + 1, F_OK) == -1) {
log_trace(ERROR, "%s is missing", conf.rmps.keyfile);
log(ERROR, "%s is missing", conf.rmps.keyfile);
failed = 1;
}
else if (access(tmp + 1, R_OK) == -1) {
log_trace(ERROR, "%s is not readable", tmp + 1);
log(ERROR, "%s is not readable", tmp + 1);
failed = 1;
} else
strncpy(conf.rmps.keyfile, tmp + 1, sizeof(conf.rmps.keyfile));
@@ -301,18 +301,18 @@ static int test_conf_syntax()
strncpy(conf.rmps.cipherlist, tmp + 1, sizeof(conf.rmps.cipherlist));
} else if (!strcmp(buf, "rmps.cafile")) {
if (access(tmp + 1, F_OK) == -1) {
log_trace(ERROR, "%s is missing", tmp + 1);
log(ERROR, "%s is missing", tmp + 1);
failed = 1;
}
else if (access(tmp + 1, R_OK) == -1) {
log_trace(ERROR, "%s is not readable\n", tmp + 1);
log(ERROR, "%s is not readable\n", tmp + 1);
failed = 1;
} else
strncpy(conf.rmps.cafile, tmp + 1, sizeof(conf.rmps.cafile));
} else
log_trace(ERROR, "Unknown config entry on line %d: %s", j, buf);
log(ERROR, "Unknown config entry on line %d: %s", j, buf);
if (!ok) {
log_trace( ERROR,
log( ERROR,
"Invalid value for \"%s\", line %d: \"%s\"",
buf, j, tmp + 1 );
ok = !ok;
@@ -326,7 +326,7 @@ static int test_conf_syntax()
return 0;
}
int confparse()
int confparse(void)
{
int result;

View File

@@ -1,7 +1,7 @@
#ifndef CONFPARSER_H
#define CONFPARSER_H
#include "log_trace.h"
#include "log.h"
#define MAXPATHSIZE 256
#define HOSTNAMESIZE 128
@@ -39,8 +39,8 @@ struct conf_table {
};
extern struct conf_table conf;
extern int confparse();
extern void confexport();
extern int confparse(void);
extern void confexport(void);
#endif /* CONFPARSER_H */

View File

@@ -2,7 +2,7 @@
#include <stdio.h>
#include <string.h>
#include "enum_functions.h"
#include "log_trace.h"
#include "log.h"
void enumtostr(char *scode, int code)
{
@@ -14,7 +14,7 @@ void enumtostr(char *scode, int code)
fp = fopen("/usr/lib/rmps/resources/enum_codes", "r");
if (fp == NULL) {
log_trace(ERROR, "Failed to fetch error enum code!");
log(ERROR, "Failed to fetch error enum code!");
return;
}
while (fgets(line, sizeof(line), fp) != NULL)

View File

@@ -3,7 +3,7 @@
#include <stdarg.h>
#include <pthread.h>
#include <openssl/err.h>
#include "log_trace.h"
#include "log.h"
#include "confparser.h"
static FILE *fderr;
@@ -35,7 +35,7 @@ static void set_fpts(void)
fdout = stdout;
}
void log_trace(LOG_LEVEL lvl, char *fmt, ... )
void log(LOG_LEVEL lvl, char *fmt, ... )
{
char fmt_with_pfx[1024];

View File

@@ -1,6 +1,9 @@
#ifndef LOG_TRACE_H
#define LOG_TRACE_H
#undef log
#define log log_trace
typedef enum {
ERROR = 1, /* Errors only */
WARNING, /* Errors & warnings */
@@ -9,6 +12,6 @@ typedef enum {
} LOG_LEVEL;
void log_ssl(void);
void log_trace(LOG_LEVEL lvl, char *fmt, ... );
void log(LOG_LEVEL lvl, char *fmt, ... );
#endif /* LOG_TRACE_H */

18
main.c
View File

@@ -5,12 +5,12 @@
#include <signal.h>
#include <unistd.h>
#include "confparser.h"
#include "log_trace.h"
#include "log.h"
#include "rmps.h"
static void usage(char *argv)
{
log_trace( ERROR,
log( ERROR,
"Usage:\n%s start|stop|restart [--daemonize=yes|no]\n",
argv );
}
@@ -45,10 +45,10 @@ int main(int argc, char *argv[])
}
}
if (confparse() != 0) {
log_trace(ERROR, "Failed to parse the conf!");
log(ERROR, "Failed to parse the conf!");
exit(EXIT_FAILURE);
}
log_trace(VERBOSE, "Conf parser finished successfully");
log(VERBOSE, "Conf parser finished successfully");
//confexport();
if (task == 2 || task == 3) {
@@ -56,16 +56,16 @@ int main(int argc, char *argv[])
int pid;
FILE *fp;
if (task == 2)
log_trace(VERBOSE, "We got a stop signal!");
log(VERBOSE, "We got a stop signal!");
else if (task == 3)
log_trace(VERBOSE, "We got a restart signal!");
log(VERBOSE, "We got a restart signal!");
fp = fopen(conf.rmps.pidfile, "r");
switch (errno) {
case EEXIST:
if (!fgets(buf, 10, fp)) {
log_trace(ERROR, "Failed to read %s!", conf.rmps.pidfile);
log(ERROR, "Failed to read %s!", conf.rmps.pidfile);
exit(EXIT_FAILURE);
}
pid = strtol(buf, NULL, 10);
@@ -73,14 +73,14 @@ int main(int argc, char *argv[])
//waitpid(TODO);
break;
case EACCES:
log_trace(ERROR, "Permission denied to read PID. Exiting!");
log(ERROR, "Permission denied to read PID. Exiting!");
exit(EXIT_FAILURE);
case ENOENT:
if (task == 2)
exit(EXIT_FAILURE);
break;
default:
log_trace( ERROR,
log( ERROR,
"Unhandled errno while opening PID: %d. Exiting!",
errno
);

68
rmps.c
View File

@@ -1,4 +1,4 @@
#include "log_trace.h"
#include "log.h"
#include "confparser.h"
#include "thread_pool.h"
#include "rmps.h"
@@ -27,9 +27,9 @@ static int pid_file_handle;
static void cleanup(void)
{
log_trace(VERBOSE, "Deleting pidfile %s", conf.rmps.pidfile);
log(VERBOSE, "Deleting pidfile %s", conf.rmps.pidfile);
if (unlink(conf.rmps.pidfile) != 0)
log_trace( WARNING,
log( WARNING,
"Failed to delete pidfile %s. Reason code: %d",
conf.rmps.pidfile, errno );
}
@@ -38,18 +38,18 @@ static void signal_handler(int sig)
{
switch (sig) {
case SIGHUP:
log_trace(WARNING, "Received SIGHUP signal. Ignoring...");
log(WARNING, "Received SIGHUP signal. Ignoring...");
break;
case SIGINT:
case SIGTERM:
log_trace(INFO, "Received SIGTERM signal.");
log_trace(INFO, "RMPS is shutting down...");
log(INFO, "Received SIGTERM signal.");
log(INFO, "RMPS is shutting down...");
rmps_shutdown();
log_trace(INFO, "RMPS has been stopped properly.");
log(INFO, "RMPS has been stopped properly.");
_exit(EXIT_SUCCESS);
break;
default:
log_trace(WARNING, "Unhandled signal %s", strsignal(sig));
log(WARNING, "Unhandled signal %s", strsignal(sig));
break;
}
}
@@ -97,12 +97,12 @@ static void daemonize(const char *rundir)
pid = fork();
if (pid < 0) {
/* Could not fork */
log_trace(ERROR, "Failed to fork the parent process. Exiting!");
log(ERROR, "Failed to fork the parent process. Exiting!");
exit(EXIT_FAILURE);
}
if (pid > 0) {
/* Child created ok, so exit parent process */
log_trace(INFO, "Child process created: %d", pid);
log(INFO, "Child process created: %d", pid);
exit(EXIT_SUCCESS);
}
/* Child continues */
@@ -110,7 +110,7 @@ static void daemonize(const char *rundir)
/* Get a new process group */
sid = setsid();
if (sid < 0) {
log_trace(ERROR, "Failed to create a process group. Exiting!");
log(ERROR, "Failed to create a process group. Exiting!");
exit(EXIT_FAILURE);
}
/* Close all file descriptors because we fork */
@@ -134,14 +134,14 @@ static void spawn_pidfile(const char *pidfile)
pid_file_handle = open(pidfile, O_RDWR|O_CREAT, 0600);
if (pid_file_handle == -1) {
/* Couldn't open lock file */
log_trace(ERROR, "Could not create PID file %s - Exiting!", pidfile);
log(ERROR, "Could not create PID file %s - Exiting!", pidfile);
exit(EXIT_FAILURE);
}
/* Try to lock file */
if (lockf(pid_file_handle, F_TLOCK, 0) == -1) {
/* Couldn't get lock on lock file */
log_trace(ERROR, "Could not lock PID file %s - Exiting!", pidfile);
log(ERROR, "Could not lock PID file %s - Exiting!", pidfile);
exit(EXIT_FAILURE);
}
@@ -169,23 +169,23 @@ static int open_listener(int port)
addr.sin_addr.s_addr = INADDR_ANY;
sd = socket(addr.sin_family, SOCK_STREAM, 0);
if (sd < 0) {
log_trace(ERROR, "Failed to create socket");
log_trace(INFO, "RMPS failed to start, shutting down...");
log(ERROR, "Failed to create socket");
log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup);
}
if (set_reuse_addr(sd) < 0) {
log_trace(ERROR, "Failed to set reuse on address - Aborting...", port);
log_trace(INFO, "RMPS failed to start, shutting down...");
log(ERROR, "Failed to set reuse on address - Aborting...", port);
log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup);
}
if (bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
log_trace(ERROR, "Failed to bind on port: %d - Aborting...", port);
log_trace(INFO, "RMPS failed to start, shutting down...");
log(ERROR, "Failed to bind on port: %d - Aborting...", port);
log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup);
}
if (listen(sd, 10) != 0) {
log_trace(ERROR, "Failed to start listener on port %d - Aborting...", port);
log_trace(INFO, "RMPS failed to start, shutting down...");
log(ERROR, "Failed to start listener on port %d - Aborting...", port);
log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup);
}
return sd;
@@ -204,8 +204,8 @@ static SSL_CTX* init_server_ctx(const char *cipherlist)
ctx = SSL_CTX_new(TLSv1_2_method()); /* create new context from method */
if (ctx == NULL) {
log_trace(ERROR, "SSL_CTX_new() returned NULL - Aborting...");
log_trace(ERROR, "RMPS failed to start, shutting down...");
log(ERROR, "SSL_CTX_new() returned NULL - Aborting...");
log(ERROR, "RMPS failed to start, shutting down...");
exit(EXIT_FAILURE);
}
SSL_CTX_set_verify( ctx, SSL_VERIFY_PEER |
@@ -218,7 +218,7 @@ static SSL_CTX* init_server_ctx(const char *cipherlist)
strcat(ciphers, cipherlist);
/* This is very delicate, try to understand the ciphers */
SSL_CTX_set_cipher_list(ctx, cipherlist);
log_trace(VERBOSE, "cipherlist = %s", cipherlist);
log(VERBOSE, "cipherlist = %s", cipherlist);
return ctx;
}
@@ -231,22 +231,22 @@ void load_certificates(SSL_CTX* ctx, const char *certfile,
{
/* set the local certificate from certfile */
if (SSL_CTX_use_certificate_file(ctx, certfile, SSL_FILETYPE_PEM) <= 0) {
log_trace(ERROR, "Failed to load certfile! SSL error below:");
log(ERROR, "Failed to load certfile! SSL error below:");
log_ssl();
log_trace(INFO, "RMPS failed to start, shutting down...");
log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup);
}
/* set the private key from KeyFile (may be the same as CertFile) */
if (SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM) <= 0) {
log_trace(ERROR, "Failed to load keyfile! SSL error below:");
log(ERROR, "Failed to load keyfile! SSL error below:");
log_ssl();
log_trace(INFO, "RMPS failed to start, shutting down...");
log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup);
}
/* verify private key */
if (!SSL_CTX_check_private_key(ctx)) {
log_trace(ERROR, "Private key does not match the public certificate.");
log_trace(INFO, "RMPS failed to start, shutting down...");
log(ERROR, "Private key does not match the public certificate.");
log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup);
}
if (cafile != NULL) {
@@ -260,7 +260,7 @@ void load_certificates(SSL_CTX* ctx, const char *certfile,
int launch_rmps(struct conf_table *conf, int fork_flag)
{
int server;
log_trace(INFO, "Starting up RMPS...");
log(INFO, "Starting up RMPS...");
/* Set signal handling */
set_env();
@@ -276,13 +276,13 @@ int launch_rmps(struct conf_table *conf, int fork_flag)
* -nodes is for not protecing with a passphrase
* http://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl
*/
log_trace(VERBOSE, "Loading crypto certs and keys.");
log(VERBOSE, "Loading crypto certs and keys.");
load_certificates(ctx, conf->rmps.certfile, conf->rmps.keyfile, conf->rmps.cafile);
log_trace(VERBOSE, "Starting listener on port: %d", atoi(conf->rmps.bind_on_port));
log(VERBOSE, "Starting listener on port: %d", atoi(conf->rmps.bind_on_port));
server = open_listener(atoi(conf->rmps.bind_on_port));
log_trace(VERBOSE, "Creating mutex for thread pool.");
log(VERBOSE, "Creating mutex for thread pool.");
ssl_pt_mutex(server, ctx, conf->rmps.threadpoolsize);
return 0;

View File

@@ -1,5 +1,5 @@
#include "thread_pool.h"
#include "log_trace.h"
#include "log.h"
#include "protocol.h"
#include <pthread.h>
#include <unistd.h>
@@ -24,18 +24,18 @@ static void show_certs(SSL *ssl)
cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */
if (SSL_get_verify_result(ssl)==X509_V_OK)
log_trace(VERBOSE, "get_verify_result == ok");
log(VERBOSE, "get_verify_result == ok");
if (cert != NULL) {
log_trace(VERBOSE, "Server certificates:");
log(VERBOSE, "Server certificates:");
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
log_trace(VERBOSE, "Subject: %s", line);
log(VERBOSE, "Subject: %s", line);
free(line);
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
log_trace(VERBOSE, "Issuer: %s", line);
log(VERBOSE, "Issuer: %s", line);
free(line);
X509_free(cert);
} else
log_trace(VERBOSE, "No certificates from peer");
log(VERBOSE, "No certificates from peer");
}
static void* servlet(void *args) /* Serve the connection -- threadable */
@@ -49,7 +49,7 @@ static void* servlet(void *args) /* Serve the connection -- threadable */
ret = SSL_accept(agent->ssl);
/* We check for unclean (ret < 0) and clean (ret == 0) failures */
if (ret <= 0) {
log_trace(WARNING, "SSL_accept() failed. Reason below:");
log(WARNING, "SSL_accept() failed. Reason below:");
log_ssl();
} else {
show_certs(agent->ssl);
@@ -60,26 +60,26 @@ static void* servlet(void *args) /* Serve the connection -- threadable */
bytes = SSL_read(agent->ssl, &buf, sizeof(buf));
if (bytes > 0) {
if (bytes != sizeof(struct msg)) {
log_trace( WARNING,
log( WARNING,
"Agent [%s] sent non-standard data!",
agent->ip );
continue;
}
log_trace(VERBOSE, "Client msg: \"%s\"", buf.chunk.data);
log(VERBOSE, "Client msg: \"%s\"", buf.chunk.data);
/* TODO: Insert msg handler here */
continue;
}
if (SSL_get_shutdown(agent->ssl) == SSL_RECEIVED_SHUTDOWN)
log_trace(VERBOSE, "SSL_RECEIVED_SHUTDOWN from agent [%s]", agent->ip);
log(VERBOSE, "SSL_RECEIVED_SHUTDOWN from agent [%s]", agent->ip);
else {
log_trace(VERBOSE, "Client didn't send data! SSL error below:");
log(VERBOSE, "Client didn't send data! SSL error below:");
//log_ssl(); /* We actually don't have anything to log from SSL */
sprintf((char*)buf.chunk.data, "%s", "Where's the data, m8?");
SSL_write(agent->ssl, &buf, sizeof(struct msg));
}
log_trace(INFO, "Agent [%s] disconnected.", agent->ip);
log(INFO, "Agent [%s] disconnected.", agent->ip);
} while (bytes);
}
SSL_free(agent->ssl); /* release SSL state */
@@ -116,7 +116,7 @@ void ssl_pt_mutex(int srv, SSL_CTX *ctx, int poolsize)
socklen_t len = sizeof(addr);
SSL *ssl;
int agent = accept(srv, (struct sockaddr*)&addr, &len);
log_trace( INFO,
log( INFO,
"Connection: %s:%d",
inet_ntop(AF_INET, &addr.sin_addr, address, sizeof(address)),
ntohs(addr.sin_port)
@@ -139,7 +139,7 @@ void ssl_pt_mutex(int srv, SSL_CTX *ctx, int poolsize)
}
}
if (i == poolsize) {
log_trace( WARNING,
log( WARNING,
"Agent [%s] dropped. Poolsize limit reached.",
inet_ntop(AF_INET, &addr.sin_addr, address, sizeof(address))
);