From ab4b9c86bff136ee2c32e911f09c4359a8817810 Mon Sep 17 00:00:00 2001 From: Yun Liu <yun@yun.(none)> Date: Thu, 23 Oct 2008 12:21:20 -0400 Subject: [PATCH] Fix bug ticket #54 --- src/global.h | 2 +- src/main.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/global.h b/src/global.h index c03cc43b72..69ac066acc 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 0247fa23a7..22a6c5b93e 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="); -- GitLab