Skip to content
Snippets Groups Projects
Commit a087b326 authored by Tristan Matthews's avatar Tristan Matthews
Browse files

* #8613: make check should fail early if another sflphone is running

parent 8b402ee6
No related branches found
No related tags found
No related merge requests found
......@@ -29,7 +29,30 @@
*/
#include <libgen.h>
#include <dirent.h>
#include <sys/stat.h>
#include <cstdio>
#include <cstdlib>
#include <signal.h>
#include "global.h"
namespace {
// returns true if directory exists
bool check_dir(const char *path)
{
DIR *dir = opendir(path);
if (!dir) { // doesn't exist
if (mkdir(path, 0755) != 0) { // couldn't create the dir
perror(path);
return false;
}
} else
closedir(dir);
return true;
}
}
namespace fileutils {
static char *program_dir = NULL;
......@@ -44,4 +67,52 @@ const char *get_program_dir()
return program_dir;
}
bool create_pidfile()
{
const char * const xdg_env = XDG_CACHE_HOME;
std::string path = xdg_env ? xdg_env : std::string(HOMEDIR) + DIR_SEPARATOR_STR ".cache/";
if (!check_dir(path.c_str()))
return false;
path += "sflphone";
if (!check_dir(path.c_str()))
return false;
std::string pidfile = path + "/" PIDFILE;
FILE *fp = fopen(pidfile.c_str(),"r");
if (fp) { // PID file exists. Check the former process still alive or not. If alive, give user a hint.
int oldPid;
if (fscanf(fp, "%d", &oldPid) != 1) {
ERROR("Couldn't read pidfile %s", pidfile.c_str());
return false;
}
fclose(fp);
if (kill(oldPid, 0) == 0) {
ERROR("There is already a sflphoned daemon running in the system. Starting Failed.");
return false;
}
}
// write pid file
fp = fopen(pidfile.c_str(),"w");
if (!fp) {
perror(pidfile.c_str());
return false;
} else {
std::ostringstream pidstr;
pidstr << getpid();
fputs(pidstr.str().c_str(), fp);
fclose(fp);
}
return true;
}
}
......@@ -34,6 +34,7 @@
namespace fileutils {
void set_program_dir(char *program_path);
const char *get_program_dir();
bool create_pidfile();
}
#endif // __FILEUTILS_H__
/*
* Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
* Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
* Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
* Author: Yan Morin <yan.morin@savoirfairelinux.com>
* Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
......@@ -30,22 +30,19 @@
* as that of the covered work.
*/
#include <libintl.h>
#include <cstring>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iostream>
#include <memory> // for auto_ptr
#include <string>
#include <dirent.h>
#include <sys/stat.h>
#include <cc++/common.h>
#include "global.h"
#include "fileutils.h"
#include "dbus/dbusmanager.h"
#include "manager.h"
#include "audio/audiolayer.h"
ost::CommandOptionNoArg console(
"console", "c", "Log in console (instead of syslog)"
);
......@@ -58,25 +55,7 @@ ost::CommandOptionNoArg help(
"help", "h", "Print help"
);
// returns true if directory exists
static bool check_dir(const char *path)
{
DIR *dir = opendir(path);
if (!dir) { // doesn't exist
if (mkdir(path, 0755) != 0) { // couldn't create the dir
perror(path);
return false;
}
} else {
closedir(dir);
}
return true;
}
int
main(int argc, char **argv)
int main(int argc, char **argv)
{
fileutils::set_program_dir(argv[0]);
// makeCommandOptionParse allocates the object with operator new, so
......@@ -84,7 +63,7 @@ main(int argc, char **argv)
// TODO: This should eventually be replaced with std::unique_ptr for C++0x
std::auto_ptr<ost::CommandOptionParse> args(ost::makeCommandOptionParse(argc, argv, ""));
printf("SFLphone Daemon "VERSION", by Savoir-Faire Linux 2004-2011\n" \
printf("SFLphone Daemon " VERSION ", by Savoir-Faire Linux 2004-2012\n" \
"http://www.sflphone.org/\n");
if (help.numSet) {
......@@ -99,50 +78,9 @@ main(int argc, char **argv)
Logger::setConsoleLog(console.numSet);
Logger::setDebugMode(debug.numSet);
const char *xdg_env = XDG_CACHE_HOME;
std::string path = xdg_env ? xdg_env : std::string(HOMEDIR) + DIR_SEPARATOR_STR ".cache/";
if (!check_dir(path.c_str()))
return 1;
path = path + "sflphone";
if (!check_dir(path.c_str()))
if (!fileutils::create_pidfile())
return 1;
std::string pidfile = path + "/" PIDFILE;
FILE *fp = fopen(pidfile.c_str(),"r");
if (fp) { // PID file exists. Check the former process still alive or not. If alive, give user a hint.
int oldPid;
if (fscanf(fp, "%d", &oldPid) != 1) {
std::cerr << "Couldn't read pidfile " << pidfile << std::endl;
return 1;
}
fclose(fp);
if (kill(oldPid, 0) == 0) {
std::cerr << "There is already a sflphoned daemon running in the system. Starting Failed." << std::endl;
return 1;
}
}
// write pid file
fp = fopen(pidfile.c_str(),"w");
if (!fp) {
perror(pidfile.c_str());
return 1;
} else {
std::ostringstream pidstr;
pidstr << getpid();
fputs(pidstr.str().c_str() , fp);
fclose(fp);
}
try {
Manager::instance().init();
} catch (const std::exception &e) {
......
......@@ -31,6 +31,7 @@
#include "logger.h"
#include "manager.h"
#include "constants.h"
#include "fileutils.h"
#include <cstdlib>
......@@ -57,6 +58,8 @@ int main(int argc, char* argv[])
printf("\nSFLphone Daemon Test Suite, by Savoir-Faire Linux 2004-2010\n\n");
Logger::setConsoleLog(true);
Logger::setDebugMode(true);
if (!fileutils::create_pidfile())
return 1;
int argvIndex = 1;
bool xmlOutput = false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment