-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathDaemonUtils.c
More file actions
211 lines (180 loc) · 5.98 KB
/
DaemonUtils.c
File metadata and controls
211 lines (180 loc) · 5.98 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "Internal.h"
#define MAX_DAEMON_NAME_LENGTH 256
// Valid systemd deamon name characters for us, not universal, add more here if necessary in the future
static bool IsValidDaemonNameCharacter(char c)
{
return ((0 == isalnum(c)) && ('_' != c) && ('-' != c) && ('.' != c)) ? false : true;
}
bool IsValidDaemonName(const char *name)
{
size_t length = 0, i = 0;
bool result = true;
if ((NULL != name) && (0 < (length = strlen(name))) && (MAX_DAEMON_NAME_LENGTH > length))
{
for (i = 0; i < length; i++)
{
if (false == (result = IsValidDaemonNameCharacter(name[i])))
{
break;
}
}
}
else
{
result = false;
}
return result;
}
static int ExecuteSystemctlCommand(const char* command, const char* daemonName, OsConfigLogHandle log)
{
const char* commandTemplate = "systemctl %s %s";
char* formattedCommand = NULL;
int result = 0;
if ((NULL == command) || (NULL == daemonName))
{
OsConfigLogError(log, "ExecuteSystemctlCommand: invalid arguments");
OSConfigTelemetryStatusTrace("command", EINVAL);
return EINVAL;
}
else if (false == IsValidDaemonName(daemonName))
{
OsConfigLogError(log, "ExecuteSystemctlCommand: invalid daemon name '%s'", daemonName);
OSConfigTelemetryStatusTrace("IsValidDaemonName", EINVAL);
return EINVAL;
}
else if (NULL == (formattedCommand = FormatAllocateString(commandTemplate, command, daemonName)))
{
OsConfigLogError(log, "ExecuteSystemctlCommand: out of memory");
OSConfigTelemetryStatusTrace("FormatAllocateString", ENOMEM);
return ENOMEM;
}
result = ExecuteCommand(NULL, formattedCommand, false, false, 0, 0, NULL, NULL, log);
FREE_MEMORY(formattedCommand);
return result;
}
bool IsDaemonActive(const char* daemonName, OsConfigLogHandle log)
{
return (IsValidDaemonName(daemonName) && (0 == ExecuteSystemctlCommand("is-active", daemonName, log))) ? true : false;
}
bool CheckDaemonActive(const char* daemonName, char** reason, OsConfigLogHandle log)
{
bool result = false;
if (true == (result = IsDaemonActive(daemonName, log)))
{
OsConfigLogInfo(log, "CheckDaemonActive: service '%s' is active", daemonName);
OsConfigCaptureSuccessReason(reason, "Service '%s' is active", daemonName);
}
else
{
OsConfigLogInfo(log, "CheckDaemonActive: service '%s' is inactive", daemonName);
OsConfigCaptureReason(reason, "Service '%s' is inactive", daemonName);
}
return result;
}
bool CheckDaemonNotActive(const char* daemonName, char** reason, OsConfigLogHandle log)
{
bool result = false;
if (true == IsDaemonActive(daemonName, log))
{
OsConfigLogInfo(log, "CheckDaemonNotActive: service '%s' is active", daemonName);
OsConfigCaptureReason(reason, "Service '%s' is active", daemonName);
result = false;
}
else
{
OsConfigLogInfo(log, "CheckDaemonNotActive: service '%s' is inactive", daemonName);
OsConfigCaptureSuccessReason(reason, "Service '%s' is inactive", daemonName);
result = true;
}
return result;
}
static bool CommandDaemon(const char* command, const char* daemonName, OsConfigLogHandle log)
{
int result = 0;
bool status = true;
if (false == IsValidDaemonName(daemonName))
{
OsConfigLogError(log, "CommandDaemon: invalid daemon name '%s'", daemonName);
OSConfigTelemetryStatusTrace("IsValidDaemonName", EINVAL);
return false;
}
if (0 == (result = ExecuteSystemctlCommand(command, daemonName, log)))
{
OsConfigLogInfo(log, "Succeeded to %s service '%s'", command, daemonName);
}
else
{
OsConfigLogInfo(log, "Cannot %s service '%s' (%d, errno: %d)", command, daemonName, result, errno);
status = false;
}
return status;
}
bool EnableDaemon(const char* daemonName, OsConfigLogHandle log)
{
return CommandDaemon("enable", daemonName, log);
}
bool StartDaemon(const char* daemonName, OsConfigLogHandle log)
{
return CommandDaemon("start", daemonName, log);
}
bool EnableAndStartDaemon(const char* daemonName, OsConfigLogHandle log)
{
bool status = false;
if (false == IsValidDaemonName(daemonName))
{
OsConfigLogError(log, "EnableAndStartDaemon: invalid daemon name '%s'", daemonName);
OSConfigTelemetryStatusTrace("IsValidDaemonName", EINVAL);
return false;
}
if (false == EnableDaemon(daemonName, log))
{
OsConfigLogError(log, "EnableAndStartDaemon: failed to enable service '%s'", daemonName);
OSConfigTelemetryStatusTrace("EnableDaemon", EINVAL);
}
else
{
if (false == IsDaemonActive(daemonName, log))
{
if (false == StartDaemon(daemonName, log))
{
OsConfigLogError(log, "EnableAndStartDaemon: failed to start service '%s'", daemonName);
OSConfigTelemetryStatusTrace("StartDaemon", EINVAL);
}
else
{
status = true;
}
}
else
{
OsConfigLogInfo(log, "Service '%s' is already running", daemonName);
status = true;
}
}
return status;
}
bool StopDaemon(const char* daemonName, OsConfigLogHandle log)
{
return CommandDaemon("stop", daemonName, log);
}
bool DisableDaemon(const char* daemonName, OsConfigLogHandle log)
{
return CommandDaemon("disable", daemonName, log);
}
void StopAndDisableDaemon(const char* daemonName, OsConfigLogHandle log)
{
if (true == StopDaemon(daemonName, log))
{
DisableDaemon(daemonName, log);
}
}
bool RestartDaemon(const char* daemonName, OsConfigLogHandle log)
{
return CommandDaemon("restart", daemonName, log);
}
bool MaskDaemon(const char* daemonName, OsConfigLogHandle log)
{
return CommandDaemon("mask", daemonName, log);
}