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

View File

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

View File

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

View File

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

6
main.c
View File

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

14
rmps.c
View File

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