NLSR Daemonized and Log Writing Enabled
diff --git a/macbook.conf b/macbook.conf
index 58d76b4..021dc23 100644
--- a/macbook.conf
+++ b/macbook.conf
@@ -13,3 +13,5 @@
lsa-refresh-time 600
router-dead-interval 900
multi-path-face-num 2
+
+logdir /Users/akmhoque/NLSR2.0
diff --git a/nlsr.c b/nlsr.c
index 345635b..591a93a 100644
--- a/nlsr.c
+++ b/nlsr.c
@@ -4,6 +4,7 @@
#include <unistd.h>
#include <getopt.h>
#include <sys/time.h>
+#include <sys/stat.h>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
@@ -13,6 +14,7 @@
+
#include <ccn/ccn.h>
#include <ccn/uri.h>
#include <ccn/keystore.h>
@@ -102,6 +104,38 @@
exit(0);
}
+void
+daemonize_nlsr(void)
+{
+ int ret;
+ pid_t process_id = 0;
+ pid_t sid = 0;
+ process_id = fork();
+ if (process_id < 0)
+ {
+ printf("fork failed!\n");
+ ON_ERROR_DESTROY(process_id);
+ }
+ if (process_id > 0)
+ {
+ printf("Process daemonized. Process id: %d \n", process_id);
+ ret=process_id;
+ exit(0);
+ }
+
+ umask(0);
+ sid = setsid();
+ if(sid < 0)
+ {
+ ON_ERROR_DESTROY(sid);
+ }
+
+ chdir("/");
+ close(STDIN_FILENO);
+ close(STDOUT_FILENO);
+ close(STDERR_FILENO);
+}
+
void
process_command_ccnneighbor(char *command)
{
@@ -392,6 +426,30 @@
}
void
+process_command_logdir(char *command)
+{
+ if(command==NULL)
+ {
+ printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
+ return;
+ }
+ char *rem;
+ const char *sep=" \t\n";
+ char *dir;
+
+ dir=strtok_r(command,sep,&rem);
+ if(dir==NULL)
+ {
+ printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
+ return;
+ }
+
+ nlsr->logDir=(char *)malloc(strlen(dir)+1);
+ memset(nlsr->logDir,0,strlen(dir)+1);
+ memcpy(nlsr->logDir,dir,strlen(dir));
+}
+
+void
process_conf_command(char *command)
{
const char *separators=" \t\n";
@@ -439,6 +497,10 @@
{
process_command_multi_path_face_num(remainder);
}
+ else if(!strcmp(cmd_type,"logdir") )
+ {
+ process_command_logdir(remainder);
+ }
else
{
printf("Wrong configuration Command %s \n",cmd_type);
@@ -605,17 +667,16 @@
{
int res, ret;
char *config_file;
- //int daemon_mode;
+ int daemon_mode=0;
- ret=init_nlsr();
- ON_ERROR_EXIT(ret);
+
while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
{
switch (res)
{
case 'd':
- //daemon_mode = 1;
+ daemon_mode = 1;
break;
case 'f':
config_file = optarg;
@@ -626,12 +687,22 @@
}
}
+ ret=init_nlsr();
+ ON_ERROR_EXIT(ret);
readConfigFile(config_file);
-
+
+ if ( daemon_mode == 1 )
+ {
+ daemonize_nlsr();
+ }
+
+ startLogging(nlsr->logDir);
+
nlsr->ccn=ccn_create();
if(ccn_connect(nlsr->ccn, NULL) == -1)
{
fprintf(stderr,"Could not connect to ccnd\n");
+ writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
ON_ERROR_DESTROY(-1);
}
struct ccn_charbuf *router_prefix;
@@ -640,6 +711,7 @@
if(res<0)
{
fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
+ writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
ON_ERROR_DESTROY(res);
}
diff --git a/nlsr.h b/nlsr.h
index 7a57c9f..a914880 100644
--- a/nlsr.h
+++ b/nlsr.h
@@ -78,6 +78,7 @@
long int lsa_refresh_time;
long int router_dead_interval;
long int multi_path_face_num;
+ char *logDir;
int semaphor;
diff --git a/utility.c b/utility.c
index 64e81a8..8326283 100644
--- a/utility.c
+++ b/utility.c
@@ -3,6 +3,12 @@
#include<stdlib.h>
#include <unistd.h>
#include <getopt.h>
+#include<ctype.h>
+#include<stdarg.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <errno.h>
+#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <assert.h>
@@ -10,6 +16,7 @@
#include <config.h>
#endif
+
#include <ccn/ccn.h>
#include <ccn/uri.h>
#include <ccn/keystore.h>
@@ -17,6 +24,7 @@
#include <ccn/schedule.h>
#include <ccn/hashtb.h>
+
#include "utility.h"
@@ -95,3 +103,114 @@
return diff_secs;
}
+
+void
+startLogging(char *loggingDir)
+{
+ struct passwd pd;
+ struct passwd* pwdptr=&pd;
+ struct passwd* tempPwdPtr;
+ char *pwdbuffer;
+ int pwdlinelen = 200;
+ char *logDir;
+ char *logFileName;
+ char *ret;
+ char *logExt;
+ char *defaultLogDir;
+ int status;
+ struct stat st;
+ int isLogDirExists=0;
+ char *time=getLocalTimeStamp();
+
+ pwdbuffer=(char *)malloc(sizeof(char)*200);
+ memset(pwdbuffer,0,200);
+ logDir=(char *)malloc(sizeof(char)*200);
+ memset(logDir,0,200);
+ logFileName=(char *)malloc(sizeof(char)*200);
+ memset(logFileName,0,200);
+ logExt=(char *)malloc(sizeof(char)*5);
+ memset(logExt,0,5);
+ defaultLogDir=(char *)malloc(sizeof(char)*10);
+ memset(defaultLogDir,0,10);
+
+ memcpy(logExt,".log",4);
+ logExt[4]='\0';
+ memcpy(defaultLogDir,"/nlsrLog",9);
+ defaultLogDir[9]='\0';
+
+ if(loggingDir!=NULL)
+ {
+ if( stat( loggingDir, &st)==0)
+ {
+ if ( st.st_mode & S_IFDIR )
+ {
+ if( st.st_mode & S_IWUSR)
+ {
+ isLogDirExists=1;
+ memcpy(logDir,loggingDir,strlen(loggingDir)+1);
+ }
+ else printf("User do not have write permission to %s \n",loggingDir);
+ }
+ else printf("Provided path for %s is not a directory!!\n",loggingDir);
+ }
+ else printf("Log directory: %s does not exists\n",loggingDir);
+ }
+
+ if(isLogDirExists == 0)
+ {
+ if ((getpwuid_r(getuid(),pwdptr,pwdbuffer,pwdlinelen,&tempPwdPtr))!=0)
+ perror("getpwuid_r() error.");
+ else
+ {
+ memcpy(logDir,pd.pw_dir,strlen(pd.pw_dir)+1);
+ memcpy(logDir+strlen(logDir),defaultLogDir,strlen(defaultLogDir)+1);
+ if(stat(logDir,&st) != 0)
+ status = mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
+ }
+ }
+ memcpy(logFileName,logDir,strlen(logDir)+1);
+ if( logDir[strlen(logDir)-1]!='/')
+ {
+ memcpy(logFileName+strlen(logFileName),"/",1);
+ memcpy(logFileName+strlen(logFileName),"\0",1);
+ }
+ memcpy(logFileName+strlen(logFileName),time,strlen(time)+1);
+ memcpy(logFileName+strlen(logFileName),logExt,strlen(logExt)+1);
+ ret=(char *)malloc(strlen(logFileName)+1);
+ memset(ret,0,strlen(logFileName)+1);
+ memcpy(ret,logFileName,strlen(logFileName)+1);
+
+ setenv("NLSR_LOG_FILE",ret,1);
+
+ free(time);
+ free(logDir);
+ free(logFileName);
+ free(pwdbuffer);
+ free(logExt);
+ free(defaultLogDir);
+ free(ret);
+}
+
+
+void
+writeLogg(const char *source_file, const char *function, const int line, const char *format, ...)
+{
+ char *file=getenv("NLSR_LOG_FILE");
+ if (file != NULL)
+ {
+ FILE *fp = fopen(file, "a");
+
+ if (fp != NULL)
+ {
+ struct timeval t;
+ gettimeofday(&t, NULL);
+ fprintf(fp,"%ld.%06u - %s, %s, %d:",(long)t.tv_sec , (unsigned)t.tv_usec , source_file , function , line);
+ va_list args;
+ va_start(args, format);
+ vfprintf(fp, format, args);
+ fclose(fp);
+ va_end(args);
+ }
+ }
+}
+
diff --git a/utility.h b/utility.h
index 6015ecc..6ae550a 100644
--- a/utility.h
+++ b/utility.h
@@ -7,4 +7,6 @@
void get_current_timestamp_micro(char * microSec);
long int get_time_diff(const char *time1, const char *time2);
+void startLogging(char *loggingDir);
+void writeLogg(const char *source_file, const char *function, const int line, const char *format, ...);
#endif