Cleanups and small fixes

This commit is contained in:
2016-08-09 14:16:30 +03:00
parent 0d5eeed758
commit 3fecfdbbc4
6 changed files with 51 additions and 49 deletions

View File

@@ -73,7 +73,7 @@ static int fopen_and_mkdir(const char *dir) {
char tmp[256];
char *p = NULL;
size_t len;
FILE *fd;
FILE *fp;
snprintf(tmp, sizeof(tmp),"%s",dir);
len = strlen(tmp);
@@ -90,12 +90,12 @@ static int fopen_and_mkdir(const char *dir) {
}
*p = '/';
}
fd = fopen(dir, "a");
if (!fd) {
fp = fopen(dir, "a");
if (!fp) {
log_trace(ERROR, "Permission denied to write into: %s", dir);
return 1;
}
fclose(fd);
fclose(fp);
return 0;
}
@@ -179,14 +179,14 @@ static int test_conf_syntax()
{
int i, j = 0, ok = 1, failed = 0;
char buf[CFGLINESIZE], *tmp;
FILE *fd = fopen("/etc/rmps/rmps.conf", "r");
FILE *fp = fopen("/etc/rmps/rmps.conf", "r");
if (fd == NULL) {
if (fp == NULL) {
log_trace(ERROR, "Failed to read /etc/rmps/rmps.conf");
return 1;
}
while (fgets(buf, CFGLINESIZE, fd) != NULL) {
while (fgets(buf, CFGLINESIZE, fp) != NULL) {
j++;
/* kill comments and ignore BLANK lines */
if ((tmp = strstr(buf, "#")))
@@ -318,7 +318,7 @@ static int test_conf_syntax()
ok = !ok;
}
}
fclose(fd);
fclose(fp);
if (failed)
return 1;

View File

@@ -7,23 +7,26 @@
void enumtostr(char *scode, int code)
{
char line[128];
FILE *fd;
FILE *fp;
int bytes = 0;
snprintf(scode, 10, "%d", code);
fd = fopen("/usr/lib/rmps/resources/enum_codes", "r");
if (fd == NULL) {
fp = fopen("/usr/lib/rmps/resources/enum_codes", "r");
if (fp == NULL) {
log_trace(ERROR, "Failed to fetch error enum code!");
return;
}
while (fgets(line, sizeof(line), fd) != NULL)
while (fgets(line, sizeof(line), fp) != NULL)
if (strstr(line, scode) != NULL) {
sprintf(scode, "%s", line);
char *byte;
bytes = sprintf(scode, "%s", line);
byte = memchr(scode, '\n', bytes);
byte[-1] = '\0';
break;
}
strncpy( scode,
memchr(scode, '\"', strlen(scode)),
strlen(scode) );
fclose(fd);
strncpy(scode, (char*)memchr(scode, '\"', bytes) + 1, bytes);
fclose(fp);
return;
}

View File

@@ -6,49 +6,51 @@
#include "log_trace.h"
#include "confparser.h"
static FILE *fderr = NULL;
static FILE *fdout = NULL;
static FILE *fderr;
static FILE *fdout;
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_once_t init_once = PTHREAD_ONCE_INIT;
static void open_logs(void)
{
if (conf.rmps.errlog)
fderr = fopen(conf.rmps.errlog, "a");
if (conf.rmps.logfile)
fdout = fopen(conf.rmps.logfile, "a");
if ((fderr = fopen(conf.rmps.errlog, "a")) == NULL)
fderr = stderr;
if ((fdout = fopen(conf.rmps.logfile, "a")) == NULL)
fdout = stdout;
}
void log_ssl()
void log_ssl(void)
{
ERR_print_errors_fp(fderr);
fflush(fderr);
}
static void set_fpts(void)
{
fderr = stderr;
fdout = stdout;
}
void log_trace(LOG_LEVEL lvl, char *fmt, ... )
{
LOG_LEVEL cur_lvl = conf.rmps.loglevel - '0';
char fmt_with_pfx[1024];
pthread_once(&init_once, set_fpts);
if (conf.isvalid)
pthread_once(&once, open_logs);
if (lvl <= cur_lvl) {
va_list list;
FILE *fd;
FILE *fp;
static const char *prefixes[] = {
"ERROR", "WARNING", "INFO", "VERBOSE"
};
time_t t = time(NULL);
struct tm tm = *localtime(&t);
if (lvl == ERROR || lvl == WARNING)
fd = fderr;
fp = fderr;
else
fd = fdout;
if (!fd) {
fprintf( stderr,
"Failed to append errlog: %s. Redirecting to stderr:\n",
conf.rmps.errlog );
fd = stderr;
}
fp = fdout;
snprintf( fmt_with_pfx,
sizeof(fmt_with_pfx),
"[%d-%02d-%02d %02d:%02d:%02d] %s: %s\n",
@@ -62,10 +64,9 @@ void log_trace(LOG_LEVEL lvl, char *fmt, ... )
fmt );
va_start(list, fmt);
vfprintf(fd, fmt_with_pfx, list);
fflush(fd);
vfprintf(fp, fmt_with_pfx, list);
fflush(fp);
va_end(list);
} else
return;
}
}

View File

@@ -8,7 +8,7 @@ typedef enum {
VERBOSE, /* Errors, warnings, events & more? */
} LOG_LEVEL;
void log_ssl();
void log_ssl(void);
void log_trace(LOG_LEVEL lvl, char *fmt, ... );
#endif /* LOG_TRACE_H */

6
main.c
View File

@@ -54,17 +54,17 @@ int main(int argc, char *argv[])
if (task == 2 || task == 3) {
char buf[10];
int pid;
FILE *fd;
FILE *fp;
if (task == 2)
log_trace(VERBOSE, "We got a stop signal!");
else if (task == 3)
log_trace(VERBOSE, "We got a restart signal!");
fd = fopen(conf.rmps.pidfile, "r");
fp = fopen(conf.rmps.pidfile, "r");
switch (errno) {
case EEXIST:
if (!fgets(buf, 10, fd)) {
if (!fgets(buf, 10, fp)) {
log_trace(ERROR, "Failed to read %s!", conf.rmps.pidfile);
exit(EXIT_FAILURE);
}

14
rmps.c
View File

@@ -10,24 +10,22 @@
#include <openssl/ssl.h>
#include <openssl/err.h>
static void rmps_shutdown();
static void rmps_shutdown(void);
static void signal_handler(int sig);
static void set_env();
static void set_env(void);
static void daemonize(const char *rundir);
static void spawn_pidfile(const char *pidfile);
static inline int set_reuse_addr(int sockfd);
static int open_listener(int port);
static void cleanup();
static void cleanup(void);
static void signal_handler(int sig);
//static void show_certs(SSL *ssl);
static void load_certificates(SSL_CTX *ctx, const char *certfile,
const char *keyfile, const char *cafile);
static SSL_CTX* init_server_ctx(const char *cipherlist);
//static void servlet(SSL *ssl);
static int pid_file_handle;
static void cleanup()
static void cleanup(void)
{
log_trace(VERBOSE, "Deleting pidfile %s", conf.rmps.pidfile);
if (unlink(conf.rmps.pidfile) != 0)
@@ -56,13 +54,13 @@ static void signal_handler(int sig)
}
}
static void rmps_shutdown()
static void rmps_shutdown(void)
{
close(pid_file_handle);
cleanup();
}
static void set_env()
static void set_env(void)
{
struct sigaction new_sigaction;
sigset_t new_sigset;