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

* #9782: use fstreams instead of fscanf

Does type checking.
parent cf06b9ae
No related branches found
No related tags found
No related merge requests found
...@@ -31,13 +31,13 @@ ...@@ -31,13 +31,13 @@
#include <libgen.h> #include <libgen.h>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <cstdio> #include <fstream>
#include <cstdlib> #include <cstdlib>
#include <signal.h> #include <signal.h>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <iostream>
#include "fileutils.h" #include "fileutils.h"
#include "logger.h"
namespace { namespace {
// returns true if directory exists // returns true if directory exists
...@@ -84,38 +84,30 @@ bool create_pidfile() ...@@ -84,38 +84,30 @@ bool create_pidfile()
return false; return false;
std::string pidfile = path + "/" PIDFILE; std::string pidfile = path + "/" PIDFILE;
FILE *fp = fopen(pidfile.c_str(),"r"); std::ifstream is(pidfile.c_str());
if (fp) { if (is) {
// PID file exists. Check if the former process is still alive or // PID file exists. Check if the former process is still alive or
// not. If alive, give user a hint. // not. If alive, give user a hint.
int oldPid; int oldPid;
is >> 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) { if (kill(oldPid, 0) == 0) {
ERROR("There is already a sflphoned daemon running in the system. Starting Failed."); // Use cerr because logging has not been initialized
std::cerr << "There is already a sflphoned daemon running in " <<
"the system. Starting Failed." << std::endl;
return false; return false;
} }
} }
// write pid file // write pid file
fp = fopen(pidfile.c_str(),"w"); std::ofstream os(pidfile.c_str());
if (!fp) { if (!os) {
perror(pidfile.c_str()); perror(pidfile.c_str());
return false; return false;
} else { } else {
std::ostringstream pidstr; os << getpid();
pidstr << getpid();
fputs(pidstr.str().c_str(), fp);
fclose(fp);
} }
return true; return true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment