-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileIO.cpp
More file actions
146 lines (132 loc) · 2.99 KB
/
FileIO.cpp
File metadata and controls
146 lines (132 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "FileIO.h"
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
FileIO::FileIO()
{
//ctor
}
FileIO::~FileIO()
{
//dtor
}
bool FileIO::FetchConfig(string *ChipName,int *Feature, int *MaxTemp,bool *UseTmp)
{
try
{
ifstream conf;
conf.open("/etc/tempMon.conf");
if(conf.is_open())
{
string line;
while(!conf.eof())
{
getline(conf,line);
if(line[0]=='#' || line=="")
{
continue;
}else{
int eq=line.find_first_of('=');
string Var="";
for(int i=0;i<=eq-1;i++)
{
Var+=line[i];
}
string Value="";
for(unsigned int i=eq+1;i<=line.length();i++)
{
Value+=line[i];
}
//Get CPU
if(Var=="Chip")
{
*ChipName=Value.c_str();
}
if(Var=="Feature")
{
*Feature=atoi(Value.c_str());
}
if(Var=="Max")
{
*MaxTemp=atoi(Value.c_str());
}
if(Var=="UseTmp")
{
Value.resize(Value.size()-1,' ');
*UseTmp = Value =="true" || Value =="True"?true:false;
}
}
}
return true;
}
return false;
}
catch(...)
{
//There is no config file
return false;
}
}
bool FileIO::WritePID(int PID,bool UseTmp)
{
try
{
ofstream scribe;
if(UseTmp)
{
scribe.open("/tmp/tempMon.pid",ios::out);
}else{
scribe.open("/var/run/tempMon.pid",ios::out);
}
if(!scribe)
{
//Permission denied
return false;
}
scribe<<PID;
scribe.close();
return true;
}
catch(...)
{
return false;
}
}
pid_t FileIO::FetchPID(bool UseTmp)
{
pid_t pid=-1;
try
{
ifstream scribe;
if(UseTmp)
{
scribe.open("/tmp/tempMon.pid",ios::in);
}else{
scribe.open("/var/run/tempMon.pid",ios::in);
}
if(!scribe)
{
//Permission denied
return pid;
}
scribe>>pid;
//Delete pid file so future instances of tempMon do not kill random processes
killPID_File(UseTmp);
return pid;
}
catch(...)
{
return pid;
}
}
void FileIO::killPID_File(bool UseTmp)
{
if(UseTmp)
{
remove("/tmp/tempMon.pid");
}else{
remove("/var/run/tempMon.pid");
}
}