aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/log.c15
-rw-r--r--src/log.h7
-rw-r--r--src/main.c44
-rw-r--r--src/main.h1
-rw-r--r--src/misc.h4
-rw-r--r--src/modem.h1
-rw-r--r--src/serial.h7
-rw-r--r--src/telnet.h3
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 <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[]);