From cdff733137e608b0dd4839fdbb09cf2165f9e2d9 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Mon, 23 Sep 2019 09:42:27 +0200 Subject: Documentation, fixes --- src/log.c | 15 ++++++++------- src/log.h | 7 ++++--- src/main.c | 44 ++++++++++++++++++++++++++++---------------- src/main.h | 1 + src/misc.h | 4 +++- src/modem.h | 1 + src/serial.h | 7 ++++++- src/telnet.h | 3 +++ 8 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/log.c b/src/log.c index 36109c6..197582c 100644 --- a/src/log.c +++ b/src/log.c @@ -10,22 +10,23 @@ const char* log_loglevel_str[5] = { int log_init_file(char* _file, unsigned int _verbosity) { - int fd = open(_file, O_WRONLY | O_APPEND | O_CREAT | O_DSYNC); + log_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; + log_fd = STDIN_FILENO; + LOGPRINTF(0, "=== RESTART ==="); LOGPRINTF(0, "Verbosity: %i", _verbosity); + return 0; } +int log_close() +{ + return close(log_fd); +} diff --git a/src/log.h b/src/log.h index 4e48dad..8b2c881 100644 --- a/src/log.h +++ b/src/log.h @@ -14,6 +14,7 @@ #define _LOG_DEBUG 4 unsigned int log_loglevel; +int log_fd; const char* log_loglevel_str[5]; @@ -35,14 +36,14 @@ const char* log_loglevel_str[5]; } -int log_init_file(char* _file, unsigned int _verbosity); /** * Opens logfile, writes filedes to _fd * */ +int log_init_file(char* _file, unsigned int _verbosity); -int log_init_stdout(unsigned int _verbosity); /** * Configures LOG macros for stdout */ +int log_init_stdout(unsigned int _verbosity); - +int log_close(); diff --git a/src/main.c b/src/main.c index fbb8ee9..1f6f583 100644 --- a/src/main.c +++ b/src/main.c @@ -9,10 +9,13 @@ int main(int argc, char* argv[]) { - log_init_stdout(_LOG_DEBUG); - - signal(SIGCHLD,SIG_IGN); //Ignore sigchld struct prog_params params = parse_args(argc, argv); + + log_init_stdout( _LOG_DEBUG ); + + signal( SIGCHLD, SIG_IGN ); //Ignore sigchld + signal( SIGKILL, terminate); + signal( SIGINT, terminate); //Fork and write PID to pidfile if(params.fork) @@ -21,39 +24,48 @@ int main(int argc, char* argv[]) if(!pidfile) { - LOGPRINTF(_LOG_ERROR,"Unable to open pidfile\n"); + LOGPRINTF( _LOG_ERROR, "Unable to open pidfile\n" ); exit(1); } pid_t pid = fork(); if(pid < 0) { - LOGPRINTF(_LOG_ERROR,"fork failed\n"); + LOGPRINTF( _LOG_ERROR, "fork failed\n" ); exit(1); } else if(pid > 0) { - fprintf(pidfile, "%i", pid); - LOGPRINTF(_LOG_INFO,"Forked with PID %i\n\n", pid); - fclose (pidfile); - exit(0); + fprintf( pidfile, "%i", pid ); + LOGPRINTF( _LOG_NOTE, "Forked with PID %i\n\n", pid ); + + if( log_fd == STDIN_FILENO ) + LOGPRINTF( _LOG_WARNING, "STDIO will be closed but no logfile is set. No logging possible\n" ); + + fclose ( pidfile ); + exit( 0 ); } fclose (pidfile); //Close STDIO - close (STDIN_FILENO); - close (STDOUT_FILENO); - close (STDERR_FILENO); + close ( STDIN_FILENO ); + close ( STDOUT_FILENO ); + close ( STDERR_FILENO ); }//if params.fork - LOGPRINTF(_LOG_DEBUG, "%s, %i\n", params.run_argv[0], params.run_argc); + LOGPRINTF(_LOG_DEBUG, "command: %s, %i\n", params.run_argv[0], params.run_argc); if ( params.serial ) - dialup_server(params); - else if (params.telnet) - telnet_server(params); + dialup_server( params ); + else if ( params.telnet ) + telnet_server( params ); return 0; } +void terminate() +{ + log_close(); + exit ( 1 ); +} diff --git a/src/main.h b/src/main.h index bf6a495..1b3fcb1 100644 --- a/src/main.h +++ b/src/main.h @@ -30,3 +30,4 @@ int main(int argc, char* argv[]); +void terminate(); diff --git a/src/misc.h b/src/misc.h index 61b1503..3d64c45 100644 --- a/src/misc.h +++ b/src/misc.h @@ -14,8 +14,10 @@ #include #include +#include "log.h" + #ifdef _DEBUG -//DEBUG Macros +//DEBUG Macros --DEPRECATED-- #warning "Compiling in DEBUG mode" #define DEBUG_PRINTF( ... ) { \ printf("%s:%d: ", __FILE__, __LINE__); \ diff --git a/src/modem.h b/src/modem.h index 544c4cc..196a551 100644 --- a/src/modem.h +++ b/src/modem.h @@ -24,6 +24,7 @@ #define _AT_MUTE "ATM0\r\n" #define _AT_RESET_ON_DTR "AT&D3\r\n" +//Uncomment to ignore wait for modem ring #define _MODEM_WAIT_RING /* diff --git a/src/serial.h b/src/serial.h index 1e3157d..77fafcb 100644 --- a/src/serial.h +++ b/src/serial.h @@ -4,7 +4,12 @@ #include #include - +/* + * set baud rate of fd to speed + */ int set_interface_attribs (int fd, int speed, int parity); +/* + * sets blocking, duh + */ void set_blocking (int fd, int should_block); diff --git a/src/telnet.h b/src/telnet.h index 104b5bc..301d5d7 100644 --- a/src/telnet.h +++ b/src/telnet.h @@ -14,6 +14,9 @@ #include "misc.h" #include "log.h" +/** + * Spawn telnet server + */ void telnet_server(struct prog_params params); void handle_connection(int _socket, struct sockaddr_in _addr, int argc, char* argv[]); -- cgit v1.2.3