diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2019-09-17 12:26:54 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2019-09-17 12:26:54 +0200 |
commit | 57cb704e6387fd1a84bb26e7242d49a1c268fb40 (patch) | |
tree | 140833a6cebb09cf5161e51f688fb88132c3ce1b /src | |
parent | 78771b749e406ba14e3014e66b4848ab1f897563 (diff) | |
download | bbs-57cb704e6387fd1a84bb26e7242d49a1c268fb40.tar.gz |
Added logging, fixed in unterminated buffers
Diffstat (limited to 'src')
-rw-r--r-- | src/log.c | 31 | ||||
-rw-r--r-- | src/log.h | 48 | ||||
-rw-r--r-- | src/main.c | 62 | ||||
-rw-r--r-- | src/main.h | 4 | ||||
-rw-r--r-- | src/modem.c | 13 |
5 files changed, 121 insertions, 37 deletions
diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..36109c6 --- /dev/null +++ b/src/log.c @@ -0,0 +1,31 @@ +#include "log.h" + +const char* log_loglevel_str[5] = { + "---", + "ERROR", + "WARNING", + "NOTE", + "DEBUG" +}; + +int log_init_file(char* _file, unsigned int _verbosity) +{ + int fd = open(_file, O_WRONLY | O_APPEND | O_CREAT | O_DSYNC); + + if(fd < 0) { + LOGPRINTF(_LOG_ERROR, "Failed to open LogFile %s", _file); + } else { + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + } + return log_init_stdout(_verbosity);; +} + +int log_init_stdout(unsigned int _verbosity) +{ + log_loglevel = _verbosity;// > _LOG_DEBUG ? _LOG_DEBUG : _verbosity; + LOGPRINTF(0, "=== RESTART ==="); + LOGPRINTF(0, "Verbosity: %i", _verbosity); + return 0; +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..4e48dad --- /dev/null +++ b/src/log.h @@ -0,0 +1,48 @@ +#pragma once + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <time.h> +#include <fcntl.h> +#include <unistd.h> + +#define _LOG_ERROR 1 +#define _LOG_WARNING 2 +#define _LOG_NOTE 3 +#define _LOG_DEBUG 4 + +unsigned int log_loglevel; + +const char* log_loglevel_str[5]; + +#define LOGPRINTF(l,...) {\ + if((l) <= log_loglevel){\ + time_t current = time (NULL);\ + struct tm *tma = localtime(¤t);\ + char* date = asctime(tma);\ + date[strlen(date) - 1] = '\0';\ + printf("[%s] %s: ", date, log_loglevel_str[(l)]);\ + if((l) == _LOG_ERROR)\ + printf("%s:", strerror(errno));\ + if((l) == _LOG_DEBUG)\ + printf("%s:%d: ", __FILE__, __LINE__);\ + printf(__VA_ARGS__);\ + printf("\n");\ + fsync(STDOUT_FILENO);\ + }\ +} + + +int log_init_file(char* _file, unsigned int _verbosity); +/** + * Opens logfile, writes filedes to _fd + * */ + +int log_init_stdout(unsigned int _verbosity); +/** + * Configures LOG macros for stdout + */ + + @@ -6,8 +6,6 @@ */ #include "main.h" -#include "serial.h" -#include "modem.h" struct prog_params parse_args(int argc, char* argv[]) { @@ -95,34 +93,9 @@ void handle_connection(int _socket, struct sockaddr_in _addr, int argc, char* ar if (pid < 0) return; - /*pid = fork(); - - if(pid == 0) {//child - close (in[1]); - close (out[0]); - - dup2 (in[0], STDIN_FILENO); - dup2 (out[1], STDOUT_FILENO); - dup2 (out[1], STDERR_FILENO); - - char* arv[argc + 1]; - - for(int i = 0; i < argc; i++) - arv[i] = argv[i]; - - arv[argc] = NULL; - - execv(argv[0], arv); - - printf("EXEC ERROR %i: %s\r\n", errno, strerror(errno)); - exit(1); - } - else if (pid < 0) { - return; - }*/ - const int buffsize = 128; - char buff[ buffsize ]; + char buff[ buffsize + 1]; + buff[buffsize] = '\0'; //close unused pipes close (in[0]); @@ -135,7 +108,7 @@ void handle_connection(int _socket, struct sockaddr_in _addr, int argc, char* ar fds[1].fd = _socket; fds[1].events = POLLIN; - printf("Forked with PID %i\n", pid); + LOGPRINTF(_LOG_NOTE, "%i: Connected to %s", pid, inet_ntoa(_addr.sin_addr)); while(1) { @@ -143,25 +116,39 @@ void handle_connection(int _socket, struct sockaddr_in _addr, int argc, char* ar if ( fds[0].revents & POLLIN ) { const int cnt = read (out[0], buff, buffsize); if(try_write(_socket, buff, cnt, 100)) { - printf("Consecutive write errors while writing to socket.\n"); + LOGPRINTF(_LOG_ERROR, "%i: Consecutive write errors while writing to socket.", pid); break; } } if ( fds[1].revents & POLLIN ) { const int cnt = read (_socket, buff, buffsize); + + if(cnt == 0) + break; + + char *needle = strstr(buff, "\r"); + if (needle) //Replace CR with space + *needle = ' '; + if(try_write(in[1], buff, cnt, 100)) { - printf("Consecutive write errors while writing to STDIN.\n"); + LOGPRINTF(_LOG_ERROR, "%i: Consecutive write errors while writing to STDIN.", pid); break; } + } - //TODO check socket active check. though somewhat handled by try_write() + if (ret < 0){ + LOGPRINTF(_LOG_ERROR, "Poll error\n"); + break; + } if(kill(pid,0)) //Check if child is still alive, if not return. break; } - printf("Connection closed.\n"); + LOGPRINTF(_LOG_NOTE, "%i: Connection closed.", pid); + + kill(pid,SIGKILL); close(_socket); exit(1); @@ -170,6 +157,13 @@ void handle_connection(int _socket, struct sockaddr_in _addr, int argc, char* ar int main(int argc, char* argv[]) { + log_init_stdout(_LOG_DEBUG); + + LOGPRINTF(_LOG_DEBUG, "debug"); + LOGPRINTF(_LOG_NOTE, "note"); + LOGPRINTF(_LOG_WARNING, "warn"); + LOGPRINTF(_LOG_ERROR, "Error"); + signal(SIGCHLD,SIG_IGN); //Ignore sigchld struct prog_params params = parse_args(argc, argv); @@ -21,6 +21,10 @@ #include <arpa/inet.h> #include "misc.h" +#include "log.h" +#include "serial.h" +#include "modem.h" + #define _DEF_MAX_BACKLOG 20 #define _DEF_PORT 23 diff --git a/src/modem.c b/src/modem.c index 0776bed..9f79ee2 100644 --- a/src/modem.c +++ b/src/modem.c @@ -12,7 +12,10 @@ int modem_accept_wait(int fd) if(!fd) return 1; - char buff[128]; + int buffsize = 128; + char buff[ buffsize + 1]; + buff[buffsize] = '\0'; + int ret; //for retturn values struct pollfd fds; //poll struct int cnt = 0; //read byte counter @@ -72,7 +75,10 @@ int modem_accept_wait(int fd) int modem_command(int fd, char* cmd, int timeout_ms) { - char buff[128]; + int buffsize = 128; + char buff[ buffsize + 1]; + buff[buffsize] = '\0'; + int ret; //for retturn values struct pollfd fds; //poll struct int cnt = 0; //read byte counter @@ -148,7 +154,8 @@ int modem_run(int fd, int argc, char* argv[]) } int buffsize = 128; - char buff[ buffsize ]; + char buff[ buffsize + 1]; + buff[buffsize] = '\0'; close (in[0]); close (out[1]); |