diff --git a/src/global.h b/src/global.h index c03cc43b72684909397a9c6944d4903fb39d0964..69ac066acc7cab7cb24911d7d022ab3edabd4b10 100644 --- a/src/global.h +++ b/src/global.h @@ -35,7 +35,7 @@ typedef short int16; #define ASSERT( expected , value) if( value == expected ) return SUCCESS; \ else return 1; - +#define PIDFILE "sfl.pid" #ifdef DATAFORMAT_IS_FLOAT #define SFLDataFormat float32 #define SFLDataFormatString "Float32" diff --git a/src/main.cpp b/src/main.cpp index 0247fa23a769c9d1165dca8e9efecaa89a42151b..22a6c5b93ed04e523a0b994daefc9470e764ed05 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,7 @@ #include <libintl.h> #include <cstring> #include <iostream> - +#include <string> //#include "config.h" #include "global.h" @@ -50,6 +50,43 @@ main (int argc, char **argv) { printf(_("See http://www.sflphone.org/ for more information\n")); } else { + FILE *fp; + char homepid[128]; + + unsigned int iPid = getpid(); + char cPid[64], cOldPid[64]; + sprintf(cPid,"%d", iPid); + + sprintf(homepid, "%s/.%s/%s", HOMEDIR, PROGDIR, PIDFILE); + + if( (fp = fopen(homepid,"r")) == NULL ){ + //PID file doesn't exists, create and write pid in it + if( (fp = fopen(homepid,"w")) == NULL ){ + fprintf(stderr, _("Creating PID file %s failed. Exited.\n"), homepid); + exit(-1); + } else { + fputs(cPid , fp ); + fclose( fp ); + } + } else { + // PID file exists. Check the former process still alive or not. If alive, kill it and start a new one. + fgets( cOldPid, 64, fp ); + fclose(fp); + if (kill(atoi(cOldPid), 0) == SUCCESS) { + fprintf(stderr, _("There is already a sflphoned daemon running in the system. Starting Failed.\n")); + exit(-1); + } else { + if( (fp = fopen(homepid,"w")) == NULL ){ + fprintf(stderr, _("Writing to PID file %s failed. Exited.\n"), homepid); + exit(-1); + } else { + fputs(cPid , fp ); + fclose( fp ); + } + + } + } + int sessionPort = 0; if (argc == 2) { char* ptrPort = strstr(argv[1], "--port=");