-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceHelper.cpp
More file actions
150 lines (111 loc) · 2.39 KB
/
Copy pathServiceHelper.cpp
File metadata and controls
150 lines (111 loc) · 2.39 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
147
148
149
#include <cstdlib>
#include <string>
#include <map>
#include <libutils/FileUtils.h>
#include <libutils/Process.h>
#include <libutils/String.h>
/*
*
* TODO: Refactor and use d-bus api?
*
*/
using namespace std;
using namespace Utils;
namespace OPI {
namespace ServiceHelper {
static bool sysop(const string& operation, const string& service)
{
bool result = false;
tie(result, std::ignore) = Process::Exec("/bin/systemctl -q " + operation +" "+ service + " &> /dev/null");
return result;
}
bool Start(const string& service)
{
return sysop("start", service);
}
bool Stop(const string& service)
{
return sysop("stop", service);
}
bool Enable(const string& service)
{
return sysop("enable", service);
}
bool Disable(const string& service)
{
return sysop("disable", service);
}
bool Reload(const string &service)
{
return sysop("reload", service);
}
bool Restart(const string &service)
{
return sysop("restart", service);
}
bool IsRunning(const string &service)
{
bool result = false;
string rets;
tie(result, rets) = Process::Exec("/bin/systemctl is-active "+ service + " 2> /dev/null");
rets = Utils::String::Chomp(rets);
if( result && rets == "active")
{
return true;
}
return false;
}
bool IsEnabled(const string &service)
{
bool result = false;
string rets;
tie(result, rets) = Process::Exec("/bin/systemctl is-enabled "+ service + " 2> /dev/null");
rets = Utils::String::Chomp(rets);
if( result && rets == "enabled")
{
return true;
}
return false;
}
/* TODO: is this used? Concider refactor to use systemctl show and parse output or remove */
pid_t GetPid(const string& service)
{
string pidfile;
pid_t pid=0;
pidfile="/run/"+service+".pid";
if( ! File::FileExists( pidfile ) )
{
pidfile="/var/run/"+service+".pid";
}
if( File::FileExists( pidfile ) )
{
string s_pid=String::Chomp(File::GetContentAsString(pidfile));
if(File::DirExists("/proc/"+s_pid))
{
pid = std::stoi(s_pid);
}
}
return pid;
}
bool IsAvailable(const string &service)
{
static const map<string, string> servicemap =
{
{"ssh", "openssh-server"},
{"dovecot", "dovecot-imapd"}
};
bool result = false;
string pkg;
if( servicemap.find(service) != servicemap.end() )
{
pkg = servicemap.at(service);
}
else
{
pkg = service;
}
tie(result, std::ignore) = Process::Exec("/usr/bin/dpkg-query -s " + pkg + " > /dev/null 2>&1 ");
return result;
}
} // End NS
} // End NS