diff options
-rw-r--r-- | src/log.c | 15 | ||||
-rw-r--r-- | src/log.h | 7 | ||||
-rw-r--r-- | src/main.c | 44 | ||||
-rw-r--r-- | src/main.h | 1 | ||||
-rw-r--r-- | src/misc.h | 4 | ||||
-rw-r--r-- | src/modem.h | 1 | ||||
-rw-r--r-- | src/serial.h | 7 | ||||
-rw-r--r-- | src/telnet.h | 3 |
8 files changed, 54 insertions, 28 deletions
@@ -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); +} @@ -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(); @@ -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 ); +} @@ -30,3 +30,4 @@ int main(int argc, char* argv[]); +void terminate(); @@ -14,8 +14,10 @@ #include <string.h> #include <stdint.h> +#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 <termios.h> #include <unistd.h> - +/* + * 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[]); |